org.infinispan.marshall
Class AbstractDelegatingMarshaller

java.lang.Object
  extended by org.infinispan.marshall.AbstractDelegatingMarshaller
All Implemented Interfaces:
Marshaller, StreamingMarshaller
Direct Known Subclasses:
CacheMarshaller, GlobalMarshaller

public abstract class AbstractDelegatingMarshaller
extends Object
implements StreamingMarshaller

With the introduction of global and cache marshallers, there's a need to separate marshallers but still rely on the same marshalling backend as previously. So, this class acts as a delegator for the new marshallers.

Since:
5.0
Author:
Galder ZamarreƱo

Field Summary
protected  StreamingMarshaller marshaller
           
 
Constructor Summary
AbstractDelegatingMarshaller()
           
 
Method Summary
 void finishObjectInput(ObjectInput oi)
          Finish using the given ObjectInput.
 void finishObjectOutput(ObjectOutput oo)
          Finish using the given ObjectOutput.
 BufferSizePredictor getBufferSizePredictor(Object o)
          Returns a marshalled payload size predictor for a particular type.
 boolean isMarshallable(Object o)
          A method that checks whether the given object is marshallable as per the rules of this marshaller.
 Object objectFromByteBuffer(byte[] buf)
          Unmarshalls an object from a byte array.
 Object objectFromByteBuffer(byte[] buf, int offset, int length)
          Unmarshalls an object from a specific portion of a byte array.
 Object objectFromInputStream(InputStream is)
          Unmarshall an object from an InputStream
 Object objectFromObjectStream(ObjectInput in)
          Unmarshalls an object from an ObjectInput
 ByteBuffer objectToBuffer(Object o)
          A method that returns an instance of ByteBuffer, which allows direct access to the byte array with minimal array copying
 byte[] objectToByteBuffer(Object obj)
          Marshalls an object to a byte array.
 byte[] objectToByteBuffer(Object obj, int estimatedSize)
          Marshalls an object to a byte array.
 void objectToObjectStream(Object obj, ObjectOutput out)
          Marshalls an object to a given ObjectOutput
 ObjectInput startObjectInput(InputStream is, boolean isReentrant)
          Create and open a new ObjectInput for the given input stream.
 ObjectOutput startObjectOutput(OutputStream os, boolean isReentrant)
          Deprecated. 
 ObjectOutput startObjectOutput(OutputStream os, boolean isReentrant, int estimatedSize)
          Create and open an ObjectOutput instance for the given output stream.
 void stop()
          Stop the marshaller.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

marshaller

protected StreamingMarshaller marshaller
Constructor Detail

AbstractDelegatingMarshaller

public AbstractDelegatingMarshaller()
Method Detail

stop

public void stop()
Description copied from interface: StreamingMarshaller
Stop the marshaller. Implementations of this method should clear up any cached data, or close any resources while marshalling/unmarshalling that have not been already closed.

Specified by:
stop in interface StreamingMarshaller

startObjectOutput

public ObjectOutput startObjectOutput(OutputStream os,
                                      boolean isReentrant,
                                      int estimatedSize)
                               throws IOException
Description copied from interface: StreamingMarshaller

Create and open an ObjectOutput instance for the given output stream. This method should be used for opening data outputs when multiple objectToObjectStream() calls will be made before the stream is closed by calling finishObjectOutput().

This method also takes a boolean that represents whether this particular call to startObjectOutput() is reentrant or not. A call to startObjectOutput() should be marked reentrant whenever a 2nd or more calls to this method are made without having called finishObjectOutput() first.

To potentially speed up calling startObjectOutput multiple times in a non-reentrant way, i.e. startObjectOutput/finishObjectOutput...startObjectOutput/finishObjectOutput...etc, which is is the most common case, the StreamingMarshaller implementation could potentially use some mechanisms to speed up this startObjectOutput call.

On the other hand, when a call is reentrant, i.e. startObjectOutput/startObjectOutput(reentrant)...finishObjectOutput/finishObjectOutput, the StreamingMarshaller implementation might treat it differently. An example of reentrancy would be marshalling of MarshalledValue. When sending or storing a MarshalledValue, a call to startObjectOutput() would occur so that the stream is open and following, a 2nd call could occur so that MarshalledValue's raw byte array version is calculated and sent across. This enables storing as binary on the receiver side which is performance gain. The StreamingMarshaller implementation could decide that it needs a separate ObjectOutput or similar for the 2nd call since it's aim is only to get the raw byte array version and the close finish with it.

