Created
November 15, 2015 03:42
-
-
Save crowjdh/0603b9173ed6d016006a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.ObjectInput; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutput; | |
import java.io.ObjectOutputStream; | |
/** | |
* Created by Dongheyon Jeong in News Kit from Yooii Studios Co., LTD. on 15. 3. 11. | |
* | |
* ObjectByteConverter | |
* Converts Object to bytes and vice versa | |
*/ | |
public class ObjectByteConverter { | |
public static byte[] toByteArray(Object object) throws IOException { | |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | |
ObjectOutput objectOutput = new ObjectOutputStream(outputStream); | |
objectOutput.writeObject(object); | |
byte[] bytes = outputStream.toByteArray(); | |
objectOutput.close(); | |
return bytes; | |
} | |
public static Object fromByteArray(byte[] bytes) throws IOException, ClassNotFoundException { | |
ByteArrayInputStream bis = new ByteArrayInputStream(bytes); | |
ObjectInput in = new ObjectInputStream(bis); | |
return in.readObject(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment