Interface DataHandle<L extends Location>
-
- All Superinterfaces:
AutoCloseable,Closeable,Comparable<Prioritized>,Contextual,DataInput,DataOutput,HasPluginInfo,Identifiable,Locatable,Logged,Prioritized,RichPlugin,SciJavaPlugin,Typed<L>,TypedPlugin<L>,Versioned,WrapperPlugin<L>
- All Known Subinterfaces:
ResettableStreamHandle<L>,SeekableStreamHandle<L>,StreamHandle<L>
- All Known Implementing Classes:
AbstractDataHandle,AbstractHigherOrderHandle,AbstractSeekableStreamHandle,AbstractStreamHandle,BytesHandle,DummyHandle,FileHandle,MultiWriteHandle,ReadBufferDataHandle,WriteBufferDataHandle
public interface DataHandle<L extends Location> extends WrapperPlugin<L>, DataInput, DataOutput, Closeable
A data handle is a plugin which provides both streaming and random access to bytes at aLocation(e.g., files or arrays).- Author:
- Curtis Rueden
- See Also:
DataHandleInputStream,DataHandleOutputStream
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classDataHandle.ByteOrder
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_BLOCK_SIZEDefault block size to use when searching through the stream.static intMAX_SEARCH_SIZEDefault bound on bytes to search when searching through the stream.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default longavailable(long count)Gets the number of bytes which can be read from, or written to, the data handle, bounded by the specified number of bytes.default Stringchecksum()Gets a "fast" checksum which succinctly represents the contents of the data stream.byte[]conversionBuffer()default voidensureReadable(long count)Ensures that the handle has sufficient bytes available to read.default booleanensureWritable(long count)Ensures that the handle has the correct length to be written to, and extends it as required.booleanexists()Tests whether this handle's location actually exists at the source.default StringfindString(boolean saveString, int blockSize, String... terminators)Reads or skips a string ending with one of the given terminating substrings, using the specified block size for buffering.default StringfindString(boolean saveString, String... terminators)Reads or skips a string ending with one of the given terminating substrings.default StringfindString(int blockSize, String... terminators)Reads a string ending with one of the given terminating substrings, using the specified block size for buffering.default StringfindString(String... terminators)Reads a string ending with one of the given terminating substrings.StringgetEncoding()Gets the native encoding of the stream.DataHandle.ByteOrdergetOrder()Returns the byte order of the stream.default booleanisBigEndian()Returns true iff the stream's order isDataHandle.ByteOrder.BIG_ENDIAN.default booleanisLittleEndian()Returns true iff the stream's order isDataHandle.ByteOrder.LITTLE_ENDIAN.booleanisReadable()Gets whether reading from this handle is supported.booleanisWritable()Gets whether writing to this handle is supported.default DatelastModified()Gets the last modified timestamp of the location.longlength()Returns the length of the data in bytes.longoffset()Returns the current offset in the stream.default intread()Reads the next byte of data from the stream.default intread(byte[] b)Reads up to b.length bytes of data from the stream into an array of bytes.intread(byte[] b, int off, int len)Reads up tolenbytes of data from the stream into an array of bytes.default booleanreadBoolean()default charreadChar()default StringreadCString()Reads a string of arbitrary length, terminated by a null char.default doublereadDouble()default floatreadFloat()default voidreadFully(byte[] b)default voidreadFully(byte[] b, int off, int len)default intreadInt()default StringreadLine()default longreadLong()default shortreadShort()default StringreadString(int n)Reads a string of up to length n.default StringreadString(String lastChars)Reads a string ending with one of the characters in the given string.default intreadUnsignedByte()default intreadUnsignedShort()default StringreadUTF()voidseek(long pos)Sets the stream offset, measured from the beginning of the stream, at which the next read or write occurs.voidsetEncoding(String encoding)Sets the native encoding of the stream.voidsetLength(long length)Sets the new length of the handle.default voidsetLittleEndian(boolean little)Sets the endianness of the stream.voidsetOrder(DataHandle.ByteOrder order)Sets the byte order of the stream.default longskip(long n)Skips over and discardsnbytes of data from the stream.default intskipBytes(int n)default voidwrite(byte[] b)default voidwriteBoolean(boolean v)default voidwriteByte(int v)default voidwriteBytes(String s)default voidwriteChar(int v)default voidwriteChars(String s)default voidwriteDouble(double v)default voidwriteFloat(float v)default voidwriteInt(int v)default voidwriteLine(String string)Writes the provided string, followed by a newline character.default voidwriteLong(long v)default voidwriteShort(int v)default voidwriteUTF(String str)-
Methods inherited from interface org.scijava.Contextual
context, getContext, setContext
-
Methods inherited from interface java.io.DataOutput
write, write
-
Methods inherited from interface org.scijava.plugin.HasPluginInfo
getInfo, setInfo
-
Methods inherited from interface org.scijava.Locatable
getLocation
-
Methods inherited from interface org.scijava.Prioritized
compareTo, getPriority, setPriority
-
Methods inherited from interface org.scijava.plugin.RichPlugin
getIdentifier, log
-
Methods inherited from interface org.scijava.Versioned
getVersion
-
Methods inherited from interface org.scijava.plugin.WrapperPlugin
get, set
-
-
-
-
Field Detail
-
DEFAULT_BLOCK_SIZE
static final int DEFAULT_BLOCK_SIZE
Default block size to use when searching through the stream.- See Also:
- Constant Field Values
-
MAX_SEARCH_SIZE
static final int MAX_SEARCH_SIZE
Default bound on bytes to search when searching through the stream.- See Also:
- Constant Field Values
-
-
Method Detail
-
isReadable
boolean isReadable()
Gets whether reading from this handle is supported.
-
isWritable
boolean isWritable()
Gets whether writing to this handle is supported.
-
exists
boolean exists() throws IOExceptionTests whether this handle's location actually exists at the source.- Returns:
- True if the location exists; false if not.
- Throws:
IOException- If something goes wrong with the existence check.
-
lastModified
default Date lastModified() throws IOException
Gets the last modified timestamp of the location.- Returns:
- The last modified timestamp, or null if the handle does not support this feature or if the location does not exist.
- Throws:
IOException- If something goes wrong with the last modified check.
-
checksum
default String checksum() throws IOException
Gets a "fast" checksum which succinctly represents the contents of the data stream. The term "fast" here refers to the idea that the checksum be retrievable quickly, without actually performing a thorough computation across the entire data stream. Typically, such a thing is feasible because the checksum was calculated a priori; e.g., artifacts deployed to remote Maven repositories are always deployed with corresponding checksum files.No guarantee is made about the exact nature of the checksum (e.g., SHA-1 or MD5), only that the value is deterministic for this particular location with its current contents. In other words: if a checksum differs from a previous inquiry, you can be sure the contents have changed; conversely, if the checksum is still the same, the contents are highly likely to be unchanged.
- Returns:
- The checksum, or null if the handle does not support this feature.
- Throws:
IOException- If something goes wrong when accessing the checksum.
-
offset
long offset() throws IOExceptionReturns the current offset in the stream.- Throws:
IOException
-
seek
void seek(long pos) throws IOException
Sets the stream offset, measured from the beginning of the stream, at which the next read or write occurs.- Throws:
IOException
-
length
long length() throws IOExceptionReturns the length of the data in bytes.- Returns:
- The length, or -1 if the length is unknown.
- Throws:
IOException
-
setLength
void setLength(long length) throws IOExceptionSets the new length of the handle.- Parameters:
length- New length.- Throws:
IOException- If there is an error changing the handle's length.
-
available
default long available(long count) throws IOExceptionGets the number of bytes which can be read from, or written to, the data handle, bounded by the specified number of bytes.In the case of reading, attempting to read the returned number of bytes is guaranteed not to throw
EOFException. However, be aware that the following methods might still process fewer bytes than indicated by this method:In the case of writing, attempting to write the returned number of bytes is guaranteed not to expand the length of the handle; i.e., the write will only overwrite bytes already within the handle's bounds.
- Parameters:
count- Desired number of bytes to read/write.- Returns:
- The actual number of bytes which could be read/written, which might be less than the requested value.
- Throws:
IOException- If something goes wrong with the check.
-
ensureReadable
default void ensureReadable(long count) throws IOExceptionEnsures that the handle has sufficient bytes available to read.- Parameters:
count- Number of bytes to read.- Throws:
EOFException- If there are insufficient bytes available.IOException- If the handle is write-only, or something goes wrong with the check.- See Also:
available(long)
-
ensureWritable
default boolean ensureWritable(long count) throws IOExceptionEnsures that the handle has the correct length to be written to, and extends it as required.- Parameters:
count- Number of bytes to write.- Returns:
trueif the handle's length was sufficient, orfalseif the handle's length required an extension.- Throws:
IOException- If the handle is read-only, or something goes wrong with the check, or there is an error changing the handle's length.
-
getOrder
DataHandle.ByteOrder getOrder()
Returns the byte order of the stream.
-
setOrder
void setOrder(DataHandle.ByteOrder order)
Sets the byte order of the stream.- Parameters:
order- Order to set.
-
isBigEndian
default boolean isBigEndian()
Returns true iff the stream's order isDataHandle.ByteOrder.BIG_ENDIAN.- See Also:
getOrder()
-
isLittleEndian
default boolean isLittleEndian()
Returns true iff the stream's order isDataHandle.ByteOrder.LITTLE_ENDIAN.- See Also:
getOrder()
-
setLittleEndian
default void setLittleEndian(boolean little)
Sets the endianness of the stream.- Parameters:
little- If true, sets the order toDataHandle.ByteOrder.LITTLE_ENDIAN; otherwise, sets the order toDataHandle.ByteOrder.BIG_ENDIAN.- See Also:
setOrder(ByteOrder)
-
getEncoding
String getEncoding()
Gets the native encoding of the stream.
-
setEncoding
void setEncoding(String encoding)
Sets the native encoding of the stream.
-
conversionBuffer
byte[] conversionBuffer()
- Returns:
- a 8 byte long buffer array used for type conversions
-
readCString
default String readCString() throws IOException
Reads a string of arbitrary length, terminated by a null char.- Throws:
IOException
-
readString
default String readString(int n) throws IOException
Reads a string of up to length n.- Throws:
IOException
-
readString
default String readString(String lastChars) throws IOException
Reads a string ending with one of the characters in the given string.- Throws:
IOException- See Also:
findString(String...)
-
findString
default String findString(String... terminators) throws IOException
Reads a string ending with one of the given terminating substrings.- Parameters:
terminators- The strings for which to search.- Returns:
- The string from the initial position through the end of the terminating sequence, or through the end of the stream if no terminating sequence is found.
- Throws:
IOException
-
findString
default String findString(boolean saveString, String... terminators) throws IOException
Reads or skips a string ending with one of the given terminating substrings.- Parameters:
saveString- Whether to collect the string from the current offset to the terminating bytes, and return it. If false, returns null.terminators- The strings for which to search.- Returns:
- The string from the initial position through the end of the terminating sequence, or through the end of the stream if no terminating sequence is found, or null if saveString flag is unset.
- Throws:
IOException- If saveString flag is set and the maximum search length (512 MB) is exceeded.
-
findString
default String findString(int blockSize, String... terminators) throws IOException
Reads a string ending with one of the given terminating substrings, using the specified block size for buffering.- Parameters:
blockSize- The block size to use when reading bytes in chunks.terminators- The strings for which to search.- Returns:
- The string from the initial position through the end of the terminating sequence, or through the end of the stream if no terminating sequence is found.
- Throws:
IOException
-
findString
default String findString(boolean saveString, int blockSize, String... terminators) throws IOException
Reads or skips a string ending with one of the given terminating substrings, using the specified block size for buffering.- Parameters:
saveString- Whether to collect the string from the current offset to the terminating bytes, and return it. If false, returns null.blockSize- The block size to use when reading bytes in chunks.terminators- The strings for which to search.- Returns:
- The string from the initial position through the end of the terminating sequence, or through the end of the stream if no terminating sequence is found, or null if saveString flag is unset.
- Throws:
IOException- If saveString flag is set and the maximum search length (512 MB) is exceeded.
-
writeLine
default void writeLine(String string) throws IOException
Writes the provided string, followed by a newline character.- Parameters:
string- The string to write.- Throws:
IOException- If an I/O error occurs.
-
read
default int read() throws IOExceptionReads the next byte of data from the stream.- Returns:
- the next byte of data, or -1 if the end of the stream is reached.
- Throws:
IOException- - if an I/O error occurs.
-
read
default int read(byte[] b) throws IOExceptionReads up to b.length bytes of data from the stream into an array of bytes.- Returns:
- the total number of bytes read into the buffer.
- Throws:
IOException
-
read
int read(byte[] b, int off, int len) throws IOExceptionReads up tolenbytes of data from the stream into an array of bytes.- Returns:
- the total number of bytes read into the buffer.
- Throws:
IOException
-
skip
default long skip(long n) throws IOExceptionSkips over and discardsnbytes of data from the stream. Theskipmethod may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly0. This may result from any of a number of conditions; reaching end of file beforenbytes have been skipped is only one possibility. The actual number of bytes skipped is returned. Ifnis negative, no bytes are skipped.- Parameters:
n- - the number of bytes to be skipped.- Returns:
- the actual number of bytes skipped.
- Throws:
IOException- - if an I/O error occurs.
-
readFully
default void readFully(byte[] b) throws IOException- Specified by:
readFullyin interfaceDataInput- Throws:
IOException
-
readFully
default void readFully(byte[] b, int off, int len) throws IOException- Specified by:
readFullyin interfaceDataInput- Throws:
IOException
-
skipBytes
default int skipBytes(int n) throws IOException- Specified by:
skipBytesin interfaceDataInput- Throws:
IOException
-
readBoolean
default boolean readBoolean() throws IOException- Specified by:
readBooleanin interfaceDataInput- Throws:
IOException
-
readUnsignedByte
default int readUnsignedByte() throws IOException- Specified by:
readUnsignedBytein interfaceDataInput- Throws:
IOException
-
readShort
default short readShort() throws IOException- Specified by:
readShortin interfaceDataInput- Throws:
IOException
-
readUnsignedShort
default int readUnsignedShort() throws IOException- Specified by:
readUnsignedShortin interfaceDataInput- Throws:
IOException
-
readChar
default char readChar() throws IOException- Specified by:
readCharin interfaceDataInput- Throws:
IOException
-
readInt
default int readInt() throws IOException- Specified by:
readIntin interfaceDataInput- Throws:
IOException
-
readLong
default long readLong() throws IOException- Specified by:
readLongin interfaceDataInput- Throws:
IOException
-
readFloat
default float readFloat() throws IOException- Specified by:
readFloatin interfaceDataInput- Throws:
IOException
-
readDouble
default double readDouble() throws IOException- Specified by:
readDoublein interfaceDataInput- Throws:
IOException
-
readLine
default String readLine() throws IOException
- Specified by:
readLinein interfaceDataInput- Throws:
IOException
-
readUTF
default String readUTF() throws IOException
- Specified by:
readUTFin interfaceDataInput- Throws:
IOException
-
write
default void write(byte[] b) throws IOException- Specified by:
writein interfaceDataOutput- Throws:
IOException
-
writeBoolean
default void writeBoolean(boolean v) throws IOException- Specified by:
writeBooleanin interfaceDataOutput- Throws:
IOException
-
writeByte
default void writeByte(int v) throws IOException- Specified by:
writeBytein interfaceDataOutput- Throws:
IOException
-
writeShort
default void writeShort(int v) throws IOException- Specified by:
writeShortin interfaceDataOutput- Throws:
IOException
-
writeChar
default void writeChar(int v) throws IOException- Specified by:
writeCharin interfaceDataOutput- Throws:
IOException
-
writeInt
default void writeInt(int v) throws IOException- Specified by:
writeIntin interfaceDataOutput- Throws:
IOException
-
writeLong
default void writeLong(long v) throws IOException- Specified by:
writeLongin interfaceDataOutput- Throws:
IOException
-
writeFloat
default void writeFloat(float v) throws IOException- Specified by:
writeFloatin interfaceDataOutput- Throws:
IOException
-
writeDouble
default void writeDouble(double v) throws IOException- Specified by:
writeDoublein interfaceDataOutput- Throws:
IOException
-
writeBytes
default void writeBytes(String s) throws IOException
- Specified by:
writeBytesin interfaceDataOutput- Throws:
IOException
-
writeChars
default void writeChars(String s) throws IOException
- Specified by:
writeCharsin interfaceDataOutput- Throws:
IOException
-
writeUTF
default void writeUTF(String str) throws IOException
- Specified by:
writeUTFin interfaceDataOutput- Throws:
IOException
-
-