public class

SharedFileInputStream

extends BufferedInputStream
implements SharedInputStream
java.lang.Object
   ↳ java.io.InputStream
     ↳ java.io.FilterInputStream
       ↳ java.io.BufferedInputStream
         ↳ com.google.code.javax.mail.util.SharedFileInputStream

Class Overview

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.

Summary

[Expand]
Inherited Constants
From class java.io.InputStream
Fields
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 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.
[Expand]
Inherited Fields
From class java.io.BufferedInputStream
From class java.io.FilterInputStream
Public Constructors
SharedFileInputStream(File file)
Creates a SharedFileInputStream for the file.
SharedFileInputStream(String file)
Creates a SharedFileInputStream for the named file
SharedFileInputStream(File file, int size)
Creates a SharedFileInputStream with the specified buffer size.
SharedFileInputStream(String file, int size)
Creates a SharedFileInputStream with the specified buffer size.
Public Methods
synchronized 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.
long getPosition()
Return the current position in the InputStream, as an offset from the beginning of the InputStream.
synchronized void mark(int readlimit)
See the general contract of the mark method of InputStream.
boolean markSupported()
Tests if this input stream supports the mark and reset methods.
InputStream newStream(long start, long end)
Return a new InputStream representing a subset of the data from this InputStream, starting at start (inclusive) up to end (exclusive).
synchronized int read()
See the general contract of the read method of InputStream.
synchronized int read(byte[] b, int off, int len)
Reads bytes from this stream into the specified byte array, starting at the given offset.
synchronized void reset()
See the general contract of the reset method of InputStream.
synchronized long skip(long n)
See the general contract of the skip method of InputStream.
Protected Methods
void finalize()
Force this stream to close.
[Expand]
Inherited Methods
From class java.io.BufferedInputStream
From class java.io.FilterInputStream
From class java.io.InputStream
From class java.lang.Object
From interface com.google.code.javax.mail.internet.SharedInputStream
From interface java.io.Closeable

Fields

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 RandomAccessFile in

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

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.

private SharedFileInputStream.SharedFile sf

protected long start

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

Public Constructors

public SharedFileInputStream (File file)

Creates a SharedFileInputStream for the file.

Parameters
file The file
Throws
IOException

public SharedFileInputStream (String file)

Creates a SharedFileInputStream for the named file

Parameters
file The file
Throws
IOException

public SharedFileInputStream (File file, int size)

Creates a SharedFileInputStream with the specified buffer size.

Parameters
file The file
size The buffer size.
Throws
IllegalArgumentException if size <= 0.
IOException

public SharedFileInputStream (String file, int size)

Creates a SharedFileInputStream with the specified buffer size.

Parameters
file The file
size The buffer size.
Throws
IllegalArgumentException if size <= 0.
IOException

Public Methods

public synchronized int available ()

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

Returns
  • the number of bytes that can be read from this input stream without blocking.
Throws
IOException if an I/O error occurs.

public void close ()

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

Throws
IOException if an I/O error occurs.

public long getPosition ()

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

Returns
  • the current position

public synchronized void mark (int readlimit)

See the general contract of the mark method of InputStream.

Parameters
readlimit The maximum limit of bytes that can be read before the mark position becomes invalid.
See Also

public boolean markSupported ()

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

Returns
  • a boolean indicating if this stream type supports the mark and reset methods.
See Also

public InputStream newStream (long start, long end)

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 The starting position
end The ending position + 1
Returns
  • the new stream

public synchronized int read ()

See the general contract of the read method of InputStream.

Returns
  • the next byte of data, or -1 if the end of the stream is reached.
Throws
IOException if an I/O error occurs.

public synchronized int read (byte[] b, int off, int len)

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 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.

public synchronized void 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.

Throws
IOException if this stream has not been marked or if the mark has been invalidated.
See Also

public synchronized long skip (long n)

See the general contract of the skip method of InputStream.

Parameters
n The number of bytes to be skipped.
Returns
  • the actual number of bytes skipped.
Throws
IOException if an I/O error occurs.

Protected Methods

protected void finalize ()

Force this stream to close.

Throws
Throwable