Package jakarta.mail.util
Class SharedFileInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
java.io.BufferedInputStream
jakarta.mail.util.SharedFileInputStream
- All Implemented Interfaces:
SharedInputStream,Closeable,AutoCloseable
A
SharedFileInputStream is a
BufferedInputStream that buffers
data from the file and supports the mark
and reset methods. It also supports the
newStream method that allows you to create
other streams that represent subsets of the file.
A RandomAccessFile object is used to
access the file data.
Note that when the SharedFileInputStream is closed,
all streams created with the newStream
method are also closed. This allows the creator of the
SharedFileInputStream object to control access to the
underlying file and ensure that it is closed when
needed, to avoid leaking file descriptors. Note also
that this behavior contradicts the requirements of
SharedInputStream and may change in a future release.
- Since:
- JavaMail 1.4
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected longThe file offset that corresponds to the first byte in the read buffer.protected intThe normal size of the read buffer.protected longThe amount of data in this subset of the file.protected RandomAccessFileThe file containing the data.protected longThe file offset of the start of data in this subset of the file. -
Constructor Summary
ConstructorsConstructorDescriptionSharedFileInputStream(File file) Creates aSharedFileInputStreamfor the file.SharedFileInputStream(File file, int size) Creates aSharedFileInputStreamwith the specified buffer size.SharedFileInputStream(String file) Creates aSharedFileInputStreamfor the named fileSharedFileInputStream(String file, int size) Creates aSharedFileInputStreamwith the specified buffer size. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the number of bytes that can be read from this input stream without blocking.voidclose()Closes this input stream and releases any system resources associated with the stream.protected voidfinalize()Force this stream to close.longReturn the current position in the InputStream, as an offset from the beginning of the InputStream.voidmark(int readlimit) See the general contract of themarkmethod ofInputStream.booleanTests if this input stream supports themarkandresetmethods.newStream(long start, long end) Return a new InputStream representing a subset of the data from this InputStream, starting atstart(inclusive) up toend(exclusive).intread()See the general contract of thereadmethod ofInputStream.intread(byte[] b, int off, int len) Reads bytes from this stream into the specified byte array, starting at the given offset.voidreset()See the general contract of theresetmethod ofInputStream.longskip(long n) See the general contract of theskipmethod ofInputStream.Methods inherited from class java.io.FilterInputStream
readMethods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Field Details
-
in
The file containing the data. Shared by all related SharedFileInputStreams. -
bufsize
protected int bufsizeThe normal size of the read buffer. -
bufpos
protected long bufposThe file offset that corresponds to the first byte in the read buffer. -
start
protected long startThe file offset of the start of data in this subset of the file. -
datalen
protected long datalenThe amount of data in this subset of the file.
-
-
Constructor Details
-
SharedFileInputStream
Creates aSharedFileInputStreamfor the file.- Parameters:
file- the file- Throws:
IOException- for errors opening the file
-
SharedFileInputStream
Creates aSharedFileInputStreamfor the named file- Parameters:
file- the file- Throws:
IOException- for errors opening the file
-
SharedFileInputStream
Creates aSharedFileInputStreamwith the specified buffer size.- Parameters:
file- the filesize- the buffer size.- Throws:
IOException- for errors opening the fileIllegalArgumentException- if size ≤ 0.
-
SharedFileInputStream
Creates aSharedFileInputStreamwith the specified buffer size.- Parameters:
file- the filesize- the buffer size.- Throws:
IOException- for errors opening the fileIllegalArgumentException- if size ≤ 0.
-
-
Method Details
-
read
See the general contract of thereadmethod ofInputStream.- Overrides:
readin classBufferedInputStream- Returns:
- the next byte of data, or
-1if the end of the stream is reached. - Throws:
IOException- if an I/O error occurs.
-
read
Reads bytes from this stream into the specified byte array, starting at the given offset.This method implements the general contract of the corresponding
method of thereadclass.InputStream- Overrides:
readin classBufferedInputStream- Parameters:
b- destination buffer.off- offset at which to start storing bytes.len- maximum number of bytes to read.- Returns:
- the number of bytes read, or
-1if the end of the stream has been reached. - Throws:
IOException- if an I/O error occurs.
-
skip
See the general contract of theskipmethod ofInputStream.- Overrides:
skipin classBufferedInputStream- Parameters:
n- the number of bytes to be skipped.- Returns:
- the actual number of bytes skipped.
- Throws:
IOException- if an I/O error occurs.
-
available
Returns the number of bytes that can be read from this input stream without blocking.- Overrides:
availablein classBufferedInputStream- Returns:
- the number of bytes that can be read from this input stream without blocking.
- Throws:
IOException- if an I/O error occurs.
-
mark
public void mark(int readlimit) See the general contract of themarkmethod ofInputStream.- Overrides:
markin classBufferedInputStream- Parameters:
readlimit- the maximum limit of bytes that can be read before the mark position becomes invalid.- See Also:
-
reset
See the general contract of theresetmethod ofInputStream.If
markposis-1(no mark has been set or the mark has been invalidated), anIOExceptionis thrown. Otherwise,posis set equal tomarkpos.- Overrides:
resetin classBufferedInputStream- Throws:
IOException- if this stream has not been marked or if the mark has been invalidated.- See Also:
-
markSupported
public boolean markSupported()Tests if this input stream supports themarkandresetmethods. ThemarkSupportedmethod ofSharedFileInputStreamreturnstrue.- Overrides:
markSupportedin classBufferedInputStream- Returns:
- a
booleanindicating if this stream type supports themarkandresetmethods. - See Also:
-
close
Closes this input stream and releases any system resources associated with the stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classBufferedInputStream- Throws:
IOException- if an I/O error occurs.
-
getPosition
public long getPosition()Return the current position in the InputStream, as an offset from the beginning of the InputStream.- Specified by:
getPositionin interfaceSharedInputStream- Returns:
- the current position
-
newStream
Return a new InputStream representing a subset of the data from this InputStream, starting atstart(inclusive) up toend(exclusive).startmust be non-negative. Ifendis -1, the new stream ends at the same place as this stream. The returned InputStream will also implement the SharedInputStream interface.- Specified by:
newStreamin interfaceSharedInputStream- Parameters:
start- the starting positionend- the ending position + 1- Returns:
- the new stream
-
finalize
Force this stream to close.
-