org.alfresco.repo.content
Class AbstractContentReader

java.lang.Object
  extended by org.alfresco.repo.content.AbstractContentAccessor
      extended by org.alfresco.repo.content.AbstractContentReader
All Implemented Interfaces:
ContentAccessor, ContentReader
Direct Known Subclasses:
EmptyContentReader, FileContentReader, HttpAlfrescoContentReader

public abstract class AbstractContentReader
extends AbstractContentAccessor
implements ContentReader

Implements all the convenience methods of the interface. The only methods that need to be implemented, i.e. provide low-level content access are:


Nested Class Summary
 
Nested classes/interfaces inherited from class org.alfresco.repo.content.AbstractContentAccessor
AbstractContentAccessor.CallbackFileChannel, AbstractContentAccessor.ChannelCloseCallbackAdvise
 
Constructor Summary
protected AbstractContentReader(java.lang.String contentUrl)
           
 
Method Summary
 void addListener(ContentStreamListener listener)
          Adds the listener after checking that the output stream isn't already in use.
static boolean compareContentReaders(ContentReader left, ContentReader right)
          Does a comparison of the binaries associated with two readers.
protected abstract  ContentReader createReader()
          A factory method for subclasses to implement that will ensure the proper implementation of the ContentReader.getReader() method.
 void getContent(java.io.File file)
          Gets content from the repository direct to file
 void getContent(java.io.OutputStream os)
          Copies the input stream to the given OutputStream
 java.io.InputStream getContentInputStream()
          Get a stream to read from the underlying channel
 java.lang.String getContentString()
          Makes use of the encoding, if available, to convert bytes to a string.
 java.lang.String getContentString(int length)
          Gets content from the repository direct to String, but limiting the string size to a given number of characters.
protected abstract  java.nio.channels.ReadableByteChannel getDirectReadableChannel()
          Provides low-level access to read content from the repository.
 java.nio.channels.FileChannel getFileChannel()
          Provides read-only, random-access to the underlying content.
 java.nio.channels.ReadableByteChannel getReadableChannel()
          Provides low-level access to the underlying content.
 ContentReader getReader()
          Performs checks and copies required reader attributes
 boolean isChannelOpen()
          Gets the open/close state of the underlying IO Channel.
 boolean isClosed()
          An automatically created listener sets the flag
 
Methods inherited from class org.alfresco.repo.content.AbstractContentAccessor
channelOpened, finalize, getCallbackFileChannel, getContentData, getContentUrl, getEncoding, getLocale, getMimetype, setContentUrl, setEncoding, setLocale, setMimetype, setRetryingTransactionHelper, toString
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.alfresco.service.cmr.repository.ContentReader
exists, getLastModified
 
Methods inherited from interface org.alfresco.service.cmr.repository.ContentAccessor
getContentData, getContentUrl, getEncoding, getLocale, getMimetype, getSize, setEncoding, setLocale, setMimetype, setRetryingTransactionHelper
 

Constructor Detail

AbstractContentReader

protected AbstractContentReader(java.lang.String contentUrl)
Parameters:
contentUrl - the content URL - this should be relative to the root of the store and not absolute: to enable moving of the stores
Method Detail

addListener

public void addListener(ContentStreamListener listener)
Adds the listener after checking that the output stream isn't already in use.

Specified by:
addListener in interface ContentAccessor
Parameters:
listener - a listener that will be called for output stream event notification
See Also:
ContentAccessor.setRetryingTransactionHelper(RetryingTransactionHelper)

createReader

protected abstract ContentReader createReader()
                                       throws ContentIOException
A factory method for subclasses to implement that will ensure the proper implementation of the ContentReader.getReader() method.

Only the instance need be constructed. The required mimetype, encoding, etc will be copied across by this class.

Returns:
Returns a reader onto the location referenced by this instance. The instance must always be a new instance.
Throws:
ContentIOException

getReader

public final ContentReader getReader()
                              throws ContentIOException
Performs checks and copies required reader attributes

Specified by:
getReader in interface ContentReader
Returns:
Returns a reader onto the underlying content
Throws:
ContentIOException

isClosed

public final boolean isClosed()
An automatically created listener sets the flag

Specified by:
isClosed in interface ContentReader
Returns:
Return true if the content input stream has been used and closed otherwise false.

