Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Read the object from Base64 string.
- */
- public static Object fromString(final String string) throws IOException, ClassNotFoundException {
- final byte[] data = Base64.getDecoder().decode(string);
- final ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(data));
- final Object object = objectInputStream.readObject();
- objectInputStream.close();
- return object;
- }
- /**
- * Write the object to a Base64 string.
- */
- public static String toString(final Serializable object) throws IOException {
- final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- final ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
- objectOutputStream.writeObject(object); // must be serializable
- objectOutputStream.close();
- return Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());
- }
- /**
- * Write the object to a byte array.
- */
- public static byte[] serialize(final Object obj) throws IOException {
- final ByteArrayOutputStream out = new ByteArrayOutputStream();
- final ObjectOutputStream os = new ObjectOutputStream(out);
- os.writeObject(obj);
- return out.toByteArray();
- }
- /**
- * Read the object from a byte array.
- */
- public static Object deserialize(final byte[] data) throws IOException, ClassNotFoundException {
- final ByteArrayInputStream in = new ByteArrayInputStream(data);
- final ObjectInputStream is = new ObjectInputStream(in);
- return is.readObject();
- }
- public static Map<String, Object> convertWithStream(final String mapAsString) {
- final Map<String, Object> map = Arrays.stream(mapAsString.split(","))
- .map(entry -> entry.split("="))
- .collect(Collectors.toMap(entry -> entry[0], entry -> entry[1]));
- return map;
- }
- public static Map<String, String> convertWithGuava(final String mapAsString) {
- return Splitter.on(',').withKeyValueSeparator('=').split(mapAsString);
- }
Add Comment
Please, Sign In to add comment