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 void
close()
void
flush()
long
getFilePointer()
boolean
isLittleEndian()
Gets the endianness of the stream.long
length()
void
order(boolean little)
Sets the endianness of the stream.void
seek(long pos)
Seeks to the given offset within the stream.void
skipBytes(int skip)
Advances the current offset by the given number of bytes.void
write(byte[] b)
void
write(byte[] b, int off, int len)
void
write(int b)
void
write(ByteBuffer b)
Writes bytes to the stream from the given buffer.void
write(ByteBuffer b, int off, int len)
void
writeBits(int value, int numBits)
Writes the given value using the given number of bits.void
writeBits(String bitString)
Writes the bits represented by a bit string to the buffer.void
writeBoolean(boolean v)
void
writeByte(int v)
void
writeBytes(String s)
void
writeChar(int v)
void
writeChars(String s)
void
writeDouble(double v)
void
writeFloat(float v)
void
writeInt(int v)
void
writeLine(String s)
Writes the given string followed by a newline character.void
writeLong(long v)
void
writeShort(int v)
void
writeUTF(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 IOException
Constructs 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 IOException
Seeks 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 IOException
Advances 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 IOException
Writes 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:
write
in interfaceDataOutput
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(byte[] b, int off, int len) throws IOException
- Specified by:
write
in interfaceDataOutput
- Overrides:
write
in 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:
write
in interfaceDataOutput
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
writeBoolean
public void writeBoolean(boolean v) throws IOException
- Specified by:
writeBoolean
in interfaceDataOutput
- Throws:
IOException
-
writeByte
public void writeByte(int v) throws IOException
- Specified by:
writeByte
in interfaceDataOutput
- Throws:
IOException
-
writeBytes
public void writeBytes(String s) throws IOException
- Specified by:
writeBytes
in interfaceDataOutput
- Throws:
IOException
-
writeChar
public void writeChar(int v) throws IOException
- Specified by:
writeChar
in interfaceDataOutput
- Throws:
IOException
-
writeChars
public void writeChars(String s) throws IOException
- Specified by:
writeChars
in interfaceDataOutput
- Throws:
IOException
-
writeDouble
public void writeDouble(double v) throws IOException
- Specified by:
writeDouble
in interfaceDataOutput
- Throws:
IOException
-
writeFloat
public void writeFloat(float v) throws IOException
- Specified by:
writeFloat
in interfaceDataOutput
- Throws:
IOException
-
writeInt
public void writeInt(int v) throws IOException
- Specified by:
writeInt
in interfaceDataOutput
- Throws:
IOException
-
writeLong
public void writeLong(long v) throws IOException
- Specified by:
writeLong
in interfaceDataOutput
- Throws:
IOException
-
writeShort
public void writeShort(int v) throws IOException
- Specified by:
writeShort
in interfaceDataOutput
- Throws:
IOException
-
writeUTF
public void writeUTF(String str) throws IOException
- Specified by:
writeUTF
in interfaceDataOutput
- Throws:
IOException
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
-
flush
public void flush() throws IOException
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
-