Java Code Examples for sun.nio.ch.FileChannelImpl#open()

The following examples show how to use sun.nio.ch.FileChannelImpl#open() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileOutputStream.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 * object associated with this file output stream.
 *
 * <p> The initial {@link java.nio.channels.FileChannel#position()
 * position} of the returned channel will be equal to the
 * number of bytes written to the file so far unless this stream is in
 * append mode, in which case it will be equal to the size of the file.
 * Writing bytes to this stream will increment the channel's position
 * accordingly.  Changing the channel's position, either explicitly or by
 * writing, will change this stream's file position.
 *
 * @return  the file channel associated with this file output stream
 *
 * @since 1.4
 * @spec JSR-51
 */
public FileChannel getChannel() {
    FileChannel fc = this.channel;
    if (fc == null) {
        synchronized (this) {
            fc = this.channel;
            if (fc == null) {
                this.channel = fc = FileChannelImpl.open(fd, path, false, true, this);
                if (closed) {
                    try {
                        // possible race with close(), benign since
                        // FileChannel.close is final and idempotent
                        fc.close();
                    } catch (IOException ioe) {
                        throw new InternalError(ioe); // should not happen
                    }
                }
            }
        }
    }
    return fc;
}
 
Example 2
Source File: FileInputStream.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 * object associated with this file input stream.
 *
 * <p> The initial {@link java.nio.channels.FileChannel#position()
 * position} of the returned channel will be equal to the
 * number of bytes read from the file so far.  Reading bytes from this
 * stream will increment the channel's position.  Changing the channel's
 * position, either explicitly or by reading, will change this stream's
 * file position.
 *
 * @return  the file channel associated with this file input stream
 *
 * @since 1.4
 * @spec JSR-51
 */
public FileChannel getChannel() {
    FileChannel fc = this.channel;
    if (fc == null) {
        synchronized (this) {
            fc = this.channel;
            if (fc == null) {
                this.channel = fc = FileChannelImpl.open(fd, path, true, false, this);
                if (closed) {
                    try {
                        // possible race with close(), benign since
                        // FileChannel.close is final and idempotent
                        fc.close();
                    } catch (IOException ioe) {
                        throw new InternalError(ioe); // should not happen
                    }
                }
            }
        }
    }
    return fc;
}
 
Example 3
Source File: RandomAccessFile.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 * object associated with this file.
 *
 * <p> The {@link java.nio.channels.FileChannel#position()
 * position} of the returned channel will always be equal to
 * this object's file-pointer offset as returned by the {@link
 * #getFilePointer getFilePointer} method.  Changing this object's
 * file-pointer offset, whether explicitly or by reading or writing bytes,
 * will change the position of the channel, and vice versa.  Changing the
 * file's length via this object will change the length seen via the file
 * channel, and vice versa.
 *
 * @return  the file channel associated with this file
 *
 * @since 1.4
 * @spec JSR-51
 */
public final FileChannel getChannel() {
    FileChannel fc = this.channel;
    if (fc == null) {
        synchronized (this) {
            fc = this.channel;
            if (fc == null) {
                this.channel = fc = FileChannelImpl.open(fd, path, true, rw, this);
                if (closed.get()) {
                    try {
                        fc.close();
                    } catch (IOException ioe) {
                        throw new InternalError(ioe); // should not happen
                    }
                }
            }
        }
    }
    return fc;
}
 
Example 4
Source File: UnixChannelFactory.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a file channel by opening a file using a dfd/path pair
 */
static FileChannel newFileChannel(int dfd,
                                  UnixPath path,
                                  String pathForPermissionCheck,
                                  Set<? extends OpenOption> options,
                                  int mode)
    throws UnixException
{
    Flags flags = Flags.toFlags(options);

    // default is reading; append => writing
    if (!flags.read && !flags.write) {
        if (flags.append) {
            flags.write = true;
        } else {
            flags.read = true;
        }
    }

    // validation
    if (flags.read && flags.append)
        throw new IllegalArgumentException("READ + APPEND not allowed");
    if (flags.append && flags.truncateExisting)
        throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");

    FileDescriptor fdObj = open(dfd, path, pathForPermissionCheck, flags, mode);
    return FileChannelImpl.open(fdObj, path.toString(), flags.read, flags.write, flags.append, null);
}
 
Example 5
Source File: WindowsChannelFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Open/creates file, returning FileChannel to access the file
 *
 * @param   pathForWindows
 *          The path of the file to open/create
 * @param   pathToCheck
 *          The path used for permission checks (if security manager)
 */
