iaik.smime
Class SharedFileInputStream

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

public class SharedFileInputStream
extends java.io.BufferedInputStream
implements javax.mail.internet.SharedInputStream

A SharedFileInputStream for reading data from a file.

This class implements the JavaMail SharedInputStream interface for creating substreams by calls to method newStream that read data from the same underlying file.

When creating a SharedFileInputStream the file from which to read has to be specified:

 File file = ...;
 SharedFileInputStream sfi = new SharedFileInputStream(file);
 
This class uses a special RandomAccessFileInputStream to read the data from a basic file that is accessed through a RandomAccessFile. Anytime when method newStream is called, a new substream is created to read data from the same underlying file.

See Also:
SharedInputStream

Field Summary
 
Fields inherited from class java.io.BufferedInputStream
buf, count, marklimit, markpos, pos
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
SharedFileInputStream(java.io.File file)
          Creates a SharedFileInputStream for reading from the given file.
SharedFileInputStream(java.io.File file, int bufSize)
          Creates a SharedFileInputStream for reading from the given file.
 
Method Summary
 int available()
          Gets the number of bytes that can be read from this stream without blocking.
 void close()
          Closes this input stream.
protected  void finalize()
          Performs finalization.
 long getPosition()
          Gets the current position of the stream as offset from the start position.
 void mark(int readlimit)
          Marks the current position in this input stream to start reading from it when method reset is called.
 boolean markSupported()
          Asks whether this input stream does support mark and reset.
 java.io.InputStream newStream(long start, long end)
          Gets a new SharedInputStream as sub-stream from this InputStream.
 int read()
          Reads one byte from this stream.
 int read(byte[] b, int off, int len)
          Reads up to len bytes from this stream.
 void reset()
          Resets the stream to the position that has been previously set by calling method mark.
 long skip(long n)
          Skips up to n bytes of data.
 
Methods inherited from class java.io.FilterInputStream
read
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SharedFileInputStream

public SharedFileInputStream(java.io.File file)
                      throws java.io.IOException
Creates a SharedFileInputStream for reading from the given file.

Parameters:
file - the file from which to read
Throws:
java.io.IOException - if an I/O error occurs

SharedFileInputStream

public SharedFileInputStream(java.io.File file,
                             int bufSize)
                      throws java.io.IOException
Creates a SharedFileInputStream for reading from the given file.

Parameters:
file - the file from which to read
bufSize - the size of the internal buffer to be used
Throws:
java.io.IOException - if an I/O error occurs
Method Detail

read

public int read()
         throws java.io.IOException
Reads one byte from this stream.

Overrides:
read in class java.io.BufferedInputStream
Returns:
the byte read from this stream or -1 if the end of the stream has been reached
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads up to len bytes from this stream.

Overrides:
read in class java.io.BufferedInputStream
Parameters:
b - the byte array into which to read the data
off - the start offset in b
len - the maximum number of bytes to read.
Returns:
the number of bytes read from the stream or -1 if the end of the stream has been reached
Throws:
java.io.IOException - if an I/O error occurs while reading from the stream

skip

public long skip(long n)
          throws java.io.IOException
Skips up to n bytes of data.

Overrides:
skip in class java.io.BufferedInputStream
Parameters:
n - the number of bytes to skip
Returns:
the number of bytes actually skipped
Throws:
java.io.IOException - error occurs while skipping

available

public int available()
              throws java.io.IOException
Gets the number of bytes that can be read from this stream without blocking.

Overrides:
available in class java.io.BufferedInputStream
Returns:
the number of bytes that can be read from this stream without blocking
Throws:
java.io.IOException - if an I/O error occurs.

mark

public void mark(int readlimit)
Marks the current position in this input stream to start reading from it when method reset is called.

Overrides:
mark in class java.io.BufferedInputStream
Parameters:
readlimit - the number of bytes that can be read before mark becomes invalid

reset

public void reset()
Resets the stream to the position that has been previously set by calling method mark.

Overrides:
reset in class java.io.BufferedInputStream

markSupported

public boolean markSupported()
Asks whether this input stream does support mark and reset.

Overrides:
markSupported in class java.io.BufferedInputStream
Returns:
true since this stream supports mark/reset

close

public void close()
           throws java.io.IOException
Closes this input stream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.BufferedInputStream
Throws:
java.io.IOException - if an I/O error occurs when closing the stream.

getPosition

public long getPosition()
Gets the current position of the stream as offset from the start position.

Specified by:
getPosition in interface javax.mail.internet.SharedInputStream
Returns:
the current read position in the stream

newStream

public java.io.InputStream newStream(long start,
                                     long end)
Gets a new SharedInputStream as sub-stream from this InputStream.

Specified by:
newStream in interface javax.mail.internet.SharedInputStream
Parameters:
start - the start position in the current stream
end - the end position (+1) in the current stream; -1 if the new stream shall end at the same position as this stream
Returns:
the new stream sub stream

finalize

protected void finalize()
                 throws java.lang.Throwable
Performs finalization.

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable - if an error occurs during finalization

This Javadoc may contain text parts from text parts from IETF Internet Standard specifications (see copyright note).

IAIK-CMS 5.1, (c) 2002 IAIK, (c) 2003 - 2010 SIC