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.Closeable
Base class for writing binary file.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.io.DataInputStream
dis
-
Constructor Summary
Constructors Modifier Constructor Description 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
checkByteOrThrow(int b)
Check if the next byte in the input stream correspond to the given byte.protected void
checkMagicNumberOrThrow(int magicNumber)
Check if the given number matches the expected magic number.protected void
checkVersionOrThrow(int version)
Check if the given version is greater than the minimum version, and update the current version if it is.void
close()
protected int
getCurrentVersion()
protected int
read24bits()
Read 24 bits in BigEndian order from the stream and return the corresponding integer value.protected java.lang.String
readFixedLengthString(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:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
checkVersionOrThrow
protected void checkVersionOrThrow(int version) throws BadVersionException
Check 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 BadMagicNumberException
Check 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.IOException
Check 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.IOException
Read 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.IOException
Read 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.
-
-