/** * Serializes an object into a byte buffer. The object has to implement interface Serializable * or Externalizable. **/ public static byte[] objectToByteBuffer(Object obj) throws Exception { byte[] result = null; synchronized (outStream) { outStream.reset(); ObjectOutputStream out = new ObjectOutputStream(outStream); out.writeObject(obj); out.flush(); result = outStream.toByteArray(); out.close(); } return result; }
private MarshObject getMarshObjectInternal(Object o, boolean init) throws IOException { byte[] bc; if (init) { bc = serializeToByteArray(o); } else { _bao.setBuffer(_byteArrayCache.get()); _bao.reset(); // check for next time if (++zipEntryCounter < MAX_ENTRIES) { _zo.putNextEntry(new ZipEntry(Integer.toString(zipEntryCounter))); _oo.reset(); } else // open new zip OutputStream for next time { zipEntryCounter = 0; _zo = new ZipOutputStream(_bao); _zo.setLevel(_level); _zo.putNextEntry(new ZipEntry(Integer.toString(zipEntryCounter))); _oo = getObjectOutputStream(_zo); // remove ObjectOutputStream header from zip stream _zo.closeEntry(); _bao.reset(); _zo.putNextEntry(new ZipEntry(Integer.toString(++zipEntryCounter))); _oo.reset(); } bc = serializeToByteArray(o); _byteArrayCache.notifyUsedSize(bc.length); _bao.setBuffer(DUMMY_BUFFER); } return new CompressedMarshObject(bc); }
public void reset() { try { if (_mos != null) { _is.reset(); _os.reset(); _mos.reset(); //this is the only way to do reset on ObjetInputStream: // add reset flag and let the ObjectInputStream to read it // so all the handles in the ObjectInputStream will be cleared byte[] buffer = _is.getBuffer(); _is.setBuffer(_resetBuffer); _mis.readObject(); _is.setBuffer(buffer); } } catch (Exception ex) { throw new RuntimeException(ex); } }
bos.reset(); mos.reset(); if (buffer != byteBuffer) // replace the buffer in soft reference if needed