Top Description Inners Fields Constructors Methods
javax.mail.util

public Class SharedFileInputStream

extends BufferedInputStream
implements SharedInputStream
Class Inheritance
All Implemented Interfaces
javax.mail.internet.SharedInputStream
Imports
java.io.*, javax.mail.internet.SharedInputStream

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.

Author
Bill Shannon
Since
JavaMail 1.4

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv 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

Modifier and TypeField and 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
protected RandomAccessFile
in

Hides java.io.FilterInputStream.in.

The file containing the data.
private boolean
master

True if this is a top level stream created directly by "new".

private SharedFileInputStream.SharedFile
protected long
start

The file offset of the start of data in this subset of the file.

Inherited from java.io.BufferedInputStream:
bufcountmarklimitmarkpospos

Constructor Summary

AccessConstructor and Description
public
SharedFileInputStream(File
the file
file
)

Creates a SharedFileInputStream for the file.

public
SharedFileInputStream(String
the file
file
)

Creates a SharedFileInputStream for the named file

public
SharedFileInputStream(File
the file
file
,
int
the buffer size.
size
)

Creates a SharedFileInputStream with the specified buffer size.

public
SharedFileInputStream(String
the file
file
,
int
the buffer size.
size
)

Creates a SharedFileInputStream with the specified buffer size.

private
SharedFileInputStream(SharedFileInputStream.SharedFile sf, long start, long len, int bufsize)

Used internally by the newStream method.

Method Summary

Modifier and TypeMethod and Description
public synchronized int

Returns:

the number of bytes that can be read from this input stream without blocking.
available
()

Overrides java.io.BufferedInputStream.available.

Returns the number of bytes that can be read from this input stream without blocking.
public void
close()

Overrides java.io.BufferedInputStream.close.

Implements java.io.Closeable.close.

Closes this input stream and releases any system resources associated with the stream.
private void
ensureOpen()

Hides java.io.BufferedInputStream.ensureOpen.

Check to make sure that this stream has not been closed
private void
fill()

Hides java.io.BufferedInputStream.fill.

Fills the buffer with more data, taking into account shuffling and other tricks for dealing with marks.
protected void
finalize()

Overrides java.lang.Object.finalize.

Force this stream to close.
public long

Returns:

the current position
getPosition
()

Implements javax.mail.internet.SharedInputStream.getPosition.

Return the current position in the InputStream, as an offset from the beginning of the InputStream.
private int
private void
public synchronized void
mark(int
the maximum limit of bytes that can be read before the mark position becomes invalid.
readlimit
)

Overrides java.io.BufferedInputStream.mark.

See the general contract of the mark method of InputStream.
public boolean

Returns:

a boolean indicating if this stream type supports the mark and reset methods.
markSupported
()

Overrides java.io.BufferedInputStream.markSupported.

Tests if this input stream supports the mark and reset methods.
public synchronized InputStream

Returns:

the new stream
newStream
(long
the starting position
start
,
long
the ending position + 1
end
)

Implements javax.mail.internet.SharedInputStream.newStream.

Return a new InputStream representing a subset of the data from this InputStream, starting at start (inclusive) up to end (exclusive).
public synchronized int

Returns:

the next byte of data, or -1 if the end of the stream is reached.
read
()

Overrides java.io.BufferedInputStream.read.

See the general contract of the read method of InputStream.
public synchronized int

Returns:

the number of bytes read, or -1 if the end of the stream has been reached.
read
(byte[]
destination buffer.
b
,
int
offset at which to start storing bytes.
off
,
int
maximum number of bytes to read.
len
)

Overrides java.io.BufferedInputStream.read.

Reads bytes from this stream into the specified byte array, starting at the given offset.
private int
read1(byte[] b, int off, int len)

Hides java.io.BufferedInputStream.read1.

Read characters into a portion of an array, reading from the underlying stream at most once if necessary.
public synchronized void
reset()

Overrides java.io.BufferedInputStream.reset.

See the general contract of the reset method of InputStream.
public synchronized long

Returns:

the actual number of bytes skipped.
skip
(long
the number of bytes to be skipped.
n
)

Overrides java.io.BufferedInputStream.skip.

See the general contract of the skip method of InputStream.
Inherited from java.io.BufferedInputStream:
transferTo

Field Detail

bufposback to summary
protected long bufpos

The file offset that corresponds to the first byte in the read buffer.

bufsizeback to summary
protected int bufsize

The normal size of the read buffer.

datalenback to summary
protected long datalen

The amount of data in this subset of the file.

defaultBufferSizeback to summary
private static int defaultBufferSize
inback to summary
protected RandomAccessFile in

Hides java.io.FilterInputStream.in.

The file containing the data. Shared by all related SharedFileInputStreams.

masterback to summary
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.

sfback to summary
private SharedFileInputStream.SharedFile sf
startback to summary
protected long start

The file offset of the start of data in this subset of the file.

Constructor Detail

SharedFileInputStreamback to summary
public SharedFileInputStream(File file) throws IOException

Creates a SharedFileInputStream for the file.

Parameters
file:File

the file

Exceptions
IOException:
for errors opening the file
SharedFileInputStreamback to summary
public SharedFileInputStream(String file) throws IOException

Creates a SharedFileInputStream for the named file

Parameters
file:String

the file

Exceptions
IOException:
for errors opening the file
SharedFileInputStreamback to summary
public SharedFileInputStream(File file, int size) throws IOException

Creates a SharedFileInputStream with the specified buffer size.

Parameters
file:File

the file

size:int

the buffer size.

Exceptions
IOException:
for errors opening the file
IllegalArgumentException:
if size ≤ 0.
SharedFileInputStreamback to summary
public SharedFileInputStream(String file, int size) throws IOException

