Package loci.common
Class RandomAccessOutputStream
java.lang.Object
java.io.OutputStream
loci.common.RandomAccessOutputStream
- All Implemented Interfaces:
java.io.Closeable
,java.io.DataOutput
,java.io.Flushable
,java.lang.AutoCloseable
public class RandomAccessOutputStream
extends java.io.OutputStream
implements java.io.DataOutput, java.io.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(java.lang.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
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(java.nio.ByteBuffer b)
Writes bytes to the stream from the given buffer.void
write(java.nio.ByteBuffer b, int off, int len)
void
writeBits(int value, int numBits)
Writes the given value using the given number of bits.void
writeBits(java.lang.String bitString)
Writes the bits represented by a bit string to the buffer.void
writeBoolean(boolean v)
void
writeByte(int v)
void
writeBytes(java.lang.String s)
void
writeChar(int v)
void
writeChars(java.lang.String s)
void
writeDouble(double v)
void
writeFloat(float v)
void
writeInt(int v)
void
writeLine(java.lang.String s)
Writes the given string followed by a newline character.void
writeLong(long v)
void
writeShort(int v)
void
writeUTF(java.lang.String str)
-
Constructor Details
-
RandomAccessOutputStream
public RandomAccessOutputStream(java.lang.String file) throws java.io.IOExceptionConstructs a random access stream around the given file.- Parameters:
file
- Filename to open the stream for.- Throws:
java.io.IOException
- If there is a problem opening the file.
-
RandomAccessOutputStream
Constructs a random access stream around the given handle.- Parameters:
handle
- Handle to open the stream for.
-
RandomAccessOutputStream
public RandomAccessOutputStream(byte[] array) throws java.io.IOExceptionConstructs a random access stream around the given byte array.- Parameters:
array
- the byte array to be written to- Throws:
java.io.IOException
- if the array cannot be wrapped in aByteArrayHandle
-
-
Method Details
-
seek
public void seek(long pos) throws java.io.IOExceptionSeeks to the given offset within the stream.- Parameters:
pos
- the new offset within the stream- Throws:
java.io.IOException
- is the seek is not successful
-
getFilePointer
public long getFilePointer() throws java.io.IOException- Returns:
- the current offset within the stream.
- Throws:
java.io.IOException
- if the offset cannot be retrieved
-
length
public long length() throws java.io.IOException- Returns:
- the length of the file
- Throws:
java.io.IOException
- if the length cannot be retrieved
-
skipBytes
public void skipBytes(int skip) throws java.io.IOExceptionAdvances the current offset by the given number of bytes.- Parameters:
skip
- the number of bytes to skip- Throws:
java.io.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(java.lang.String s) throws java.io.IOExceptionWrites 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:
java.io.IOException
- if writing is not possible
-
writeBits
public void writeBits(int value, int numBits) throws java.io.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:
java.io.IOException
- if writing fails for any reason
-
writeBits
public void writeBits(java.lang.String bitString) throws java.io.IOExceptionWrites 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:
java.lang.IllegalArgumentException
- If any characters other than '0' and '1' appear in the string.java.io.IOException
- if writing fails for any other reason
-
write
public void write(byte[] b) throws java.io.IOException- Specified by:
write
in interfacejava.io.DataOutput
- Overrides:
write
in classjava.io.OutputStream
- 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
- Overrides:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
write
public void write(java.nio.ByteBuffer b) throws java.io.IOExceptionWrites bytes to the stream from the given buffer.- Parameters:
b
- Source buffer to read data from.- Throws:
java.io.IOException
- If there is an error writing to the stream.
-
write
public void write(java.nio.ByteBuffer b, int off, int len) throws java.io.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:
java.io.IOException
- If there is an error writing to the stream.
-
write
public void write(int b) throws java.io.IOException- Specified by:
write
in interfacejava.io.DataOutput
- Specified by:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
writeBoolean
public void writeBoolean(boolean v) throws java.io.IOException- Specified by:
writeBoolean
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeByte
public void writeByte(int v) throws java.io.IOException- Specified by:
writeByte
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeBytes
public void writeBytes(java.lang.String s) throws java.io.IOException- Specified by:
writeBytes
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeChar
public void writeChar(int v) throws java.io.IOException- Specified by:
writeChar
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeChars
public void writeChars(java.lang.String s) throws java.io.IOException- Specified by:
writeChars
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeDouble
public void writeDouble(double v) throws java.io.IOException- Specified by:
writeDouble
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeFloat
public void writeFloat(float v) throws java.io.IOException- Specified by:
writeFloat
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeInt
public void writeInt(int v) throws java.io.IOException- Specified by:
writeInt
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeLong
public void writeLong(long v) throws java.io.IOException- Specified by:
writeLong
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeShort
public void writeShort(int v) throws java.io.IOException- Specified by:
writeShort
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeUTF
public void writeUTF(java.lang.String str) throws java.io.IOException- Specified by:
writeUTF
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException- Specified by:
flush
in interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.OutputStream
- Throws:
java.io.IOException
-