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. - Since:
- JavaMail 1.4
- Author:
- Bill Shannon
-
Field Summary
Modifier and TypeFieldDescriptionprotected long
The file offset that corresponds to the first byte in the read buffer.protected int
The normal size of the read buffer.protected long
The amount of data in this subset of the file.protected RandomAccessFile
The file containing the data.protected long
The file offset of the start of data in this subset of the file. -
Constructor Summary
ConstructorDescriptionSharedFileInputStream
(File file) Creates aSharedFileInputStream
for the file.SharedFileInputStream
(File file, int size) Creates aSharedFileInputStream
with the specified buffer size.SharedFileInputStream
(String file) Creates aSharedFileInputStream
for the named fileSharedFileInputStream
(String file, int size) Creates aSharedFileInputStream
with the specified buffer size. -
Method Summary
Modifier and TypeMethodDescriptionint
Returns the number of bytes that can be read from this input stream without blocking.void
close()
Closes this input stream and releases any system resources associated with the stream.protected void
finalize()
Force this stream to close.long
Return the current position in the InputStream, as an offset from the beginning of the InputStream.void
mark
(int readlimit) See the general contract of themark
method ofInputStream
.boolean
Tests if this input stream supports themark
andreset
methods.newStream
(long start, long end) Return a new InputStream representing a subset of the data from this InputStream, starting atstart
(inclusive) up toend
(exclusive).int
read()
See the general contract of theread
method ofInputStream
.int
read
(byte[] b, int off, int len) Reads bytes from this stream into the specified byte array, starting at the given offset.void
reset()
See the general contract of thereset
method ofInputStream
.long
skip
(long n) See the general contract of theskip
method ofInputStream
.Methods inherited from class java.io.FilterInputStream
read
Methods 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 aSharedFileInputStream
for the file.- Parameters:
file
- the file- Throws:
IOException
- for errors opening the file
-
SharedFileInputStream
Creates aSharedFileInputStream
for the named file- Parameters:
file
- the file- Throws:
IOException
- for errors opening the file
-
SharedFileInputStream
Creates aSharedFileInputStream
with the specified buffer size.- Parameters:
file
- the filesize
- the buffer size.- Throws:
IOException
- for errors opening the fileIllegalArgumentException
- if size ≤ 0.
-
SharedFileInputStream
Creates aSharedFileInputStream
with 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 theread
method ofInputStream
.- Overrides:
read
in classBufferedInputStream
- 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
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 theread
class.InputStream
- Overrides:
read
in 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
-1
if the end of the stream has been reached. - Throws:
IOException
- if an I/O error occurs.
-
skip
See the general contract of theskip
method ofInputStream
.- Overrides:
skip
in 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:
available
in 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 themark
method ofInputStream
.- Overrides:
mark
in 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 thereset
method ofInputStream
.If
markpos
is-1
(no mark has been set or the mark has been invalidated), anIOException
is thrown. Otherwise,pos
is set equal tomarkpos
.- Overrides:
reset
in 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 themark
andreset
methods. ThemarkSupported
method ofSharedFileInputStream
returnstrue
.- Overrides:
markSupported
in classBufferedInputStream
- Returns:
- a
boolean
indicating if this stream type supports themark
andreset
methods. - See Also:
-
close
Closes this input stream and releases any system resources associated with the stream.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in 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:
getPosition
in 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).start
must be non-negative. Ifend
is -1, the new stream ends at the same place as this stream. The returned InputStream will also implement the SharedInputStream interface.- Specified by:
newStream
in interfaceSharedInputStream
- Parameters:
start
- the starting positionend
- the ending position + 1- Returns:
- the new stream
-
finalize
Force this stream to close.
-