org.gnu.jcifs
Class CifsRandomAccessFile

java.lang.Object
  |
  +--org.gnu.jcifs.CifsRandomAccessFile

public class CifsRandomAccessFile
extends java.lang.Object

The CifsRandomAccessFile class allows reading and writing data from and to any specified location in a file. This class does not use any buffers, all read and write operations are performed remotely.

Since:
1.1

Constructor Summary
CifsRandomAccessFile(CifsDisk disk, java.lang.String file, java.lang.String mode)
          Creates a random access file stream to read from, and optionally to write to, a file with the specified name.
CifsRandomAccessFile(CifsFile file, java.lang.String mode)
          Creates a random access file stream to read from, and optionally to write to, a file with the specified name.
CifsRandomAccessFile(java.lang.String sessionname, java.lang.String file, java.lang.String mode)
          Creates a random access file stream to read from, and optionally to write to, a file with the specified name.
 
Method Summary
 void close()
          Closes the input file
 void finalize()
          Finalizes this object
 CifsDisk getDisk()
          Returns the disk on which the file resides
 long getFilePointer()
          Returns the current offset in this file.
 long length()
          Returns the length of this file.
protected  void open(java.lang.String file, CifsDisk disk, java.lang.String mode)
           
 int read(byte[] b, int off, int len)
          Reads bytes from the current offset
 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.
 int skipBytes(int n)
          Attempts to skip over n bytes of input discarding the skipped bytes.
 void write(byte[] b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this file.
 
Methods inherited from class java.lang.Object
, clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CifsRandomAccessFile

public CifsRandomAccessFile(CifsDisk disk,
                            java.lang.String file,
                            java.lang.String mode)
                     throws java.io.IOException
Creates a random access file stream to read from, and optionally to write to, a file with the specified name.

The mode argument must either be equal to "r" or "rw", indicating that the file is to be opened for input only or for both input and output, respectively. The write methods on this object will always throw an IOException if the file is opened with a mode of "r". If the mode is "rw" and the file does not exist, then an attempt is made to create it.

Parameters:
disk - disk session
file - file name
mode - the access mode.
Throws:
java.lang.IllegalArgumentException - if the mode argument is not equal to "r" or to "rw".
java.io.IOException - if an I/O error occurs.

CifsRandomAccessFile

public CifsRandomAccessFile(java.lang.String sessionname,
                            java.lang.String file,
                            java.lang.String mode)
                     throws java.io.IOException
Creates a random access file stream to read from, and optionally to write to, a file with the specified name.

The mode argument must either be equal to "r" or "rw", indicating that the file is to be opened for input only or for both input and output, respectively. The write methods on this object will always throw an IOException if the file is opened with a mode of "r". If the mode is "rw" and the file does not exist, then an attempt is made to create it.

Parameters:
sessionname - disk session name
file - file name
mode - the access mode.
Throws:
java.lang.IllegalArgumentException - if the mode argument is not equal to "r" or to "rw".
java.io.IOException - if an I/O error occurs.

CifsRandomAccessFile

public CifsRandomAccessFile(CifsFile file,
                            java.lang.String mode)
                     throws java.io.IOException
Creates a random access file stream to read from, and optionally to write to, a file with the specified name.

The mode argument must either be equal to "r" or "rw", indicating that the file is to be opened for input only or for both input and output, respectively. The write methods on this object will always throw an IOException if the file is opened with a mode of "r". If the mode is "rw" and the file does not exist, then an attempt is made to create it.

Parameters:
file - file object
mode - the access mode.
Throws:
java.lang.IllegalArgumentException - if the mode argument is not equal to "r" or to "rw".
java.io.IOException - if an I/O error occurs.
Method Detail

open

protected void open(java.lang.String file,
                    CifsDisk disk,
                    java.lang.String mode)
             throws java.io.IOException

length

public long length()
Returns the length of this file.
Returns:
the length of this file, measured in bytes.
Throws:
java.io.IOException - if an I/O error occurs.

setLength

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

If the present length of the file as returned by the length method is greater than the newLength argument then the file will be truncated. In this case, if the file offset as returned by the getFilePointer method is greater then newLength then after this method returns the offset will be equal to newLength.

If the present length of the file as returned by the length method is smaller than the newLength argument then the file will be extended. In this case, the contents of the extended portion of the file are not defined.

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

getDisk

public CifsDisk getDisk()
                 throws java.io.IOException
Returns the disk on which the file resides
Returns:
CifsDisk disk object
Throws:
java.io.IOException - if file is closed

close

public void close()
           throws java.io.IOException
Closes the input file
Throws:
java.io.IOException - if an I/O error occurs.

getFilePointer

public long getFilePointer()
                    throws java.io.IOException
Returns the current offset in this file.
Returns:
the offset from the beginning of the file, in bytes, at which the next read or write occurs.
Throws:
java.io.IOException - if an I/O error occurs.

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads bytes from the current offset
Parameters:
b - destination buffer.
off - offset at which to start storing bytes.
len - maximum number of bytes to read.
Returns:
the number of bytes read, or -1 if the end of the stream has been reached.
Throws:
java.io.IOException - if an I/O error occurs.

write

public 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 this file.
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.

skipBytes

public int skipBytes(int n)
              throws java.io.IOException
Attempts to skip over n bytes of input discarding the skipped bytes.

This method may skip over some smaller number of bytes, possibly zero. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. This method never throws an EOFException. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.

Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
java.io.IOException - if an I/O error occurs.

seek

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

finalize

public void finalize()
Finalizes this object
Overrides:
finalize in class java.lang.Object