| 12 | import org.apache.commons.codec.binary.Base64; |
| 13 | |
| 14 | public final class SerialUtils { |
| 15 | |
| 16 | public static String bytesToEncodedString(final byte[] bytes) { |
| 17 | final byte[] enc = Base64.encodeBase64(bytes); |
| 18 | final String s = new String(enc); |
| 19 | return s.replaceAll("\\s+",""); |
| 20 | } |
| 21 | |
| 22 | public static byte[] bytesFromEncodedString(final String s) { |
| 23 | final byte[] bytes = Base64.decodeBase64(s.replaceAll("\\s+","").getBytes()); |
| 24 | return bytes; |
| 25 | } |
| 26 | |
| 27 | public static String serializableToString(final Serializable o) throws IOException { |
| 28 | final ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 29 | final ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(bos)); |
| 30 | oos.writeObject(o); |
| 31 | oos.close(); |
| 32 | return bytesToEncodedString(bos.toByteArray()); |
| 33 | } |
| 34 | |
| 35 | @SuppressWarnings("unchecked") |
| 36 | public static <Y extends Serializable> Y readSerialiazlabeFromString(final String s) throws IOException, ClassNotFoundException { |
| 37 | final byte[] bytes = bytesFromEncodedString(s); |
| 38 | final ByteArrayInputStream bis = new ByteArrayInputStream(bytes); |
| 39 | final ObjectInputStream ois = new ObjectInputStream(new GZIPInputStream(bis)); |
| 40 | final Object o = ois.readObject(); |
| 41 | ois.close(); |
| 42 | return (Y)o; |
| 43 | } |
| 44 | } |
nothing calls this directly
no outgoing calls
no test coverage detected