Java tutorial
package org.apache.http.entity; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; import org.apache.http.annotation.NotThreadSafe; import org.apache.http.util.Args; @NotThreadSafe public class SerializableEntity extends AbstractHttpEntity { private Serializable objRef; private byte[] objSer; public SerializableEntity(Serializable serializable, boolean z) throws IOException { Args.notNull(serializable, "Source object"); if (z) { createBytes(serializable); } else { this.objRef = serializable; } } public SerializableEntity(Serializable serializable) { Args.notNull(serializable, "Source object"); this.objRef = serializable; } private void createBytes(Serializable serializable) throws IOException { OutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(serializable); objectOutputStream.flush(); this.objSer = byteArrayOutputStream.toByteArray(); } public InputStream getContent() throws IOException, IllegalStateException { if (this.objSer == null) { createBytes(this.objRef); } return new ByteArrayInputStream(this.objSer); } public long getContentLength() { if (this.objSer == null) { return -1; } return (long) this.objSer.length; } public boolean isRepeatable() { return true; } public boolean isStreaming() { return this.objSer == null; } public void writeTo(OutputStream outputStream) throws IOException { Args.notNull(outputStream, "Output stream"); if (this.objSer == null) { ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeObject(this.objRef); objectOutputStream.flush(); return; } outputStream.write(this.objSer); outputStream.flush(); } }