fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / libjava / java / io / FileInputStream.java
blobc88f83db445cb7b1c798f2eb3b4f05a5a941d7ad
1 /* FileInputStream.java -- An input stream that reads from disk files.
2 Copyright (C) 1998, 2002, 2003 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 java.nio.channels.FileChannel;
42 import java.nio.channels.FileChannelImpl;
44 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
45 * "The Java Language Specification", ISBN 0-201-63451-1
46 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
47 * Status: Believed complete and correct.
50 /**
51 * This class is a stream that reads its bytes from a file.
53 * @author Aaron M. Renn <arenn@urbanophile.com>
54 * @author Warren Levy <warrenl@cygnus.com>
56 public class FileInputStream extends InputStream
58 /**
59 * This is the native file handle for the file this stream is reading from
61 private FileDescriptor fd;
63 private FileChannel ch; /* cached associated file-channel */
65 /**
66 * This method initializes a <code>FileInputStream</code> to read from the
67 * specified named file. A security check is first made to determine
68 * whether or not access to this file is allowed. This is done by
69 * calling the <code>checkRead()</code> method of the
70 * <code>SecurityManager</code>
71 * (if one exists) with the name of this file. An exception is thrown
72 * if reading is not allowed. If the file does not exist, an exception
73 * is also thrown.
75 * @param name The name of the file this stream should read from
77 * @exception SecurityException If read access to the file is not allowed
78 * @exception FileNotFoundException If the file does not exist.
80 public FileInputStream(String name) throws FileNotFoundException
82 this(new File(name));
85 /**
86 * This method initializes a <code>FileInputStream</code> to read from the
87 * specified <code>File</code> object. A security check is first
88 * made to determine
89 * whether or not access to this file is allowed. This is done by
90 * calling the <code>checkRead()</code> method of the
91 * <code>SecurityManager</code>
92 * (if one exists) with the name of this file. An exception is thrown
93 * if reading is not allowed. If the file does not exist, an exception
94 * is also thrown.
96 * @param file The <code>File</code> object this stream should read from
98 * @exception SecurityException If read access to the file is not allowed
99 * @exception FileNotFoundException If the file does not exist.
101 public FileInputStream(File file) throws FileNotFoundException
103 SecurityManager s = System.getSecurityManager();
104 if (s != null)
105 s.checkRead(file.getPath());
107 if (file.isDirectory())
108 throw new FileNotFoundException(file.getPath() + " is a directory");
110 fd = new FileDescriptor(file.getPath(), FileDescriptor.READ);
114 * This method initializes a <code>FileInputStream</code> to read from the
115 * specified <code>FileDescriptor</code> object. A security
116 * check is first made to
117 * determine whether or not access to this file is allowed. This is done by
118 * calling the <code>checkRead()</code> method of the
119 * <code>SecurityManager</code>
120 * (if one exists) with the specified <code>FileDescriptor</code>
121 * An exception is
122 * thrown if reading is not allowed.
124 * @param fd The <code>FileDescriptor</code> object this stream
125 * should read from
127 * @exception SecurityException If read access to the file is not allowed
129 public FileInputStream(FileDescriptor fdObj)
131 SecurityManager s = System.getSecurityManager();
132 if (s != null)
133 s.checkRead(fdObj);
135 fd = fdObj;
139 * This method returns the number of bytes that can be read from this
140 * stream before a read can block. A return of 0 indicates that blocking
141 * might (or might not) occur on the very next read attempt.
142 * <p>
143 * This method returns the number of unread bytes remaining in the file if
144 * the descriptor being read from is an actual file. If this method is
145 * reading from a ''special'' file such a the standard input, this method
146 * will return the appropriate value for the stream being read.
147 * <p>
148 * Be aware that reads on plain files that do not reside locally might
149 * possibly block even if this method says they should not. For example,
150 * a remote server might crash, preventing an NFS mounted file from being
151 * read.
153 * @return The number of bytes that can be read before blocking could occur
155 * @exception IOException If an error occurs
157 public int available() throws IOException
159 return fd.available();
163 * This method closes the stream. Any futher attempts to read from the
164 * stream will likely generate an IOException since the underlying file
165 * will be closed.
167 * @exception IOException If an error occurs.
169 public void close() throws IOException
171 if (fd.valid())
172 fd.close();
175 protected void finalize() throws IOException
177 // We don't actually need this, but we include it because it is
178 // mentioned in the JCL.
182 * This method returns a <code>FileDescriptor</code> object representing the
183 * underlying native file handle of the file this stream is reading
184 * from
186 * @return A <code>FileDescriptor</code> for this stream
188 * @exception IOException If an error occurs
190 public final FileDescriptor getFD() throws IOException
192 if (!fd.valid())
193 throw new IOException();
194 return fd;
198 * This method reads an unsigned byte from the input stream and returns it
199 * as an int in the range of 0-255. This method also will return -1 if
200 * the end of the stream has been reached.
201 * <p>
202 * This method will block until the byte can be read.
204 * @return The byte read or -1 if end of stream
206 * @exception IOException If an error occurs
208 public int read() throws IOException
210 return fd.read();
214 * This method reads bytes from a stream and stores them into a caller
215 * supplied buffer. This method attempts to completely fill the buffer,
216 * but can return before doing so. The actual number of bytes read is
217 * returned as an int. A -1 is returned to indicate the end of the stream.
218 * <p>
219 * This method will block until some data can be read.
220 * <p>
221 * This method operates by calling an overloaded read method like so:
222 * <code>read(buf, 0, buf.length)</code>
224 * @param buf The buffer into which the bytes read will be stored.
226 * @return The number of bytes read or -1 if end of stream.
228 * @exception IOException If an error occurs.
230 public int read(byte[] buf) throws IOException
232 return read(buf, 0, buf.length);
236 * This method read bytes from a stream and stores them into a caller
237 * supplied buffer. It starts storing the data at index
238 * <code>offset</code> into
239 * the buffer and attempts to read <code>len</code> bytes. This method can
240 * return before reading the number of bytes requested. The actual number
241 * of bytes read is returned as an int. A -1 is returned to indicate the
242 * end of the stream.
243 * <p>
244 * This method will block until some data can be read.
246 * @param buf The array into which the bytes read should be stored
247 * @param offset The offset into the array to start storing bytes
248 * @param len The requested number of bytes to read
250 * @return The actual number of bytes read, or -1 if end of stream.
252 * @exception IOException If an error occurs.
254 public int read(byte[] buf, int offset, int len) throws IOException
256 if (offset < 0
257 || len < 0
258 || offset + len > buf.length)
259 throw new ArrayIndexOutOfBoundsException();
261 return fd.read(buf, offset, len);
265 * This method skips the specified number of bytes in the stream. It
266 * returns the actual number of bytes skipped, which may be less than the
267 * requested amount.
268 * <p>
269 * @param numBytes The requested number of bytes to skip
271 * @return The actual number of bytes skipped.
273 * @exception IOException If an error occurs
275 public synchronized long skip (long numBytes) throws IOException
277 if (numBytes < 0)
278 throw new IllegalArgumentException ("Can't skip negative bytes: " +
279 numBytes);
281 if (numBytes == 0)
282 return 0;
284 long curPos = fd.getFilePointer ();
285 long newPos = fd.seek (numBytes, FileDescriptor.CUR, true);
286 return newPos - curPos;
290 * This method creates a java.nio.channels.FileChannel.
291 * Nio does not allow one to create a file channel directly.
292 * A file channel must be created by first creating an instance of
293 * Input/Output/RandomAccessFile and invoking the getChannel() method on it.
295 public synchronized FileChannel getChannel ()
297 if (ch == null)
298 ch = new FileChannelImpl (fd, false, this);
300 return ch;
303 } // class FileInputStream