use of java.io.ObjectOutputStream in project platform_frameworks_base by android.
the class Parcel method writeSerializable.
/**
* Write a generic serializable object in to a Parcel. It is strongly
* recommended that this method be avoided, since the serialization
* overhead is extremely large, and this approach will be much slower than
* using the other approaches to writing data in to a Parcel.
*/
public final void writeSerializable(Serializable s) {
if (s == null) {
writeString(null);
return;
}
String name = s.getClass().getName();
writeString(name);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(s);
oos.close();
writeByteArray(baos.toByteArray());
} catch (IOException ioe) {
throw new RuntimeException("Parcelable encountered " + "IOException writing serializable object (name = " + name + ")", ioe);
}
}
use of java.io.ObjectOutputStream in project carat by amplab.
the class CaratSampleDB method addSample.
/**
* Add a sample to the database.
*
* @return rowId or -1 if failed
*/
private long addSample(Sample s) {
Log.d("CaratSampleDB.addSample()", "The sample's battery level=" + s.getBatteryLevel());
ContentValues initialValues = new ContentValues();
initialValues.put(COLUMN_TIMESTAMP, s.timestamp);
// Write the sample hashmap as a blob
if (s != null) {
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(SampleReader.writeSample(s));
initialValues.put(COLUMN_SAMPLE, bo.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
}
return db.insert(SAMPLES_VIRTUAL_TABLE, null, initialValues);
}
use of java.io.ObjectOutputStream in project flink by apache.
the class MetricDumpSerializerTest method testJavaSerialization.
@Test
public void testJavaSerialization() throws IOException {
MetricDumpSerialization.MetricDumpSerializer serializer = new MetricDumpSerialization.MetricDumpSerializer();
final ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
final ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(serializer.serialize(new HashMap<Counter, Tuple2<QueryScopeInfo, String>>(), new HashMap<Gauge<?>, Tuple2<QueryScopeInfo, String>>(), new HashMap<Histogram, Tuple2<QueryScopeInfo, String>>(), new HashMap<Meter, Tuple2<QueryScopeInfo, String>>()));
}
use of java.io.ObjectOutputStream in project flink by apache.
the class KeyedOneInputStreamOperatorTestHarness method snapshotLegacy.
/**
*
*/
@Override
public StreamStateHandle snapshotLegacy(long checkpointId, long timestamp) throws Exception {
// simply use an in-memory handle
MemoryStateBackend backend = new MemoryStateBackend();
CheckpointStreamFactory streamFactory = backend.createStreamFactory(new JobID(), "test_op");
CheckpointStreamFactory.CheckpointStateOutputStream outStream = streamFactory.createCheckpointStateOutputStream(checkpointId, timestamp);
if (operator instanceof StreamCheckpointedOperator) {
((StreamCheckpointedOperator) operator).snapshotState(outStream, checkpointId, timestamp);
}
if (keyedStateBackend != null) {
RunnableFuture<KeyGroupsStateHandle> keyedSnapshotRunnable = keyedStateBackend.snapshot(checkpointId, timestamp, streamFactory, CheckpointOptions.forFullCheckpoint());
if (!keyedSnapshotRunnable.isDone()) {
Thread runner = new Thread(keyedSnapshotRunnable);
runner.start();
}
outStream.write(1);
ObjectOutputStream oos = new ObjectOutputStream(outStream);
oos.writeObject(keyedSnapshotRunnable.get());
oos.flush();
} else {
outStream.write(0);
}
return outStream.closeAndGetHandle();
}
use of java.io.ObjectOutputStream in project flink by apache.
the class CommonTestUtils method createCopySerializable.
/**
* Creates a copy of an object via Java Serialization.
*
* @param original The original object.
* @return The copied object.
*/
public static <T extends java.io.Serializable> T createCopySerializable(T original) throws IOException {
if (original == null) {
throw new IllegalArgumentException();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(original);
oos.close();
baos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try (ObjectInputStream ois = new ObjectInputStream(bais)) {
@SuppressWarnings("unchecked") T copy = (T) ois.readObject();
return copy;
} catch (ClassNotFoundException e) {
throw new IOException(e);
}
}
Aggregations