Start line:  
End line:  

Snippet Preview

Snippet HTML Code

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

Author(s):
Galder ZamarreƱo
Since:
4.1
 
 public abstract class AbstractMarshaller implements Marshaller {
 
 
    @Override
    }

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

Parameters:
o object to marshall
estimatedSize an estimate of how large the resulting byte array may be
 
    protected abstract ByteBuffer objectToBuffer(Object oint estimatedSizethrows IOExceptionInterruptedException;
 
    @Override
       if (obj != null) {
          BufferSizePredictor sizePredictor = 
                .getBufferSizePredictor(obj.getClass());
          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);
          }
          sizePredictor.recordSize(length);
          return byteBuffer;
       } else {
          return objectToBuffer(null, 1);
       }
    }
 
    @Override
    public byte[] objectToByteBuffer(Object othrows IOExceptionInterruptedException {
       if (o != null) {
          BufferSizePredictor sizePredictor = 
                .getBufferSizePredictor(o.getClass());
          byte[] bytes = objectToByteBuffer(osizePredictor.nextSize(o));
          sizePredictor.recordSize(bytes.length);
          return bytes;
       } else {
          return objectToByteBuffer(null, 1);
       }
    }
 
    @Override
    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;
    }
 
    @Override
    public Object objectFromByteBuffer(byte[] bufthrows IOExceptionClassNotFoundException {
       return objectFromByteBuffer(buf, 0, buf.length);
    }

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