All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.monarchinitiative.phenol.io.utils.ObjHexStringConverter Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package org.monarchinitiative.phenol.io.utils;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import java.io.*;

public class ObjHexStringConverter {

  public static String object2hex(Serializable obj) throws IOException {
    ByteArrayOutputStream bytesout = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bytesout);
    oos.writeObject(obj);
    oos.flush();
    String hexString = Hex.encodeHexString(bytesout.toByteArray());
    bytesout.close();
    oos.close();
    return hexString;
  }

  public static Object hex2obj(String hexString) throws DecoderException, IOException, ClassNotFoundException {
    byte[] bytes = Hex.decodeHex(hexString.toCharArray());
    ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes);
    ObjectInputStream objIn = new ObjectInputStream(bytesIn);
    Object obj = objIn.readObject();
    bytesIn.close();
    objIn.close();
    return obj;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy