KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > net > SocketInputStream


1 /*
2  * @(#)SocketInputStream.java 1.35 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.net;
9
10 import java.io.FileDescriptor JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.nio.channels.FileChannel JavaDoc;
14
15 import sun.net.ConnectionResetException;
16
17 /**
18  * This stream extends FileInputStream to implement a
19  * SocketInputStream. Note that this class should <b>NOT</b> be
20  * public.
21  *
22  * @version 1.35, 11/17/05
23  * @author Jonathan Payne
24  * @author Arthur van Hoff
25  */

26 class SocketInputStream extends FileInputStream JavaDoc
27 {
28     static {
29         init();
30     }
31     
32     private boolean eof;
33     private PlainSocketImpl JavaDoc impl = null;
34     private byte temp[];
35     private Socket JavaDoc socket = null;
36
37     /**
38      * Creates a new SocketInputStream. Can only be called
39      * by a Socket. This method needs to hang on to the owner Socket so
40      * that the fd will not be closed.
41      * @param impl the implemented socket input stream
42      */

43     SocketInputStream(PlainSocketImpl JavaDoc impl) throws IOException JavaDoc {
44     super(impl.getFileDescriptor());
45     this.impl = impl;
46     socket = impl.getSocket();
47     }
48
49     /**
50      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
51      * object associated with this file input stream.</p>
52      *
53      * The <code>getChannel</code> method of <code>SocketInputStream</code>
54      * returns <code>null</code> since it is a socket based stream.</p>
55      *
56      * @return the file channel associated with this file input stream
57      *
58      * @since 1.4
59      * @spec JSR-51
60      */

61     public final FileChannel JavaDoc getChannel() {
62     return null;
63     }
64
65     /**
66      * Reads into an array of bytes at the specified offset using
67      * the received socket primitive.
68      * @param fd the FileDescriptor
69      * @param b the buffer into which the data is read
70      * @param off the start offset of the data
71      * @param len the maximum number of bytes read
72      * @param timeout the read timeout in ms
73      * @return the actual number of bytes read, -1 is
74      * returned when the end of the stream is reached.
75      * @exception IOException If an I/O error has occurred.
76      */

77     private native int socketRead0(FileDescriptor JavaDoc fd,
78                        byte b[], int off, int len,
79                    int timeout)
80     throws IOException JavaDoc;
81
82     /**
83      * Reads into a byte array data from the socket.
84      * @param b the buffer into which the data is read
85      * @return the actual number of bytes read, -1 is
86      * returned when the end of the stream is reached.
87      * @exception IOException If an I/O error has occurred.
88      */

89     public int read(byte b[]) throws IOException JavaDoc {
90     return read(b, 0, b.length);
91     }
92
93     /**
94      * Reads into a byte array <i>b</i> at offset <i>off</i>,
95      * <i>length</i> bytes of data.
96      * @param b the buffer into which the data is read
97      * @param off the start offset of the data
98      * @param len the maximum number of bytes read
99      * @return the actual number of bytes read, -1 is
100      * returned when the end of the stream is reached.
101      * @exception IOException If an I/O error has occurred.
102      */

103     public int read(byte b[], int off, int length) throws IOException JavaDoc {
104     int n;
105
106     // EOF already encountered
107
if (eof) {
108         return -1;
109     }
110
111     // connection reset
112
if (impl.isConnectionReset()) {
113         throw new SocketException JavaDoc("Connection reset");
114     }
115
116     // bounds check
117
if (length <= 0 || off < 0 || off + length > b.length) {
118         if (length == 0) {
119         return 0;
120         }
121         throw new ArrayIndexOutOfBoundsException JavaDoc();
122     }
123
124     boolean gotReset = false;
125
126     // acquire file descriptor and do the read
127
FileDescriptor JavaDoc fd = impl.acquireFD();
128     try {
129         n = socketRead0(fd, b, off, length, impl.getTimeout());
130         if (n > 0) {
131         return n;
132         }
133     } catch (ConnectionResetException rstExc) {
134         gotReset = true;
135     } finally {
136         impl.releaseFD();
137     }
138
139     /*
140      * We receive a "connection reset" but there may be bytes still
141      * buffered on the socket
142      */

143     if (gotReset) {
144         impl.setConnectionResetPending();
145         impl.acquireFD();
146         try {
147             n = socketRead0(fd, b, off, length, impl.getTimeout());
148         if (n > 0) {
149             return n;
150         }
151         } catch (ConnectionResetException rstExc) {
152         } finally {
153         impl.releaseFD();
154         }
155     }
156
157     /*
158      * If we get here we are at EOF, the socket has been closed,
159      * or the connection has been reset.
160      */

161         if (impl.isClosedOrPending()) {
162             throw new SocketException JavaDoc("Socket closed");
163         }
164     if (impl.isConnectionResetPending()) {
165         impl.setConnectionReset();
166     }
167     if (impl.isConnectionReset()) {
168         throw new SocketException JavaDoc("Connection reset");
169     }
170     eof = true;
171     return -1;
172     }
173
174     /**
175      * Reads a single byte from the socket.
176      */

177     public int read() throws IOException JavaDoc {
178     if (eof) {
179         return -1;
180     }
181     temp = new byte[1];
182     int n = read(temp, 0, 1);
183     if (n <= 0) {
184         return -1;
185     }
186     return temp[0] & 0xff;
187     }
188
189     /**
190      * Skips n bytes of input.
191      * @param n the number of bytes to skip
192      * @return the actual number of bytes skipped.
193      * @exception IOException If an I/O error has occurred.
194      */

195     public long skip(long numbytes) throws IOException JavaDoc {
196     if (numbytes <= 0) {
197         return 0;
198     }
199     long n = numbytes;
200     int buflen = (int) Math.min(1024, n);
201     byte data[] = new byte[buflen];
202     while (n > 0) {
203         int r = read(data, 0, (int) Math.min((long) buflen, n));
204         if (r < 0) {
205         break;
206         }
207         n -= r;
208     }
209     return numbytes - n;
210     }
211
212     /**
213      * Returns the number of bytes that can be read without blocking.
214      * @return the number of immediately available bytes
215      */

216     public int available() throws IOException JavaDoc {
217     return impl.available();
218     }
219
220     /**
221      * Closes the stream.
222      */

223     private boolean closing = false;
224     public void close() throws IOException JavaDoc {
225     // Prevent recursion. See BugId 4484411
226
if (closing)
227         return;
228     closing = true;
229     if (socket != null) {
230         if (!socket.isClosed())
231         socket.close();
232     } else
233         impl.close();
234     closing = false;
235     }
236
237     void setEOF(boolean eof) {
238     this.eof = eof;
239     }
240
241     /**
242      * Overrides finalize, the fd is closed by the Socket.
243      */

244     protected void finalize() {}
245
246     /**
247      * Perform class load-time initializations.
248      */

249     private native static void init();
250 }
251
252
Popular Tags