Merge from the pain train
[official-gcc.git] / libjava / java / io / FileInputStream.java
blob17aaf799be06fe7a21f6090c7a55f1cbadeaf0ec
1 /* FileInputStream.java -- An input stream that reads from disk files.
2 Copyright (C) 1998, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.io;
41 import gnu.java.nio.channels.FileChannelImpl;
43 import java.nio.channels.FileChannel;
45 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
46 * "The Java Language Specification", ISBN 0-201-63451-1
47 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
48 * Status: Believed complete and correct.
51 /**
52 * This class is a stream that reads its bytes from a file.
54 * @author Aaron M. Renn (arenn@urbanophile.com)
55 * @author Warren Levy (warrenl@cygnus.com)
57 public class FileInputStream extends InputStream
59 /**
60 * This is the native file handle for the file this stream is reading from
62 private FileDescriptor fd;
64 private FileChannelImpl ch;
66 /**
67 * This method initializes a <code>FileInputStream</code> to read from the
68 * specified named file. A security check is first made to determine
69 * whether or not access to this file is allowed. This is done by
70 * calling the <code>checkRead()</code> method of the
71 * <code>SecurityManager</code>
72 * (if one exists) with the name of this file. An exception is thrown
73 * if reading is not allowed. If the file does not exist, an exception
74 * is also thrown.
76 * @param name The name of the file this stream should read from
78 * @exception SecurityException If read access to the file is not allowed
79 * @exception FileNotFoundException If the file does not exist.
81 public FileInputStream(String name) throws FileNotFoundException
83 this(new File(name));
86 /**
87 * This method initializes a <code>FileInputStream</code> to read from the
88 * specified <code>File</code> object. A security check is first
89 * made to determine
90 * whether or not access to this file is allowed. This is done by
91 * calling the <code>checkRead()</code> method of the
92 * <code>SecurityManager</code>
93 * (if one exists) with the name of this file. An exception is thrown
94 * if reading is not allowed. If the file does not exist, an exception
95 * is also thrown.
97 * @param file The <code>File</code> object this stream should read from
99 * @exception SecurityException If read access to the file is not allowed
100 * @exception FileNotFoundException If the file does not exist.
102 public FileInputStream(File file) throws FileNotFoundException
104 SecurityManager s = System.getSecurityManager();
105 if (s != null)
106 s.checkRead(file.getPath());
108 if (file.isDirectory())
109 throw new FileNotFoundException(file.getPath() + " is a directory");
111 ch = new FileChannelImpl (file.getPath(), FileChannelImpl.READ);
115 * This method initializes a <code>FileInputStream</code> to read from the
116 * specified <code>FileDescriptor</code> object. A security
117 * check is first made to
118 * determine whether or not access to this file is allowed. This is done by
119 * calling the <code>checkRead()</code> method of the
120 * <code>SecurityManager</code>
121 * (if one exists) with the specified <code>FileDescriptor</code>
122 * An exception is
123 * thrown if reading is not allowed.
125 * @param fdObj The <code>FileDescriptor</code> object this stream
126 * should read from
128 * @exception SecurityException If read access to the file is not allowed
130 public FileInputStream(FileDescriptor fdObj)
132 SecurityManager s = System.getSecurityManager();
133 if (s != null)
134 s.checkRead(fdObj);
136 fd = fdObj;
137 ch = (FileChannelImpl) fdObj.channel;
140 FileInputStream(FileChannelImpl ch)
142 this.ch = ch;
146 * This method returns the number of bytes that can be read from this
147 * stream before a read can block. A return of 0 indicates that blocking
148 * might (or might not) occur on the very next read attempt.
149 * <p>
150 * This method returns the number of unread bytes remaining in the file if
151 * the descriptor being read from is an actual file. If this method is
152 * reading from a ''special'' file such a the standard input, this method
153 * will return the appropriate value for the stream being read.
154 * <p>
155 * Be aware that reads on plain files that do not reside locally might
156 * possibly block even if this method says they should not. For example,
157 * a remote server might crash, preventing an NFS mounted file from being
158 * read.
160 * @return The number of bytes that can be read before blocking could occur
162 * @exception IOException If an error occurs
164 public int available() throws IOException
166 return ch.available();
170 * This method closes the stream. Any futher attempts to read from the
171 * stream will likely generate an IOException since the underlying file
172 * will be closed.
174 * @exception IOException If an error occurs.
176 public void close() throws IOException
178 ch.close();
181 protected void finalize() throws IOException
183 // We don't actually need this, but we include it because it is
184 // mentioned in the JCL.
188 * This method returns a <code>FileDescriptor</code> object representing the
189 * underlying native file handle of the file this stream is reading
190 * from
192 * @return A <code>FileDescriptor</code> for this stream
194 * @exception IOException If an error occurs
196 public final FileDescriptor getFD() throws IOException
198 synchronized (this)
200 if (fd == null)
201 fd = new FileDescriptor (ch);
202 return fd;
207 * This method reads an unsigned byte from the input stream and returns it
208 * as an int in the range of 0-255. This method also will return -1 if
209 * the end of the stream has been reached.
210 * <p>
211 * This method will block until the byte can be read.
213 * @return The byte read or -1 if end of stream
215 * @exception IOException If an error occurs
217 public int read() throws IOException
219 return ch.read();
223 * This method reads bytes from a stream and stores them into a caller
224 * supplied buffer. This method attempts to completely fill the buffer,
225 * but can return before doing so. The actual number of bytes read is
226 * returned as an int. A -1 is returned to indicate the end of the stream.
227 * <p>
228 * This method will block until some data can be read.
229 * <p>
230 * This method operates by calling an overloaded read method like so:
231 * <code>read(buf, 0, buf.length)</code>
233 * @param buf The buffer into which the bytes read will be stored.
235 * @return The number of bytes read or -1 if end of stream.
237 * @exception IOException If an error occurs.
239 public int read(byte[] buf) throws IOException
241 return read(buf, 0, buf.length);
245 * This method read bytes from a stream and stores them into a caller
246 * supplied buffer. It starts storing the data at index
247 * <code>offset</code> into
248 * the buffer and attempts to read <code>len</code> bytes. This method can
249 * return before reading the number of bytes requested. The actual number
250 * of bytes read is returned as an int. A -1 is returned to indicate the
251 * end of the stream.
252 * <p>
253 * This method will block until some data can be read.
255 * @param buf The array into which the bytes read should be stored
256 * @param offset The offset into the array to start storing bytes
257 * @param len The requested number of bytes to read
259 * @return The actual number of bytes read, or -1 if end of stream.
261 * @exception IOException If an error occurs.
263 public int read(byte[] buf, int offset, int len) throws IOException
265 if (offset < 0
266 || len < 0
267 || offset + len > buf.length)
268 throw new ArrayIndexOutOfBoundsException();
270 return ch.read(buf, offset, len);
274 * This method skips the specified number of bytes in the stream. It
275 * returns the actual number of bytes skipped, which may be less than the
276 * requested amount.
277 * <p>
278 * @param numBytes The requested number of bytes to skip
280 * @return The actual number of bytes skipped.
282 * @exception IOException If an error occurs
284 public synchronized long skip (long numBytes) throws IOException
286 if (numBytes < 0)
287 throw new IllegalArgumentException ("Can't skip negative bytes: " +
288 numBytes);
290 if (numBytes == 0)
291 return 0;
293 long oldPos = ch.position ();
294 ch.position(oldPos + numBytes);
295 return ch.position() - oldPos;
299 * This method creates a java.nio.channels.FileChannel.
300 * Nio does not allow one to create a file channel directly.
301 * A file channel must be created by first creating an instance of
302 * Input/Output/RandomAccessFile and invoking the getChannel() method on it.
304 public synchronized FileChannel getChannel ()
306 return ch;
309 } // class FileInputStream