org.entityfs.util
Class ByteArrayWritableFile

java.lang.Object
  extended by org.entityfs.util.base.AbstractWritableFile
      extended by org.entityfs.util.ByteArrayWritableFile
All Implemented Interfaces:
Lockable, WriteLockable, WritableFile

public class ByteArrayWritableFile
extends AbstractWritableFile

This is a write only WritableFile implementation that store everything that is written to a byte array. The data written to this file can be retrieved by calling toByteArray().

Locking instances of this class returns a NoObjectDummyLock, so instances of this class are not thread safe.

Since:
1.0
Author:
Karl Gustafsson
In_jar:
entityfs-util

Constructor Summary
ByteArrayWritableFile()
          Create a byte array backed writable file with the default initial capacity (FileSystemBuilder.DEFAULT_BUFFER_SIZE bytes).
ByteArrayWritableFile(int capacity)
          Create a byte array backed writable file with the provided initial capacity.
 
Method Summary
 OutputStream openForAppend()
          Open the file for appending.
 OutputStream openForWrite()
          Opens the file for writing.
 byte[] toByteArray()
          Get the data written to the file as a byte array.
 
Methods inherited from class org.entityfs.util.base.AbstractWritableFile
getWriteLock, isWriteLockedByCurrentThread, lockForWriting, openChannelForAppend, openChannelForWrite
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteArrayWritableFile

public ByteArrayWritableFile()
Create a byte array backed writable file with the default initial capacity (FileSystemBuilder.DEFAULT_BUFFER_SIZE bytes).


ByteArrayWritableFile

public ByteArrayWritableFile(int capacity)
Create a byte array backed writable file with the provided initial capacity.

Parameters:
capacity - The initial capacity, in bytes.
Method Detail

openForWrite

public OutputStream openForWrite()
Description copied from interface: WritableFile
Opens the file for writing. This deletes all previous contents of the file. Use WritableFile.openForAppend() to append to the file.

The caller is responsible for closing the stream.

When two output streams, one writing and one appending are open on the same file, the stream opened for write will ignore the contents written by the other stream and the content written by the other stream will be overwritten. When two or more appending streams are opened on a file, a stream will not overwrite the content written by the other streams.

Although a write lock on the file is required for the execution thread opening the stream, the stream itself is not protected from access by other threads. It can be protected by wrapping it in a LockAwareOutputStream.

Returns:
An OutputStream to write to. The stream must be closed by the caller.
See Also:
WritableFile.openForAppend(), WritableFile.openChannelForWrite()

openForAppend

public OutputStream openForAppend()
Description copied from interface: WritableFile
Open the file for appending. This preserves all previous contents of the file. Use WritableFile.openForWrite() to delete the previous contents before writing. This requires that the file supports the FCAppendable capability (and/or that the file system supports the FSCAppendableFiles capability.

The caller is responsible for closing the stream.

When two appending output streams are open on the same file, one stream will not overwrite contents written by the other stream. Compare this with the behavior of one writing and one appending stream.

Although a write lock on the file is required for the execution thread opening the stream, the stream itself is not protected from access by other threads. It can be protected by wrapping it in a LockAwareWritableByteChannel.

Returns:
An OutputStream to write to. The stream must be closed by the caller.
See Also:
WritableFile.openForWrite(), WritableFile.openChannelForAppend()

toByteArray

public byte[] toByteArray()
Get the data written to the file as a byte array.

Returns:
The data written to the file.