com.sun.labs.minion.util.buffer
Class FileWriteableBuffer

java.lang.Object
  extended by com.sun.labs.minion.util.buffer.FileWriteableBuffer
All Implemented Interfaces:
Buffer, WriteableBuffer

public class FileWriteableBuffer
extends java.lang.Object
implements WriteableBuffer

A class for a writeable buffer that encodes data directly to a file, utilizing an in-memory buffer. While the positional encoding methods are supported, you are strongly advised against using this implementation of WriteableBuffer for anything other than writing successive bytes.

This implementation is primarily used by the DictionaryWriter class to write interim pieces of dictionaries to a file during dumping and merging operations.

Note that this implementation provides a flush() method that will ensure that any data currently in memory is written to disk.


Field Summary
protected  int bPos
          The position in our in-memory buffer.
protected  byte[] buff
          Our in memory buffer.
protected static int DEFAULT_BUFF_SIZE
          Our default buffer size.
protected static java.lang.String logTag
          A tag for our log entries.
protected  long off
          The offset in the file where we started writing our buffer.
protected  int pos
          The position in the buffer we're writing.
protected  java.io.RandomAccessFile raf
          The file to which we'll write our data.
 
Constructor Summary
FileWriteableBuffer(java.io.RandomAccessFile raf)
          Creates a buffer that will write to the underlying file.
FileWriteableBuffer(java.io.RandomAccessFile raf, int size)
          Creates a buffer that will write to the underlying file.
 
Method Summary
 WriteableBuffer append(ReadableBuffer b)
          Appends a readable buffer onto this buffer.
 WriteableBuffer append(ReadableBuffer b, int n)
          Appends a given number of bytes from a readable buffer onto this buffer.
 WriteableBuffer byteEncode(int pos, long n, int nBytes)
          Encodes a positive long directly, using a given number of bytes, starting at the given position in the units.
 int byteEncode(long n)
          Encodes an integer in a byte-aligned fashion, using the minimal number of bytes.
 WriteableBuffer byteEncode(long n, int nBytes)
          Encodes a positive long onto a writeable in a given number of bytes.
 WriteableBuffer capacity(int n)
          Sets the capacity of this buffer to the given amount.
 WriteableBuffer clear()
          Clears the buffer.
 WriteableBuffer encode(java.lang.CharSequence s)
          Encodes a character sequence onto the buffer.
 WriteableBuffer encode(float f)
          Encodes a floating point value in 4 bytes.
 void flush()
          Flushes our in-memory buffer to the disk.
 ReadableBuffer getReadableBuffer()
          Gets a readable buffer from this writeable one.
 int limit()
          Gets the limit of this buffer, i.e., the last writeable position.
 void limit(int l)
          Sets the limit of this buffer, i.e., the last readable position.
 WriteableBuffer or(ReadableBuffer b)
          Computes the logical OR of this buffer and another.
 int position()
          Gets the current position.
 void position(int p)
          Sets the current position.
 WriteableBuffer put(byte b)
          Puts a single byte onto this buffer.
 WriteableBuffer put(int p, byte b)
          Puts a single byte onto this buffer at the given position.
 int remaining()
          Returns the number of bytes remaining in the buffer, which is nonsense.
 WriteableBuffer set(int bitIndex)
          Sets the given bit to 1 in the given buffer.
 void write(java.nio.ByteBuffer b)
          Write the buffer to a new IO buffer.
 void write(java.io.DataOutput o)
          Write the buffer to a data output.
 void write(java.io.OutputStream os)
          Write the buffer to a stream.
 void write(java.nio.channels.WritableByteChannel chan)
          Write the buffer to a channel.
 WriteableBuffer xor(ReadableBuffer b)
          Computes the logical XOR of this buffer and another.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

raf

protected java.io.RandomAccessFile raf
The file to which we'll write our data.


buff

protected byte[] buff
Our in memory buffer.


off

protected long off
The offset in the file where we started writing our buffer.


pos