Specified by:
startObjectOutput in interface StreamingMarshaller
Parameters:
os - output stream
isReentrant - whether the call is reentrant or not.
estimatedSize - estimated size in bytes of the output. Only meant as a possible performance optimization.
Returns:
ObjectOutput to write to
Throws:
IOException

startObjectOutput

@Deprecated
public ObjectOutput startObjectOutput(OutputStream os,
                                                 boolean isReentrant)
                               throws IOException
Deprecated. 

Description copied from interface: StreamingMarshaller
Use StreamingMarshaller.startObjectOutput(OutputStream, boolean, int) instead

Specified by:
startObjectOutput in interface StreamingMarshaller
Throws:
IOException

finishObjectOutput

public void finishObjectOutput(ObjectOutput oo)
Description copied from interface: StreamingMarshaller
Finish using the given ObjectOutput. After opening a ObjectOutput and calling objectToObjectStream() multiple times, use this method to flush the data and close if necessary

Specified by:
finishObjectOutput in interface StreamingMarshaller
Parameters:
oo - data output that finished using

objectToObjectStream

public void objectToObjectStream(Object obj,
                                 ObjectOutput out)
                          throws IOException
Description copied from interface: StreamingMarshaller
Marshalls an object to a given ObjectOutput

Specified by:
objectToObjectStream in interface StreamingMarshaller
Parameters:
obj - object to marshall
out - stream to marshall to
Throws:
IOException

startObjectInput

public ObjectInput startObjectInput(InputStream is,
                                    boolean isReentrant)
                             throws IOException
Description copied from interface: StreamingMarshaller

Create and open a new ObjectInput for the given input stream. This method should be used for opening data inputs when multiple objectFromObjectStream() calls will be made before the stream is closed.

This method also takes a boolean that represents whether this particular call to startObjectInput() is reentrant or not. A call to startObjectInput() should be marked reentrant whenever a 2nd or more calls to this method are made without having called finishObjectInput() first.

To potentially speed up calling startObjectInput multiple times in a non-reentrant way, i.e. startObjectInput/finishObjectInput...startObjectInput/finishObjectInput...etc, which is is the most common case, the StreamingMarshaller implementation could potentially use some mechanisms to speed up this startObjectInput call.

Specified by:
startObjectInput in interface StreamingMarshaller
Parameters:
is - input stream
isReentrant - whether the call is reentrant or not.
Returns:
ObjectInput to read from
Throws:
IOException

finishObjectInput

public void finishObjectInput(ObjectInput oi)
Description copied from interface: StreamingMarshaller
Finish using the given ObjectInput. After opening a ObjectInput and calling objectFromObjectStream() multiple times, use this method to flush the data and close if necessary

Specified by:
finishObjectInput in interface StreamingMarshaller
Parameters:
oi - data input that finished using

objectFromObjectStream

public Object objectFromObjectStream(ObjectInput in)
                              throws IOException,
                                     ClassNotFoundException,
                                     InterruptedException
Description copied from interface: StreamingMarshaller
Unmarshalls an object from an ObjectInput

Specified by:
objectFromObjectStream in interface StreamingMarshaller
Parameters:
in - stream to unmarshall from
Throws:
IOException - if unmarshalling cannot complete due to some I/O error
ClassNotFoundException - if the class of the object trying to unmarshall is unknown
InterruptedException - if the unmarshalling was interrupted. Clients should take this as a sign that the marshaller is no longer available, maybe due to shutdown, and so no more unmarshalling should be attempted.

objectFromInputStream

public Object objectFromInputStream(InputStream is)
                             throws IOException,
                                    ClassNotFoundException
Description copied from interface: StreamingMarshaller
Unmarshall an object from an InputStream

Specified by:
objectFromInputStream in interface StreamingMarshaller
Parameters:
is - stream to unmarshall from
Returns:
the unmarshalled object instance
Throws:
IOException - if unmarshalling cannot complete due to some I/O error
ClassNotFoundException - if the class of the object trying to unmarshall is unknown

