org.abstracthorizon.mercury.maildir.file
Class SharedInputStreamImpl

java.lang.Object
  extended by java.io.InputStream
      extended by org.abstracthorizon.mercury.maildir.file.SharedInputStreamImpl
All Implemented Interfaces:
Closeable, javax.mail.internet.SharedInputStream

public class SharedInputStreamImpl
extends InputStream
implements javax.mail.internet.SharedInputStream

SharedInputStream implementation

Author:
Daniel Sendula

Field Summary
protected  byte[] buffer
          Stream's buffer
static int BUFFER_SIZE
          Stream's buffer size
protected  int buflen
          Stream's buffer content len
protected  long bufptr
          Stream's buffer start pointer
protected  long end
          Stream end offset
protected  FileProvider fileProvider
          File provider reference
protected  long fileSize
          Cached value of file's size
protected  int id
          Unique Id - needed for debug purposes only
protected  long lastAccessed
          Timestamp this stream's file is accessed last time
protected  long mark
          Mark
protected static int num
          Next Unique Id - needed for debug purposes only
protected  SharedInputStreamPool parent
          Pool that created this object
protected  long ptr
          Stream's pointer
protected  RandomAccessFile raf
          Random access file reference
protected  long start
          Stream start offset
 
Constructor Summary
SharedInputStreamImpl(SharedInputStreamPool parent, FileProvider fileProvider, long start, long end)
          Constructor.
 
Method Summary
 int available()
          Returns number of available bytes in stream (to end of stream).
protected  void checkOpened()
          Checks if underlaying file is opened.
 void close()
          Closes the stream and releases allocated resources.
 void closeImpl()
          This method actually releases the resources (random access file)
protected  long getFileSize()
          Returns file's size.
 long getPosition()
          Returns current pointer in stream
 void mark(int readlimit)
          Sets mark.
 boolean markSupported()
          Returns true
 InputStream newStream(long pos, long end)
          Creates new stream from this stream.
 int read()
          Reads one byte or returns -1 if EOF is reached (end of stream really).
 int read(byte[] buf)
          Reads whole buffer.
 int read(byte[] buf, int off, int len)
          Reads bytes from the underlaying file.
 boolean ready()
          Returns true
 void reset()
          Resets stream to the mark
 long skip(long n)
          Skips number of bytes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

num

protected static int num
Next Unique Id - needed for debug purposes only


id

protected int id
Unique Id - needed for debug purposes only


start

protected long start
Stream start offset


end

protected long end
Stream end offset


ptr

protected long ptr
Stream's pointer


mark

protected long mark
Mark


BUFFER_SIZE

public static final int BUFFER_SIZE
Stream's buffer size

See Also:
Constant Field Values

buffer

protected byte[] buffer
Stream's buffer


bufptr

protected long bufptr
Stream's buffer start pointer


buflen

protected int buflen
Stream's buffer content len


fileSize

protected long fileSize
Cached value of file's size


fileProvider

protected FileProvider fileProvider
File provider reference


raf

protected RandomAccessFile raf
Random access file reference


parent

protected SharedInputStreamPool parent
Pool that created this object


lastAccessed

protected long lastAccessed
Timestamp this stream's file is accessed last time

Constructor Detail

SharedInputStreamImpl

public SharedInputStreamImpl(SharedInputStreamPool parent,
                             FileProvider fileProvider,
                             long start,
                             long end)
Constructor.

Parameters:
parent - pool that is creating this stream
fileProvider - file provider
start - stream start
end - stream end
Method Detail

read

public int read(byte[] buf,
                int off,
                int len)
         throws IOException
Reads bytes from the underlaying file. It opens it if it wasn't open at the moment of call. Also, uses internal buffer too.

Overrides:
read in class InputStream
Parameters:
buf - buffer to be read into
off - offset in buffer
len - length to be read
Returns:
number of actually read bytes
Throws:
IOException

read

public int read(byte[] buf)
         throws IOException
Reads whole buffer.

Overrides:
read in class InputStream
Parameters:
buf - buffer to be read into
Returns:
number of actually read bytes
Throws:
IOException

read

public int read()
         throws IOException
Reads one byte or returns -1 if EOF is reached (end of stream really).

Specified by:
read in class InputStream
Returns:
read byte
Throws:
IOException

available

public int available()
              throws IOException
Returns number of available bytes in stream (to end of stream).

Overrides:
available in class InputStream
Returns:
number of available bytes
Throws:
IOException

close

public void close()
           throws IOException
Closes the stream and releases allocated resources.

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

closeImpl

public void closeImpl()
               throws IOException
This method actually releases the resources (random access file)

Throws:
IOException

markSupported

public boolean markSupported()
Returns true

Overrides:
markSupported in class InputStream
Returns:
true

mark

public void mark(int readlimit)
Sets mark.

Overrides:
mark in class InputStream
Parameters:
readlimit - ignored

reset

public void reset()
           throws IOException
Resets stream to the mark

Overrides:
reset in class InputStream
Throws:
IOException

ready

public boolean ready()
Returns true

Returns:
true

skip

public long skip(long n)
          throws IOException
Skips number of bytes

Overrides:
skip in class InputStream
Parameters:
n - number of bytes to be skipped
Returns:
current pointer in stream
Throws:
IOException

getPosition

public long getPosition()
Returns current pointer in stream

Specified by:
getPosition in interface javax.mail.internet.SharedInputStream
Returns:
current pointer in stream

newStream

public InputStream newStream(long pos,
                             long end)
Creates new stream from this stream.

Specified by:
newStream in interface javax.mail.internet.SharedInputStream
Parameters:
pos - new relative start position
end - new relative end position
Returns:
new instance with given start and end.

checkOpened

protected void checkOpened()
                    throws IOException
Checks if underlaying file is opened. It uses FileProvider to obtain file.

Throws:
IOException

getFileSize

protected long getFileSize()
Returns file's size. If not cached then caches it using FileProvider

Returns:
file's size.


Copyright © 2005-2009 Abstract Horizon. All Rights Reserved.