public class AppendingObjectOutputStream extends ObjectOutputStream { public AppendingObjectOutputStream(OutputStream out) throws IOException { super(out); } @Override protected void writeStreamHeader() throws IOException { // do not write a header, but reset: // this line added after another question // showed a problem with the original reset(); } }
/** {@inheritDoc} */ public void clearInstanceCache() throws IOException { oos.reset(); }
/** {@inheritDoc} */ public void clearClassCache() throws IOException { oos.reset(); }
protected void resetOutputStreamCaches() throws IOException { os.reset(); }
@Override public void serialize(Serializable object) throws IOException { oos.reset(); // clear (class) back-references oos.writeObject(object); }
Object obj = ...; // Your object you want to write through the stream. (Needs to implement java.io.Serializable) ObjectOutputStream oos = new ObjectOuptputStream(socket.getOutputStream()); oos.writeObject(oos); oos.reset(); // (***) // Don't close it! Otherwise the connection will be closed as well.
public void marshal(Object command, DataOutput ds) throws IOException { ObjectOutputStream out = new ObjectOutputStream((OutputStream)ds); out.writeObject(command); out.flush(); out.reset(); }
private void sendCachedEvents(ObjectOutputStream stream) throws IOException { if (buffer != null) { for (int i = 0; i < buffer.length(); i++) { stream.writeObject(buffer.get(i)); } stream.flush(); stream.reset(); } }
@Override protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception { ObjectOutputStream oos = newObjectOutputStream(new ByteBufOutputStream(out)); try { if (resetInterval != 0) { // Resetting will prevent OOM on the receiving side. writtenObjects ++; if (writtenObjects % resetInterval == 0) { oos.reset(); } } oos.writeObject(msg); oos.flush(); } finally { oos.close(); } } }
private void send(Command cmd) throws IOException { if (oos == null) { throw new IllegalStateException(); } oos.reset(); WireIO.write(oos, cmd); }
@Override protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception { ObjectOutputStream oos = newObjectOutputStream(new ByteBufOutputStream(out)); try { if (resetInterval != 0) { // Resetting will prevent OOM on the receiving side. writtenObjects ++; if (writtenObjects % resetInterval == 0) { oos.reset(); } } oos.writeObject(msg); oos.flush(); } finally { oos.close(); } } }
@Override protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception { ObjectOutputStream oos = newObjectOutputStream(new ByteBufOutputStream(out)); try { if (resetInterval != 0) { // Resetting will prevent OOM on the receiving side. writtenObjects ++; if (writtenObjects % resetInterval == 0) { oos.reset(); } } oos.writeObject(msg); oos.flush(); } finally { oos.close(); } } }
public byte[] marshal(Context ctx, ObjectOutputStream objectOutputStream, Object o) throws IOException { ByteArrayOutputStream buff = new ByteArrayOutputStream(); try (ObjectOutputStream oos = new ObjectOutputStream(buff)) { Documents documents = (Documents) o; // Write the number of documents in the list. oos.writeInt(documents.getDocuments().size()); for (Document nextDocument : documents.getDocuments()) { // Use the DocumentMarshallingStrategy to marshal individual documents. byte[] nextMarshalledDocument = docMarshallingStrategy.marshal(ctx, objectOutputStream, nextDocument); oos.writeInt(nextMarshalledDocument.length); oos.write(nextMarshalledDocument); // Need to call reset on the stream in order for the Document bytes to be written correctly. oos.reset(); } } return buff.toByteArray(); }
@Override public void storeContent() { ByteSequence bodyAsBytes = getContent(); if (bodyAsBytes == null && object != null) { try { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); OutputStream os = bytesOut; ActiveMQConnection connection = getConnection(); if (connection != null && connection.isUseCompression()) { compressed = true; os = new DeflaterOutputStream(os); } DataOutputStream dataOut = new DataOutputStream(os); ObjectOutputStream objOut = new ObjectOutputStream(dataOut); objOut.writeObject(object); objOut.flush(); objOut.reset(); objOut.close(); setContent(bytesOut.toByteSequence()); } catch (IOException ioe) { throw new RuntimeException(ioe.getMessage(), ioe); } } }
oos.reset();
@Override protected Object encode(ChannelHandlerContext context, Channel channel, Object msg) throws Exception { ChannelBuffer buffer = buffer(context); ObjectOutputStream oout = this.oout; if (resetInterval != 0) { // Resetting will prevent OOM on the receiving side. writtenObjects ++; if (writtenObjects % resetInterval == 0) { oout.reset(); // Also discard the byproduct to avoid OOM on the sending side. buffer.discardReadBytes(); } } oout.writeObject(msg); oout.flush(); ChannelBuffer encoded = buffer.readBytes(buffer.readableBytes()); return encoded; }
oos.reset();
oos.reset(); } catch (SocketException e) { isConnected = false;
data.objOut.reset(); data.objOut.flush();
private void sendCachedEvents(ObjectOutputStream stream) throws IOException { if (buffer != null) { for (int i = 0; i < buffer.length(); i++) { stream.writeObject(buffer.get(i)); } stream.flush(); stream.reset(); } }