javax.mail.util
Class SharedFileInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by java.io.BufferedInputStream
              extended by javax.mail.util.SharedFileInputStream
All Implemented Interfaces:
Closeable, SharedInputStream

public class SharedFileInputStream
extends BufferedInputStream
implements SharedInputStream


Field Summary
protected  long bufpos
          The file offset that is the first byte in the read buffer.
protected  int bufsize
          The normal size of the read buffer.
protected  long datalen
          The size of the file subset represented by this stream instance.
protected  RandomAccessFile in
          The source of the file data.
protected  long start
          The starting position of data represented by this stream relative to the start of the file data.
 
Fields inherited from class java.io.BufferedInputStream
buf, count, marklimit, markpos, pos
 
Constructor Summary
SharedFileInputStream(File file)
          Construct a SharedFileInputStream from a File object, using the default buffer size.
SharedFileInputStream(File file, int bufferSize)
          Construct a SharedFileInputStream from a File object, with a given initial buffer size.
SharedFileInputStream(String file)
          Construct a SharedFileInputStream from a file name, using the default buffer size.
SharedFileInputStream(String file, int bufferSize)
          Construct a SharedFileInputStream from a file name, with a given initial buffer size.
 
Method Summary
 int available()
          Return the number of bytes available for reading without blocking for a long period.
 void close()
          Close the stream.
 long getPosition()
          Return the current read position of the stream.
 void mark(int readlimit)
          Mark the current position for retracing.
 boolean markSupported()
          Indicates the mark() operation is supported.
 InputStream newStream(long offset, long end)
          Create a new stream from this stream, using the given start offset and length.
 int read()
          Read a single byte of data from the input stream.
 int read(byte[] buffer, int offset, int length)
          Read multiple bytes of data and place them directly into a byte-array buffer.
 void reset()
          Reset the mark position.
 long skip(long n)
          Skip the read pointer ahead a given number of bytes.
 
Methods inherited from class java.io.FilterInputStream
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bufpos

protected long bufpos
The file offset that is the first byte in the read buffer.


bufsize

protected int bufsize
The normal size of the read buffer.


datalen

protected long datalen
The size of the file subset represented by this stream instance.


in

protected RandomAccessFile in
The source of the file data. This is shared across multiple instances.


start

protected long start
The starting position of data represented by this stream relative to the start of the file data. This stream instance represents data in the range start to (start + datalen - 1).

Constructor Detail

SharedFileInputStream

public SharedFileInputStream(String file)
                      throws IOException
Construct a SharedFileInputStream from a file name, using the default buffer size.

Parameters:
file - The name of the file.
Throws:
IOException

SharedFileInputStream

public SharedFileInputStream(File file)
                      throws IOException
Construct a SharedFileInputStream from a File object, using the default buffer size.

Parameters:
file - The name of the file.
Throws:
IOException

SharedFileInputStream

public SharedFileInputStream(String file,
                             int bufferSize)
                      throws IOException
Construct a SharedFileInputStream from a file name, with a given initial buffer size.

Parameters:
file - The name of the file.
bufferSize - The initial buffer size.
Throws:
IOException

SharedFileInputStream

public SharedFileInputStream(File file,
                             int bufferSize)
                      throws IOException
Construct a SharedFileInputStream from a File object, with a given initial buffer size.

Parameters:
file - The name of the file.
bufferSize - The initial buffer size.
Throws:
IOException
Method Detail

available

public int available()
              throws IOException
Return the number of bytes available for reading without blocking for a long period.

Overrides:
available in class BufferedInputStream
Returns:
For this stream, this is the number of bytes between the current read position and the indicated end of the file.
Throws:
IOException

getPosition

public long getPosition()
Return the current read position of the stream.

Specified by:
getPosition in interface SharedInputStream
Returns:
The current position relative to the beginning of the stream. This is not the position relative to the start of the file, since the stream starting position may be other than the beginning.

mark

public void mark(int readlimit)
Mark the current position for retracing.

Overrides:
mark in class BufferedInputStream
Parameters:
readlimit - The limit for the distance the read position can move from the mark position before the mark is reset.

read

public int read()
         throws IOException
Read a single byte of data from the input stream.

Overrides:
read in class BufferedInputStream
Returns:
The read byte. Returns -1 if an eof condition has been hit.
Throws:
IOException

read

public int read(byte[] buffer,
                int offset,
                int length)
         throws IOException
Read multiple bytes of data and place them directly into a byte-array buffer.

Overrides:
read in class BufferedInputStream
Parameters:
buffer - The target buffer.
offset - The offset within the buffer to place the data.
length - The length to attempt to read.
Returns:
The number of bytes actually read. Returns -1 for an EOF condition.
Throws:
IOException

skip

public long skip(long n)
          throws IOException
Skip the read pointer ahead a given number of bytes.

Overrides:
skip in class BufferedInputStream
Parameters:
n - The number of bytes to skip.
Returns:
The number of bytes actually skipped.
Throws:
IOException

reset

public void reset()
           throws IOException
Reset the mark position.

Overrides:
reset in class BufferedInputStream
Throws:
IOException

markSupported

public boolean markSupported()
Indicates the mark() operation is supported.

Overrides:
markSupported in class BufferedInputStream
Returns:
Always returns true.

close

public void close()
           throws IOException
Close the stream. This does not close the source file until the last shared instance is closed.

Specified by:
close in interface Closeable
Overrides:
close in class BufferedInputStream
Throws:
IOException

newStream

public InputStream newStream(long offset,
                             long end)
Create a new stream from this stream, using the given start offset and length.

Specified by:
newStream in interface SharedInputStream
Parameters:
offset - The offset relative to the start of this stream instance.
end - The end offset of the substream. If -1, the end of the parent stream is used.
Returns:
A new SharedFileInputStream object sharing the same source input file.


Copyright © 2003-2008 The Apache Software Foundation. All Rights Reserved.