001: /*
002: * @(#)FDInputStream.java 1.44 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.io;
029:
030: import java.io.*;
031:
032: /**
033: * A file input stream is an input stream for reading data from a
034: * <code>File</code> or from a <code>FileDescriptor</code>.
035: *
036: * @author Arthur van Hoff
037: * @version 1.40, 08/19/02
038: * @see java.io.File
039: * @see java.io.FileDescriptor
040: * @see java.io.FileOutputStream
041: * @since JDK1.0
042: */
043: public class FDInputStream extends InputStream {
044: static {
045: java.security.AccessController
046: .doPrivileged(new sun.security.action.LoadLibraryAction(
047: "javafile"));
048: }
049: /* File Descriptor - handle to the open file */
050: private FileDescriptor fd;
051:
052: /**
053: * Creates an input file stream to read from a file with the
054: * specified name.
055: *
056: * @param name the system-dependent file name.
057: * @exception SecurityException if a security manager exists, its
058: * <code>checkRead</code> method is called with the name
059: * argument to see if the application is allowed read access
060: * to the file.
061: * @see java.lang.SecurityManager#checkRead(java.lang.String)
062: * @since JDK1.0
063: */
064: public FDInputStream(String name) throws IOException {
065: SecurityManager security = System.getSecurityManager();
066: if (security != null) {
067: security.checkRead(name);
068: }
069: try {
070: fd = new FileDescriptor();
071: open(name);
072: } catch (IOException e) {
073: throw new IOException(name);
074: }
075: }
076:
077: /**
078: * Creates an input file stream to read from the specified file descriptor.
079: *
080: * @param fdObj the file descriptor to be opened for reading.
081: * @exception SecurityException if a security manager exists, its
082: * <code>checkRead</code> method is called with the file
083: * descriptor to see if the application is allowed to read
084: * from the specified file descriptor.
085: * @see java.lang.SecurityManager#checkRead(java.io.FileDescriptor)
086: * @since JDK1.0
087: */
088: public FDInputStream(FileDescriptor fdObj) {
089: SecurityManager security = System.getSecurityManager();
090: if (fdObj == null) {
091: throw new NullPointerException();
092: }
093: if (security != null) {
094: security.checkRead(fdObj);
095: }
096: fd = fdObj;
097: }
098:
099: /**
100: * Opens the specified file for reading.
101: * @param name the name of the file
102: */
103: public native void open(String name) throws IOException;
104:
105: /**
106: * Reads a byte of data from this input stream. This method blocks
107: * if no input is yet available.
108: *
109: * @return the next byte of data, or <code>-1</code> if the end of the
110: * file is reached.
111: * @exception IOException if an I/O error occurs.
112: * @since JDK1.0
113: */
114: public native int read() throws IOException;
115:
116: /**
117: * Reads a subarray as a sequence of bytes.
118: * @param b the data to be written
119: * @param off the start offset in the data
120: * @param len the number of bytes that are written
121: * @exception IOException If an I/O error has occurred.
122: */
123: public native int readBytes(byte b[], int off, int len)
124: throws IOException;
125:
126: /**
127: * Reads up to <code>b.length</code> bytes of data from this input
128: * stream into an array of bytes. This method blocks until some input
129: * is available.
130: *
131: * @param b the buffer into which the data is read.
132: * @return the total number of bytes read into the buffer, or
133: * <code>-1</code> if there is no more data because the end of
134: * the file has been reached.
135: * @exception IOException if an I/O error occurs.
136: * @since JDK1.0
137: */
138: public int read(byte b[]) throws IOException {
139: return readBytes(b, 0, b.length);
140: }
141:
142: /**
143: * Reads up to <code>len</code> bytes of data from this input stream
144: * into an array of bytes. This method blocks until some input is
145: * available.
146: *
147: * @param b the buffer into which the data is read.
148: * @param off the start offset of the data.
149: * @param len the maximum number of bytes read.
150: * @return the total number of bytes read into the buffer, or
151: * <code>-1</code> if there is no more data because the end of
152: * the file has been reached.
153: * @exception IOException if an I/O error occurs.
154: * @since JDK1.0
155: */
156: public int read(byte b[], int off, int len) throws IOException {
157: return readBytes(b, off, len);
158: }
159:
160: /**
161: * Skips over and discards <code>n</code> bytes of data from the
162: * input stream. The <code>skip</code> method may, for a variety of
163: * reasons, end up skipping over some smaller number of bytes,
164: * possibly <code>0</code>. The actual number of bytes skipped is returned.
165: *
166: * @param n the number of bytes to be skipped.
167: * @return the actual number of bytes skipped.
168: * @exception IOException if an I/O error occurs.
169: * @since JDK1.0
170: */
171: public native long skip(long n) throws IOException;
172:
173: /**
174: * Returns the number of bytes that can be read from this file input
175: * stream without blocking.
176: *
177: * @return the number of bytes that can be read from this file input
178: * stream without blocking.
179: * @exception IOException if an I/O error occurs.
180: * @since JDK1.0
181: */
182: public native int available() throws IOException;
183:
184: /**
185: * Closes this file input stream and releases any system resources
186: * associated with the stream.
187: *
188: * @exception IOException if an I/O error occurs.
189: * @since JDK1.0
190: */
191: public native void close() throws IOException;
192:
193: /**
194: * Returns the opaque file descriptor object associated with this stream.
195: *
196: * @return the file descriptor object associated with this stream.
197: * @exception IOException if an I/O error occurs.
198: * @see java.io.FileDescriptor
199: * @since JDK1.0
200: */
201: public final FileDescriptor getFD() throws IOException {
202: if (fd != null)
203: return fd;
204: throw new IOException();
205: }
206:
207: /**
208: * Ensures that the <code>close</code> method of this file input stream is
209: * called when there are no more references to it.
210: *
211: * @exception IOException if an I/O error occurs.
212: * @see java.io.FileInputStream#close()
213: * @since JDK1.0
214: */
215: protected void finalize() throws IOException {
216: if (fd != null) {
217: if (fd != fd.in) {
218: close();
219: }
220: }
221: }
222: }
|