| 1 | /* Copyright (C) 1998, 1999, 2001 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 | /**
|
|---|
| 12 | * @author Warren Levy <[email protected]>
|
|---|
| 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
|
|---|
| 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
|
|---|
| 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.valid())
|
|---|
| 55 | fd.close();
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | protected void finalize() throws IOException
|
|---|
| 59 | {
|
|---|
| 60 | if (fd != null)
|
|---|
| 61 | fd.finalize();
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | public final FileDescriptor getFD() throws IOException
|
|---|
| 65 | {
|
|---|
| 66 | if (!fd.valid())
|
|---|
| 67 | throw new IOException();
|
|---|
| 68 | return fd;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | public int read() throws IOException
|
|---|
| 72 | {
|
|---|
| 73 | return fd.read();
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | public int read(byte[] b) throws IOException
|
|---|
| 77 | {
|
|---|
| 78 | return fd.read(b, 0, b.length);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | public int read(byte[] b, int off, int len) throws IOException
|
|---|
| 82 | {
|
|---|
| 83 | if (off < 0 || len < 0 || off + len > b.length)
|
|---|
| 84 | throw new ArrayIndexOutOfBoundsException();
|
|---|
| 85 |
|
|---|
| 86 | return fd.read(b, off, len);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | public long skip(long n) throws IOException
|
|---|
| 90 | {
|
|---|
| 91 | long startPos = fd.getFilePointer();
|
|---|
| 92 | long endPos = fd.seek(n, FileDescriptor.CUR, true);
|
|---|
| 93 | return endPos - startPos;
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|