1 7 8 package java.io; 9 10 import java.nio.channels.FileChannel ; 11 import sun.nio.ch.FileChannelImpl; 12 13 14 30 public 31 class FileInputStream extends InputStream 32 { 33 34 private FileDescriptor fd; 35 36 private FileChannel channel = null; 37 38 65 public FileInputStream(String name) throws FileNotFoundException { 66 this(name != null ? new File (name) : null); 67 } 68 69 96 public FileInputStream(File file) throws FileNotFoundException { 97 String name = (file != null ? file.getPath() : null); 98 SecurityManager security = System.getSecurityManager(); 99 if (security != null) { 100 security.checkRead(name); 101 } 102 if (name == null) { 103 throw new NullPointerException (); 104 } 105 fd = new FileDescriptor (); 106 open(name); 107 } 108 109 128 public FileInputStream(FileDescriptor fdObj) { 129 SecurityManager security = System.getSecurityManager(); 130 if (fdObj == null) { 131 throw new NullPointerException (); 132 } 133 if (security != null) { 134 security.checkRead(fdObj); 135 } 136 fd = fdObj; 137 } 138 139 143 private native void open(String name) throws FileNotFoundException ; 144 145 153 public native int read() throws IOException ; 154 155 156 163 private native int readBytes(byte b[], int off, int len) throws IOException ; 164 165 176 public int read(byte b[]) throws IOException { 177 return readBytes(b, 0, b.length); 178 } 179 180 193 public int read(byte b[], int off, int len) throws IOException { 194 return readBytes(b, off, len); 195 } 196 197 218 public native long skip(long n) throws IOException ; 219 220 228 public native int available() throws IOException ; 229 230 242 public void close() throws IOException { 243 if (channel != null) 244 channel.close(); 245 close0(); 246 } 247 248 258 public final FileDescriptor getFD() throws IOException { 259 if (fd != null) return fd; 260 throw new IOException (); 261 } 262 263 279 public FileChannel getChannel() { 280 synchronized (this) { 281 if (channel == null) 282 channel = FileChannelImpl.open(fd, true, false, this); 283 return channel; 284 } 285 } 286 287 private static native void initIDs(); 288 289 private native void close0() throws IOException ; 290 291 static { 292 initIDs(); 293 } 294 295 302 protected void finalize() throws IOException { 303 if (fd != null) { 304 if (fd != fd.in) { 305 close(); 306 } 307 } 308 } 309 } 310 | Popular Tags |