1 | /* Copyright (C) 1998, 1999, 2001, 2002, 2003 Free Software Foundation
|
---|
2 |
|
---|
3 | This file is part of libgcj.
|
---|
4 |
|
---|
5 | This software is copyrighted work licensed under the terms of the
|
---|
6 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
---|
7 | details. */
|
---|
8 |
|
---|
9 | package java.io;
|
---|
10 |
|
---|
11 | import java.nio.channels.FileChannel;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * @author Warren Levy <warrenl@cygnus.com>
|
---|
15 | * @date October 28, 1998.
|
---|
16 | */
|
---|
17 | /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
|
---|
18 | * "The Java Language Specification", ISBN 0-201-63451-1
|
---|
19 | * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
|
---|
20 | * Status: Believed complete and correct.
|
---|
21 | */
|
---|
22 |
|
---|
23 | public class FileInputStream extends InputStream
|
---|
24 | {
|
---|
25 | /* Contains the file descriptor for referencing the actual file. */
|
---|
26 | private FileDescriptor fd;
|
---|
27 |
|
---|
28 | private FileChannel ch;
|
---|
29 |
|
---|
30 | public FileInputStream(String name) throws FileNotFoundException
|
---|
31 | {
|
---|
32 | SecurityManager s = System.getSecurityManager();
|
---|
33 | if (s != null)
|
---|
34 | s.checkRead(name);
|
---|
35 | fd = new FileDescriptor(name, FileDescriptor.READ);
|
---|
36 | }
|
---|
37 |
|
---|
38 | public FileInputStream(File file) throws FileNotFoundException
|
---|
39 | {
|
---|
40 | this(file.getPath());
|
---|
41 | }
|
---|
42 |
|
---|
43 | public FileInputStream(FileDescriptor fdObj)
|
---|
44 | {
|
---|
45 | SecurityManager s = System.getSecurityManager();
|
---|
46 | if (s != null)
|
---|
47 | s.checkRead(fdObj);
|
---|
48 | fd = fdObj;
|
---|
49 | }
|
---|
50 |
|
---|
51 | public int available() throws IOException
|
---|
52 | {
|
---|
53 | return fd.available();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void close() throws IOException
|
---|
57 | {
|
---|
58 | if (fd.valid())
|
---|
59 | fd.close();
|
---|
60 | }
|
---|
61 |
|
---|
62 | protected void finalize() throws IOException
|
---|
63 | {
|
---|
64 | // We don't actually need this, but we include it because it is
|
---|
65 | // mentioned in the JCL.
|
---|
66 | }
|
---|
67 |
|
---|
68 | public final FileDescriptor getFD() throws IOException
|
---|
69 | {
|
---|
70 | if (!fd.valid())
|
---|
71 | throw new IOException();
|
---|
72 | return fd;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public int read() throws IOException
|
---|
76 | {
|
---|
77 | return fd.read();
|
---|
78 | }
|
---|
79 |
|
---|
80 | public int read(byte[] b) throws IOException
|
---|
81 | {
|
---|
82 | return fd.read(b, 0, b.length);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public int read(byte[] b, int off, int len) throws IOException
|
---|
86 | {
|
---|
87 | if (off < 0 || len < 0 || off + len > b.length)
|
---|
88 | throw new ArrayIndexOutOfBoundsException();
|
---|
89 |
|
---|
90 | return fd.read(b, off, len);
|
---|
91 | }
|
---|
92 |
|
---|
93 | public long skip(long n) throws IOException
|
---|
94 | {
|
---|
95 | long startPos = fd.getFilePointer();
|
---|
96 | long endPos = fd.seek(n, FileDescriptor.CUR, true);
|
---|
97 | return endPos - startPos;
|
---|
98 | }
|
---|
99 |
|
---|
100 | public FileChannel getChannel ()
|
---|
101 | {
|
---|
102 | return ch;
|
---|
103 | }
|
---|
104 | }
|
---|