Package org.insa.graphs.model.io
Class BinaryReader
- java.lang.Object
-
- org.insa.graphs.model.io.BinaryReader
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
BinaryGraphReader,BinaryPathReader
public abstract class BinaryReader extends java.lang.Object implements java.lang.AutoCloseable, java.io.CloseableBase class for writing binary file.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.io.DataInputStreamdis
-
Constructor Summary
Constructors Modifier Constructor Description protectedBinaryReader(int magicNumber, int minVersion, java.io.DataInputStream dis)Create a new BinaryReader that reads from the given stream and that expected the given magic number and at least the given minimum version.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcheckByteOrThrow(int b)Check if the next byte in the input stream correspond to the given byte.protected voidcheckMagicNumberOrThrow(int magicNumber)Check if the given number matches the expected magic number.protected voidcheckVersionOrThrow(int version)Check if the given version is greater than the minimum version, and update the current version if it is.voidclose()protected intgetCurrentVersion()protected intread24bits()Read 24 bits in BigEndian order from the stream and return the corresponding integer value.protected java.lang.StringreadFixedLengthString(int length, java.lang.String charset)Read a byte array of fixed length from the input and convert it to a string using the given charset, removing any trailing '\0'.
-
-
-
Constructor Detail
-
BinaryReader
protected BinaryReader(int magicNumber, int minVersion, java.io.DataInputStream dis)Create a new BinaryReader that reads from the given stream and that expected the given magic number and at least the given minimum version.- Parameters:
magicNumber- Magic number of files to be read.minVersion- Minimum version of files to be read.dis- Input stream from which to read.
-
-
Method Detail
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
checkVersionOrThrow
protected void checkVersionOrThrow(int version) throws BadVersionExceptionCheck if the given version is greater than the minimum version, and update the current version if it is.- Parameters:
version- Version to check.- Throws:
BadVersionException- if the given version is not greater than the minimum version.
-
getCurrentVersion
protected int getCurrentVersion()
- Returns:
- The current version.
-
checkMagicNumberOrThrow
protected void checkMagicNumberOrThrow(int magicNumber) throws BadMagicNumberExceptionCheck if the given number matches the expected magic number.- Parameters:
magicNumber- The magic number to check.- Throws:
BadMagicNumberException- If the two magic numbers are not equal.
-
checkByteOrThrow
protected void checkByteOrThrow(int b) throws java.io.IOExceptionCheck if the next byte in the input stream correspond to the given byte. This function consumes the next byte in the input stream.- Parameters:
b- Byte to check.- Throws:
java.io.IOException- if an error occurs while reading the byte.BadFormatException- if the byte read is not the expected one.
-
readFixedLengthString
protected java.lang.String readFixedLengthString(int length, java.lang.String charset) throws java.io.IOExceptionRead a byte array of fixed length from the input and convert it to a string using the given charset, removing any trailing '\0'.- Parameters:
length- Number of bytes to read.charset- Charset to use to convert the bytes into a string.- Returns:
- The convert string.
- Throws:
java.io.IOException- if an error occurs while reading or converting.
-
read24bits
protected int read24bits() throws java.io.IOExceptionRead 24 bits in BigEndian order from the stream and return the corresponding integer value.- Returns:
- Integer value read from the next 24 bits of the stream.
- Throws:
java.io.IOException- if an error occurs while reading from the stream.
-
-