Package htsjdk.samtools.util
Class BinaryCodec
java.lang.Object
htsjdk.samtools.util.BinaryCodec
- All Implemented Interfaces:
Closeable
,AutoCloseable
Encapsulates file representation of various primitive data types. Forces little-endian disk representation.
Note that this class is currently not very efficient. There are plans to increase the size of the ByteBuffer,
and move data between the ByteBuffer and the underlying input or output stream in larger chunks.
All the read methods throw RuntimeEOFException if the input stream is exhausted before the required number
of bytes are read.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final long
static final long
static final long
-
Constructor Summary
ConstructorsConstructorDescriptionAmbiguous whether reading or writing until set{In,Out}putStream is calledBinaryCodec
(File file, boolean writing) Constructs BinaryCodec from a file and set its mode to writing or notBinaryCodec
(InputStream inputStream) Constructs BinaryCodec from an input streamBinaryCodec
(OutputStream outputStream) Constructs BinaryCodec from an output streamBinaryCodec
(String fileName, boolean writing) Constructs BinaryCodec from a file name and set its mode to writing or notBinaryCodec
(Path path, boolean writing) Constructs BinaryCodec from a file and set its mode to writing or not -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close the appropriate streamboolean
boolean
boolean
Reads a boolean off the input stream, represented as a byte with value 1 or 0byte
readByte()
void
readBytes
(byte[] buffer) Read a byte array from the input stream.void
readBytes
(byte[] buffer, int offset, int length) Read a byte array from the input streamint
readBytesOrFewer
(byte[] buffer, int offset, int length) Reads a byte array from the input stream.double
Reads a double off the input streamfloat
Reads a float off the input streamint
readInt()
Read an int off the input streamreadLengthAndString
(boolean devourNull) Read an int length, and then a String of that lengthlong
readLong()
Reads a long off the input streamRead ASCII bytes from the input stream until a null byte is readshort
readString
(int length) Read a string off the input stream, as ASCII bytesshort
Reads an 8-bit unsigned byte from the input stream.long
readUInt()
Reads a 32-bit unsigned int from the input stream.int
Reads a 16-bit unsigned short from the input stream.void
setInputFileName
(String inputFileName) void
void
setOutputFileName
(String outputFileName) void
void
writeBoolean
(boolean value) Writes a boolean (1 byte) to the output buffervoid
writeByte
(byte bite) Writes a byte to the output buffervoid
writeByte
(int b) void
writeBytes
(byte[] bytes) Writes a byte array to the output buffervoid
writeBytes
(byte[] bytes, int startOffset, int numBytes) void
writeDouble
(double value) Write a double (8 bytes) to the output streamvoid
writeFloat
(float value) Write a float (4 bytes) to the output streamvoid
writeInt
(int value) Write a 32-bit int to the output streamvoid
writeLong
(long value) Write a 64-bit long to the output streamvoid
writeShort
(short value) Write a 16-bit short to output streamvoid
writeString
(String value, boolean writeLength, boolean appendNull) Writes a string to the buffer as ASCII bytesvoid
writeUByte
(short val) Write an 8-bit unsigned byte.void
writeUInt
(long val) Write a 32-bit unsigned int.void
writeUShort
(int val) Write a 16-bit unsigned short.
-
Field Details
-
MAX_UBYTE
public static final long MAX_UBYTE- See Also:
-
MAX_USHORT
public static final long MAX_USHORT- See Also:
-
MAX_UINT
public static final long MAX_UINT- See Also:
-
-
Constructor Details
-
BinaryCodec
Constructs BinaryCodec from a file and set its mode to writing or not- Parameters:
path
- file to be written to or read fromwriting
- whether the file is being written to
-
BinaryCodec
Constructs BinaryCodec from a file and set its mode to writing or not- Parameters:
file
- file to be written to or read fromwriting
- whether the file is being written to
-
BinaryCodec
Constructs BinaryCodec from a file name and set its mode to writing or not- Parameters:
fileName
- name of the file to be written to or read fromwriting
- writing whether the file is being written to
-
BinaryCodec
Constructs BinaryCodec from an output stream- Parameters:
outputStream
- Stream to write to, since it's an output stream we know that isWriting should be set to true
-
BinaryCodec
Constructs BinaryCodec from an input stream- Parameters:
inputStream
- Stream to read from, since we are reading isWriting is set to false
-
BinaryCodec
public BinaryCodec()Ambiguous whether reading or writing until set{In,Out}putStream is called
-
-
Method Details
-
writeByte
public void writeByte(byte bite) Writes a byte to the output buffer- Parameters:
bite
- byte array to write
-
writeByte
public void writeByte(int b) -
writeBytes
public void writeBytes(byte[] bytes) Writes a byte array to the output buffer- Parameters:
bytes
- value to write
-
writeBytes
public void writeBytes(byte[] bytes, int startOffset, int numBytes) -
writeInt
public void writeInt(int value) Write a 32-bit int to the output stream- Parameters:
value
- int to write
-
writeDouble
public void writeDouble(double value) Write a double (8 bytes) to the output stream- Parameters:
value
- double to write
-
writeLong
public void writeLong(long value) Write a 64-bit long to the output stream- Parameters:
value
- long to write
-
writeShort
public void writeShort(short value) Write a 16-bit short to output stream -
writeFloat
public void writeFloat(float value) Write a float (4 bytes) to the output stream- Parameters:
value
- float to write
-
writeBoolean
public void writeBoolean(boolean value) Writes a boolean (1 byte) to the output buffer- Parameters:
value
- boolean to write
-
writeString
Writes a string to the buffer as ASCII bytes- Parameters:
value
- string to write to bufferwriteLength
- prefix the string with the length as a 32-bit intappendNull
- add a null byte to the end of the string
-
writeUByte
public void writeUByte(short val) Write an 8-bit unsigned byte. NOTE: This method will break if we change to big-endian. -
writeUShort
public void writeUShort(int val) Write a 16-bit unsigned short. NOTE: This method will break if we change to big-endian. -
writeUInt
public void writeUInt(long val) Write a 32-bit unsigned int. NOTE: This method will break if we change to big-endian. -
readBytes
public void readBytes(byte[] buffer) Read a byte array from the input stream.- Throws:
RuntimeEOFException
- if fewer than buffer.length bytes to read
-
readBytes
public void readBytes(byte[] buffer, int offset, int length) Read a byte array from the input stream- Parameters:
buffer
- where to put bytes readoffset
- offset to start putting bytes into bufferlength
- number of bytes to read- Throws:
RuntimeEOFException
- if fewer than length bytes to read
-
readBytesOrFewer
public int readBytesOrFewer(byte[] buffer, int offset, int length) Reads a byte array from the input stream.- Parameters:
buffer
- where to put bytes readoffset
- offset to start putting bytes into bufferlength
- number of bytes to read. Fewer bytes may be read if EOF is reached before length bytes have been read.- Returns:
- the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
-
readByte
public byte readByte()- Returns:
- a single byte read from the input stream.
-
knownAtEof
public boolean knownAtEof()- Returns:
- true if it is possible to know for sure if at EOF, and it is known for sure. If the input stream is a ByteArrayInputStream, this is faster than causing a RuntimeEOFException to be thrown.
-
readString
Read a string off the input stream, as ASCII bytes- Parameters:
length
- length of string to read- Returns:
- String read from stream
-
readNullTerminatedString
Read ASCII bytes from the input stream until a null byte is read- Returns:
- String constructed from the ASCII bytes read
-
readLengthAndString
Read an int length, and then a String of that length- Parameters:
devourNull
- if true, the length include a null terminator, which is read and discarded
-
readInt
public int readInt()Read an int off the input stream- Returns:
- int from input stream
-
readDouble
public double readDouble()Reads a double off the input stream- Returns:
- double
-
readLong
public long readLong()Reads a long off the input stream- Returns:
- long
-
readShort
public short readShort() -
readFloat
public float readFloat()Reads a float off the input stream- Returns:
- float
-
readBoolean
public boolean readBoolean()Reads a boolean off the input stream, represented as a byte with value 1 or 0- Returns:
- boolean
-
readUByte
public short readUByte()Reads an 8-bit unsigned byte from the input stream. This method assumes little-endianness. -
readUShort
public int readUShort()Reads a 16-bit unsigned short from the input stream. This method assumes little-endianness. -
readUInt
public long readUInt()Reads a 32-bit unsigned int from the input stream. This method assumes little-endianness. -
close
public void close()Close the appropriate stream- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
getInputFileName
-
getOutputFileName
-
setOutputFileName
-
setInputFileName
-
isWriting
public boolean isWriting() -
getOutputStream
-
getInputStream
-
setInputStream
-
setOutputStream
-