Android Utililty Methods Object Serialization

List of utility methods to do Object Serialization

Description

The list of methods to do Object Serialization are organized into topic(s).

Method

ObjectloadObjectFromFile(String filename)
load Object From File
if (filename.startsWith(SERIALPREFIX)) {
    filename = serialFileName(filename);
File f = new File(filename);
try {
    InputStream is = new BufferedInputStream(new FileInputStream(f));
    if (filename.endsWith(".gz")) {
        is = new GZIPInputStream(is);
...
TreadObjectFromFile(File fileLocation)
read Object From File
InputStream file = new FileInputStream(fileLocation);
InputStream buffer = new GZIPInputStream(file);
try (ObjectInput input = new ObjectInputStream(buffer)) {
    return (T) input.readObject();
} catch (ClassNotFoundException e) {
    throw new IOException(e);
ObjectreadObjectFromFile(String filename)
Read a serialized object from a file
try {
    InputStream file = new FileInputStream(filename);
    InputStream buffer = new BufferedInputStream(file);
    ObjectInput input = new ObjectInputStream(buffer);
    try {
        return input.readObject();
    } finally {
        input.close();
...
voidsaveObjectAsFile(String filename, Object object)
save Object As File
if (filename.startsWith(SERIALPREFIX)) {
    filename = serialFileName(filename);
File f = new File(filename);
try {
    ensureWriteable(f);
    OutputStream os = null;
    os = new BufferedOutputStream(new FileOutputStream(f));
...
voidwriteObjectToFile(File fileLocation, Object obj)
write Object To File
OutputStream file = new FileOutputStream(fileLocation);
OutputStream buffer = new GZIPOutputStream(file);
try (ObjectOutput output = new ObjectOutputStream(buffer)) {
    output.writeObject(obj);
booleanwriteObjectToFile(Serializable obj, String filename)
Write a serializable object to a file
try {
    OutputStream file = new FileOutputStream(filename);
    OutputStream buffer = new BufferedOutputStream(file);
    ObjectOutput output = new ObjectOutputStream(buffer);
    try {
        output.writeObject(obj);
    } finally {
        output.close();
...
StringobjectToString(Object o)
object To String
if (o == null) {
    return null;
ByteArrayOutputStream baos = new ByteArrayOutputStream(32000);
try {
    ObjectOutputStream os = new ObjectOutputStream(
            new BufferedOutputStream(baos));
    os.flush();
...
ObjectstringToObject(String s)
string To Object
if (s == null) {
    return null;
byte byteArray[] = decode(s);
ByteArrayInputStream baos = new ByteArrayInputStream(byteArray);
try {
    ObjectInputStream is = new ObjectInputStream(
            new BufferedInputStream(baos));
...
byte[]getBytes(Serializable obj)
get Bytes
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(obj);
out.flush();
byte[] bytes = bout.toByteArray();
bout.close();
out.close();
return bytes;
...
Mapdeserialize(final File file, final long serialTtl)
deserialize
if (file.exists()
        && file.lastModified() < System.currentTimeMillis()
                - serialTtl) {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Deleting previous serial %s "
                + "because it's older then %s m/s", file.getPath(),
                serialTtl));
    file.delete();
Map<String, Long> o = null;
try {
    if (file.exists()) {
        final ObjectInputStream in = new ObjectInputStream(
                new FileInputStream(file));
        try {
            o = (Map<String, Long>) in.readObject();
        } finally {
            if (in != null) {
                in.close();
} catch (Exception e) {
    log.error("Error: ", e);
if (o == null) {
    o = new HashMap<String, Long>();
return o;