/** Serializes and deserializes the specified object. */ @SuppressWarnings("unchecked") static <T> T reserialize(T object) { checkNotNull(object); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try { ObjectOutputStream out = new ObjectOutputStream(bytes); out.writeObject(object); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())); return (T) in.readObject(); } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); } }
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); ObjectOutputStream os = new ObjectOutputStream(fos); os.writeObject(this); os.close(); fos.close();
protected byte[] toByteArray(Object value) throws IOException { if (value == null) { return null; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(value); out.flush(); return bos.toByteArray(); }
public static void testSerialization(Object o) throws IOException { OutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(o); }
/** * Writes the source object to an output stream using Java serialization. * The source object must implement {@link Serializable}. * @see ObjectOutputStream#writeObject(Object) */ @Override public void serialize(Object object, OutputStream outputStream) throws IOException { if (!(object instanceof Serializable)) { throw new IllegalArgumentException(getClass().getSimpleName() + " requires a Serializable payload " + "but received an object of type [" + object.getClass().getName() + "]"); } ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeObject(object); objectOutputStream.flush(); }
/** * Serialize the given RemoteInvocation to the given OutputStream. * <p>The default implementation gives {@code decorateOutputStream} a chance * to decorate the stream first (for example, for custom encryption or compression). * Creates an {@code ObjectOutputStream} for the final stream and calls * {@code doWriteRemoteInvocation} to actually write the object. * <p>Can be overridden for custom serialization of the invocation. * @param invocation the RemoteInvocation object * @param os the OutputStream to write to * @throws IOException if thrown by I/O methods * @see #decorateOutputStream * @see #doWriteRemoteInvocation */ protected void writeRemoteInvocation(RemoteInvocation invocation, OutputStream os) throws IOException { ObjectOutputStream oos = new ObjectOutputStream(decorateOutputStream(os)); try { doWriteRemoteInvocation(invocation, oos); } finally { oos.close(); } }
@SuppressWarnings("unchecked") static <T> T reserialize(T object) { try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bytes); out.writeObject(object); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())); return (T) in.readObject(); } catch (IOException | ClassNotFoundException e) { Helpers.fail(e, e.getMessage()); } throw new AssertionError("not reachable"); }
protected byte[] toByteArray(Object value) throws IOException { if (value == null) { return null; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(value); out.flush(); return bos.toByteArray(); }
private void save() throws IOException { ObjectOutputStream stream = null; try { stream = new ObjectOutputStream(new FileOutputStream(fHistoryStore)); stream.writeObject(this); } finally { if (stream != null) { stream.close(); } } }
public static byte[] serialize(Object obj) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(out); os.writeObject(obj); return out.toByteArray(); } public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException { ByteArrayInputStream in = new ByteArrayInputStream(data); ObjectInputStream is = new ObjectInputStream(in); return is.readObject(); }
protected byte[] toByteArray(Object value) throws IOException { if (value == null) { return null; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(value); out.flush(); return bos.toByteArray(); }
private void save() throws IOException { ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream( fHistoryStore)); stream.writeObject(this); stream.close(); }
public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(buffer); out.writeObject(o); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); return in.readObject(); }
protected byte[] toByteArray(Object value) throws IOException { if (value == null) { return null; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(value); out.flush(); return bos.toByteArray(); }
public static <T> void serializeCounter(Counter<T> c, String filename) throws IOException { // serialize to file ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(filename))); out.writeObject(c); out.close(); }
@NonNull private <T extends Serializable> T clone(@NonNull T item, @NonNull final Class<T> type) throws Exception { final ByteArrayOutputStream bytesOutput = new ByteArrayOutputStream(); try (final ObjectOutputStream objectOutput = new ObjectOutputStream(bytesOutput)) { objectOutput.writeObject(item); objectOutput.flush(); } final Object clone = new ObjectInputStream( new ByteArrayInputStream(bytesOutput.toByteArray())).readObject(); return type.cast(clone); }
public static byte[] serializeObject(Object o) throws IOException { try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(o); oos.flush(); return baos.toByteArray(); } }
public void save(String path) throws IOException { // save the CRF this.classifier.serializeClassifier(path); // save the additional arguments FileOutputStream fos = new FileOutputStream(path + ".extra"); ObjectOutputStream out = new ObjectOutputStream(fos); out.writeObject(this.gazetteerLocation); out.writeObject(this.annotationsToSkip); out.writeObject(this.useSubTypes); out.writeObject(this.useBIO); out.close(); }
static public void main(String args[]) throws Exception { ByteArrayOutputStream pout = new ByteArrayOutputStream(); ObjectOutputStream oout = new ObjectOutputStream(pout); oout.writeObject(new ExternalizableTest(42)); oout.close(); byte b[] = pout.toByteArray(); ByteArrayInputStream pin = new ByteArrayInputStream(b); ObjectInputStream oin = new ObjectInputStream(pin); Object o = oin.readObject(); System.out.println("read object"); System.out.println(o); }
public static byte[] serializeObject(Object o) throws IOException { try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(o); oos.flush(); return baos.toByteArray(); } }