public class BinaryCodec
extends java.lang.Object
implements java.io.Closeable
Modifier and Type | Field and Description |
---|---|
static long |
MAX_UBYTE |
static long |
MAX_UINT |
static long |
MAX_USHORT |
Constructor and Description |
---|
BinaryCodec()
Ambiguous whether reading or writing until set{In,Out}putStream is called
|
BinaryCodec(java.io.File file,
boolean writing)
Constructs BinaryCodec from a file and set it's mode to writing or not
|
BinaryCodec(java.io.InputStream inputStream)
Constructs BinaryCodec from an input stream
|
BinaryCodec(java.io.OutputStream outputStream)
Constructs BinaryCodec from an output stream
|
BinaryCodec(java.lang.String fileName,
boolean writing)
Constructs BinaryCodec from a file name and set it's mode to writing or not
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the appropriate stream
|
java.lang.String |
getInputFileName() |
java.io.InputStream |
getInputStream() |
java.lang.String |
getOutputFileName() |
java.io.OutputStream |
getOutputStream() |
boolean |
isWriting() |
boolean |
knownAtEof() |
boolean |
readBoolean()
Reads a boolean off the input stream, represented as a byte with value 1 or 0
|
byte |
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 stream
|
int |
readBytesOrFewer(byte[] buffer,
int offset,
int length)
Reads a byte array from the input stream.
|
double |
readDouble()
Reads a double off the input stream
|
float |
readFloat()
Reads a float off the input stream
|
int |
readInt()
Read an int off the input stream
|
java.lang.String |
readLengthAndString(boolean devourNull)
Read an int length, and then a String of that length
|
long |
readLong()
Reads a long off the input stream
|
java.lang.String |
readNullTerminatedString()
Read ASCII bytes from the input stream until a null byte is read
|
short |
readShort() |
java.lang.String |
readString(int length)
Read a string off the input stream, as ASCII bytes
|
short |
readUByte()
Reads an 8-bit unsigned byte from the input stream.
|
long |
readUInt()
Reads a 32-bit unsigned int from the input stream.
|
int |
readUShort()
Reads a 16-bit unsigned short from the input stream.
|
void |
setInputFileName(java.lang.String inputFileName) |
void |
setInputStream(java.io.InputStream is) |
void |
setOutputFileName(java.lang.String outputFileName) |
void |
setOutputStream(java.io.OutputStream os) |
void |
writeBoolean(boolean value)
Writes a boolean (1 byte) to the output buffer
|
void |
writeByte(byte bite)
Writes a byte to the output buffer
|
void |
writeByte(int b) |
void |
writeBytes(byte[] bytes)
Writes a byte array to the output buffer
|
void |
writeBytes(byte[] bytes,
int startOffset,
int numBytes) |
void |
writeDouble(double value)
Write a double (8 bytes) to the output stream
|
void |
writeFloat(float value)
Write a float (4 bytes) to the output stream
|
void |
writeInt(int value)
Write a 32-bit int to the output stream
|
void |
writeLong(long value)
Write a 64-bit long to the output stream
|
void |
writeShort(short value)
Write a 16-bit short to output stream
|
void |
writeString(java.lang.String value,
boolean writeLength,
boolean appendNull)
Writes a string to the buffer as ASCII bytes
|
void |
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.
|
public static final long MAX_UBYTE
public static final long MAX_USHORT
public static final long MAX_UINT
public BinaryCodec(java.io.File file, boolean writing)
file
- file to be written to or read fromwriting
- whether the file is being written topublic BinaryCodec(java.lang.String fileName, boolean writing)
fileName
- name of the file to be written to or read fromwriting
- writing whether the file is being written topublic BinaryCodec(java.io.OutputStream outputStream)
outputStream
- Stream to write to, since it's an output stream we know that isWriting
should be set to truepublic BinaryCodec(java.io.InputStream inputStream)
inputStream
- Stream to read from, since we are reading isWriting is set to falsepublic BinaryCodec()
public void writeByte(byte bite)
bite
- byte array to writepublic void writeByte(int b)
public void writeBytes(byte[] bytes)
bytes
- value to writepublic void writeBytes(byte[] bytes, int startOffset, int numBytes)
public void writeInt(int value)
value
- int to writepublic void writeDouble(double value)
value
- double to writepublic void writeLong(long value)
value
- long to writepublic void writeShort(short value)
public void writeFloat(float value)
value
- float to writepublic void writeBoolean(boolean value)
value
- boolean to writepublic void writeString(java.lang.String value, boolean writeLength, boolean appendNull)
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 stringpublic void writeUByte(short val)
public void writeUShort(int val)
public void writeUInt(long val)
public void readBytes(byte[] buffer)
RuntimeEOFException
- if fewer than buffer.length bytes to readpublic void readBytes(byte[] buffer, int offset, int length)
buffer
- where to put bytes readoffset
- offset to start putting bytes into bufferlength
- number of bytes to readRuntimeEOFException
- if fewer than length bytes to readpublic int readBytesOrFewer(byte[] buffer, int offset, int length)
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.public byte readByte()
public boolean knownAtEof()
public java.lang.String readString(int length)
length
- length of string to readpublic java.lang.String readNullTerminatedString()
public java.lang.String readLengthAndString(boolean devourNull)
devourNull
- if true, the length include a null terminator, which is read and discardedpublic int readInt()
public double readDouble()
public long readLong()
public short readShort()
public float readFloat()
public boolean readBoolean()
public short readUByte()
public int readUShort()
public long readUInt()
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
public java.lang.String getInputFileName()
public java.lang.String getOutputFileName()
public void setOutputFileName(java.lang.String outputFileName)
public void setInputFileName(java.lang.String inputFileName)
public boolean isWriting()
public java.io.OutputStream getOutputStream()
public java.io.InputStream getInputStream()
public void setInputStream(java.io.InputStream is)
public void setOutputStream(java.io.OutputStream os)