jdk/src/share/classes/java/nio/channels/FileChannel.java
author alanb
Fri, 28 Jun 2013 16:10:23 +0100
changeset 18574 4aeaeb541678
parent 18164 68f1bc4eadd4
child 21801 b8a5ff5f0c2a
permissions -rw-r--r--
8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.** Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13795
diff changeset
     2
 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3631
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3631
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3631
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3631
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3631
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.nio.channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.MappedByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.spi.AbstractInterruptibleChannel;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    32
import java.nio.file.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    33
import java.nio.file.attribute.FileAttribute;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    34
import java.nio.file.spi.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    35
import java.util.Set;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    36
import java.util.HashSet;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    37
import java.util.Collections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A channel for reading, writing, mapping, and manipulating a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
3631
4dc04372d56b 6874521: Remove @note tags
alanb
parents: 2057
diff changeset
    42
 * <p> A file channel is a {@link SeekableByteChannel} that is connected to
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    43
 * a file. It has a current <i>position</i> within its file which can
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    44
 * be both {@link #position() <i>queried</i>} and {@link #position(long)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    45
 * <i>modified</i>}.  The file itself contains a variable-length sequence
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * of bytes that can be read and written and whose current {@link #size
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    47
 * <i>size</i>} can be queried.  The size of the file increases
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * when bytes are written beyond its current size; the size of the file
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
    49
 * decreases when it is {@link #truncate <i>truncated</i>}.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * file may also have some associated <i>metadata</i> such as access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * permissions, content type, and last-modification time; this class does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * define methods for metadata access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p> In addition to the familiar read, write, and close operations of byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * channels, this class defines the following file-specific operations: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    59
 *   <li><p> Bytes may be {@link #read(ByteBuffer, long) read} or
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    60
 *   {@link #write(ByteBuffer, long) <i>written</i>} at an absolute
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   position in a file in a way that does not affect the channel's current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *   position.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    64
 *   <li><p> A region of a file may be {@link #map <i>mapped</i>}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *   directly into memory; for large files this is often much more efficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *   than invoking the usual <tt>read</tt> or <tt>write</tt> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    69
 *   <li><p> Updates made to a file may be {@link #force <i>forced
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    70
 *   out</i>} to the underlying storage device, ensuring that data are not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *   lost in the event of a system crash.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    73
 *   <li><p> Bytes can be transferred from a file {@link #transferTo <i>to
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    74
 *   some other channel</i>}, and {@link #transferFrom <i>vice
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    75
 *   versa</i>}, in a way that can be optimized by many operating systems
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *   into a very fast transfer directly to or from the filesystem cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    79
 *   <li><p> A region of a file may be {@link FileLock <i>locked</i>}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *   against access by other programs.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <p> File channels are safe for use by multiple concurrent threads.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * {@link Channel#close close} method may be invoked at any time, as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * by the {@link Channel} interface.  Only one operation that involves the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * channel's position or can change its file's size may be in progress at any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * given time; attempts to initiate a second such operation while the first is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * still in progress will block until the first operation completes.  Other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * operations, in particular those that take an explicit position, may proceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * concurrently; whether they in fact do so is dependent upon the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * implementation and is therefore unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p> The view of a file provided by an instance of this class is guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * to be consistent with other views of the same file provided by other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * instances in the same program.  The view provided by an instance of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * class may or may not, however, be consistent with the views seen by other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * concurrently-running programs due to caching performed by the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * operating system and delays induced by network-filesystem protocols.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * is true regardless of the language in which these other programs are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * written, and whether they are running on the same machine or on some other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * machine.  The exact nature of any such inconsistencies are system-dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * and are therefore unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   105
 * <p> A file channel is created by invoking one of the {@link #open open}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   106
 * methods defined by this class. A file channel can also be obtained from an
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   107
 * existing {@link java.io.FileInputStream#getChannel FileInputStream}, {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * java.io.FileOutputStream#getChannel FileOutputStream}, or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * java.io.RandomAccessFile#getChannel RandomAccessFile} object by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * that object's <tt>getChannel</tt> method, which returns a file channel that
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   111
 * is connected to the same underlying file. Where the file channel is obtained
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   112
 * from an existing stream or random access file then the state of the file
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   113
 * channel is intimately connected to that of the object whose <tt>getChannel</tt>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   114
 * method returned the channel.  Changing the channel's position, whether
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   115
 * explicitly or by reading or writing bytes, will change the file position of
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   116
 * the originating object, and vice versa. Changing the file's length via the
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   117
 * file channel will change the length seen via the originating object, and vice
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   118
 * versa.  Changing the file's content by writing bytes will change the content
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   119
 * seen by the originating object, and vice versa.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   121
 * <a name="open-mode"></a> <p> At various points this class specifies that an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * instance that is "open for reading," "open for writing," or "open for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * reading and writing" is required.  A channel obtained via the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * java.io.FileInputStream#getChannel getChannel} method of a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * java.io.FileInputStream} instance will be open for reading.  A channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * obtained via the {@link java.io.FileOutputStream#getChannel getChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * method of a {@link java.io.FileOutputStream} instance will be open for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * writing.  Finally, a channel obtained via the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * java.io.RandomAccessFile#getChannel getChannel} method of a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * java.io.RandomAccessFile} instance will be open for reading if the instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * was created with mode <tt>"r"</tt> and will be open for reading and writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * if the instance was created with mode <tt>"rw"</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   134
 * <a name="append-mode"></a><p> A file channel that is open for writing may be in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <i>append mode</i>, for example if it was obtained from a file-output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * that was created by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * java.io.FileOutputStream#FileOutputStream(java.io.File,boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * FileOutputStream(File,boolean)} constructor and passing <tt>true</tt> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * the second parameter.  In this mode each invocation of a relative write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * operation first advances the position to the end of the file and then writes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * the requested data.  Whether the advancement of the position and the writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * of the data are done in a single atomic operation is system-dependent and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * therefore unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * @see java.io.FileInputStream#getChannel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * @see java.io.FileOutputStream#getChannel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * @see java.io.RandomAccessFile#getChannel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * @author Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
public abstract class FileChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    extends AbstractInterruptibleChannel
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   157
    implements SeekableByteChannel, GatheringByteChannel, ScatteringByteChannel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Initializes a new instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    protected FileChannel() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   164
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   165
     * Opens or creates a file, returning a file channel to access the file.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   166
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   167
     * <p> The {@code options} parameter determines how the file is opened.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   168
     * The {@link StandardOpenOption#READ READ} and {@link StandardOpenOption#WRITE
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   169
     * WRITE} options determine if the file should be opened for reading and/or
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   170
     * writing. If neither option (or the {@link StandardOpenOption#APPEND APPEND}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   171
     * option) is contained in the array then the file is opened for reading.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   172
     * By default reading or writing commences at the beginning of the file.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   173
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   174
     * <p> In the addition to {@code READ} and {@code WRITE}, the following
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   175
     * options may be present:
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   176
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   177
     * <table border=1 cellpadding=5 summary="">
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   178
     * <tr> <th>Option</th> <th>Description</th> </tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   179
     * <tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   180
     *   <td> {@link StandardOpenOption#APPEND APPEND} </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   181
     *   <td> If this option is present then the file is opened for writing and
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   182
     *     each invocation of the channel's {@code write} method first advances
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   183
     *     the position to the end of the file and then writes the requested
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   184
     *     data. Whether the advancement of the position and the writing of the
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   185
     *     data are done in a single atomic operation is system-dependent and
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   186
     *     therefore unspecified. This option may not be used in conjunction
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   187
     *     with the {@code READ} or {@code TRUNCATE_EXISTING} options. </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   188
     * </tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   189
     * <tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   190
     *   <td> {@link StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING} </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   191
     *   <td> If this option is present then the existing file is truncated to
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   192
     *   a size of 0 bytes. This option is ignored when the file is opened only
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   193
     *   for reading. </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   194
     * </tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   195
     * <tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   196
     *   <td> {@link StandardOpenOption#CREATE_NEW CREATE_NEW} </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   197
     *   <td> If this option is present then a new file is created, failing if
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   198
     *   the file already exists. When creating a file the check for the
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   199
     *   existence of the file and the creation of the file if it does not exist
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   200
     *   is atomic with respect to other file system operations. This option is
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   201
     *   ignored when the file is opened only for reading. </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   202
     * </tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   203
     * <tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   204
     *   <td > {@link StandardOpenOption#CREATE CREATE} </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   205
     *   <td> If this option is present then an existing file is opened if it
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   206
     *   exists, otherwise a new file is created. When creating a file the check
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   207
     *   for the existence of the file and the creation of the file if it does
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   208
     *   not exist is atomic with respect to other file system operations. This
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   209
     *   option is ignored if the {@code CREATE_NEW} option is also present or
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   210
     *   the file is opened only for reading. </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   211
     * </tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   212
     * <tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   213
     *   <td > {@link StandardOpenOption#DELETE_ON_CLOSE DELETE_ON_CLOSE} </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   214
     *   <td> When this option is present then the implementation makes a
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   215
     *   <em>best effort</em> attempt to delete the file when closed by the
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   216
     *   the {@link #close close} method. If the {@code close} method is not
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   217
     *   invoked then a <em>best effort</em> attempt is made to delete the file
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   218
     *   when the Java virtual machine terminates. </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   219
     * </tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   220
     * <tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   221
     *   <td>{@link StandardOpenOption#SPARSE SPARSE} </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   222
     *   <td> When creating a new file this option is a <em>hint</em> that the
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   223
     *   new file will be sparse. This option is ignored when not creating
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   224
     *   a new file. </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   225
     * </tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   226
     * <tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   227
     *   <td> {@link StandardOpenOption#SYNC SYNC} </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   228
     *   <td> Requires that every update to the file's content or metadata be
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   229
     *   written synchronously to the underlying storage device. (see <a
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   230
     *   href="../file/package-summary.html#integrity"> Synchronized I/O file
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   231
     *   integrity</a>). </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   232
     * <tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   233
     * <tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   234
     *   <td> {@link StandardOpenOption#DSYNC DSYNC} </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   235
     *   <td> Requires that every update to the file's content be written
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   236
     *   synchronously to the underlying storage device. (see <a
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   237
     *   href="../file/package-summary.html#integrity"> Synchronized I/O file
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   238
     *   integrity</a>). </td>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   239
     * </tr>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   240
     * </table>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   241
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   242
     * <p> An implementation may also support additional options.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   243
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   244
     * <p> The {@code attrs} parameter is an optional array of file {@link
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   245
     * FileAttribute file-attributes} to set atomically when creating the file.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   246
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   247
     * <p> The new channel is created by invoking the {@link
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   248
     * FileSystemProvider#newFileChannel newFileChannel} method on the
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   249
     * provider that created the {@code Path}.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   250
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   251
     * @param   path
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   252
     *          The path of the file to open or create
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   253
     * @param   options
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   254
     *          Options specifying how the file is opened
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   255
     * @param   attrs
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   256
     *          An optional list of file attributes to set atomically when
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   257
     *          creating the file
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   258
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   259
     * @return  A new file channel
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   260
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   261
     * @throws  IllegalArgumentException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   262
     *          If the set contains an invalid combination of options
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   263
     * @throws  UnsupportedOperationException
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   264
     *          If the {@code path} is associated with a provider that does not
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   265
     *          support creating file channels, or an unsupported open option is
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   266
     *          specified, or the array contains an attribute that cannot be set
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   267
     *          atomically when creating the file
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   268
     * @throws  IOException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   269
     *          If an I/O error occurs
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   270
     * @throws  SecurityException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   271
     *          If a security manager is installed and it denies an
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   272
     *          unspecified permission required by the implementation.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   273
     *          In the case of the default provider, the {@link
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   274
     *          SecurityManager#checkRead(String)} method is invoked to check
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   275
     *          read access if the file is opened for reading. The {@link
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   276
     *          SecurityManager#checkWrite(String)} method is invoked to check
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   277
     *          write access if the file is opened for writing
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   278
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   279
     * @since   1.7
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   280
     */
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   281
    public static FileChannel open(Path path,
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   282
                                   Set<? extends OpenOption> options,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   283
                                   FileAttribute<?>... attrs)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   284
        throws IOException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   285
    {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   286
        FileSystemProvider provider = path.getFileSystem().provider();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   287
        return provider.newFileChannel(path, options, attrs);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   288
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   289
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 10137
diff changeset
   290
    @SuppressWarnings({"unchecked", "rawtypes"}) // generic array construction
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   291
    private static final FileAttribute<?>[] NO_ATTRIBUTES = new FileAttribute[0];
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   292
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   293
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   294
     * Opens or creates a file, returning a file channel to access the file.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   295
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   296
     * <p> An invocation of this method behaves in exactly the same way as the
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   297
     * invocation
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   298
     * <pre>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   299
     *     fc.{@link #open(Path,Set,FileAttribute[]) open}(file, opts, new FileAttribute&lt;?&gt;[0]);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   300
     * </pre>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   301
     * where {@code opts} is a set of the options specified in the {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   302
     * options} array.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   303
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   304
     * @param   path
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   305
     *          The path of the file to open or create
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   306
     * @param   options
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   307
     *          Options specifying how the file is opened
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   308
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   309
     * @return  A new file channel
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   310
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   311
     * @throws  IllegalArgumentException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   312
     *          If the set contains an invalid combination of options
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   313
     * @throws  UnsupportedOperationException
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   314
     *          If the {@code path} is associated with a provider that does not
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   315
     *          support creating file channels, or an unsupported open option is
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   316
     *          specified
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   317
     * @throws  IOException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   318
     *          If an I/O error occurs
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   319
     * @throws  SecurityException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   320
     *          If a security manager is installed and it denies an
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   321
     *          unspecified permission required by the implementation.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   322
     *          In the case of the default provider, the {@link
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   323
     *          SecurityManager#checkRead(String)} method is invoked to check
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   324
     *          read access if the file is opened for reading. The {@link
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   325
     *          SecurityManager#checkWrite(String)} method is invoked to check
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   326
     *          write access if the file is opened for writing
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   327
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   328
     * @since   1.7
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   329
     */
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   330
    public static FileChannel open(Path path, OpenOption... options)
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   331
        throws IOException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   332
    {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   333
        Set<OpenOption> set = new HashSet<OpenOption>(options.length);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   334
        Collections.addAll(set, options);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   335
        return open(path, set, NO_ATTRIBUTES);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   336
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    // -- Channel operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Reads a sequence of bytes from this channel into the given buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * <p> Bytes are read starting at this channel's current file position, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * then the file position is updated with the number of bytes actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * read.  Otherwise this method behaves exactly as specified in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * ReadableByteChannel} interface. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    public abstract int read(ByteBuffer dst) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Reads a sequence of bytes from this channel into a subsequence of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * given buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * <p> Bytes are read starting at this channel's current file position, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * then the file position is updated with the number of bytes actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * read.  Otherwise this method behaves exactly as specified in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * ScatteringByteChannel} interface.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public abstract long read(ByteBuffer[] dsts, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Reads a sequence of bytes from this channel into the given buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <p> Bytes are read starting at this channel's current file position, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * then the file position is updated with the number of bytes actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * read.  Otherwise this method behaves exactly as specified in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * ScatteringByteChannel} interface.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public final long read(ByteBuffer[] dsts) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return read(dsts, 0, dsts.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Writes a sequence of bytes to this channel from the given buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <p> Bytes are written starting at this channel's current file position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * unless the channel is in append mode, in which case the position is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * first advanced to the end of the file.  The file is grown, if necessary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * to accommodate the written bytes, and then the file position is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * with the number of bytes actually written.  Otherwise this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * behaves exactly as specified by the {@link WritableByteChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * interface. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public abstract int write(ByteBuffer src) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * Writes a sequence of bytes to this channel from a subsequence of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * given buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <p> Bytes are written starting at this channel's current file position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * unless the channel is in append mode, in which case the position is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * first advanced to the end of the file.  The file is grown, if necessary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * to accommodate the written bytes, and then the file position is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * with the number of bytes actually written.  Otherwise this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * behaves exactly as specified in the {@link GatheringByteChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * interface.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public abstract long write(ByteBuffer[] srcs, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Writes a sequence of bytes to this channel from the given buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * <p> Bytes are written starting at this channel's current file position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * unless the channel is in append mode, in which case the position is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * first advanced to the end of the file.  The file is grown, if necessary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * to accommodate the written bytes, and then the file position is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * with the number of bytes actually written.  Otherwise this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * behaves exactly as specified in the {@link GatheringByteChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * interface.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public final long write(ByteBuffer[] srcs) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return write(srcs, 0, srcs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    // -- Other operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 14342
diff changeset
   421
     * Returns this channel's file position.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @return  This channel's file position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *          a non-negative integer counting the number of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *          from the beginning of the file to the current position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public abstract long position() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Sets this channel's file position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * <p> Setting the position to a value that is greater than the file's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * current size is legal but does not change the size of the file.  A later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * attempt to read bytes at such a position will immediately return an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * end-of-file indication.  A later attempt to write bytes at such a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * position will cause the file to be grown to accommodate the new bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * the values of any bytes between the previous end-of-file and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * newly-written bytes are unspecified.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @param  newPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *         The new position, a non-negative integer counting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *         the number of bytes from the beginning of the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @return  This file channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *          If the new position is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    public abstract FileChannel position(long newPosition) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 14342
diff changeset
   464
     * Returns the current size of this channel's file.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @return  The current size of this channel's file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *          measured in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public abstract long size() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Truncates this channel's file to the given size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * <p> If the given size is less than the file's current size then the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * is truncated, discarding any bytes beyond the new end of the file.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * the given size is greater than or equal to the file's current size then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * the file is not modified.  In either case, if this channel's file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * position is greater than the given size then it is set to that size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @param  size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *         The new size, a non-negative byte count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @return  This file channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @throws  NonWritableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *          If this channel was not opened for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *          If the new size is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public abstract FileChannel truncate(long size) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * Forces any updates to this channel's file to be written to the storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * device that contains it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * <p> If this channel's file resides on a local storage device then when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * this method returns it is guaranteed that all changes made to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * since this channel was created, or since this method was last invoked,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * will have been written to that device.  This is useful for ensuring that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * critical information is not lost in the event of a system crash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * <p> If the file does not reside on a local device then no such guarantee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * is made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * <p> The <tt>metaData</tt> parameter can be used to limit the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * I/O operations that this method is required to perform.  Passing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * <tt>false</tt> for this parameter indicates that only updates to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * file's content need be written to storage; passing <tt>true</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * indicates that updates to both the file's content and metadata must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * written, which generally requires at least one more I/O operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Whether this parameter actually has any effect is dependent upon the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * underlying operating system and is therefore unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * <p> Invoking this method may cause an I/O operation to occur even if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * channel was only opened for reading.  Some operating systems, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * example, maintain a last-access time as part of a file's metadata, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * this time is updated whenever the file is read.  Whether or not this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * actually done is system-dependent and is therefore unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * <p> This method is only guaranteed to force changes that were made to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * this channel's file via the methods defined in this class.  It may or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * may not force changes that were made by modifying the content of a
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   537
     * {@link MappedByteBuffer <i>mapped byte buffer</i>} obtained by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * invoking the {@link #map map} method.  Invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * MappedByteBuffer#force force} method of the mapped byte buffer will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * force changes made to the buffer's content to be written.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @param   metaData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *          If <tt>true</tt> then this method is required to force changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *          to both the file's content and metadata to be written to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *          storage; otherwise, it need only force content changes to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *          written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    public abstract void force(boolean metaData) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * Transfers bytes from this channel's file to the given writable byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * <p> An attempt is made to read up to <tt>count</tt> bytes starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * the given <tt>position</tt> in this channel's file and write them to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * target channel.  An invocation of this method may or may not transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * all of the requested bytes; whether or not it does so depends upon the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * natures and states of the channels.  Fewer than the requested number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * bytes are transferred if this channel's file contains fewer than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * <tt>count</tt> bytes starting at the given <tt>position</tt>, or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * target channel is non-blocking and it has fewer than <tt>count</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * bytes free in its output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * <p> This method does not modify this channel's position.  If the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * position is greater than the file's current size then no bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * transferred.  If the target channel has a position then bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * written starting at that position and then the position is incremented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * by the number of bytes written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * <p> This method is potentially much more efficient than a simple loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * that reads from this channel and writes to the target channel.  Many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * operating systems can transfer bytes directly from the filesystem cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * to the target channel without actually copying them.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @param  position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *         The position within the file at which the transfer is to begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *         must be non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @param  count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *         The maximum number of bytes to be transferred; must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *         non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @param  target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *         The target channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @return  The number of bytes, possibly zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *          that were actually transferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *         If the preconditions on the parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @throws  NonReadableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *          If this channel was not opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @throws  NonWritableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *          If the target channel was not opened for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *          If either this channel or the target channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *          If another thread closes either channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *          while the transfer is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *          If another thread interrupts the current thread while the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *          transfer is in progress, thereby closing both channels and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *          setting the current thread's interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    public abstract long transferTo(long position, long count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                                    WritableByteChannel target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Transfers bytes into this channel's file from the given readable byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * <p> An attempt is made to read up to <tt>count</tt> bytes from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * source channel and write them to this channel's file starting at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * given <tt>position</tt>.  An invocation of this method may or may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * transfer all of the requested bytes; whether or not it does so depends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * upon the natures and states of the channels.  Fewer than the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * number of bytes will be transferred if the source channel has fewer than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * <tt>count</tt> bytes remaining, or if the source channel is non-blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * and has fewer than <tt>count</tt> bytes immediately available in its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * <p> This method does not modify this channel's position.  If the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * position is greater than the file's current size then no bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * transferred.  If the source channel has a position then bytes are read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * starting at that position and then the position is incremented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * number of bytes read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * <p> This method is potentially much more efficient than a simple loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * that reads from the source channel and writes to this channel.  Many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * operating systems can transfer bytes directly from the source channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * into the filesystem cache without actually copying them.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @param  src
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *         The source channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @param  position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *         The position within the file at which the transfer is to begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *         must be non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @param  count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *         The maximum number of bytes to be transferred; must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *         non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @return  The number of bytes, possibly zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *          that were actually transferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *         If the preconditions on the parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @throws  NonReadableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *          If the source channel was not opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @throws  NonWritableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *          If this channel was not opened for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *          If either this channel or the source channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *          If another thread closes either channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *          while the transfer is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *          If another thread interrupts the current thread while the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *          transfer is in progress, thereby closing both channels and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *          setting the current thread's interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    public abstract long transferFrom(ReadableByteChannel src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                                      long position, long count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * Reads a sequence of bytes from this channel into the given buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * starting at the given file position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * <p> This method works in the same manner as the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * #read(ByteBuffer)} method, except that bytes are read starting at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * given file position rather than at the channel's current position.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * method does not modify this channel's position.  If the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * is greater than the file's current size then no bytes are read.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @param  dst
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *         The buffer into which bytes are to be transferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @param  position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *         The file position at which the transfer is to begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *         must be non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * @return  The number of bytes read, possibly zero, or <tt>-1</tt> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *          given position is greater than or equal to the file's current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *          size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *          If the position is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * @throws  NonReadableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     *          If this channel was not opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *          while the read operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *          while the read operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    public abstract int read(ByteBuffer dst, long position) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * Writes a sequence of bytes to this channel from the given buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * starting at the given file position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * <p> This method works in the same manner as the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * #write(ByteBuffer)} method, except that bytes are written starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * the given file position rather than at the channel's current position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * This method does not modify this channel's position.  If the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * position is greater than the file's current size then the file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * grown to accommodate the new bytes; the values of any bytes between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * previous end-of-file and the newly-written bytes are unspecified.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @param  src
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     *         The buffer from which bytes are to be transferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @param  position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *         The file position at which the transfer is to begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *         must be non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @return  The number of bytes written, possibly zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     *          If the position is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @throws  NonWritableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *          If this channel was not opened for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *          while the write operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *          while the write operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    public abstract int write(ByteBuffer src, long position) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    // -- Memory-mapped buffers --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * A typesafe enumeration for file-mapping modes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @see java.nio.channels.FileChannel#map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    public static class MapMode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
         * Mode for a read-only mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        public static final MapMode READ_ONLY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            = new MapMode("READ_ONLY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
         * Mode for a read/write mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        public static final MapMode READ_WRITE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            = new MapMode("READ_WRITE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
         * Mode for a private (copy-on-write) mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        public static final MapMode PRIVATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            = new MapMode("PRIVATE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        private MapMode(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
         * Returns a string describing this file-mapping mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
         * @return  A descriptive string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * Maps a region of this channel's file directly into memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * <p> A region of a file may be mapped into memory in one of three modes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   833
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *   <li><p> <i>Read-only:</i> Any attempt to modify the resulting buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *   will cause a {@link java.nio.ReadOnlyBufferException} to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *   ({@link MapMode#READ_ONLY MapMode.READ_ONLY}) </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *   <li><p> <i>Read/write:</i> Changes made to the resulting buffer will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *   eventually be propagated to the file; they may or may not be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     *   visible to other programs that have mapped the same file.  ({@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *   MapMode#READ_WRITE MapMode.READ_WRITE}) </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *   <li><p> <i>Private:</i> Changes made to the resulting buffer will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *   be propagated to the file and will not be visible to other programs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *   that have mapped the same file; instead, they will cause private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     *   copies of the modified portions of the buffer to be created.  ({@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *   MapMode#PRIVATE MapMode.PRIVATE}) </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * <p> For a read-only mapping, this channel must have been opened for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * reading; for a read/write or private mapping, this channel must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * been opened for both reading and writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   856
     * <p> The {@link MappedByteBuffer <i>mapped byte buffer</i>}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * returned by this method will have a position of zero and a limit and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * capacity of <tt>size</tt>; its mark will be undefined.  The buffer and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * the mapping that it represents will remain valid until the buffer itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * is garbage-collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * <p> A mapping, once established, is not dependent upon the file channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * that was used to create it.  Closing the channel, in particular, has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * effect upon the validity of the mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * <p> Many of the details of memory-mapped files are inherently dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * upon the underlying operating system and are therefore unspecified.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * behavior of this method when the requested region is not completely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * contained within this channel's file is unspecified.  Whether changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * made to the content or size of the underlying file, by this program or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * another, are propagated to the buffer is unspecified.  The rate at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * changes to the buffer are propagated to the file is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * <p> For most operating systems, mapping a file into memory is more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * expensive than reading or writing a few tens of kilobytes of data via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * the usual {@link #read read} and {@link #write write} methods.  From the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * standpoint of performance it is generally only worth mapping relatively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * large files into memory.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * @param  mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     *         One of the constants {@link MapMode#READ_ONLY READ_ONLY}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     *         MapMode#READ_WRITE READ_WRITE}, or {@link MapMode#PRIVATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     *         PRIVATE} defined in the {@link MapMode} class, according to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     *         whether the file is to be mapped read-only, read/write, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *         privately (copy-on-write), respectively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @param  position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     *         The position within the file at which the mapped region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *         is to start; must be non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * @param  size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *         The size of the region to be mapped; must be non-negative and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *         no greater than {@link java.lang.Integer#MAX_VALUE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   895
     * @return  The mapped byte buffer
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   896
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * @throws NonReadableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *         If the <tt>mode</tt> is {@link MapMode#READ_ONLY READ_ONLY} but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     *         this channel was not opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * @throws NonWritableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *         If the <tt>mode</tt> is {@link MapMode#READ_WRITE READ_WRITE} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *         {@link MapMode#PRIVATE PRIVATE} but this channel was not opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     *         for both reading and writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     *         If the preconditions on the parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * @throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     *         If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * @see java.nio.channels.FileChannel.MapMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @see java.nio.MappedByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    public abstract MappedByteBuffer map(MapMode mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                                         long position, long size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    // -- Locks --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * Acquires a lock on the given region of this channel's file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * <p> An invocation of this method will block until the region can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * locked, this channel is closed, or the invoking thread is interrupted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * whichever comes first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * <p> If this channel is closed by another thread during an invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * this method then an {@link AsynchronousCloseException} will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * <p> If the invoking thread is interrupted while waiting to acquire the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * lock then its interrupt status will be set and a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * FileLockInterruptionException} will be thrown.  If the invoker's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * interrupt status is set when this method is invoked then that exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * will be thrown immediately; the thread's interrupt status will not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * <p> The region specified by the <tt>position</tt> and <tt>size</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * parameters need not be contained within, or even overlap, the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * underlying file.  Lock regions are fixed in size; if a locked region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * initially contains the end of the file and the file grows beyond the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * region then the new portion of the file will not be covered by the lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * If a file is expected to grow in size and a lock on the entire file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * required then a region starting at zero, and no smaller than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * expected maximum size of the file, should be locked.  The zero-argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * {@link #lock()} method simply locks a region of size {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * Long#MAX_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * <p> Some operating systems do not support shared locks, in which case a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * request for a shared lock is automatically converted into a request for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * an exclusive lock.  Whether the newly-acquired lock is shared or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * exclusive may be tested by invoking the resulting lock object's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * FileLock#isShared() isShared} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * <p> File locks are held on behalf of the entire Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * They are not suitable for controlling access to a file by multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * threads within the same virtual machine.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * @param  position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     *         The position at which the locked region is to start; must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     *         non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * @param  size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     *         The size of the locked region; must be non-negative, and the sum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     *         <tt>position</tt>&nbsp;+&nbsp;<tt>size</tt> must be non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * @param  shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     *         <tt>true</tt> to request a shared lock, in which case this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     *         channel must be open for reading (and possibly writing);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     *         <tt>false</tt> to request an exclusive lock, in which case this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     *         channel must be open for writing (and possibly reading)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * @return  A lock object representing the newly-acquired lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *          If the preconditions on the parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *          If another thread closes this channel while the invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *          thread is blocked in this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * @throws  FileLockInterruptionException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     *          If the invoking thread is interrupted while blocked in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *          method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * @throws  OverlappingFileLockException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     *          If a lock that overlaps the requested region is already held by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *          this Java virtual machine, or if another thread is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     *          blocked in this method and is attempting to lock an overlapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     *          region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * @throws  NonReadableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     *          If <tt>shared</tt> is <tt>true</tt> this channel was not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     *          opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * @throws  NonWritableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     *          If <tt>shared</tt> is <tt>false</tt> but this channel was not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *          opened for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * @see     #lock()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @see     #tryLock()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * @see     #tryLock(long,long,boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    public abstract FileLock lock(long position, long size, boolean shared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * Acquires an exclusive lock on this channel's file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * <p> An invocation of this method of the form <tt>fc.lock()</tt> behaves
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     *     fc.{@link #lock(long,long,boolean) lock}(0L, Long.MAX_VALUE, false) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * @return  A lock object representing the newly-acquired lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     *          If another thread closes this channel while the invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *          thread is blocked in this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @throws  FileLockInterruptionException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *          If the invoking thread is interrupted while blocked in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *          method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * @throws  OverlappingFileLockException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     *          If a lock that overlaps the requested region is already held by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     *          this Java virtual machine, or if another thread is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     *          blocked in this method and is attempting to lock an overlapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     *          region of the same file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * @throws  NonWritableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *          If this channel was not opened for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * @see     #lock(long,long,boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * @see     #tryLock()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * @see     #tryLock(long,long,boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    public final FileLock lock() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        return lock(0L, Long.MAX_VALUE, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * Attempts to acquire a lock on the given region of this channel's file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * <p> This method does not block.  An invocation always returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * immediately, either having acquired a lock on the requested region or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * having failed to do so.  If it fails to acquire a lock because an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * overlapping lock is held by another program then it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * <tt>null</tt>.  If it fails to acquire a lock for any other reason then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * an appropriate exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * <p> The region specified by the <tt>position</tt> and <tt>size</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * parameters need not be contained within, or even overlap, the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * underlying file.  Lock regions are fixed in size; if a locked region
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * initially contains the end of the file and the file grows beyond the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * region then the new portion of the file will not be covered by the lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * If a file is expected to grow in size and a lock on the entire file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * required then a region starting at zero, and no smaller than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * expected maximum size of the file, should be locked.  The zero-argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * {@link #tryLock()} method simply locks a region of size {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * Long#MAX_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * <p> Some operating systems do not support shared locks, in which case a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * request for a shared lock is automatically converted into a request for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * an exclusive lock.  Whether the newly-acquired lock is shared or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * exclusive may be tested by invoking the resulting lock object's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * FileLock#isShared() isShared} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * <p> File locks are held on behalf of the entire Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * They are not suitable for controlling access to a file by multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * threads within the same virtual machine.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * @param  position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     *         The position at which the locked region is to start; must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     *         non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * @param  size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     *         The size of the locked region; must be non-negative, and the sum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     *         <tt>position</tt>&nbsp;+&nbsp;<tt>size</tt> must be non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * @param  shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     *         <tt>true</tt> to request a shared lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     *         <tt>false</tt> to request an exclusive lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * @return  A lock object representing the newly-acquired lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *          or <tt>null</tt> if the lock could not be acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     *          because another program holds an overlapping lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     *          If the preconditions on the parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * @throws  OverlappingFileLockException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     *          If a lock that overlaps the requested region is already held by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *          this Java virtual machine, or if another thread is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     *          blocked in this method and is attempting to lock an overlapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     *          region of the same file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * @see     #lock()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * @see     #lock(long,long,boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * @see     #tryLock()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    public abstract FileLock tryLock(long position, long size, boolean shared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * Attempts to acquire an exclusive lock on this channel's file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * <p> An invocation of this method of the form <tt>fc.tryLock()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     *     fc.{@link #tryLock(long,long,boolean) tryLock}(0L, Long.MAX_VALUE, false) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * @return  A lock object representing the newly-acquired lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     *          or <tt>null</tt> if the lock could not be acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     *          because another program holds an overlapping lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * @throws  OverlappingFileLockException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *          If a lock that overlaps the requested region is already held by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     *          this Java virtual machine, or if another thread is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     *          blocked in this method and is attempting to lock an overlapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     *          region
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * @see     #lock()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * @see     #lock(long,long,boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * @see     #tryLock(long,long,boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    public final FileLock tryLock() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        return tryLock(0L, Long.MAX_VALUE, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
}