uk.ac.bristol.star.cdf.record
Interface Buf

All Known Implementing Classes:
BankBuf, SimpleNioBuf, WrapperBuf

public interface Buf

Represents a sequence of bytes along with operations to read primitive values from it. This interface abstracts away implementation details such as storage mechanism, data encoding and pointer length. It is capable of dealing with 64-bit lengths and offsets. All of the read* methods are safe for use from multiple threads concurrently.

Since:
18 Jun 2013

Method Summary
 java.io.InputStream createInputStream(long offset)
          Returns an input stream consisting of all the bytes in this buf starting from the given offset.
 Buf fillNewBuf(long count, java.io.InputStream in)
          Creates a new Buf of a given length populated from a given input stream.
 long getLength()
          Returns the extent of this buf in bytes.
 boolean isBigendian()
          Determines the data encoding of this buf.
 boolean isBit64()
          Determines the 64bit-ness of this buf.
 java.lang.String readAsciiString(Pointer ptr, int nbyte)
          Reads a fixed number of bytes interpreting them as ASCII characters and returns the result as a string.
 void readDataBytes(long offset, int count, byte[] array)
          Reads a sequence of byte values from this buf into an array.
 void readDataDoubles(long offset, int count, double[] array)
          Reads a sequence of double values from this buf into an array.
 void readDataFloats(long offset, int count, float[] array)
          Reads a sequence of float values from this buf into an array.
 void readDataInts(long offset, int count, int[] array)
          Reads a sequence of int values from this buf into an array.
 void readDataLongs(long offset, int count, long[] array)
          Reads a sequence of long integer values from this buf into an array.
 void readDataShorts(long offset, int count, short[] array)
          Reads a sequence of short values from this buf into an array.
 int readInt(Pointer ptr)
          Reads a signed big-endian 4-byte integer from the pointer position.
 long readOffset(Pointer ptr)
          Reads a file offset or size from the pointer position.
 int readUnsignedByte(Pointer ptr)
          Reads a single byte from the pointer position, returning a value in the range 0..255.
 void setBit64(boolean isBit64)
          Sets the 64bit-ness of this buf.
 void setEncoding(boolean isBigendian)
          Sets the encoding for reading numeric values as performed by the readData* methods.
 

Method Detail

getLength

long getLength()
Returns the extent of this buf in bytes.

Returns:
buffer length

readUnsignedByte

int readUnsignedByte(Pointer ptr)
                     throws java.io.IOException
Reads a single byte from the pointer position, returning a value in the range 0..255. Pointer position is moved on appropriately.

Parameters:
ptr - pointer
Returns:
byte value
Throws:
java.io.IOException

readInt

int readInt(Pointer ptr)
            throws java.io.IOException
Reads a signed big-endian 4-byte integer from the pointer position. Pointer position is moved on appropriately.

Parameters:
ptr - pointer
Returns:
integer value
Throws:
java.io.IOException

readOffset

long readOffset(Pointer ptr)
                throws java.io.IOException
Reads a file offset or size from the pointer position. This is a signed big-endian integer, occupying either 4 or 8 bytes according to the return value of isBit64(). Pointer position is moved on appropriately.

Returns:
buffer size or offset value
Throws:
java.io.IOException

readAsciiString

java.lang.String readAsciiString(Pointer ptr,
                                 int nbyte)
                                 throws java.io.IOException
Reads a fixed number of bytes interpreting them as ASCII characters and returns the result as a string. If a character 0x00 appears before nbyte bytes have been read, it is taken as the end of the string. Pointer position is moved on appropriately.

Parameters:
ptr - pointer
nbyte - maximum number of bytes in string
Returns:
ASCII string
Throws:
java.io.IOException

setBit64

void setBit64(boolean isBit64)
Sets the 64bit-ness of this buf. This determines whether readOffset reads 4- or 8-byte values.

This method should be called before the readOffset method is invoked.

Parameters:
isBit64 - true for 8-byte offsets, false for 4-byte offsets

isBit64

boolean isBit64()
Determines the 64bit-ness of this buf. This determines whether readOffset reads 4- or 8-byte values.

Returns:
true for 8-byte offsets, false for 4-byte offsets

setEncoding

void setEncoding(boolean isBigendian)
Sets the encoding for reading numeric values as performed by the readData* methods.

As currently specified, there are only two possibiliies, Big-Endian and Little-Endian. Interface and implementation would need to be reworked somewhat to accommodate the (presumably, rarely seen in this day and age) D_FLOAT and G_FLOAT encodings supported by the CDF standard.

This method should be called before any of the readData* methods are invoked.

Parameters:
isBigendian - true for big-endian, false for little-endian

isBigendian

boolean isBigendian()
Determines the data encoding of this buf.

Returns:
true for big-endian, false for little-endian

readDataBytes

void readDataBytes(long offset,
                   int count,
                   byte[] array)
                   throws java.io.IOException
Reads a sequence of byte values from this buf into an array.

Parameters:
offset - position sequence start in this buffer in bytes
count - number of byte values to read
array - array to receive values, starting at array element 0
Throws:
java.io.IOException

readDataShorts

void readDataShorts(long offset,
                    int count,
                    short[] array)
                    throws java.io.IOException
Reads a sequence of short values from this buf into an array.

Parameters:
offset - position sequence start in this buffer in bytes
count - number of short values to read
array - array to receive values, starting at array element 0
Throws:
java.io.IOException

readDataInts

void readDataInts(long offset,
                  int count,
                  int[] array)
                  throws java.io.IOException
Reads a sequence of int values from this buf into an array.

Parameters:
offset - position sequence start in this buffer in bytes
count - number of int values to read
array - array to receive values, starting at array element 0
Throws:
java.io.IOException

readDataLongs

void readDataLongs(long offset,
                   int count,
                   long[] array)
                   throws java.io.IOException
Reads a sequence of long integer values from this buf into an array.

Parameters:
offset - position sequence start in this buffer in bytes
count - number of long values to read
array - array to receive values, starting at array element 0
Throws:
java.io.IOException

readDataFloats

void readDataFloats(long offset,
                    int count,
                    float[] array)
                    throws java.io.IOException
Reads a sequence of float values from this buf into an array.

Parameters:
offset - position sequence start in this buffer in bytes
count - number of float values to read
array - array to receive values, starting at array element 0
Throws:
java.io.IOException

readDataDoubles

void readDataDoubles(long offset,
                     int count,
                     double[] array)
                     throws java.io.IOException
Reads a sequence of double values from this buf into an array.

Parameters:
offset - position sequence start in this buffer in bytes
count - number of double values to read
array - array to receive values, starting at array element 0
Throws:
java.io.IOException

createInputStream

java.io.InputStream createInputStream(long offset)
Returns an input stream consisting of all the bytes in this buf starting from the given offset.

Parameters:
offset - position of first byte in buf that will appear in the returned stream
Returns:
input stream

fillNewBuf

Buf fillNewBuf(long count,
               java.io.InputStream in)
               throws java.io.IOException
Creates a new Buf of a given length populated from a given input stream. The new buf object must have the same data encoding and 64bit-ness as this one.

Parameters:
count - size of new buffer in bytes
in - input stream capable of supplying (at least) count bytes
Returns:
new buffer of length count filled with bytes from in
Throws:
java.io.IOException