Package loci.common
Class RandomAccessOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- loci.common.RandomAccessOutputStream
-
- All Implemented Interfaces:
Closeable,DataOutput,Flushable,AutoCloseable
public class RandomAccessOutputStream extends OutputStream implements DataOutput, Closeable
RandomAccessOutputStream provides methods for writing to files and byte arrays.
-
-
Constructor Summary
Constructors Constructor Description RandomAccessOutputStream(byte[] array)Constructs a random access stream around the given byte array.RandomAccessOutputStream(String file)Constructs a random access stream around the given file.RandomAccessOutputStream(IRandomAccess handle)Constructs a random access stream around the given handle.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidflush()longgetFilePointer()booleanisLittleEndian()Gets the endianness of the stream.longlength()voidorder(boolean little)Sets the endianness of the stream.voidseek(long pos)Seeks to the given offset within the stream.voidskipBytes(int skip)Advances the current offset by the given number of bytes.voidwrite(byte[] b)voidwrite(byte[] b, int off, int len)voidwrite(int b)voidwrite(ByteBuffer b)Writes bytes to the stream from the given buffer.voidwrite(ByteBuffer b, int off, int len)voidwriteBits(int value, int numBits)Writes the given value using the given number of bits.voidwriteBits(String bitString)Writes the bits represented by a bit string to the buffer.voidwriteBoolean(boolean v)voidwriteByte(int v)voidwriteBytes(String s)voidwriteChar(int v)voidwriteChars(String s)voidwriteDouble(double v)voidwriteFloat(float v)voidwriteInt(int v)voidwriteLine(String s)Writes the given string followed by a newline character.voidwriteLong(long v)voidwriteShort(int v)voidwriteUTF(String str)-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Constructor Detail
-
RandomAccessOutputStream
public RandomAccessOutputStream(String file) throws IOException
Constructs a random access stream around the given file.- Parameters:
file- Filename to open the stream for.- Throws:
IOException- If there is a problem opening the file.
-
RandomAccessOutputStream
public RandomAccessOutputStream(IRandomAccess handle)
Constructs a random access stream around the given handle.- Parameters:
handle- Handle to open the stream for.
-
RandomAccessOutputStream
public RandomAccessOutputStream(byte[] array) throws IOExceptionConstructs a random access stream around the given byte array.- Parameters:
array- the byte array to be written to- Throws:
IOException- if the array cannot be wrapped in aByteArrayHandle
-
-
Method Detail
-
seek
public void seek(long pos) throws IOExceptionSeeks to the given offset within the stream.- Parameters:
pos- the new offset within the stream- Throws:
IOException- is the seek is not successful
-
getFilePointer
public long getFilePointer() throws IOException- Returns:
- the current offset within the stream.
- Throws:
IOException- if the offset cannot be retrieved
-
length
public long length() throws IOException- Returns:
- the length of the file
- Throws:
IOException- if the length cannot be retrieved
-
skipBytes
public void skipBytes(int skip) throws IOExceptionAdvances the current offset by the given number of bytes.- Parameters:
skip- the number of bytes to skip- Throws:
IOException- if the offset cannot be changed
-
order
public void order(boolean little)
Sets the endianness of the stream.- Parameters:
little- true if the byte order of the stream is little-endian
-
isLittleEndian
public boolean isLittleEndian()
Gets the endianness of the stream.- Returns:
- true if the byte order of the stream is little-endian
-
writeLine
public void writeLine(String s) throws IOException
Writes the given string followed by a newline character.- Parameters:
s- the line of text to be written. A newline does not need to be appended, as this method automatically writes a newline character.- Throws:
IOException- if writing is not possible
-
writeBits
public void writeBits(int value, int numBits) throws IOExceptionWrites the given value using the given number of bits.- Parameters:
value- int value to be writtennumBits- exact number of bits to be written- Throws:
IOException- if writing fails for any reason
-
writeBits
public void writeBits(String bitString) throws IOException
Writes the bits represented by a bit string to the buffer.- Parameters:
bitString- a string of '0' and/or '1' characters representing bits to be written. Each character in the string results in a call towriteBits(int, int)that writes 1 bit.- Throws:
IllegalArgumentException- If any characters other than '0' and '1' appear in the string.IOException- if writing fails for any other reason
-
write
public void write(byte[] b) throws IOException- Specified by:
writein interfaceDataOutput- Overrides:
writein classOutputStream- Throws:
IOException
-
write
public void write(byte[] b, int off, int len) throws IOException- Specified by:
writein interfaceDataOutput- Overrides:
writein classOutputStream- Throws:
IOException
-
write
public void write(ByteBuffer b) throws IOException
Writes bytes to the stream from the given buffer.- Parameters:
b- Source buffer to read data from.- Throws:
IOException- If there is an error writing to the stream.
-
write
public void write(ByteBuffer b, int off, int len) throws IOException
- Parameters:
b- Source buffer to read data from.off- Offset within the buffer to start reading from.len- Number of bytes to read.- Throws:
IOException- If there is an error writing to the stream.
-
write
public void write(int b) throws IOException- Specified by:
writein interfaceDataOutput- Specified by:
writein classOutputStream- Throws:
IOException
-
writeBoolean
public void writeBoolean(boolean v) throws IOException- Specified by:
writeBooleanin interfaceDataOutput- Throws:
IOException
-
writeByte
public void writeByte(int v) throws IOException- Specified by:
writeBytein interfaceDataOutput- Throws:
IOException
-
writeBytes
public void writeBytes(String s) throws IOException
- Specified by:
writeBytesin interfaceDataOutput- Throws:
IOException
-
writeChar
public void writeChar(int v) throws IOException- Specified by:
writeCharin interfaceDataOutput- Throws:
IOException
-
writeChars
public void writeChars(String s) throws IOException
- Specified by:
writeCharsin interfaceDataOutput- Throws:
IOException
-
writeDouble
public void writeDouble(double v) throws IOException- Specified by:
writeDoublein interfaceDataOutput- Throws:
IOException
-
writeFloat
public void writeFloat(float v) throws IOException- Specified by:
writeFloatin interfaceDataOutput- Throws:
IOException
-
writeInt
public void writeInt(int v) throws IOException- Specified by:
writeIntin interfaceDataOutput- Throws:
IOException
-
writeLong
public void writeLong(long v) throws IOException- Specified by:
writeLongin interfaceDataOutput- Throws:
IOException
-
writeShort
public void writeShort(int v) throws IOException- Specified by:
writeShortin interfaceDataOutput- Throws:
IOException
-
writeUTF
public void writeUTF(String str) throws IOException
- Specified by:
writeUTFin interfaceDataOutput- Throws:
IOException
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
flush
public void flush() throws IOException- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException
-
-