Package com.twelvemonkeys.io
Class LittleEndianRandomAccessFile
java.lang.Object
com.twelvemonkeys.io.LittleEndianRandomAccessFile
- All Implemented Interfaces:
DataInput
,DataOutput
A replacement for
RandomAccessFile
that is capable of reading
and writing data in little endian byte order.
Warning:
The DataInput
and DataOutput
interfaces
specifies big endian byte order in their documentation.
This means that this class is, strictly speaking, not a proper
implementation. However, I don't see a reason for the these interfaces to
specify the byte order of their underlying representations.
- Version:
- $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/io/LittleEndianRandomAccessFile.java#1 $
- Author:
- Elliotte Rusty Harold, Harald Kuhr, last modified by $Author: haku $
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionLittleEndianRandomAccessFile
(File pFile, String pMode) LittleEndianRandomAccessFile
(String pName, String pMode) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
getFD()
long
long
length()
int
read()
int
read
(byte[] b) int
read
(byte[] b, int off, int len) boolean
Reads aboolean
from the underlying input stream by reading a single byte.byte
readByte()
Reads a signedbyte
from the underlying input stream with value between -128 and 127char
readChar()
Reads a two byte Unicodechar
from the underlying input stream in little endian order, low byte first.final double
final float
void
readFully
(byte[] b) void
readFully
(byte[] b, int off, int len) int
readInt()
Reads a four byte signedint
from the underlying input stream in little endian order, low byte first.readLine()
long
readLong()
Reads an eight byte signedint
from the underlying input stream in little endian order, low byte first.short
Reads a two byte signedshort
from the underlying input stream in little endian order, low byte first.int
Reads an unsignedbyte
from the underlying input stream with value between 0 and 255int
Reads a two byte unsignedshort
from the underlying input stream in little endian order, low byte first.readUTF()
Reads a string of no more than 65,535 characters from the underlying input stream using UTF-8 encoding.void
seek
(long pos) Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.void
setLength
(long newLength) int
skipBytes
(int n) void
write
(byte[] b) void
write
(byte[] b, int off, int len) void
write
(int b) void
writeBoolean
(boolean pBoolean) Writes aboolean
to the underlying output stream as a single byte.void
writeByte
(int pByte) Writes out abyte
to the underlying output streamvoid
writeBytes
(String pString) Writes a string to the underlying output stream as a sequence of bytes.void
writeChar
(int pChar) Writes a two bytechar
to the underlying output stream in little endian order, low byte first.void
writeChars
(String pString) Writes a string to the underlying output stream as a sequence of characters.final void
writeDouble
(double d) Writes an 8 byte Java double to the underlying output stream in little endian order.final void
writeFloat
(float f) Writes a 4 byte Java float to the underlying output stream in little endian order.void
writeInt
(int pInt) Writes a four-byteint
to the underlying output stream in little endian order, low byte first, high byte lastvoid
writeLong
(long pLong) Writes an eight-bytelong
to the underlying output stream in little endian order, low byte first, high byte lastvoid
writeShort
(int pShort) Writes a two byteshort
to the underlying output stream in little endian order, low byte first.void
Writes a string of no more than 65,535 characters to the underlying output stream using UTF-8 encoding.
-
Constructor Details
-
LittleEndianRandomAccessFile
- Throws:
FileNotFoundException
-
LittleEndianRandomAccessFile
- Throws:
FileNotFoundException
-
-
Method Details
-
close
- Throws:
IOException
-
getChannel
-
getFD
- Throws:
IOException
-
getFilePointer
- Throws:
IOException
-
length
- Throws:
IOException
-
read
- Throws:
IOException
-
read
- Throws:
IOException
-
read
- Throws:
IOException
-
readFully
- Specified by:
readFully
in interfaceDataInput
- Throws:
IOException
-
readFully
- Specified by:
readFully
in interfaceDataInput
- Throws:
IOException
-
readLine
- Specified by:
readLine
in interfaceDataInput
- Throws:
IOException
-
readBoolean
Reads aboolean
from the underlying input stream by reading a single byte. If the byte is zero, false is returned. If the byte is positive, true is returned.- Specified by:
readBoolean
in interfaceDataInput
- Returns:
- the
boolean
value read. - Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readByte
Reads a signedbyte
from the underlying input stream with value between -128 and 127- Specified by:
readByte
in interfaceDataInput
- Returns:
- the
byte
value read. - Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readUnsignedByte
Reads an unsignedbyte
from the underlying input stream with value between 0 and 255- Specified by:
readUnsignedByte
in interfaceDataInput
- Returns:
- the
byte
value read. - Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readShort
Reads a two byte signedshort
from the underlying input stream in little endian order, low byte first.- Specified by:
readShort
in interfaceDataInput
- Returns:
- the
short
read. - Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readUnsignedShort
Reads a two byte unsignedshort
from the underlying input stream in little endian order, low byte first.- Specified by:
readUnsignedShort
in interfaceDataInput
- Returns:
- the int value of the unsigned short read.
- Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readChar
Reads a two byte Unicodechar
from the underlying input stream in little endian order, low byte first.- Specified by:
readChar
in interfaceDataInput
- Returns:
- the int value of the unsigned short read.
- Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readInt
Reads a four byte signedint
from the underlying input stream in little endian order, low byte first.- Specified by:
readInt
in interfaceDataInput
- Returns:
- the
int
read. - Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readLong
Reads an eight byte signedint
from the underlying input stream in little endian order, low byte first.- Specified by:
readLong
in interfaceDataInput
- Returns:
- the
int
read. - Throws:
EOFException
- if the end of the underlying input stream has been reachedIOException
- if the underlying stream throws an IOException.
-
readUTF
Reads a string of no more than 65,535 characters from the underlying input stream using UTF-8 encoding. This method first reads a two byte short in big endian order as required by the UTF-8 specification. This gives the number of bytes in the UTF-8 encoded version of the string. Next this many bytes are read and decoded as UTF-8 encoded characters.- Specified by:
readUTF
in interfaceDataInput
- Returns:
- the decoded string
- Throws:
UTFDataFormatException
- if the string cannot be decodedIOException
- if the underlying stream throws an IOException.
-
readDouble
- Specified by:
readDouble
in interfaceDataInput
- Returns:
- the next eight bytes of this input stream, interpreted as a
little endian
double
. - Throws:
EOFException
- if end of stream occurs before eight bytes have been read.IOException
- if an I/O error occurs.
-
readFloat
- Specified by:
readFloat
in interfaceDataInput
- Returns:
- the next four bytes of this input stream, interpreted as a
little endian
int
. - Throws:
EOFException
- if end of stream occurs before four bytes have been read.IOException
- if an I/O error occurs.
-
seek
Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length. The file length will change only by writing after the offset has been set beyond the end of the file.- Parameters:
pos
- the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.- Throws:
IOException
- ifpos
is less than0
or if an I/O error occurs.
-
setLength
- Throws:
IOException
-
skipBytes
- Specified by:
skipBytes
in interfaceDataInput
- Throws:
IOException
-
write
- Specified by:
write
in interfaceDataOutput
- Throws:
IOException
-
write
- Specified by:
write
in interfaceDataOutput
- Throws:
IOException
-
write
- Specified by:
write
in interfaceDataOutput
- Throws:
IOException
-
writeBoolean
Writes aboolean
to the underlying output stream as a single byte. If the argument is true, the byte value 1 is written. If the argument is false, the byte value0
in written.- Specified by:
writeBoolean
in interfaceDataOutput
- Parameters:
pBoolean
- theboolean
value to be written.- Throws:
IOException
- if the underlying stream throws an IOException.
-
writeByte
Writes out abyte
to the underlying output stream- Specified by:
writeByte
in interfaceDataOutput
- Parameters:
pByte
- thebyte
value to be written.- Throws:
IOException
- if the underlying stream throws an IOException.
-
writeShort
Writes a two byteshort
to the underlying output stream in little endian order, low byte first.- Specified by:
writeShort
in interfaceDataOutput
- Parameters:
pShort
- theshort
to be written.- Throws:
IOException
- if the underlying stream throws an IOException.
-
writeChar
Writes a two bytechar
to the underlying output stream in little endian order, low byte first.- Specified by:
writeChar
in interfaceDataOutput
- Parameters:
pChar
- thechar
value to be written.- Throws:
IOException
- if the underlying stream throws an IOException.
-
writeInt
Writes a four-byteint
to the underlying output stream in little endian order, low byte first, high byte last- Specified by:
writeInt
in interfaceDataOutput
- Parameters:
pInt
- theint
to be written.- Throws:
IOException
- if the underlying stream throws an IOException.
-
writeLong
Writes an eight-bytelong
to the underlying output stream in little endian order, low byte first, high byte last- Specified by:
writeLong
in interfaceDataOutput
- Parameters:
pLong
- thelong
to be written.- Throws:
IOException
- if the underlying stream throws an IOException.
-
writeFloat
Writes a 4 byte Java float to the underlying output stream in little endian order.- Specified by:
writeFloat
in interfaceDataOutput
- Parameters:
f
- thefloat
value to be written.- Throws:
IOException
- if an I/O error occurs.
-
writeDouble
Writes an 8 byte Java double to the underlying output stream in little endian order.- Specified by:
writeDouble
in interfaceDataOutput
- Parameters:
d
- thedouble
value to be written.- Throws:
IOException
- if an I/O error occurs.
-
writeBytes
Writes a string to the underlying output stream as a sequence of bytes. Each character is written to the data output stream as if by thewriteByte()
method.- Specified by:
writeBytes
in interfaceDataOutput
- Parameters:
pString
- theString
value to be written.- Throws:
IOException
- if the underlying stream throws an IOException.- See Also:
-
writeByte(int)
file
-
writeChars
Writes a string to the underlying output stream as a sequence of characters. Each character is written to the data output stream as if by thewriteChar
method.- Specified by:
writeChars
in interfaceDataOutput
- Parameters:
pString
- aString
value to be written.- Throws:
IOException
- if the underlying stream throws an IOException.- See Also:
-
writeChar(int)
file
-
writeUTF
Writes a string of no more than 65,535 characters to the underlying output stream using UTF-8 encoding. This method first writes a two byte short in big endian order as required by the UTF-8 specification. This gives the number of bytes in the UTF-8 encoded version of the string, not the number of characters in the string. Next each character of the string is written using the UTF-8 encoding for the character.- Specified by:
writeUTF
in interfaceDataOutput
- Parameters:
pString
- the string to be written.- Throws:
UTFDataFormatException
- if the string is longer than 65,535 characters.IOException
- if the underlying stream throws an IOException.
-