42 import java.security.AccessController; |
42 import java.security.AccessController; |
43 import java.util.ArrayList; |
43 import java.util.ArrayList; |
44 import java.util.List; |
44 import java.util.List; |
45 |
45 |
46 import sun.misc.Cleaner; |
46 import sun.misc.Cleaner; |
|
47 import sun.misc.JavaIOFileDescriptorAccess; |
|
48 import sun.misc.SharedSecrets; |
47 import sun.security.action.GetPropertyAction; |
49 import sun.security.action.GetPropertyAction; |
48 |
50 |
49 public class FileChannelImpl |
51 public class FileChannelImpl |
50 extends FileChannel |
52 extends FileChannel |
51 { |
53 { |
52 // Memory allocation size for mapping buffers |
54 // Memory allocation size for mapping buffers |
53 private static final long allocationGranularity; |
55 private static final long allocationGranularity; |
54 |
56 |
|
57 // Access to FileDispatcher internals |
|
58 private static final JavaIOFileDescriptorAccess fdAccess = |
|
59 SharedSecrets.getJavaIOFileDescriptorAccess(); |
|
60 |
55 // Used to make native read and write calls |
61 // Used to make native read and write calls |
56 private final FileDispatcher nd; |
62 private final FileDispatcher nd; |
57 |
63 |
58 // File descriptor |
64 // File descriptor |
59 private final FileDescriptor fd; |
65 private final FileDescriptor fd; |
60 |
66 |
61 // File access mode (immutable) |
67 // File access mode (immutable) |
62 private final boolean writable; |
68 private final boolean writable; |
63 private final boolean readable; |
69 private final boolean readable; |
64 private final boolean append; |
|
65 |
70 |
66 // Required to prevent finalization of creating stream (immutable) |
71 // Required to prevent finalization of creating stream (immutable) |
67 private final Object parent; |
72 private final Object parent; |
68 |
73 |
69 // The path of the referenced file |
74 // The path of the referenced file |
75 |
80 |
76 // Lock for operations involving position and size |
81 // Lock for operations involving position and size |
77 private final Object positionLock = new Object(); |
82 private final Object positionLock = new Object(); |
78 |
83 |
79 private FileChannelImpl(FileDescriptor fd, String path, boolean readable, |
84 private FileChannelImpl(FileDescriptor fd, String path, boolean readable, |
80 boolean writable, boolean append, Object parent) |
85 boolean writable, Object parent) |
81 { |
86 { |
82 this.fd = fd; |
87 this.fd = fd; |
83 this.readable = readable; |
88 this.readable = readable; |
84 this.writable = writable; |
89 this.writable = writable; |
85 this.append = append; |
|
86 this.parent = parent; |
90 this.parent = parent; |
87 this.path = path; |
91 this.path = path; |
88 this.nd = new FileDispatcherImpl(append); |
92 this.nd = new FileDispatcherImpl(); |
89 } |
93 } |
90 |
94 |
91 // Used by FileInputStream.getChannel() and RandomAccessFile.getChannel() |
95 // Used by FileInputStream.getChannel(), FileOutputStream.getChannel |
|
96 // and RandomAccessFile.getChannel() |
92 public static FileChannel open(FileDescriptor fd, String path, |
97 public static FileChannel open(FileDescriptor fd, String path, |
93 boolean readable, boolean writable, |
98 boolean readable, boolean writable, |
94 Object parent) |
99 Object parent) |
95 { |
100 { |
96 return new FileChannelImpl(fd, path, readable, writable, false, parent); |
101 return new FileChannelImpl(fd, path, readable, writable, parent); |
97 } |
|
98 |
|
99 // Used by FileOutputStream.getChannel |
|
100 public static FileChannel open(FileDescriptor fd, String path, |
|
101 boolean readable, boolean writable, |
|
102 boolean append, Object parent) |
|
103 { |
|
104 return new FileChannelImpl(fd, path, readable, writable, append, parent); |
|
105 } |
102 } |
106 |
103 |
107 private void ensureOpen() throws IOException { |
104 private void ensureOpen() throws IOException { |
108 if (!isOpen()) |
105 if (!isOpen()) |
109 throw new ClosedChannelException(); |
106 throw new ClosedChannelException(); |
110 } |
107 } |
111 |
|
112 |
108 |
113 // -- Standard channel operations -- |
109 // -- Standard channel operations -- |
114 |
110 |
115 protected void implCloseChannel() throws IOException { |
111 protected void implCloseChannel() throws IOException { |
116 // Release and invalidate any locks that we still hold |
112 // Release and invalidate any locks that we still hold |