protected int pos
The position in the buffer we're writing.


bPos

protected int bPos
The position in our in-memory buffer.


DEFAULT_BUFF_SIZE

protected static int DEFAULT_BUFF_SIZE
Our default buffer size.


logTag

protected static java.lang.String logTag
A tag for our log entries.

Constructor Detail

FileWriteableBuffer

public FileWriteableBuffer(java.io.RandomAccessFile raf)
                    throws java.io.IOException
Creates a buffer that will write to the underlying file.

Parameters:
raf - The file to which we'll write our data.
Throws:
java.io.IOException - If there is an error getting the current file pointer.

FileWriteableBuffer

public FileWriteableBuffer(java.io.RandomAccessFile raf,
                           int size)
                    throws java.io.IOException
Creates a buffer that will write to the underlying file.

Parameters:
raf - The file to which we'll write our data.
size - The size of the in-memory buffer to use.
Throws:
java.io.IOException - If there is an error getting the current file pointer.
Method Detail

limit

public int limit()
Gets the limit of this buffer, i.e., the last writeable position.

Specified by:
limit in interface Buffer
Returns:
Integer.MAX_VALUE, since the underlying representation is a file.

limit

public void limit(int l)
Sets the limit of this buffer, i.e., the last readable position.

Specified by:
limit in interface Buffer
Parameters:
l - The limit to set. This has no effect on this buffer.

position

public int position()
Gets the current position.

Specified by:
position in interface Buffer
Returns:
The current position.

position

public void position(int p)
Sets the current position. This will flush the buffer to disk and reposition the file relative to the initial offset at which the buffer was started.

Specified by:
position in interface Buffer
Parameters:
p - The position.

remaining

public int remaining()
Returns the number of bytes remaining in the buffer, which is nonsense.

Specified by:
remaining in interface Buffer
Returns:
Integer.MAX_VALUE, since the file may contain that much data.

flush

public void flush()
Flushes our in-memory buffer to the disk.


capacity

public WriteableBuffer capacity(int n)
Sets the capacity of this buffer to the given amount.

Specified by:
capacity in interface WriteableBuffer
Parameters:
n - The capacity that we want.
Returns:
This buffer, for chained invocations.

put

public WriteableBuffer put(byte b)
Puts a single byte onto this buffer.

Specified by:
put in interface WriteableBuffer
Parameters:
b - The byte to put on the buffer
Returns:
This buffer, allowing chained invocations.

put

public WriteableBuffer put(int p,
                           byte b)
Puts a single byte onto this buffer at the given position. This is horribly inefficient, but it will work. If you need to do absolute puts, you should consider a different implementation of WriteableBuffer.

Specified by:
put in interface WriteableBuffer
Parameters:
p - The position where the byte should be put.
b - The byte to put on the buffer
Returns:
This buffer, allowing chained invocations.

byteEncode

public WriteableBuffer byteEncode(long n,
                                  int nBytes)
Encodes a positive long onto a writeable in a given number of bytes.

Specified by:
byteEncode in interface WriteableBuffer
Parameters:
n - The number to encode.
nBytes - The number of bytes to use in the encoding.
Returns:
The buffer, to allow chained invocations.

byteEncode

public WriteableBuffer byteEncode(int pos,
                                  long n,
                                  int nBytes)
Encodes a positive long directly, using a given number of bytes, starting at the given position in the units.

Specified by:
byteEncode in interface WriteableBuffer
Parameters:
pos - The position to start encoding.
n - The number to encode.
nBytes - The number of bytes to use in the encoding.
Returns:
The buffer, to allow chained invocations.

byteEncode

public int byteEncode(long n)
Encodes an integer in a byte-aligned fashion, using the minimal number of bytes. The basic idea: use the 7 lower order bits of a byte to encode a number. If the 8th bit is 0, then there are no further bytes in this number. If the 8th bit is one, then the next byte continues the number. Note that this means that a number that would completly fill an integer will take 5 bytes to encode.

