org.gcube.contentmanagement.baselayer.inMessageImpl
Class InMemoryContentStream

java.lang.Object
  extended by org.gcube.contentmanagement.baselayer.BaseLayerStream
      extended by org.gcube.contentmanagement.baselayer.inMessageImpl.InMemoryContentStream

public class InMemoryContentStream
extends BaseLayerStream

Convenience implementation of a stream that wraps a byte array in main memory. Thus, all operations on streams can be reused and hardly any special treatment of content send in message is needed except for wrapping it with this class.

Author:
michaelspringmann

Field Summary
 
Fields inherited from class org.gcube.contentmanagement.baselayer.BaseLayerStream
END_OF_STREAM, NO_LIMIT, STREAM_LENGTH_IMPLICITE, URL_CONNECTION_LENGTH_UNKNOWN
 
Constructor Summary
InMemoryContentStream(BaseLayerStream binaryFileContent, java.io.ByteArrayOutputStream baosToUse)
          Caches content of an existing stream in memory by copying all bytes
InMemoryContentStream(byte[] content)
          Creates a new stream of bytes
 
Method Summary
static long copyBytes(BaseLayerStream binaryFileContent, java.io.ByteArrayOutputStream baos)
          Copies all bytes of a stream to the in memory stream, thus caches the content
 void dispose()
          Closes the BaseLayerStream which implies that all bound resources should be freed.
 byte[] getContent()
          Returns the content of the stream
 
Methods inherited from class org.gcube.contentmanagement.baselayer.BaseLayerStream
getBufferedStream, getBufferSize, getDefaultBufferSize, getLength, getLimit, getStream, handleStartOffset, hasImpliciteLength, isBuffered, needsLimitTreatment, setBuffered, setBufferSize, setDefaultBufferSize, setLimit, setLimit, toFixedLengthStream, writeToStreamRespectingLimit
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InMemoryContentStream

public InMemoryContentStream(byte[] content)
Creates a new stream of bytes

Parameters:
content - the content of the stream

InMemoryContentStream

public InMemoryContentStream(BaseLayerStream binaryFileContent,
                             java.io.ByteArrayOutputStream baosToUse)
                      throws BaseLayerException
Caches content of an existing stream in memory by copying all bytes

Parameters:
binaryFileContent - the content to copy
baosToUse - a pre-initialized byte array stream for dirct access to the content (if needed)
Throws:
BaseLayerException - if something fails
Method Detail

getContent

public byte[] getContent()
Returns the content of the stream

Returns:
the content

copyBytes

public static long copyBytes(BaseLayerStream binaryFileContent,
                             java.io.ByteArrayOutputStream baos)
                      throws BaseLayerException
Copies all bytes of a stream to the in memory stream, thus caches the content

Parameters:
binaryFileContent - the content to cache
baos - the stream to copy the bytes to
Returns:
the number of bytes copied
Throws:
BaseLayerException - if something goes wrong

dispose

public void dispose()
             throws BaseLayerException
Closes the BaseLayerStream which implies that all bound resources should be freed.

Overrides:
dispose in class BaseLayerStream
Throws:
BaseLayerException - if something goes wrong