Class FileCachedInputStream
- java.lang.Object
-
- java.io.InputStream
-
- net.sf.okapi.common.io.FileCachedInputStream
-
- All Implemented Interfaces:
Closeable,AutoCloseable,Channel,WritableByteChannel
public class FileCachedInputStream extends InputStream implements WritableByteChannel
An instance of this class acts as a buffer holding the bytes written through its
WritableByteChannelinterface (which can be easily turned into anOutputStreamusingChannels#newOutputStream(WritableByteChannel)). The data can be discarded at any time usingclear(). The firstinspectablebytes ofbuffercontains the first bytes written. Whenbufferis full, the bytes are written to an overflow file. Note that you must arbitrate carefully write and read accesses, as it is always possible to callwrite(ByteBuffer)and thus modify the lengthThe method
dispose()can be used to release the resources associated with the stream.Buffering
This class provides no form of buffering except for the memory buffer described above, both when reading and when writing. Users should consider wrapping instances of this class, as reads after the buffer has been exhausted will be performed directly on a
RandomAccessFile.
-
-
Field Summary
Fields Modifier and Type Field Description byte[]bufferThe inspection buffer.static booleanDEBUGstatic intDEFAULT_BUFFER_SIZEThe default buffer size (1024KiB).intinspectableThe number of valid bytes currently inbuffer.
-
Constructor Summary
Constructors Constructor Description FileCachedInputStream()Creates a new instance with default buffer size and overflow-file directory.FileCachedInputStream(int bufferSize)Creates a new instance with specified buffer size and default overflow-file directory.FileCachedInputStream(int bufferSize, File overflowFile)Creates a new instance with specified buffer size and overlow-file directory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()voidclear()Clears the content of thisFileCachedInputStream, zeroing the length of the represented stream.voidclose()Makes the stream unreadable until the nextclear().voiddispose()Disposes this stream, deleting the overflow file.booleanisOpen()longlength()voidmark(int readlimit)booleanmarkSupported()longposition()voidposition(long position)Positions the input stream.intread()intread(byte[] b)intread(byte[] b, int offset, int length)voidreopen()Makes the stream readable again after aclose().voidreset()longskip(long n)voidtruncate(long size)Truncates the overflow file to a given size if possible.intwrite(ByteBuffer byteBuffer)Appends the content of a specified buffer to the end of the currently represented stream.-
Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
-
-
-
-
Field Detail
-
DEBUG
public static final boolean DEBUG
- See Also:
- Constant Field Values
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
The default buffer size (1024KiB).- See Also:
- Constant Field Values
-
buffer
public byte[] buffer
The inspection buffer. The firstinspectablebytes contain the first part of the input stream. The buffer is available for inspection, but users should not modify its content.
-
inspectable
public int inspectable
The number of valid bytes currently inbuffer.
-
-
Constructor Detail
-
FileCachedInputStream
public FileCachedInputStream(int bufferSize, File overflowFile)Creates a new instance with specified buffer size and overlow-file directory.- Parameters:
bufferSize- the buffer size, in bytes.overflowFile- the directory where the overflow file should be created, ornullfor the default temporary directory.
-
FileCachedInputStream
public FileCachedInputStream(int bufferSize)
Creates a new instance with specified buffer size and default overflow-file directory.- Parameters:
bufferSize- the buffer size, in bytes.- Throws:
IOException- if an I/O error occurs.
-
FileCachedInputStream
public FileCachedInputStream()
Creates a new instance with default buffer size and overflow-file directory.- Throws:
IOException- if an I/O error occurs.
-
-
Method Detail
-
clear
public void clear() throws IOExceptionClears the content of thisFileCachedInputStream, zeroing the length of the represented stream.- Throws:
IOException- if an I/O error occurs.
-
write
public int write(ByteBuffer byteBuffer) throws IOException
Appends the content of a specified buffer to the end of the currently represented stream.- Specified by:
writein interfaceWritableByteChannel- Parameters:
byteBuffer- a byte buffer.- Returns:
- the number of bytes appended (i.e.,
byteBuffer.remaining()). - Throws:
IOException
-
truncate
public void truncate(long size) throws FileNotFoundException, IOExceptionTruncates the overflow file to a given size if possible.- Parameters:
size- the new size; the final size is the maximum between the current write position (i.e., the length of the represented stream minus the length of the inspection buffer) and this value.- Throws:
FileNotFoundException- Tif the file cannot be found.IOException- if an I/O error occurs.
-
close
public void close()
Makes the stream unreadable until the nextclear().- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- See Also:
reopen()
-
reopen
public void reopen() throws IOExceptionMakes the stream readable again after aclose().- Throws:
IOException- if an I/O error occurs.- See Also:
close()
-
dispose
public void dispose() throws IOExceptionDisposes this stream, deleting the overflow file. After that, the stream is unusable.- Throws:
IOException- if an I/O error occurs.
-
available
public int available() throws IOException- Overrides:
availablein classInputStream- Throws:
IOException
-
read
public int read(byte[] b, int offset, int length) throws IOException- Overrides:
readin classInputStream- Throws:
IOException
-
read
public int read(byte[] b) throws IOException- Overrides:
readin classInputStream- Throws:
IOException
-
skip
public long skip(long n) throws IOException- Overrides:
skipin classInputStream- Throws:
IOException
-
read
public int read() throws IOException- Specified by:
readin classInputStream- Throws:
IOException
-
length
public long length() throws IOException- Throws:
IOException
-
position
public long position() throws IOException- Throws:
IOException
-
position
public void position(long position) throws IOExceptionPositions the input stream.- Parameters:
position- the new position (will be minimized withlength()).- Throws:
IOException- if an I/O error occurs.
-
mark
public void mark(int readlimit)
- Overrides:
markin classInputStream
-
reset
public void reset() throws IOException- Overrides:
resetin classInputStream- Throws:
IOException
-
markSupported
public boolean markSupported()
- Overrides:
markSupportedin classInputStream
-
-