Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.commons.marshall;
Abstract Marshaller implementation containing shared implementations.

Galder ZamarreƱo
 public abstract class AbstractMarshaller implements Marshaller {

This is a convenience method for converting an object into a which takes an estimated size as parameter. A allows direct access to the byte array with minimal array copying

o object to marshall
estimatedSize an estimate of how large the resulting byte array may be
    protected abstract ByteBuffer objectToBuffer(Object oint estimatedSizethrows IOExceptionInterruptedException;
       if (obj != null) {
          BufferSizePredictor sizePredictor = 
          int estimatedSize = sizePredictor.nextSize(obj);
          ByteBuffer byteBuffer = objectToBuffer(objestimatedSize);
          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);
          return byteBuffer;
       } else {
          return objectToBuffer(null, 1);
    public byte[] objectToByteBuffer(Object othrows IOExceptionInterruptedException {
       if (o != null) {
          BufferSizePredictor sizePredictor = 
          byte[] bytes = objectToByteBuffer(osizePredictor.nextSize(o));
          return bytes;
       } else {
          return objectToByteBuffer(null, 1);
    public byte[] objectToByteBuffer(Object objint estimatedSizethrows IOExceptionInterruptedException {
       ByteBuffer b = objectToBuffer(objestimatedSize);
       return trimBuffer(b);
    private byte[] trimBuffer(ByteBuffer b) {
       byte[] bytes = new byte[b.getLength()];
       System.arraycopy(b.getBuf(), b.getOffset(), bytes, 0, b.getLength());
       return bytes;
    public Object objectFromByteBuffer(byte[] bufthrows IOExceptionClassNotFoundException {
       return objectFromByteBuffer(buf, 0, buf.length);

This method implements StreamingMarshaller.objectFromInputStream(, 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 inputStreamthrows IOExceptionClassNotFoundException {
       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 =, 0, buf.length)) != -1) bytes.write(buf, 0, bytesRead);
      return objectFromByteBuffer(bytes.getRawBuffer(), 0, bytes.size());
New to GrepCode? Check out our FAQ X