Creates a SharedFileInputStream with the specified buffer size.

Parameters
file:String

the file

size:int

the buffer size.

Exceptions
IOException:
for errors opening the file
IllegalArgumentException:
if size ≤ 0.
SharedFileInputStreamback to summary
private SharedFileInputStream(SharedFileInputStream.SharedFile sf, long start, long len, int bufsize)

Used internally by the newStream method.

Method Detail

availableback to summary
public synchronized int available() throws IOException

Overrides java.io.BufferedInputStream.available.

Returns the number of bytes that can be read from this input stream without blocking.

Returns:int

the number of bytes that can be read from this input stream without blocking.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
closeback to summary
public void close() throws IOException

Overrides java.io.BufferedInputStream.close.

Implements java.io.Closeable.close.

Closes this input stream and releases any system resources associated with the stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
ensureOpenback to summary
private void ensureOpen() throws IOException

Hides java.io.BufferedInputStream.ensureOpen.

Check to make sure that this stream has not been closed

fillback to summary
private void fill() throws IOException

Hides java.io.BufferedInputStream.fill.

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.

finalizeback to summary
protected void finalize() throws Throwable

Overrides java.lang.Object.finalize.

Force this stream to close.

Annotations
@Override
Exceptions
Throwable:

Doc from java.lang.Object.finalize.

the Exception raised by this method

getPositionback to summary
public long getPosition()

Implements javax.mail.internet.SharedInputStream.getPosition.

Return the current position in the InputStream, as an offset from the beginning of the InputStream.

Returns:long

the current position

Annotations
@Override
in_availableback to summary
private int in_available() throws IOException
initback to summary
private void init(SharedFileInputStream.SharedFile sf, int size) throws IOException
markback to summary
public synchronized void mark(int readlimit)

Overrides java.io.BufferedInputStream.mark.

See the general contract of the mark method of InputStream.

Parameters
readlimit:int

the maximum limit of bytes that can be read before the mark position becomes invalid.

Annotations
@Override
See Also
reset()
markSupportedback to summary
public boolean markSupported()

Overrides java.io.BufferedInputStream.markSupported.

Tests if this input stream supports the mark and reset methods. The markSupported method of SharedFileInputStream returns true.

Returns:boolean

a boolean indicating if this stream type supports the mark and reset methods.

Annotations
@Override
See Also
java.io.InputStream#mark(int), java.io.InputStream#reset()
newStreamback to summary
public synchronized InputStream newStream(long start, long end)

Implements javax.mail.internet.SharedInputStream.newStream.

Return a new InputStream representing a subset of the data from this InputStream, starting at start (inclusive) up to end (exclusive). start must be non-negative. If end is -1, the new stream ends at the same place as this stream. The returned InputStream will also implement the SharedInputStream interface.

Parameters
start:long

the starting position

end:long

the ending position + 1

Returns:InputStream

the new stream

Annotations
@Override
readback to summary
public synchronized int read() throws IOException

Overrides java.io.BufferedInputStream.read.

See the general contract of the read method of InputStream.

Returns:int

the next byte of data, or -1 if the end of the stream is reached.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
readback to summary
public synchronized int read(byte[] b, int off, int len) throws IOException

Overrides java.io.BufferedInputStream.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 read method of the java.io.InputStream class.

Parameters
b:byte[]

destination buffer.

off:int

offset at which to start storing bytes.

len:int

maximum number of bytes to read.

Returns:int

the number of bytes read, or -1 if the end of the stream has been reached.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
read1back to summary
private int read1(byte[] b, int off, int len) throws IOException

Hides java.io.BufferedInputStream.read1.

Read characters into a portion of an array, reading from the underlying stream at most once if necessary.

resetback to summary
public synchronized void reset() throws IOException

Overrides java.io.BufferedInputStream.reset.

See the general contract of the reset method of InputStream.

If markpos is -1 (no mark has been set or the mark has been invalidated), an IOException is thrown. Otherwise, pos is set equal to markpos.

Annotations
@Override
Exceptions
IOException:
if this stream has not been marked or if the mark has been invalidated.
See Also
mark(int)
skipback to summary
public synchronized long skip(long n) throws IOException

Overrides java.io.BufferedInputStream.skip.

See the general contract of the skip method of InputStream.

Parameters
n:long

the number of bytes to be skipped.

Returns:long

the actual number of bytes skipped.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
javax.mail.util back to summary

pack-priv Class SharedFileInputStream.SharedFile

extends Object
Class Inheritance

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

Modifier and TypeField and Description
private int
private RandomAccessFile

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv

Method Summary

Modifier and TypeMethod and Description
public synchronized void
protected void
finalize()

Overrides java.lang.Object.finalize.

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
public synchronized void
public synchronized RandomAccessFile
open()

Inherited from java.lang.Object:
cloneequalsgetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cntback to summary
private int cnt
inback to summary
private RandomAccessFile in

Constructor Detail

SharedFileback to summary
pack-priv SharedFile(String file) throws IOException
SharedFileback to summary
pack-priv SharedFile(File file) throws IOException

Method Detail

closeback to summary
public synchronized void close() throws IOException
finalizeback to summary
protected void finalize() throws Throwable

Overrides java.lang.Object.finalize.

Doc from java.lang.Object.finalize.

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

When running in a Java virtual machine in which finalization has been disabled or removed, the garbage collector will never call finalize(). In a Java virtual machine in which finalization is enabled, the garbage collector might call finalize only after an indefinite delay.

The general contract of finalize is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

Annotations
@Override
Exceptions
Throwable:
the Exception raised by this method
forceCloseback to summary
public synchronized void forceClose() throws IOException
openback to summary
public synchronized RandomAccessFile open()