1 /* SocketInputStream.java -- An InputStream for Sockets
2 Copyright (C) 1998, 2000, 2002 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)
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
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
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. */
41 import java.io.InputStream;
42 import java.io.IOException;
45 * This class contains an implementation of <code>InputStream</code> for
46 * sockets. It in an internal only class used by <code>PlainSocketImpl</code>.
48 * @author Aaron M. Renn (arenn@urbanophile.com)
50 class SocketInputStream extends InputStream
53 /*************************************************************************/
60 * The PlainSocketImpl object this stream is associated with
62 private PlainSocketImpl impl;
64 /*************************************************************************/
71 * Builds an instance of this class from a PlainSocketImpl object
74 SocketInputStream(PlainSocketImpl impl)
79 /*************************************************************************/
86 * Returns the number of bytes available to be read before blocking
89 available() throws IOException
91 return(impl.available());
94 /*************************************************************************/
97 * Determines if "mark" functionality is supported on this stream. For
98 * sockets, this is always false. Note that the superclass default is
99 * false, but it is overridden out of safety concerns and/or paranoia.
107 /*************************************************************************/
110 * Do nothing mark method since we don't support this functionality. Again,
111 * overriding out of paranoia.
113 * @param readlimit In theory, the number of bytes we can read before the mark becomes invalid
120 /*************************************************************************/
123 * Since we don't support mark, this method always throws an exception
125 * @exception IOException Everytime since we don't support this functionality
128 reset() throws IOException
130 throw new IOException("Socket InputStreams do not support mark/reset");
133 /*************************************************************************/
136 * This method not only closes the stream, it closes the underlying socket
137 * (and thus any connection) and invalidates any other Input/Output streams
138 * for the underlying impl object
141 close() throws IOException
146 /*************************************************************************/
149 * Reads the next byte of data and returns it as an int.
151 * @return The byte read (as an int) or -1 if end of stream);
153 * @exception IOException If an error occurs.
156 read() throws IOException
158 byte buf[] = new byte[1];
160 int bytes_read = read(buf, 0, buf.length);
162 if (bytes_read != -1)
163 return(buf[0] & 0xFF);
168 /*************************************************************************/
171 * Reads up to buf.length bytes of data into the caller supplied buffer.
173 * @return The actual number of bytes read or -1 if end of stream
175 * @exception IOException If an error occurs.
178 read(byte[] buf) throws IOException
180 return(read(buf, 0, buf.length));
183 /*************************************************************************/
186 * Reads up to len bytes of data into the caller supplied buffer starting
187 * at offset bytes from the start of the buffer
189 * @return The number of bytes actually read or -1 if end of stream
191 * @exception IOException If an error occurs.
194 read(byte[] buf, int offset, int len) throws IOException
196 int bytes_read = impl.read(buf, offset, len);
203 } // class SocketInputStream