@Override public Object objectFromByteBuffer(byte[] buf) throws IOException, ClassNotFoundException { return objectFromByteBuffer(buf, 0, buf.length); }
@Override public byte[] objectToByteBuffer(Object obj, int estimatedSize) throws IOException, InterruptedException { ByteBuffer b = objectToBuffer(obj, estimatedSize); return trimBuffer(b); }
@Override public byte[] objectToByteBuffer(Object o) throws IOException, InterruptedException { if (o != null) { BufferSizePredictor sizePredictor = marshallableTypeHints .getBufferSizePredictor(o.getClass()); byte[] bytes = objectToByteBuffer(o, sizePredictor.nextSize(o)); sizePredictor.recordSize(bytes.length); return bytes; } else { return objectToByteBuffer(null, 1); } }
@Override public ByteBuffer objectToBuffer(Object obj) throws IOException, InterruptedException { if (obj != null) { BufferSizePredictor sizePredictor = marshallableTypeHints .getBufferSizePredictor(obj.getClass()); int estimatedSize = sizePredictor.nextSize(obj); ByteBuffer byteBuffer = objectToBuffer(obj, estimatedSize); int length = byteBuffer.getLength(); // If the prediction is way off, then trim it if (estimatedSize > (length * 4)) { byte[] buffer = trimBuffer(byteBuffer); byteBuffer = new ByteBufferImpl(buffer, 0, buffer.length); } sizePredictor.recordSize(length); return byteBuffer; } else { return objectToBuffer(null, 1); } }
/** * This method implements {@link StreamingMarshaller#objectFromInputStream(java.io.InputStream)}, but its * implementation has been moved here rather that keeping under a class that implements StreamingMarshaller * in order to avoid code duplication. */ public Object objectFromInputStream(InputStream inputStream) throws IOException, ClassNotFoundException { int len = inputStream.available(); ExposedByteArrayOutputStream bytes; byte[] buf; if(len > 0) { bytes = new ExposedByteArrayOutputStream(len); buf = new byte[Math.min(len, 1024)]; } else { // Some input stream providers do not implement available() bytes = new ExposedByteArrayOutputStream(); buf = new byte[1024]; } int bytesRead; while ((bytesRead = inputStream.read(buf, 0, buf.length)) != -1) bytes.write(buf, 0, bytesRead); return objectFromByteBuffer(bytes.getRawBuffer(), 0, bytes.size()); }