static FileChannel newFileChannel(String pathForWindows,
                                  String pathToCheck,
                                  Set<? extends OpenOption> options,
                                  long pSecurityDescriptor)
    throws WindowsException
{
    Flags flags = Flags.toFlags(options);

    // default is reading; append => writing
    if (!flags.read && !flags.write) {
        if (flags.append) {
            flags.write = true;
        } else {
            flags.read = true;
        }
    }

    // validation
    if (flags.read && flags.append)
        throw new IllegalArgumentException("READ + APPEND not allowed");
    if (flags.append && flags.truncateExisting)
        throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");

    FileDescriptor fdObj = open(pathForWindows, pathToCheck, flags, pSecurityDescriptor);
    return FileChannelImpl.open(fdObj, pathForWindows, flags.read, flags.write, flags.append, null);
}
 
Example 6
Source File: UnixChannelFactory.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a file channel by opening a file using a dfd/path pair
 */
static FileChannel newFileChannel(int dfd,
                                  UnixPath path,
                                  String pathForPermissionCheck,
                                  Set<? extends OpenOption> options,
                                  int mode)
    throws UnixException
{
    Flags flags = Flags.toFlags(options);

    // default is reading; append => writing
    if (!flags.read && !flags.write) {
        if (flags.append) {
            flags.write = true;
        } else {
            flags.read = true;
        }
    }

    // validation
    if (flags.read && flags.append)
        throw new IllegalArgumentException("READ + APPEND not allowed");
    if (flags.append && flags.truncateExisting)
        throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");

    FileDescriptor fdObj = open(dfd, path, pathForPermissionCheck, flags, mode);
    return FileChannelImpl.open(fdObj, flags.read, flags.write, flags.append, null);
}
 
Example 7
Source File: WindowsChannelFactory.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Open/creates file, returning FileChannel to access the file
 *
 * @param   pathForWindows
 *          The path of the file to open/create
 * @param   pathToCheck
 *          The path used for permission checks (if security manager)
 */
static FileChannel newFileChannel(String pathForWindows,
                                  String pathToCheck,
                                  Set<? extends OpenOption> options,
                                  long pSecurityDescriptor)
    throws WindowsException
{
    Flags flags = Flags.toFlags(options);

    // default is reading; append => writing
    if (!flags.read && !flags.write) {
        if (flags.append) {
            flags.write = true;
        } else {
            flags.read = true;
        }
    }

    // validation
    if (flags.read && flags.append)
        throw new IllegalArgumentException("READ + APPEND not allowed");
    if (flags.append && flags.truncateExisting)
        throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");

    FileDescriptor fdObj = open(pathForWindows, pathToCheck, flags, pSecurityDescriptor);
    return FileChannelImpl.open(fdObj, pathForWindows, flags.read, flags.write, flags.append, null);
}
 
Example 8
Source File: UnixChannelFactory.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a file channel by opening a file using a dfd/path pair
 */
static FileChannel newFileChannel(int dfd,
                                  UnixPath path,
                                  String pathForPermissionCheck,
                                  Set<? extends OpenOption> options,
                                  int mode)
    throws UnixException
{
    Flags flags = Flags.toFlags(options);

    // default is reading; append => writing
    if (!flags.read && !flags.write) {
        if (flags.append) {
            flags.write = true;
        } else {
            flags.read = true;
        }
    }

    // validation
    if (flags.read && flags.append)
        throw new IllegalArgumentException("READ + APPEND not allowed");
    if (flags.append && flags.truncateExisting)
        throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");

    FileDescriptor fdObj = open(dfd, path, pathForPermissionCheck, flags, mode);
    return FileChannelImpl.open(fdObj, path.toString(), flags.read, flags.write, flags.append, null);
}
 
Example 9
Source File: UnixChannelFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a file channel by opening a file using a dfd/path pair
 */
static FileChannel newFileChannel(int dfd,
                                  UnixPath path,
                                  String pathForPermissionCheck,
                                  Set<? extends OpenOption> options,
                                  int mode)
    throws UnixException
{
    Flags flags = Flags.toFlags(options);

    // default is reading; append => writing
    if (!flags.read && !flags.write) {
        if (flags.append) {
            flags.write = true;
        } else {
            flags.read = true;
        }
    }

    // validation
    if (flags.read && flags.append)
        throw new IllegalArgumentException("READ + APPEND not allowed");
    if (flags.append && flags.truncateExisting)
        throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");

    FileDescriptor fdObj = open(dfd, path, pathForPermissionCheck, flags, mode);
    return FileChannelImpl.open(fdObj, path.toString(), flags.read, flags.write, flags.append, null);
}
 
