001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.util;
020
021import java.io.*;
022import java.util.HashMap;
023import java.util.Iterator;
024import java.util.Map;
025import java.util.Set;
026
027public class Serializer {
028    public static byte[] serialize (Object obj) throws IOException {
029        ByteArrayOutputStream baos = new ByteArrayOutputStream();
030        ObjectOutputStream os = new ObjectOutputStream(baos);
031        os.writeObject(obj);
032        return baos.toByteArray();
033    }
034    public static Object deserialize (byte[] b) throws IOException, ClassNotFoundException {
035        ByteArrayInputStream bais = new ByteArrayInputStream(b);
036        ObjectInputStream is = new ObjectInputStream(bais);
037        return is.readObject();
038    }
039    @SuppressWarnings("unchecked")
040    public static <T> T deserialize (byte[] b, Class<T> clazz) throws IOException, ClassNotFoundException {
041        return (T) deserialize(b);
042    }
043    @SuppressWarnings("unchecked")
044    public static <T> T serializeDeserialize (T obj) throws IOException, ClassNotFoundException {
045        return (T) deserialize (serialize(obj));
046    }
047
048    public static byte[] serializeStringMap (Map<String,String> m)
049      throws IOException
050    {
051        ByteArrayOutputStream baos = new ByteArrayOutputStream();
052        ObjectOutputStream     oos = new ObjectOutputStream (baos);
053        Set s = m.entrySet();
054        oos.writeInt (s.size());
055        for (Object value : s) {
056            Map.Entry entry = (Map.Entry) value;
057            oos.writeObject(entry.getKey());
058            oos.writeObject(entry.getValue());
059        }
060        oos.close();
061        return baos.toByteArray();
062    }
063    public static Map<String,String> deserializeStringMap (byte[] buf)
064      throws ClassNotFoundException, IOException
065    {
066        ByteArrayInputStream  bais = new ByteArrayInputStream (buf);
067        ObjectInputStream     ois  = new ObjectInputStream( bais );
068        Map<String,String> m = new HashMap<>();
069        int size = ois.readInt();
070        for (int i=0; i<size; i++) {
071            m.put (
072              (String) ois.readObject(),
073              (String) ois.readObject()
074            );
075        }
076        return m;
077    }
078}