Package com.twelvemonkeys.io
Class LittleEndianRandomAccessFile
- java.lang.Object
-
- com.twelvemonkeys.io.LittleEndianRandomAccessFile
-
- All Implemented Interfaces:
java.io.DataInput
,java.io.DataOutput
public class LittleEndianRandomAccessFile extends java.lang.Object implements java.io.DataInput, java.io.DataOutput
A replacement forRandomAccessFile
that is capable of reading and writing data in little endian byte order.Warning: The
DataInput
andDataOutput
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:
LittleEndianDataInputStream
,LittleEndianDataOutputStream
,RandomAccessFile
,DataInput
,DataOutput
-
-
Constructor Summary
Constructors Constructor Description LittleEndianRandomAccessFile(java.io.File pFile, java.lang.String pMode)
LittleEndianRandomAccessFile(java.lang.String pName, java.lang.String pMode)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
java.nio.channels.FileChannel
getChannel()
java.io.FileDescriptor
getFD()
long
getFilePointer()
long
length()
int
read()
int
read(byte[] b)
int
read(byte[] b, int off, int len)
boolean
readBoolean()
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.double
readDouble()
float
readFloat()
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.java.lang.String
readLine()
long
readLong()
Reads an eight byte signedint
from the underlying input stream in little endian order, low byte first.short
readShort()
Reads a two byte signedshort
from the underlying input stream in little endian order, low byte first.int
readUnsignedByte()
Reads an unsignedbyte
from the underlying input stream with value between 0 and 255int
readUnsignedShort()
Reads a two byte unsignedshort
from the underlying input stream in little endian order, low byte first.java.lang.String
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(java.lang.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(java.lang.String pString)
Writes a string to the underlying output stream as a sequence of characters.void
writeDouble(double d)
Writes an 8 byte Java double to the underlying output stream in little endian order.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
writeUTF(java.lang.String pString)
Writes a string of no more than 65,535 characters to the underlying output stream using UTF-8 encoding.
-
-
-
Constructor Detail
-
LittleEndianRandomAccessFile
public LittleEndianRandomAccessFile(java.lang.String pName, java.lang.String pMode) throws java.io.FileNotFoundException
- Throws:
java.io.FileNotFoundException
-
LittleEndianRandomAccessFile
public LittleEndianRandomAccessFile(java.io.File pFile, java.lang.String pMode) throws java.io.FileNotFoundException
- Throws:
java.io.FileNotFoundException
-
-
Method Detail
-
close
public void close() throws java.io.IOException
- Throws:
java.io.IOException
-
getChannel
public java.nio.channels.FileChannel getChannel()
-
getFD
public java.io.FileDescriptor getFD() throws java.io.IOException
- Throws:
java.io.IOException
-
getFilePointer
public long getFilePointer() throws java.io.IOException
- Throws:
java.io.IOException
-
length
public long length() throws java.io.IOException
- Throws:
java.io.IOException
-
read
public int read() throws java.io.IOException
- Throws:
java.io.IOException
-
read
public int read(byte[] b) throws java.io.IOException
- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException
- Throws:
java.io.IOException
-
readFully
public void readFully(byte[] b) throws java.io.IOException
- Specified by:
readFully
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readFully
public void readFully(byte[] b, int off, int len) throws java.io.IOException
- Specified by:
readFully
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readLine
public java.lang.String readLine() throws java.io.IOException
- Specified by:
readLine
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readBoolean
public boolean readBoolean() throws java.io.IOException
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 interfacejava.io.DataInput
- Returns:
- the
boolean
value read. - Throws:
java.io.EOFException
- if the end of the underlying input stream has been reachedjava.io.IOException
- if the underlying stream throws an IOException.
-
readByte
public byte readByte() throws java.io.IOException
Reads a signedbyte
from the underlying input stream with value between -128 and 127- Specified by:
readByte
in interfacejava.io.DataInput
- Returns:
- the
byte
value read. - Throws:
java.io.EOFException
- if the end of the underlying input stream has been reachedjava.io.IOException
- if the underlying stream throws an IOException.
-
readUnsignedByte
public int readUnsignedByte() throws java.io.IOException
Reads an unsignedbyte
from the underlying input stream with value between 0 and 255- Specified by:
readUnsignedByte
in interfacejava.io.DataInput
- Returns:
- the
byte
value read. - Throws:
java.io.EOFException
- if the end of the underlying input stream has been reachedjava.io.IOException
- if the underlying stream throws an IOException.
-
readShort
public short readShort() throws java.io.IOException
Reads a two byte signedshort
from the underlying input stream in little endian order, low byte first.- Specified by:
readShort
in interfacejava.io.DataInput
- Returns:
- the
short
read. - Throws:
java.io.EOFException
- if the end of the underlying input stream has been reachedjava.io.IOException
- if the underlying stream throws an IOException.
-
readUnsignedShort
public int readUnsignedShort() throws java.io.IOException
Reads a two byte unsignedshort
from the underlying input stream in little endian order, low byte first.- Specified by:
readUnsignedShort
in interfacejava.io.DataInput
- Returns:
- the int value of the unsigned short read.
- Throws:
java.io.EOFException
- if the end of the underlying input stream has been reachedjava.io.IOException
- if the underlying stream throws an IOException.
-
readChar
public char readChar() throws java.io.IOException
Reads a two byte Unicodechar
from the underlying input stream in little endian order, low byte first.- Specified by:
readChar
in interfacejava.io.DataInput
- Returns:
- the int value of the unsigned short read.
- Throws:
java.io.EOFException
- if the end of the underlying input stream has been reachedjava.io.IOException
- if the underlying stream throws an IOException.
-
readInt
public int readInt() throws java.io.IOException
Reads a four byte signedint
from the underlying input stream in little endian order, low byte first.- Specified by:
readInt
in interfacejava.io.DataInput
- Returns:
- the
int
read. - Throws:
java.io.EOFException
- if the end of the underlying input stream has been reachedjava.io.IOException
- if the underlying stream throws an IOException.
-
readLong
public long readLong() throws java.io.IOException
Reads an eight byte signedint
from the underlying input stream in little endian order, low byte first.- Specified by:
readLong
in interfacejava.io.DataInput
- Returns:
- the
int
read. - Throws:
java.io.EOFException
- if the end of the underlying input stream has been reachedjava.io.IOException
- if the underlying stream throws an IOException.
-
readUTF
public java.lang.String readUTF() throws java.io.IOException
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 interfacejava.io.DataInput
- Returns:
- the decoded string
- Throws:
java.io.UTFDataFormatException
- if the string cannot be decodedjava.io.IOException
- if the underlying stream throws an IOException.
-
readDouble
public final double readDouble() throws java.io.IOException
- Specified by:
readDouble
in interfacejava.io.DataInput
- Returns:
- the next eight bytes of this input stream, interpreted as a
little endian
double
. - Throws:
java.io.EOFException
- if end of stream occurs before eight bytes have been read.java.io.IOException
- if an I/O error occurs.
-
readFloat
public final float readFloat() throws java.io.IOException
- Specified by:
readFloat
in interfacejava.io.DataInput
- Returns:
- the next four bytes of this input stream, interpreted as a
little endian
int
. - Throws:
java.io.EOFException
- if end of stream occurs before four bytes have been read.java.io.IOException
- if an I/O error occurs.
-
seek
public void seek(long pos) throws java.io.IOException
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:
java.io.IOException
- ifpos
is less than0
or if an I/O error occurs.
-
setLength
public void setLength(long newLength) throws java.io.IOException
- Throws:
java.io.IOException
-
skipBytes
public int skipBytes(int n) throws java.io.IOException
- Specified by:
skipBytes
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
write
public void write(byte[] b) throws java.io.IOException
- Specified by:
write
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException
- Specified by:
write
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOException
- Specified by:
write
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeBoolean
public void writeBoolean(boolean pBoolean) throws java.io.IOException
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 interfacejava.io.DataOutput
- Parameters:
pBoolean
- theboolean
value to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.
-
writeByte
public void writeByte(int pByte) throws java.io.IOException
Writes out abyte
to the underlying output stream- Specified by:
writeByte
in interfacejava.io.DataOutput
- Parameters:
pByte
- thebyte
value to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.
-
writeShort
public void writeShort(int pShort) throws java.io.IOException
Writes a two byteshort
to the underlying output stream in little endian order, low byte first.- Specified by:
writeShort
in interfacejava.io.DataOutput
- Parameters:
pShort
- theshort
to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.
-
writeChar
public void writeChar(int pChar) throws java.io.IOException
Writes a two bytechar
to the underlying output stream in little endian order, low byte first.- Specified by:
writeChar
in interfacejava.io.DataOutput
- Parameters:
pChar
- thechar
value to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.
-
writeInt
public void writeInt(int pInt) throws java.io.IOException
Writes a four-byteint
to the underlying output stream in little endian order, low byte first, high byte last- Specified by:
writeInt
in interfacejava.io.DataOutput
- Parameters:
pInt
- theint
to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.
-
writeLong
public void writeLong(long pLong) throws java.io.IOException
Writes an eight-bytelong
to the underlying output stream in little endian order, low byte first, high byte last- Specified by:
writeLong
in interfacejava.io.DataOutput
- Parameters:
pLong
- thelong
to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.
-
writeFloat
public final void writeFloat(float f) throws java.io.IOException
Writes a 4 byte Java float to the underlying output stream in little endian order.- Specified by:
writeFloat
in interfacejava.io.DataOutput
- Parameters:
f
- thefloat
value to be written.- Throws:
java.io.IOException
- if an I/O error occurs.
-
writeDouble
public final void writeDouble(double d) throws java.io.IOException
Writes an 8 byte Java double to the underlying output stream in little endian order.- Specified by:
writeDouble
in interfacejava.io.DataOutput
- Parameters:
d
- thedouble
value to be written.- Throws:
java.io.IOException
- if an I/O error occurs.
-
writeBytes
public void writeBytes(java.lang.String pString) throws java.io.IOException
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 interfacejava.io.DataOutput
- Parameters:
pString
- theString
value to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.- See Also:
writeByte(int)
,file
-
writeChars
public void writeChars(java.lang.String pString) throws java.io.IOException
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 interfacejava.io.DataOutput
- Parameters:
pString
- aString
value to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.- See Also:
writeChar(int)
,file
-
writeUTF
public void writeUTF(java.lang.String pString) throws java.io.IOException
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 interfacejava.io.DataOutput
- Parameters:
pString
- the string to be written.- Throws:
java.io.UTFDataFormatException
- if the string is longer than 65,535 characters.java.io.IOException
- if the underlying stream throws an IOException.
-
-