Example 10
Source File: UnixChannelFactory.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a file channel by opening a file using a dfd/path pair
 */
static FileChannel newFileChannel(int dfd,
                                  UnixPath path,
                                  String pathForPermissionCheck,
                                  Set<? extends OpenOption> options,
                                  int mode)
    throws UnixException
{
    Flags flags = Flags.toFlags(options);

    // default is reading; append => writing
    if (!flags.read && !flags.write) {
        if (flags.append) {
            flags.write = true;
        } else {
            flags.read = true;
        }
    }

    // validation
    if (flags.read && flags.append)
        throw new IllegalArgumentException("READ + APPEND not allowed");
    if (flags.append && flags.truncateExisting)
        throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");

    FileDescriptor fdObj = open(dfd, path, pathForPermissionCheck, flags, mode);
    return FileChannelImpl.open(fdObj, flags.read, flags.write, flags.append, null);
}
 
Example 11
Source File: WindowsChannelFactory.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Open/creates file, returning FileChannel to access the file
 *
 * @param   pathForWindows
 *          The path of the file to open/create
 * @param   pathToCheck
 *          The path used for permission checks (if security manager)
 */
static FileChannel newFileChannel(String pathForWindows,
                                  String pathToCheck,
                                  Set<? extends OpenOption> options,
                                  long pSecurityDescriptor)
    throws WindowsException
{
    Flags flags = Flags.toFlags(options);

    // default is reading; append => writing
    if (!flags.read && !flags.write) {
        if (flags.append) {
            flags.write = true;
        } else {
            flags.read = true;
        }
    }

    // validation
    if (flags.read && flags.append)
        throw new IllegalArgumentException("READ + APPEND not allowed");
    if (flags.append && flags.truncateExisting)
        throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");

    FileDescriptor fdObj = open(pathForWindows, pathToCheck, flags, pSecurityDescriptor);
    return FileChannelImpl.open(fdObj, pathForWindows, flags.read, flags.write, flags.append, null);
}
 
Example 12
Source File: WindowsChannelFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Open/creates file, returning FileChannel to access the file
 *
 * @param   pathForWindows
 *          The path of the file to open/create
 * @param   pathToCheck
 *          The path used for permission checks (if security manager)
 */
static FileChannel newFileChannel(String pathForWindows,
                                  String pathToCheck,
                                  Set<? extends OpenOption> options,
                                  long pSecurityDescriptor)
    throws WindowsException
{
    Flags flags = Flags.toFlags(options);

    // default is reading; append => writing
    if (!flags.read && !flags.write) {
        if (flags.append) {
            flags.write = true;
        } else {
            flags.read = true;
        }
    }

    // validation
    if (flags.read && flags.append)
        throw new IllegalArgumentException("READ + APPEND not allowed");
    if (flags.append && flags.truncateExisting)
        throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");

    FileDescriptor fdObj = open(pathForWindows, pathToCheck, flags, pSecurityDescriptor);
    return FileChannelImpl.open(fdObj, pathForWindows, flags.read, flags.write, null);
}
 
Example 13
Source File: UnixChannelFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs a file channel from an existing (open) file descriptor
 */
static FileChannel newFileChannel(int fd, String path, boolean reading, boolean writing) {
    FileDescriptor fdObj = new FileDescriptor();
    fdAccess.set(fdObj, fd);
    return FileChannelImpl.open(fdObj, path, reading, writing, null);
}
 
Example 14
Source File: RandomAccessFile.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 * object associated with this file.
 *
 * <p> The {@link java.nio.channels.FileChannel#position()
 * position} of the returned channel will always be equal to
 * this object's file-pointer offset as returned by the {@link
 * #getFilePointer getFilePointer} method.  Changing this object's
 * file-pointer offset, whether explicitly or by reading or writing bytes,
 * will change the position of the channel, and vice versa.  Changing the
 * file's length via this object will change the length seen via the file
 * channel, and vice versa.
 *
 * @return  the file channel associated with this file
 *
 * @since 1.4
 * @spec JSR-51
 */
public final FileChannel getChannel() {
    synchronized (this) {
        if (channel == null) {
            channel = FileChannelImpl.open(fd, path, true, rw, this);
        }
        return channel;
    }
}
 
