com.mucommander.io
Class RandomAccessInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.mucommander.io.RandomAccessInputStream
All Implemented Interfaces:
RandomAccess, java.io.Closeable
Direct Known Subclasses:
BlockRandomInputStream, LocalFile.LocalRandomAccessInputStream, NFSFile.NFSRandomAccessInputStream, SMBFile.SMBRandomAccessInputStream

public abstract class RandomAccessInputStream
extends java.io.InputStream
implements RandomAccess

RandomAccessInputStream is an InputStream with random access. The following java.io.InputStream methods are overridden to provide an improved implementation:

Important: BufferedInputStream or any wrapper InputStream class that uses a read buffer CANNOT be used with a RandomAccessInputStream if the RandomAccess.seek(long) method is to be used. Doing so would corrupt the read buffer and yield to data inconsistencies.

Author:
Maxence Bernard

Constructor Summary
RandomAccessInputStream()
          Creates a new RandomAccessInputStream.
 
Method Summary
 int available()
          Return the number of bytes that are available for reading, that is: RandomAccess.getLength() - RandomAccess.getOffset() - 1.
abstract  void close()
          Closes this stream and releases any system resources associated with the stream.
 void mark(int readLimit)
          Overrides InputStream.mark() to provide a working implementation of the method.
 boolean markSupported()
          Always returns true: mark(int) and reset() methods are supported.
abstract  int read(byte[] b, int off, int len)
          Reads up to len bytes of data from this file into an array of bytes.
 void readFully(byte[] b)
          Reads b.length bytes from this file into the byte array, starting at the current file pointer.
 void readFully(byte[] b, int off, int len)
          Reads exactly len bytes from this file into the byte array, starting at the current file pointer.
 void reset()
          Overrides InputStream.mark() to provide a working implementation of the method.
 long skip(long n)
          Skips (up to) the specified number of bytes and returns the number of bytes effectively skipped.
 
Methods inherited from class java.io.InputStream
read, read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.mucommander.io.RandomAccess
getLength, getOffset, seek
 

Constructor Detail

RandomAccessInputStream

public RandomAccessInputStream()
Creates a new RandomAccessInputStream.

Method Detail

readFully

public void readFully(byte[] b)
               throws java.io.IOException
Reads b.length bytes from this file into the byte array, starting at the current file pointer. This method reads repeatedly from the file until the requested number of bytes are read. This method blocks until the requested number of bytes are read, the end of the stream is detected, or an exception is thrown.

Parameters:
b - the buffer into which the data is read.
Throws:
java.io.EOFException - if this file reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.

readFully

public void readFully(byte[] b,
                      int off,
                      int len)
               throws java.io.IOException
Reads exactly len bytes from this file into the byte array, starting at the current file pointer. This method reads repeatedly from the file until the requested number of bytes are read. This method blocks until the requested number of bytes are read, the end of the stream is detected, or an exception is thrown.

Parameters:
b - the buffer into which the data is read.
off - the start offset of the data.
len - the number of bytes to read.
Throws:
java.io.EOFException - if this file reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.

skip

public long skip(long n)
          throws java.io.IOException
Skips (up to) the specified number of bytes and returns the number of bytes effectively skipped. The exact given number of bytes will be skipped as long as the current offset as returned by RandomAccess.getOffset() plus the number of bytes to skip doesn't exceed the length of this stream as returned by RandomAccess.getLength(). If it does, all the remaining bytes will be skipped so that the offset of this stream will be positionned to RandomAccess.getLength(). Returns -1 if the offset is already positionned to the end of the stream when this method is called.

Overrides:
skip in class java.io.InputStream
Parameters:
n - number of bytes to skip
Returns:
the number of bytes that have effectively been skipped, -1 if the offset is already positionned to the end of the stream when this method is called
Throws:
java.io.IOException - if something went wrong

available

public int available()
              throws java.io.IOException
Return the number of bytes that are available for reading, that is: RandomAccess.getLength() - RandomAccess.getOffset() - 1. Since InputStream.available() returns an int and this method overrides it, a maximum of Integer.MAX_VALUE can be returned, even if this stream has more bytes available.

Overrides:
available in class java.io.InputStream
Returns:
the number of bytes that are available for reading.
Throws:
java.io.IOException - if something went wrong

mark

public void mark(int readLimit)
Overrides InputStream.mark() to provide a working implementation of the method. The given readLimit is simply ignored, the stream can be repositionned using reset() with no limit on the number of bytes read after mark() has been called.

Overrides:
mark in class java.io.InputStream
Parameters:
readLimit - this parameter has no effect and is simply ignored

reset

public void reset()
           throws java.io.IOException
Overrides InputStream.mark() to provide a working implementation of the method.

Overrides:
reset in class java.io.InputStream
Throws:
java.io.IOException - if something went wrong

markSupported

public boolean markSupported()
Always returns true: mark(int) and reset() methods are supported.

Overrides:
markSupported in class java.io.InputStream

read

public abstract int read(byte[] b,
                         int off,
                         int len)
                  throws java.io.IOException
Reads up to len bytes of data from this file into an array of bytes. This method blocks until at least one byte of input is available.

Overrides:
read in class java.io.InputStream
Parameters:
b - the buffer into which the data is read
off - the start offset of the data
len - the maximum number of bytes read
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
Throws:
java.io.IOException - if an I/O error occurs

close

public abstract void close()
                    throws java.io.IOException
Closes this stream and releases any system resources associated with the stream. A closed stream cannot perform input operations and cannot be reopened.

Specified by:
close in interface RandomAccess
Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException - if an I/O error occurs.


This file is part of muCommander - Copyright (C) 2002-2008 Maxence Bernard