Example usage for java.io RandomAccessFile toString

List of usage examples for java.io RandomAccessFile toString

Introduction

In this page you can find the example usage for java.io RandomAccessFile toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:jag.sftp.VirtualFileSystem.java

/**
*
*
* @param path/*from www . java 2  s .c  om*/
* @param flags
* @param attrs
*
* @return
*
* @throws PermissionDeniedException
* @throws FileNotFoundException
* @throws IOException
*/
public byte[] openFile(String path, UnsignedInteger32 flags, FileAttributes attrs)
        throws PermissionDeniedException, FileNotFoundException, IOException {
    System.out.println(path);
    path = VirtualFileSystem.translateVFSPath(path);
    System.out.println(path);
    File f = new File(path);
    verifyPermissions(SshThread.getCurrentThreadUser(), path, "r");

    // Check if the file does not exist and process according to flags
    if (!f.exists()) {
        if ((flags.intValue() & NativeFileSystemProvider.OPEN_CREATE) == NativeFileSystemProvider.OPEN_CREATE) {
            // The file does not exist and the create flag is present so lets create it
            if (!f.createNewFile()) {
                throw new IOException(translateNFSPath(path) + " could not be created");
            }
        } else {
            // The file does not exist and no create flag present
            throw new FileNotFoundException(translateNFSPath(path) + " does not exist");
        }
    } else {
        if (((flags.intValue() & NativeFileSystemProvider.OPEN_CREATE) == NativeFileSystemProvider.OPEN_CREATE)
                && ((flags.intValue()
                        & NativeFileSystemProvider.OPEN_EXCLUSIVE) == NativeFileSystemProvider.OPEN_EXCLUSIVE)) {
            // The file exists but the EXCL flag is set which requires that the
            // file should not exist prior to creation, so throw a status exception
            throw new IOException(translateNFSPath(path) + " already exists");
        }
    }

    // The file now exists so open the file according to the flags yb building the relevant
    // flags for the RandomAccessFile class
    String mode = "r"
            + (((flags.intValue() & NativeFileSystemProvider.OPEN_WRITE) == NativeFileSystemProvider.OPEN_WRITE)
                    ? "ws"
                    : "");
    RandomAccessFile raf = new RandomAccessFile(f, mode);

    // Determine whether we need to truncate the file
    if (((flags.intValue() & NativeFileSystemProvider.OPEN_CREATE) == NativeFileSystemProvider.OPEN_CREATE)
            && ((flags.intValue()
                    & NativeFileSystemProvider.OPEN_TRUNCATE) == NativeFileSystemProvider.OPEN_TRUNCATE)) {
        // Set the length to zero
        raf.setLength(0);
    }

    // Record the open file
    openFiles.put(raf.toString(), new OpenFile(f, raf, flags));

    // Return the handle
    return raf.toString().getBytes("US-ASCII");
}

From source file:com.sshtools.daemon.vfs.VirtualFileSystem.java

/**
 *
 *
 * @param path/*  w  w  w  .j a  v  a  2  s . c  o  m*/
 * @param flags
 * @param attrs
 *
 * @return
 *
 * @throws PermissionDeniedException
 * @throws FileNotFoundException
 * @throws IOException
 */
public byte[] openFile(String path, UnsignedInteger32 flags, FileAttributes attrs)
        throws PermissionDeniedException, FileNotFoundException, IOException {
    path = VirtualFileSystem.translateVFSPath(path);

    File f = new File(path);

    verifyPermissions(SshThread.getCurrentThreadUser(), path, "r");

    // Check if the file does not exist and process according to flags
    if (!f.exists()) {
        if ((flags.intValue() & NativeFileSystemProvider.OPEN_CREATE) == NativeFileSystemProvider.OPEN_CREATE) {
            // The file does not exist and the create flag is present so lets create it
            if (!f.createNewFile()) {
                throw new IOException(translateNFSPath(path) + " could not be created");
            }
        } else {
            // The file does not exist and no create flag present
            throw new FileNotFoundException(translateNFSPath(path) + " does not exist");
        }
    } else {
        if (((flags.intValue() & NativeFileSystemProvider.OPEN_CREATE) == NativeFileSystemProvider.OPEN_CREATE)
                && ((flags.intValue()
                        & NativeFileSystemProvider.OPEN_EXCLUSIVE) == NativeFileSystemProvider.OPEN_EXCLUSIVE)) {
            // The file exists but the EXCL flag is set which requires that the
            // file should not exist prior to creation, so throw a status exception
            throw new IOException(translateNFSPath(path) + " already exists");
        }
    }

    // The file now exists so open the file according to the flags yb building the relevant
    // flags for the RandomAccessFile class
    String mode = "r"
            + (((flags.intValue() & NativeFileSystemProvider.OPEN_WRITE) == NativeFileSystemProvider.OPEN_WRITE)
                    ? "ws"
                    : "");

    RandomAccessFile raf = new RandomAccessFile(f, mode);

    // Determine whether we need to truncate the file
    if (((flags.intValue() & NativeFileSystemProvider.OPEN_CREATE) == NativeFileSystemProvider.OPEN_CREATE)
            && ((flags.intValue()
                    & NativeFileSystemProvider.OPEN_TRUNCATE) == NativeFileSystemProvider.OPEN_TRUNCATE)) {
        // Set the length to zero
        raf.setLength(0);
    }

    // Record the open file
    openFiles.put(raf.toString(), new OpenFile(f, raf, flags));

    // Return the handle
    return raf.toString().getBytes("US-ASCII");
}