Example 15
Source File: FileInputStream.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 * object associated with this file input stream.
 *
 * <p> The initial {@link java.nio.channels.FileChannel#position()
 * position} of the returned channel will be equal to the
 * number of bytes read from the file so far.  Reading bytes from this
 * stream will increment the channel's position.  Changing the channel's
 * position, either explicitly or by reading, will change this stream's
 * file position.
 *
 * @return  the file channel associated with this file input stream
 *
 * @since 1.4
 * @spec JSR-51
 */
public FileChannel getChannel() {
    synchronized (this) {
        if (channel == null) {
            channel = FileChannelImpl.open(fd, path, true, false, this);
        }
        return channel;
    }
}
 
Example 16
Source File: RandomAccessFile.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 * object associated with this file.
 *
 * <p> The {@link java.nio.channels.FileChannel#position()
 * position} of the returned channel will always be equal to
 * this object's file-pointer offset as returned by the {@link
 * #getFilePointer getFilePointer} method.  Changing this object's
 * file-pointer offset, whether explicitly or by reading or writing bytes,
 * will change the position of the channel, and vice versa.  Changing the
 * file's length via this object will change the length seen via the file
 * channel, and vice versa.
 *
 * @return  the file channel associated with this file
 *
 * @since 1.4
 * @spec JSR-51
 */
public final FileChannel getChannel() {
    synchronized (this) {
        if (channel == null) {
            channel = FileChannelImpl.open(fd, path, true, rw, this);
        }
        return channel;
    }
}
 
Example 17
Source File: FileOutputStream.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 * object associated with this file output stream.
 *
 * <p> The initial {@link java.nio.channels.FileChannel#position()
 * position} of the returned channel will be equal to the
 * number of bytes written to the file so far unless this stream is in
 * append mode, in which case it will be equal to the size of the file.
 * Writing bytes to this stream will increment the channel's position
 * accordingly.  Changing the channel's position, either explicitly or by
 * writing, will change this stream's file position.
 *
 * @return  the file channel associated with this file output stream
 *
 * @since 1.4
 * @spec JSR-51
 */
public FileChannel getChannel() {
    synchronized (this) {
        if (channel == null) {
            channel = FileChannelImpl.open(fd, path, false, true, append, this);
        }
        return channel;
    }
}
 
Example 18
Source File: RandomAccessFile.java    From Java8CN with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 * object associated with this file.
 *
 * <p> The {@link java.nio.channels.FileChannel#position()
 * position} of the returned channel will always be equal to
 * this object's file-pointer offset as returned by the {@link
 * #getFilePointer getFilePointer} method.  Changing this object's
 * file-pointer offset, whether explicitly or by reading or writing bytes,
 * will change the position of the channel, and vice versa.  Changing the
 * file's length via this object will change the length seen via the file
 * channel, and vice versa.
 *
 * @return  the file channel associated with this file
 *
 * @since 1.4
 * @spec JSR-51
 */
public final FileChannel getChannel() {
    synchronized (this) {
        if (channel == null) {
            channel = FileChannelImpl.open(fd, path, true, rw, this);
        }
        return channel;
    }
}
 
Example 19
Source File: FileInputStream.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 * object associated with this file input stream.
 *
 * <p> The initial {@link java.nio.channels.FileChannel#position()
 * position} of the returned channel will be equal to the
 * number of bytes read from the file so far.  Reading bytes from this
 * stream will increment the channel's position.  Changing the channel's
 * position, either explicitly or by reading, will change this stream's
 * file position.
 *
 * @return  the file channel associated with this file input stream
 *
 * @since 1.4
 * @spec JSR-51
 */
public FileChannel getChannel() {
    synchronized (this) {
        if (channel == null) {
            channel = FileChannelImpl.open(fd, true, false, this);
        }
        return channel;
    }
}
 
Example 20
Source File: FileInputStream.java    From dragonwell8_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 * object associated with this file input stream.
 *
 * <p> The initial {@link java.nio.channels.FileChannel#position()
 * position} of the returned channel will be equal to the
 * number of bytes read from the file so far.  Reading bytes from this
 * stream will increment the channel's position.  Changing the channel's
 * position, either explicitly or by reading, will change this stream's
 * file position.
 *
 * @return  the file channel associated with this file input stream
 *
 * @since 1.4
 * @spec JSR-51
 */
public FileChannel getChannel() {
    synchronized (this) {
        if (channel == null) {
            channel = FileChannelImpl.open(fd, path, true, false, this);
        }
        return channel;
    }
}