Search in sources :

Example 61 with ObjectOutputStream

use of java.io.ObjectOutputStream in project facebook-android-sdk by facebook.

the class AppEventStore method saveEventsToDisk.

// Only call from singleThreadExecutor
private static void saveEventsToDisk(PersistedEvents eventsToPersist) {
    ObjectOutputStream oos = null;
    Context context = FacebookSdk.getApplicationContext();
    try {
        oos = new ObjectOutputStream(new BufferedOutputStream(context.openFileOutput(PERSISTED_EVENTS_FILENAME, 0)));
        oos.writeObject(eventsToPersist);
    } catch (Exception e) {
        Log.w(TAG, "Got unexpected exception while persisting events: ", e);
        try {
            context.getFileStreamPath(PERSISTED_EVENTS_FILENAME).delete();
        } catch (Exception innerException) {
        // ignore
        }
    } finally {
        Utility.closeQuietly(oos);
    }
}
Also used : Context(android.content.Context) ObjectOutputStream(java.io.ObjectOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 62 with ObjectOutputStream

use of java.io.ObjectOutputStream in project gephi by gephi.

the class GenericPropertyEditor method getAsText.

@Override
public String getAsText() {
    if (val != null) {
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(val);
        } catch (Exception e) {
            e.printStackTrace();
            ;
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException ex) {
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException ex) {
                }
            }
        }
        if (bos != null) {
            return Base64.encodeBase64String(bos.toByteArray());
        }
    }
    return "null";
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException)

Example 63 with ObjectOutputStream

use of java.io.ObjectOutputStream in project facebook-android-sdk by facebook.

the class TestUtils method serializeAndUnserialize.

public static <T extends Serializable> T serializeAndUnserialize(final T t) {
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        new ObjectOutputStream(os).writeObject(t);
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        @SuppressWarnings("unchecked") T ret = (T) (new ObjectInputStream(is)).readObject();
        return ret;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 64 with ObjectOutputStream

use of java.io.ObjectOutputStream in project gitblit by gitblit.

the class DeepCopier method checksum.

/**
	 * Utility method to calculate the checksum of an object.
	 * @param sourceObject The object from which to establish the checksum.
	 * @return The checksum
	 * @throws IOException
	 */
public static BigInteger checksum(Object sourceObject) {
    if (sourceObject == null) {
        return BigInteger.ZERO;
    }
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(sourceObject);
        oos.close();
        MessageDigest m = MessageDigest.getInstance("SHA-1");
        m.update(baos.toByteArray());
        return new BigInteger(1, m.digest());
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
    // impossible
    }
    return BigInteger.ZERO;
}
Also used : BigInteger(java.math.BigInteger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ObjectOutputStream(java.io.ObjectOutputStream) MessageDigest(java.security.MessageDigest)

Example 65 with ObjectOutputStream

use of java.io.ObjectOutputStream in project gitblit by gitblit.

the class DeepCopier method copy.

/**
	 * Produce a deep copy of the given object. Serializes the entire object to
	 * a byte array in memory. Recommended for relatively small objects.
	 */
@SuppressWarnings("unchecked")
public static <T> T copy(T original) {
    T o = null;
    try {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(byteOut);
        oos.writeObject(original);
        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(byteIn);
        try {
            o = (T) ois.readObject();
        } catch (ClassNotFoundException cex) {
        // actually can not happen in this instance
        }
    } catch (IOException iox) {
        // doesn't seem likely to happen as these streams are in memory
        throw new RuntimeException(iox);
    }
    return o;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ObjectOutputStream (java.io.ObjectOutputStream)1102 ByteArrayOutputStream (java.io.ByteArrayOutputStream)760 ObjectInputStream (java.io.ObjectInputStream)428 ByteArrayInputStream (java.io.ByteArrayInputStream)375 IOException (java.io.IOException)358 FileOutputStream (java.io.FileOutputStream)161 Test (org.junit.Test)161 File (java.io.File)96 BufferedOutputStream (java.io.BufferedOutputStream)52 ObjectOutput (java.io.ObjectOutput)47 OutputStream (java.io.OutputStream)37 HashMap (java.util.HashMap)37 ArrayList (java.util.ArrayList)33 FileInputStream (java.io.FileInputStream)25 InputStream (java.io.InputStream)23 FileNotFoundException (java.io.FileNotFoundException)22 Serializable (java.io.Serializable)17 GZIPOutputStream (java.util.zip.GZIPOutputStream)16 Test (org.testng.annotations.Test)15 NotSerializableException (java.io.NotSerializableException)14