Package javax.mail.util
Class SharedFileInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- java.io.BufferedInputStream
-
- javax.mail.util.SharedFileInputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,SharedInputStream
- Direct Known Subclasses:
WritableSharedFile
public class SharedFileInputStream extends java.io.BufferedInputStream implements SharedInputStream
ASharedFileInputStream
is aBufferedInputStream
that buffers data from the file and supports themark
andreset
methods. It also supports thenewStream
method that allows you to create other streams that represent subsets of the file. ARandomAccessFile
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
SharedFileInputStream.SharedFile
A shared class that keeps track of the references to a particular file so it can be closed when the last reference is gone.
-
Field Summary
Fields Modifier and Type Field Description protected long
bufpos
The file offset that corresponds to the first byte in the read buffer.protected int
bufsize
The normal size of the read buffer.protected long
datalen
The amount of data in this subset of the file.private static int
defaultBufferSize
protected java.io.RandomAccessFile
in
The file containing the data.private boolean
master
True if this is a top level stream created directly by "new".private SharedFileInputStream.SharedFile
sf
protected long
start
The file offset of the start of data in this subset of the file.
-
Constructor Summary
Constructors Modifier Constructor Description SharedFileInputStream(java.io.File file)
Creates aSharedFileInputStream
for the file.SharedFileInputStream(java.io.File file, int size)
Creates aSharedFileInputStream
with the specified buffer size.SharedFileInputStream(java.lang.String file)
Creates aSharedFileInputStream
for the named fileSharedFileInputStream(java.lang.String file, int size)
Creates aSharedFileInputStream
with the specified buffer size.private
SharedFileInputStream(SharedFileInputStream.SharedFile sf, long start, long len, int bufsize)
Used internally by thenewStream
method.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
available()
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.private void
ensureOpen()
Check to make sure that this stream has not been closedprivate void
fill()
Fills the buffer with more data, taking into account shuffling and other tricks for dealing with marks.protected void
finalize()
Force this stream to close.long
getPosition()
Return the current position in the InputStream, as an offset from the beginning of the InputStream.private int
in_available()
private void
init(SharedFileInputStream.SharedFile sf, int size)
void
mark(int readlimit)
See the general contract of themark
method ofInputStream
.boolean
markSupported()
Tests if this input stream supports themark
andreset
methods.java.io.InputStream
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.private int
read1(byte[] b, int off, int len)
Read characters into a portion of an array, reading from the underlying stream at most once if necessary.void
reset()
See the general contract of thereset
method ofInputStream
.long
skip(long n)
See the general contract of theskip
method ofInputStream
.
-
-
-
Field Detail
-
defaultBufferSize
private static int defaultBufferSize
-
in
protected java.io.RandomAccessFile in
The file containing the data. Shared by all related SharedFileInputStreams.
-
bufsize
protected int bufsize
The normal size of the read buffer.
-
bufpos
protected long bufpos
The file offset that corresponds to the first byte in the read buffer.
-
start
protected long start
The file offset of the start of data in this subset of the file.
-
datalen
protected long datalen
The amount of data in this subset of the file.
-
master
private boolean master
True if this is a top level stream created directly by "new". False if this is a derived stream created by newStream.
-
sf
private SharedFileInputStream.SharedFile sf
-
-
Constructor Detail
-
SharedFileInputStream
public SharedFileInputStream(java.io.File file) throws java.io.IOException
Creates aSharedFileInputStream
for the file.- Parameters:
file
- the file- Throws:
java.io.IOException
- for errors opening the file
-
SharedFileInputStream
public SharedFileInputStream(java.lang.String file) throws java.io.IOException
Creates aSharedFileInputStream
for the named file- Parameters:
file
- the file- Throws:
java.io.IOException
- for errors opening the file
-
SharedFileInputStream
public SharedFileInputStream(java.io.File file, int size) throws java.io.IOException
Creates aSharedFileInputStream
with the specified buffer size.- Parameters:
file
- the filesize
- the buffer size.- Throws:
java.io.IOException
- for errors opening the filejava.lang.IllegalArgumentException
- if size ≤ 0.
-
SharedFileInputStream
public SharedFileInputStream(java.lang.String file, int size) throws java.io.IOException
Creates aSharedFileInputStream
with the specified buffer size.- Parameters:
file
- the filesize
- the buffer size.- Throws:
java.io.IOException
- for errors opening the filejava.lang.IllegalArgumentException
- if size ≤ 0.
-
SharedFileInputStream
private SharedFileInputStream(SharedFileInputStream.SharedFile sf, long start, long len, int bufsize)
Used internally by thenewStream
method.
-
-
Method Detail
-
ensureOpen
private void ensureOpen() throws java.io.IOException
Check to make sure that this stream has not been closed- Throws:
java.io.IOException
-
init
private void init(SharedFileInputStream.SharedFile sf, int size) throws java.io.IOException
- Throws:
java.io.IOException
-
fill
private void fill() throws java.io.IOException
Fills the buffer with more data, taking into account shuffling and other tricks for dealing with marks. Assumes that it is being called by a synchronized method. This method also assumes that all data has already been read in, hence pos > count.- Throws:
java.io.IOException
-
read
public int read() throws java.io.IOException
See the general contract of theread
method ofInputStream
.- Overrides:
read
in classjava.io.BufferedInputStream
- Returns:
- the next byte of data, or
-1
if the end of the stream is reached. - Throws:
java.io.IOException
- if an I/O error occurs.
-
read1
private int read1(byte[] b, int off, int len) throws java.io.IOException
Read characters into a portion of an array, reading from the underlying stream at most once if necessary.- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException
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 classjava.io.BufferedInputStream
- 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:
java.io.IOException
- if an I/O error occurs.
-
skip
public long skip(long n) throws java.io.IOException
See the general contract of theskip
method ofInputStream
.- Overrides:
skip
in classjava.io.BufferedInputStream
- Parameters:
n
- the number of bytes to be skipped.- Returns:
- the actual number of bytes skipped.
- Throws:
java.io.IOException
- if an I/O error occurs.
-
available
public int available() throws java.io.IOException
Returns the number of bytes that can be read from this input stream without blocking.- Overrides:
available
in classjava.io.BufferedInputStream
- Returns:
- the number of bytes that can be read from this input stream without blocking.
- Throws:
java.io.IOException
- if an I/O error occurs.
-
in_available
private int in_available() throws java.io.IOException
- Throws:
java.io.IOException
-
mark
public void mark(int readlimit)
See the general contract of themark
method ofInputStream
.- Overrides:
mark
in classjava.io.BufferedInputStream
- Parameters:
readlimit
- the maximum limit of bytes that can be read before the mark position becomes invalid.- See Also:
reset()
-
reset
public void reset() throws java.io.IOException
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 classjava.io.BufferedInputStream
- Throws:
java.io.IOException
- if this stream has not been marked or if the mark has been invalidated.- See Also:
mark(int)
-
markSupported
public boolean markSupported()
Tests if this input stream supports themark
andreset
methods. ThemarkSupported
method ofSharedFileInputStream
returnstrue
.- Overrides:
markSupported
in classjava.io.BufferedInputStream
- Returns:
- a
boolean
indicating if this stream type supports themark
andreset
methods. - See Also:
InputStream.mark(int)
,InputStream.reset()
-
close
public void close() throws java.io.IOException
Closes this input stream and releases any system resources associated with the stream.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.BufferedInputStream
- Throws:
java.io.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
public java.io.InputStream newStream(long start, long end)
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
protected void finalize() throws java.lang.Throwable
Force this stream to close.- Overrides:
finalize
in classjava.lang.Object
- Throws:
java.lang.Throwable
-
-