OSDN Git Service

f2d4d399218b89d49c75c86a24a6cbf656876627
[pf3gnuchains/gcc-fork.git] / libjava / java / net / SocketInputStream.java
1 /* SocketInputStream.java -- An InputStream for Sockets
2    Copyright (C) 1998, 2000, 2002 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10  
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.
15
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.
20
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.
25
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. */
37
38
39 package java.net;
40
41 import java.io.InputStream;
42 import java.io.IOException;
43
44 /**
45   * This class contains an implementation of <code>InputStream</code> for 
46   * sockets.  It in an internal only class used by <code>PlainSocketImpl</code>.
47   *
48   * @author Aaron M. Renn (arenn@urbanophile.com)
49   */
50 class SocketInputStream extends InputStream
51 {
52
53 /*************************************************************************/
54
55 /*
56  * Instance Variables
57  */
58
59 /**
60   * The PlainSocketImpl object this stream is associated with
61   */
62 private PlainSocketImpl impl;
63
64 /*************************************************************************/
65
66 /*
67  * Constructors
68  */
69
70 /**
71   * Builds an instance of this class from a PlainSocketImpl object
72   */
73 protected
74 SocketInputStream(PlainSocketImpl impl)
75 {
76   this.impl = impl;
77 }
78
79 /*************************************************************************/
80
81 /*
82  * Instance Methods
83  */
84
85 /**
86   * Returns the number of bytes available to be read before blocking
87   */
88 public int
89 available() throws IOException
90 {
91   return(impl.available());
92 }
93
94 /*************************************************************************/
95
96 /**
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.
100   */
101 public boolean
102 markSupported()
103 {
104   return(false);
105 }
106
107 /*************************************************************************/
108
109 /**
110   * Do nothing mark method since we don't support this functionality.  Again,
111   * overriding out of paranoia.
112   *
113   * @param readlimit In theory, the number of bytes we can read before the mark becomes invalid
114   */
115 public void
116 mark(int readlimit)
117 {
118 }
119
120 /*************************************************************************/
121
122 /**
123   * Since we don't support mark, this method always throws an exception
124   *
125   * @exception IOException Everytime since we don't support this functionality
126   */
127 public void
128 reset() throws IOException
129 {
130   throw new IOException("Socket InputStreams do not support mark/reset");
131 }
132
133 /*************************************************************************/
134
135 /**
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
139   */
140 public void
141 close() throws IOException
142 {
143   impl.close();
144
145
146 /*************************************************************************/
147
148 /**
149   * Reads the next byte of data and returns it as an int.  
150   *
151   * @return The byte read (as an int) or -1 if end of stream);
152   *
153   * @exception IOException If an error occurs.
154   */
155 public int
156 read() throws IOException
157 {
158   byte buf[] = new byte[1];
159
160   int bytes_read = read(buf, 0, buf.length);
161  
162   if (bytes_read != -1)
163     return(buf[0] & 0xFF);
164   else
165     return(-1);
166 }
167
168 /*************************************************************************/
169
170 /**
171   * Reads up to buf.length bytes of data into the caller supplied buffer.
172   *
173   * @return The actual number of bytes read or -1 if end of stream
174   *
175   * @exception IOException If an error occurs.
176   */
177 public int
178 read(byte[] buf) throws IOException
179 {
180   return(read(buf, 0, buf.length));
181 }
182
183 /*************************************************************************/
184
185 /**
186   * Reads up to len bytes of data into the caller supplied buffer starting
187   * at offset bytes from the start of the buffer
188   *
189   * @return The number of bytes actually read or -1 if end of stream
190   *
191   * @exception IOException If an error occurs.
192   */
193 public int
194 read(byte[] buf, int offset, int len) throws IOException
195 {
196   int bytes_read = impl.read(buf, offset, len);
197   if (bytes_read == 0)
198     return(-1);
199
200   return(bytes_read);
201 }
202
203 } // class SocketInputStream
204