Package uk.ac.bristol.star.cdf.record
Class SimpleNioBuf
- java.lang.Object
-
- uk.ac.bristol.star.cdf.record.SimpleNioBuf
-
- All Implemented Interfaces:
Buf
public class SimpleNioBuf extends java.lang.Object implements Buf
Buf implementation based on a single NIO ByteBuffer. This works fine as long as it doesn't need to be more than 2^31 bytes (2Gb), which is the maximum length of a ByteBuffer.- Since:
- 18 Jun 2013
- See Also:
ByteBuffer
-
-
Constructor Summary
Constructors Constructor Description SimpleNioBuf(java.nio.ByteBuffer byteBuf, boolean isBit64, boolean isBigendian)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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 bigend)
Sets the encoding for reading numeric values as performed by thereadData*
methods.
-
-
-
Method Detail
-
getLength
public long getLength()
Description copied from interface:Buf
Returns the extent of this buf in bytes.
-
readUnsignedByte
public int readUnsignedByte(Pointer ptr)
Description copied from interface:Buf
Reads a single byte from the pointer position, returning a value in the range 0..255. Pointer position is moved on appropriately.- Specified by:
readUnsignedByte
in interfaceBuf
- Parameters:
ptr
- pointer- Returns:
- byte value
-
readInt
public int readInt(Pointer ptr)
Description copied from interface:Buf
Reads a signed big-endian 4-byte integer from the pointer position. Pointer position is moved on appropriately.
-
readOffset
public long readOffset(Pointer ptr)
Description copied from interface:Buf
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 ofBuf.isBit64()
. Pointer position is moved on appropriately.- Specified by:
readOffset
in interfaceBuf
- Returns:
- buffer size or offset value
-
readAsciiString
public java.lang.String readAsciiString(Pointer ptr, int nbyte)
Description copied from interface:Buf
Reads a fixed number of bytes interpreting them as ASCII characters and returns the result as a string. If a character 0x00 appears beforenbyte
bytes have been read, it is taken as the end of the string. Pointer position is moved on appropriately.- Specified by:
readAsciiString
in interfaceBuf
- Parameters:
ptr
- pointernbyte
- maximum number of bytes in string- Returns:
- ASCII string
-
setBit64
public void setBit64(boolean isBit64)
Description copied from interface:Buf
Sets the 64bit-ness of this buf. This determines whetherreadOffset
reads 4- or 8-byte values.This method should be called before the
readOffset
method is invoked.
-
setEncoding
public void setEncoding(boolean bigend)
Description copied from interface:Buf
Sets the encoding for reading numeric values as performed by thereadData*
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.- Specified by:
setEncoding
in interfaceBuf
- Parameters:
bigend
- true for big-endian, false for little-endian
-
isBigendian
public boolean isBigendian()
Description copied from interface:Buf
Determines the data encoding of this buf.- Specified by:
isBigendian
in interfaceBuf
- Returns:
- true for big-endian, false for little-endian
-
isBit64
public boolean isBit64()
Description copied from interface:Buf
Determines the 64bit-ness of this buf. This determines whetherreadOffset
reads 4- or 8-byte values.
-
readDataBytes
public void readDataBytes(long offset, int count, byte[] array)
Description copied from interface:Buf
Reads a sequence of byte values from this buf into an array.- Specified by:
readDataBytes
in interfaceBuf
- Parameters:
offset
- position sequence start in this buffer in bytescount
- number of byte values to readarray
- array to receive values, starting at array element 0
-
readDataShorts
public void readDataShorts(long offset, int count, short[] array)
Description copied from interface:Buf
Reads a sequence of short values from this buf into an array.- Specified by:
readDataShorts
in interfaceBuf
- Parameters:
offset
- position sequence start in this buffer in bytescount
- number of short values to readarray
- array to receive values, starting at array element 0
-
readDataInts
public void readDataInts(long offset, int count, int[] array)
Description copied from interface:Buf
Reads a sequence of int values from this buf into an array.- Specified by:
readDataInts
in interfaceBuf
- Parameters:
offset
- position sequence start in this buffer in bytescount
- number of int values to readarray
- array to receive values, starting at array element 0
-
readDataLongs
public void readDataLongs(long offset, int count, long[] array)
Description copied from interface:Buf
Reads a sequence of long integer values from this buf into an array.- Specified by:
readDataLongs
in interfaceBuf
- Parameters:
offset
- position sequence start in this buffer in bytescount
- number of long values to readarray
- array to receive values, starting at array element 0
-
readDataFloats
public void readDataFloats(long offset, int count, float[] array)
Description copied from interface:Buf
Reads a sequence of float values from this buf into an array.- Specified by:
readDataFloats
in interfaceBuf
- Parameters:
offset
- position sequence start in this buffer in bytescount
- number of float values to readarray
- array to receive values, starting at array element 0
-
readDataDoubles
public void readDataDoubles(long offset, int count, double[] array)
Description copied from interface:Buf
Reads a sequence of double values from this buf into an array.- Specified by:
readDataDoubles
in interfaceBuf
- Parameters:
offset
- position sequence start in this buffer in bytescount
- number of double values to readarray
- array to receive values, starting at array element 0
-
createInputStream
public java.io.InputStream createInputStream(long offset)
Description copied from interface:Buf
Returns an input stream consisting of all the bytes in this buf starting from the given offset.- Specified by:
createInputStream
in interfaceBuf
- Parameters:
offset
- position of first byte in buf that will appear in the returned stream- Returns:
- input stream
-
fillNewBuf
public Buf fillNewBuf(long count, java.io.InputStream in) throws java.io.IOException
Description copied from interface:Buf
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.- Specified by:
fillNewBuf
in interfaceBuf
- Parameters:
count
- size of new buffer in bytesin
- input stream capable of supplying (at least)count
bytes- Returns:
- new buffer of length
count
filled with bytes fromin
- Throws:
java.io.IOException
-
-