org.fuin.utils4j
Class RandomAccessFileInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.fuin.utils4j.RandomAccessFileInputStream
All Implemented Interfaces:
java.io.Closeable

public final class RandomAccessFileInputStream
extends java.io.InputStream

A random access based file input stream. The readlimit argument of the mark(int) method is ignored (there is no limit).


Constructor Summary
RandomAccessFileInputStream(java.io.File file, java.lang.String mode)
          Constructor with file.
RandomAccessFileInputStream(RandomAccessFileInputStream in)
          Constructor with input stream.
RandomAccessFileInputStream(RandomAccessFileOutputStream out)
          Constructor with output stream.
 
Method Summary
 int available()
          
 void close()
          
 long fileSize()
          Returns the size of the underlying file.
 java.nio.channels.FileChannel getChannel()
          Returns the channel used by the random access file.
 java.nio.channels.FileLock lock(int tryLockMax, long tryWaitMillis)
          Lock the file.
 void mark(int readlimit)
          
 boolean markSupported()
          
 int read()
          
 int read(byte[] b)
          
 int read(byte[] b, int off, int len)
          
 void reset()
          
 void seek(long pos)
          Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
 long skip(long n)
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandomAccessFileInputStream

public RandomAccessFileInputStream(java.io.File file,
                                   java.lang.String mode)
                            throws java.io.FileNotFoundException
Constructor with file.

Parameters:
file - File the stream operates on - Cannot be null.
mode - Access mode, as described in RandomAccessFile - Cannot be null.
Throws:
java.io.FileNotFoundException - If the mode is "r" but the given file object does not denote an existing regular file, or if the mode begins with "rw" but the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.

RandomAccessFileInputStream

public RandomAccessFileInputStream(RandomAccessFileInputStream in)
Constructor with input stream. The new stream shares the RandomAccessFile with the argument. Be aware that closing this stream will also close the file used by the argument!

Parameters:
in - The RandomAccessFile instance from this argument will be used - Cannot be null.

RandomAccessFileInputStream

public RandomAccessFileInputStream(RandomAccessFileOutputStream out)
Constructor with output stream. The new stream shares the RandomAccessFile with the argument. Be aware that closing this stream will also close the file used by the argument!

Parameters:
out - The RandomAccessFile instance from this argument will be used - Cannot be null.
Method Detail

read

public final int read()
               throws java.io.IOException

Specified by:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public final int read(byte[] b)
               throws java.io.IOException

Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public final int read(byte[] b,
                      int off,
                      int len)
               throws java.io.IOException

Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

skip

public final long skip(long n)
                throws java.io.IOException

Overrides:
skip in class java.io.InputStream
Throws:
java.io.IOException

fileSize

public final long fileSize()
                    throws java.io.IOException
Returns the size of the underlying file.

Returns:
Number of bytes.
Throws:
java.io.IOException - IOException if an I/O error occurs.

available

public final int available()
                    throws java.io.IOException

Overrides:
available in class java.io.InputStream
Throws:
java.io.IOException

close

public final void close()
                 throws java.io.IOException

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

mark

public void mark(int readlimit)

Overrides:
mark in class java.io.InputStream

reset

public final void reset()
                 throws java.io.IOException

Overrides:
reset in class java.io.InputStream
Throws:
java.io.IOException

markSupported

public final boolean markSupported()

Overrides:
markSupported in class java.io.InputStream

getChannel

public final java.nio.channels.FileChannel getChannel()
Returns the channel used by the random access file.

Returns:
Channel.

seek

public final void seek(long pos)
                throws java.io.IOException
Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length. The file length will change only by writing after the offset has been set beyond the end of the file.

Parameters:
pos - the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.
Throws:
java.io.IOException - if pos is less than 0 or if an I/O error occurs.

lock

public final java.nio.channels.FileLock lock(int tryLockMax,
                                             long tryWaitMillis)
                                      throws LockingFailedException
Lock the file.

Parameters:
tryLockMax - Number of tries to lock before throwing an exception.
tryWaitMillis - Milliseconds to sleep between retries.
Returns:
FileLock.
Throws:
LockingFailedException - Locking the file failed.


Copyright © 2009 Future Invent Informationsmanagement GmbH. All Rights Reserved.