Specified by:
byteEncode in interface WriteableBuffer
Parameters:
n - The number to encode.
Returns:
the number of bytes used to encode the number.

encode

public WriteableBuffer encode(float f)
Encodes a floating point value in 4 bytes.

Specified by:
encode in interface WriteableBuffer
Parameters:
f - the floating point number to encode
Returns:
the number of bytes used to encode the float, which is always 4.

append

public WriteableBuffer append(ReadableBuffer b)
Appends a readable buffer onto this buffer.

Specified by:
append in interface WriteableBuffer
Parameters:
b - The buffer that we wish to append onto this buffer.
Returns:
The buffer, to allow chained invocations.

append

public WriteableBuffer append(ReadableBuffer b,
                              int n)
Appends a given number of bytes from a readable buffer onto this buffer.

Specified by:
append in interface WriteableBuffer
Parameters:
b - The buffer that we wish to append onto this buffer.
n - The number of bytes to append onto this buffer.
Returns:
The buffer, to allow chained invocations.

or

public WriteableBuffer or(ReadableBuffer b)
Computes the logical OR of this buffer and another.

Specified by:
or in interface WriteableBuffer
Parameters:
b - The buffer to or with this one.
Returns:
The buffer, to allow chained invocations.

xor

public WriteableBuffer xor(ReadableBuffer b)
Computes the logical XOR of this buffer and another.

Specified by:
xor in interface WriteableBuffer
Parameters:
b - The buffer to or with this one.
Returns:
The buffer, to allow chained invocations.

encode

public WriteableBuffer encode(java.lang.CharSequence s)
Encodes a character sequence onto the buffer. The sequence is encoded in the following way: first, the number of bytes used to encode the string is encoded using the byteEncode method. Then, the characters in the sequence are encoded using a UTF-8 encoding.

Specified by:
encode in interface WriteableBuffer
Parameters:
s - The character sequence to encode.
Returns:
This buffer, allowing chained invocations.

set

public WriteableBuffer set(int bitIndex)
Sets the given bit to 1 in the given buffer.

Specified by:
set in interface WriteableBuffer
Parameters:
bitIndex - the index of the bit to set to 1.
Returns:
This buffer, allowing chained invocations.

clear

public WriteableBuffer clear()
Clears the buffer. This seeks to the initial offset and truncates the file to that length.

Specified by:
clear in interface WriteableBuffer
Returns:
This buffer, allowing chained invocations.

getReadableBuffer

public ReadableBuffer getReadableBuffer()
Gets a readable buffer from this writeable one. It is OK for the readable version of the buffer to share the representation of the buffer contents.

Specified by:
getReadableBuffer in interface WriteableBuffer
Returns:
An instance of FileReadableBuffer that is backed by the same file.
See Also:
FileReadableBuffer

write

public void write(java.nio.ByteBuffer b)
Write the buffer to a new IO buffer.

Specified by:
write in interface WriteableBuffer
Parameters:
b - The buffer to which we'll write our data.

write

public void write(java.nio.channels.WritableByteChannel chan)
           throws java.io.IOException
Write the buffer to a channel. This is done efficiently if the channel is an instance of FileChannel.

Specified by:
write in interface WriteableBuffer
Parameters:
chan - The channel to which the buffer should be written.
Throws:
java.io.IOException - If there is an error writing the data.

write

public void write(java.io.DataOutput o)
           throws java.io.IOException
Write the buffer to a data output. This is done efficiently if the output is a RandomAccessFile.

Specified by:
write in interface WriteableBuffer
Parameters:
o - The output to which the buffer should be written.
Throws:
java.io.IOException - If there is an error writing the data.

write

public void write(java.io.OutputStream os)
           throws java.io.IOException
Write the buffer to a stream. If the stream is an instance of FileOutputStream, this is done efficiently.

Specified by:
write in interface WriteableBuffer
Parameters:
os - The stream to which the buffer should be written.
Throws:
java.io.IOException - If there is any error writing the data.
See Also:
write(WritableByteChannel)