objectToByteBuffer

public byte[] objectToByteBuffer(Object obj,
                                 int estimatedSize)
                          throws IOException,
                                 InterruptedException
Description copied from interface: Marshaller
Marshalls an object to a byte array. The estimatedSize parameter is a hint that can be passed in to allow for efficient sizing of the byte array before attempting to marshall the object. The more accurate this estimate is, the less likely byte[]s will need to be resized to hold the byte stream generated by marshalling the object.

Specified by:
objectToByteBuffer in interface Marshaller
Parameters:
obj - object to convert to a byte array. Must not be null.
estimatedSize - an estimate of how large the resulting byte array may be
Returns:
a byte array with the marshalled form of the object
Throws:
IOException - if marshalling cannot complete due to some I/O error
InterruptedException - if the marshalling was interrupted. Clients should take this as a sign that the marshaller is no longer available, maybe due to shutdown, and so no more unmarshalling should be attempted.

objectToByteBuffer

public byte[] objectToByteBuffer(Object obj)
                          throws IOException,
                                 InterruptedException
Description copied from interface: Marshaller
Marshalls an object to a byte array.

Specified by:
objectToByteBuffer in interface Marshaller
Parameters:
obj - object to convert to a byte array. Must not be null.
Returns:
a byte array
Throws:
IOException - if marshalling cannot complete due to some I/O error
InterruptedException - if the marshalling process was interrupted. Clients should take this as a sign that the marshaller is no longer available, maybe due to shutdown, and so no more marshalling should be attempted.

objectFromByteBuffer

public Object objectFromByteBuffer(byte[] buf)
                            throws IOException,
                                   ClassNotFoundException
Description copied from interface: Marshaller
Unmarshalls an object from a byte array.

Specified by:
objectFromByteBuffer in interface Marshaller
Parameters:
buf - byte array containing the binary representation of an object. Must not be null.
Returns:
an object
Throws:
IOException - if unmarshalling cannot complete due to some I/O error
ClassNotFoundException - if the class of the object trying to unmarshall is unknown

objectFromByteBuffer

public Object objectFromByteBuffer(byte[] buf,
                                   int offset,
                                   int length)
                            throws IOException,
                                   ClassNotFoundException
Description copied from interface: Marshaller
Unmarshalls an object from a specific portion of a byte array.

Specified by:
objectFromByteBuffer in interface Marshaller
Parameters:
buf - byte array containing the binary representation of an object. Must not be null.
offset - point in buffer to start reading
length - number of bytes to consider
Returns:
an object
Throws:
IOException - if unmarshalling cannot complete due to some I/O error
ClassNotFoundException - if the class of the object trying to unmarshall is unknown

objectToBuffer

public ByteBuffer objectToBuffer(Object o)
                          throws IOException,
                                 InterruptedException
Description copied from interface: Marshaller
A method that returns an instance of ByteBuffer, which allows direct access to the byte array with minimal array copying

Specified by:
objectToBuffer in interface Marshaller
Parameters:
o - object to marshall
Returns:
a ByteBuffer
Throws:
IOException - if marshalling cannot complete due to some I/O error
InterruptedException - if the marshalling process was interrupted. Clients should take this as a sign that the marshaller is no longer available, maybe due to shutdown, and so no more marshalling should be attempted.

isMarshallable

public boolean isMarshallable(Object o)
                       throws Exception
Description copied from interface: Marshaller
A method that checks whether the given object is marshallable as per the rules of this marshaller.

Specified by:
isMarshallable in interface Marshaller
Parameters:
o - object to verify whether it's marshallable or not
Returns:
true if the object is marshallable, otherwise false
Throws:
Exception - if while checking whether the object was serializable or not, an exception arose

getBufferSizePredictor

public BufferSizePredictor getBufferSizePredictor(Object o)
Description copied from interface: Marshaller
Returns a marshalled payload size predictor for a particular type. Accurate prediction of a type's serialized payload size helps avoid unnecessary copying and speeds up application performance.

Specified by:
getBufferSizePredictor in interface Marshaller
Parameters:
o - Object for which serialized predictor will be returned
Returns:
an instance of BufferSizePredictor

-->

Copyright © 2012 JBoss, a division of Red Hat. All Rights Reserved.