isChannelOpen

public boolean isChannelOpen()
Description copied from interface: ContentAccessor
Gets the open/close state of the underlying IO Channel.

Specified by:
isChannelOpen in interface ContentAccessor
Returns:
Returns true if the underlying IO Channel is open

getDirectReadableChannel

protected abstract java.nio.channels.ReadableByteChannel getDirectReadableChannel()
                                                                           throws ContentIOException
Provides low-level access to read content from the repository.

This is the only of the content reading methods that needs to be implemented by derived classes. All other content access methods make use of this in their underlying implementations.

Returns:
Returns a channel from which content can be read
Throws:
ContentIOException - if the channel could not be opened or the underlying content has disappeared

getReadableChannel

public final java.nio.channels.ReadableByteChannel getReadableChannel()
                                                               throws ContentIOException
Description copied from interface: ContentReader
Provides low-level access to the underlying content.

Once the stream is provided to a client it should remain active (subject to any timeouts) until closed by the client.

Specified by:
getReadableChannel in interface ContentReader
Returns:
Returns a stream that can be read at will, but must be closed when completed
Throws:
ContentIOException
See Also:
AbstractContentReader.getDirectReadableChannel(), AbstractContentReader.getCallbackReadableChannel(ReadableByteChannel, List)

getFileChannel

public java.nio.channels.FileChannel getFileChannel()
                                             throws ContentIOException
Provides read-only, random-access to the underlying content. In general, this method should be considered more expensive than the sequential-access method, ContentReader.getReadableChannel().

Specified by:
getFileChannel in interface ContentReader
Returns:
Returns a random-access channel onto the content
Throws:
ContentIOException
See Also:
ContentReader.getReadableChannel(), RandomAccessFile.getChannel()

getContentInputStream

public java.io.InputStream getContentInputStream()
                                          throws ContentIOException
Description copied from interface: ContentReader
Get a stream to read from the underlying channel

Specified by:
getContentInputStream in interface ContentReader
Returns:
Returns an input stream onto the underlying channel
Throws:
ContentIOException
See Also:
Channels.newInputStream(java.nio.channels.ReadableByteChannel)

getContent

public final void getContent(java.io.OutputStream os)
                      throws ContentIOException
Copies the input stream to the given OutputStream

Specified by:
getContent in interface ContentReader
Parameters:
os - the stream to which to write the content
Throws:
ContentIOException
See Also:
ContentReader.getReadableChannel()

getContent

public final void getContent(java.io.File file)
                      throws ContentIOException
Description copied from interface: ContentReader
Gets content from the repository direct to file

All resources will be closed automatically.

Specified by:
getContent in interface ContentReader
Parameters:
file - the file to write the content to - it will be overwritten
Throws:
ContentIOException
See Also:
ContentReader.getContentInputStream()

getContentString

public final java.lang.String getContentString(int length)
                                        throws ContentIOException
Description copied from interface: ContentReader
Gets content from the repository direct to String, but limiting the string size to a given number of characters.

If the encoding is known then it will be used otherwise the default system byte[] to String conversion will be used.

All resources will be closed automatically.

Specified by:
getContentString in interface ContentReader
Parameters:
length - the maximum number of characters to retrieve
Returns:
Returns a truncated String representation of the content
Throws:
ContentIOException
See Also:
ContentReader.getContentString(), ContentReader.getContentInputStream(), String.String(byte[])

getContentString

public final java.lang.String getContentString()
                                        throws ContentIOException
Makes use of the encoding, if available, to convert bytes to a string.

All the content is streamed into memory. So, like the interface said, be careful with this method.

Specified by:
getContentString in interface ContentReader
Returns:
Returns a String representation of the content
Throws:
ContentIOException
See Also:
ContentAccessor.getEncoding()

compareContentReaders

public static boolean compareContentReaders(ContentReader left,
                                            ContentReader right)
                                     throws ContentIOException
Does a comparison of the binaries associated with two readers. Several shortcuts are assumed to be valid:
- if the readers are the same instance, then the binaries are the same
- if the size field is different, then the binaries are different
Otherwise the binaries are compared.

Returns:
Returns true if the underlying binaries are the same
Throws:
ContentIOException


Copyright © 2005 - 2010 Alfresco Software, Inc. All Rights Reserved.