org.fuin.utils4j
Class RandomAccessFileOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.fuin.utils4j.RandomAccessFileOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class RandomAccessFileOutputStream
extends java.io.OutputStream

A random access based file output stream.


Constructor Summary
RandomAccessFileOutputStream(java.io.File file, java.lang.String mode)
          Constructor with file.
RandomAccessFileOutputStream(RandomAccessFileInputStream in)
          Constructor with input stream.
RandomAccessFileOutputStream(RandomAccessFileOutputStream out)
          Constructor with output stream.
 
Method Summary
 void close()
          Closes the underlying file and sets the length to the current position.
 void flush()
          Calls sync() of the underlying file's descriptor.
 java.nio.channels.FileChannel getChannel()
          Returns the channel used by the random access file.
 long getCounter()
          Returns the number of bytes written since start or since last call to resetCounter().
 java.nio.channels.FileLock lock(int tryLockMax, long tryWaitMillis)
          Lock the file.
 void resetCounter()
          Sets the internal counter to 0.
 void seek(long pos)
          Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
 void setLength(long newLength)
          Sets the length of this file.
 void truncate()
          Sets the length of the file to the number of written bytes.
 void write(byte[] b)
          Writes b.length bytes from the specified byte array to this file, starting at the current file pointer.
 void write(byte[] b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to the file.
 void write(int b)
          Writes the specified byte to this file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandomAccessFileOutputStream

public RandomAccessFileOutputStream(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.

RandomAccessFileOutputStream

public RandomAccessFileOutputStream(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.

RandomAccessFileOutputStream

public RandomAccessFileOutputStream(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.
Method Detail

write

public final void write(int b)
                 throws java.io.IOException
Writes the specified byte to this file. The write starts at the current file pointer.

Specified by:
write in class java.io.OutputStream
Parameters:
b - the byte to be written.
Throws:
java.io.IOException - if an I/O error occurs.

write

public final void write(byte[] b)
                 throws java.io.IOException
Writes b.length bytes from the specified byte array to this file, starting at the current file pointer.

Overrides:
write in class java.io.OutputStream
Parameters:
b - the data.
Throws:
java.io.IOException - if an I/O error occurs.

write

public final void write(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
Writes len bytes from the specified byte array starting at offset off to the file.

Overrides:
write in class java.io.OutputStream
Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.
Throws:
java.io.IOException - If an I/O error occurs.

flush

public final void flush()
                 throws java.io.IOException
Calls sync() of the underlying file's descriptor.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - If an I/O error occurs.

close

public final void close()
                 throws java.io.IOException
Closes the underlying file and sets the length to the current position.

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

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.

setLength

public final void setLength(long newLength)
                     throws java.io.IOException
Sets the length of this file.

Parameters:
newLength - The desired length of the file
Throws:
java.io.IOException - If an I/O error occurs

resetCounter

public final void resetCounter()
Sets the internal counter to 0.


getCounter

public final long getCounter()
Returns the number of bytes written since start or since last call to resetCounter().

Returns:
Number of bytes written.

truncate

public final void truncate()
                    throws java.io.IOException
Sets the length of the file to the number of written bytes. This is the same as calling setLength(getCounter()).

Throws:
java.io.IOException - Error setting the file length.

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.