12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.nio.ch;
27
28 import java.io.FileDescriptor;
29 import java.io.IOException;
30 import java.nio.ByteBuffer;
31 import java.nio.MappedByteBuffer;
32 import java.nio.channels.*;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.security.AccessController;
36 import sun.misc.Cleaner;
37 import sun.security.action.GetPropertyAction;
38
39 public class FileChannelImpl
40 extends FileChannel
41 {
42 // Memory allocation size for mapping buffers
43 private static final long allocationGranularity;
44
45 // Used to make native read and write calls
46 private final FileDispatcher nd;
47
48 // File descriptor
49 private final FileDescriptor fd;
50
51 // File access mode (immutable)
52 private final boolean writable;
53 private final boolean readable;
54 private final boolean append;
55
56 // Required to prevent finalization of creating stream (immutable)
57 private final Object parent;
58
59 // Thread-safe set of IDs of native threads, for signalling
60 private final NativeThreadSet threads = new NativeThreadSet(2);
61
62 // Lock for operations involving position and size
63 private final Object positionLock = new Object();
64
65 private FileChannelImpl(FileDescriptor fd, boolean readable,
66 boolean writable, boolean append, Object parent)
67 {
68 this.fd = fd;
69 this.readable = readable;
70 this.writable = writable;
71 this.append = append;
72 this.parent = parent;
73 this.nd = new FileDispatcherImpl(append);
74 }
75
76 // Used by FileInputStream.getChannel() and RandomAccessFile.getChannel()
77 public static FileChannel open(FileDescriptor fd,
78 boolean readable, boolean writable,
79 Object parent)
80 {
81 return new FileChannelImpl(fd, readable, writable, false, parent);
82 }
83
84 // Used by FileOutputStream.getChannel
85 public static FileChannel open(FileDescriptor fd,
86 boolean readable, boolean writable,
87 boolean append, Object parent)
88 {
89 return new FileChannelImpl(fd, readable, writable, append, parent);
90 }
91
92 private void ensureOpen() throws IOException {
93 if (!isOpen())
94 throw new ClosedChannelException();
95 }
96
97
98 // -- Standard channel operations --
99
100 protected void implCloseChannel() throws IOException {
101 // Release and invalidate any locks that we still hold
102 if (fileLockTable != null) {
103 for (FileLock fl: fileLockTable.removeAll()) {
104 synchronized (fl) {
105 if (fl.isValid()) {
106 nd.release(fd, fl.position(), fl.size());
107 ((FileLockImpl)fl).invalidate();
108 }
109 }
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.nio.ch;
27
28 import java.io.FileDescriptor;
29 import java.io.IOException;
30 import java.nio.ByteBuffer;
31 import java.nio.MappedByteBuffer;
32 import java.nio.channels.ClosedByInterruptException;
33 import java.nio.channels.ClosedChannelException;
34 import java.nio.channels.FileChannel;
35 import java.nio.channels.FileLock;
36 import java.nio.channels.FileLockInterruptionException;
37 import java.nio.channels.NonReadableChannelException;
38 import java.nio.channels.NonWritableChannelException;
39 import java.nio.channels.OverlappingFileLockException;
40 import java.nio.channels.ReadableByteChannel;
41 import java.nio.channels.WritableByteChannel;
42 import java.security.AccessController;
43 import java.util.ArrayList;
44 import java.util.List;
45
46 import sun.misc.Cleaner;
47 import sun.security.action.GetPropertyAction;
48
49 public class FileChannelImpl
50 extends FileChannel
51 {
52 // Memory allocation size for mapping buffers
53 private static final long allocationGranularity;
54
55 // Used to make native read and write calls
56 private final FileDispatcher nd;
57
58 // File descriptor
59 private final FileDescriptor fd;
60
61 // File access mode (immutable)
62 private final boolean writable;
63 private final boolean readable;
64 private final boolean append;
65
66 // Required to prevent finalization of creating stream (immutable)
67 private final Object parent;
68
69 // The path of the referenced file (null if the parent stream is created with a file descriptor)
70 private final String path;
71
72 // Thread-safe set of IDs of native threads, for signalling
73 private final NativeThreadSet threads = new NativeThreadSet(2);
74
75 // Lock for operations involving position and size
76 private final Object positionLock = new Object();
77
78 private FileChannelImpl(FileDescriptor fd, String path, boolean readable,
79 boolean writable, boolean append, Object parent)
80 {
81 this.fd = fd;
82 this.readable = readable;
83 this.writable = writable;
84 this.append = append;
85 this.parent = parent;
86 this.path = path;
87 this.nd = new FileDispatcherImpl(append);
88 }
89
90 // Used by FileInputStream.getChannel() and RandomAccessFile.getChannel()
91 public static FileChannel open(FileDescriptor fd, String path,
92 boolean readable, boolean writable,
93 Object parent)
94 {
95 return new FileChannelImpl(fd, path, readable, writable, false, parent);
96 }
97
98 // Used by FileOutputStream.getChannel
99 public static FileChannel open(FileDescriptor fd, String path,
100 boolean readable, boolean writable,
101 boolean append, Object parent)
102 {
103 return new FileChannelImpl(fd, path, readable, writable, append, parent);
104 }
105
106 private void ensureOpen() throws IOException {
107 if (!isOpen())
108 throw new ClosedChannelException();
109 }
110
111
112 // -- Standard channel operations --
113
114 protected void implCloseChannel() throws IOException {
115 // Release and invalidate any locks that we still hold
116 if (fileLockTable != null) {
117 for (FileLock fl: fileLockTable.removeAll()) {
118 synchronized (fl) {
119 if (fl.isValid()) {
120 nd.release(fd, fl.position(), fl.size());
121 ((FileLockImpl)fl).invalidate();
122 }
123 }
|