OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / libjava / java / io / FileInputStream.java
1 /* Copyright (C) 1998, 1999  Cygnus Solutions
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 /**
12  * @author Warren Levy <warrenl@cygnus.com>
13  * @date October 28, 1998.  
14  */
15 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
16  * "The Java Language Specification", ISBN 0-201-63451-1
17  * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
18  * Status:  Believed complete and correct.
19  */
20  
21 public class FileInputStream extends InputStream
22 {
23   /* Contains the file descriptor for referencing the actual file. */
24   private FileDescriptor fd;
25
26   public FileInputStream(String name) throws FileNotFoundException, IOException
27   {
28     SecurityManager s = System.getSecurityManager();
29     if (s != null)
30       s.checkRead(name);
31     fd = new FileDescriptor(name, FileDescriptor.READ);
32   }
33
34   public FileInputStream(File file) throws FileNotFoundException, IOException
35   {
36     this(file.getPath());
37   }
38
39   public FileInputStream(FileDescriptor fdObj)
40   {
41     SecurityManager s = System.getSecurityManager();
42     if (s != null)
43       s.checkRead(fdObj);
44     fd = fdObj;
45   }
46
47   public int available() throws IOException
48   {
49     return fd.available();
50   }
51
52   public void close() throws IOException
53   {
54     if (fd == null)
55       return;
56
57     fd.close();
58     fd = null;
59   }
60
61   protected void finalize() throws IOException
62   {
63     if (fd != null)
64       fd.finalize();
65   }
66
67   public final FileDescriptor getFD() throws IOException
68   {
69     if (!fd.valid())
70       throw new IOException();
71     return fd;
72   }
73
74   public int read() throws IOException
75   {
76     return fd.read();
77   }
78
79   public int read(byte[] b) throws IOException
80   {
81     return fd.read(b, 0, b.length);
82   }
83
84   public int read(byte[] b, int off, int len) throws IOException
85   {
86     if (off < 0 || len < 0 || off + len > b.length)
87       throw new ArrayIndexOutOfBoundsException();
88
89     return fd.read(b, off, len);
90   }
91
92   public long skip(long n) throws IOException
93   {
94     return fd.seek(n, FileDescriptor.CUR);
95   }
96 }