13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.net;
27
28 import java.io.FileDescriptor;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.nio.channels.FileChannel;
32
33 import sun.net.ConnectionResetException;
34
35 /**
36 * This stream extends FileInputStream to implement a
37 * SocketInputStream. Note that this class should <b>NOT</b> be
38 * public.
39 *
40 * @author Jonathan Payne
41 * @author Arthur van Hoff
42 */
43 class SocketInputStream extends FileInputStream
44 {
45 static {
46 init();
47 }
48
49 private boolean eof;
50 private AbstractPlainSocketImpl impl = null;
51 private byte temp[];
52 private Socket socket = null;
105 */
106 public int read(byte b[]) throws IOException {
107 return read(b, 0, b.length);
108 }
109
110 /**
111 * Reads into a byte array <i>b</i> at offset <i>off</i>,
112 * <i>length</i> bytes of data.
113 * @param b the buffer into which the data is read
114 * @param off the start offset of the data
115 * @param length the maximum number of bytes read
116 * @return the actual number of bytes read, -1 is
117 * returned when the end of the stream is reached.
118 * @exception IOException If an I/O error has occurred.
119 */
120 public int read(byte b[], int off, int length) throws IOException {
121 return read(b, off, length, impl.getTimeout());
122 }
123
124 int read(byte b[], int off, int length, int timeout) throws IOException {
125 int n;
126
127 // EOF already encountered
128 if (eof) {
129 return -1;
130 }
131
132 // connection reset
133 if (impl.isConnectionReset()) {
134 throw new SocketException("Connection reset");
135 }
136
137 // bounds check
138 if (length <= 0 || off < 0 || off + length > b.length) {
139 if (length == 0) {
140 return 0;
141 }
142 throw new ArrayIndexOutOfBoundsException();
143 }
144
145 boolean gotReset = false;
146
147 // acquire file descriptor and do the read
148 FileDescriptor fd = impl.acquireFD();
149 try {
150 n = socketRead0(fd, b, off, length, timeout);
151 if (n > 0) {
152 return n;
153 }
154 } catch (ConnectionResetException rstExc) {
155 gotReset = true;
156 } finally {
157 impl.releaseFD();
158 }
159
160 /*
161 * We receive a "connection reset" but there may be bytes still
162 * buffered on the socket
163 */
164 if (gotReset) {
165 impl.setConnectionResetPending();
166 impl.acquireFD();
167 try {
168 n = socketRead0(fd, b, off, length, timeout);
169 if (n > 0) {
170 return n;
171 }
172 } catch (ConnectionResetException rstExc) {
173 } finally {
174 impl.releaseFD();
175 }
176 }
177
178 /*
179 * If we get here we are at EOF, the socket has been closed,
180 * or the connection has been reset.
181 */
182 if (impl.isClosedOrPending()) {
183 throw new SocketException("Socket closed");
184 }
185 if (impl.isConnectionResetPending()) {
186 impl.setConnectionReset();
187 }
188 if (impl.isConnectionReset()) {
189 throw new SocketException("Connection reset");
190 }
191 eof = true;
192 return -1;
193 }
194
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.net;
27
28 import java.io.FileDescriptor;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.nio.channels.FileChannel;
32
33 import sun.misc.IoTrace;
34 import sun.misc.IoTraceContext;
35 import sun.net.ConnectionResetException;
36
37 /**
38 * This stream extends FileInputStream to implement a
39 * SocketInputStream. Note that this class should <b>NOT</b> be
40 * public.
41 *
42 * @author Jonathan Payne
43 * @author Arthur van Hoff
44 */
45 class SocketInputStream extends FileInputStream
46 {
47 static {
48 init();
49 }
50
51 private boolean eof;
52 private AbstractPlainSocketImpl impl = null;
53 private byte temp[];
54 private Socket socket = null;
107 */
108 public int read(byte b[]) throws IOException {
109 return read(b, 0, b.length);
110 }
111
112 /**
113 * Reads into a byte array <i>b</i> at offset <i>off</i>,
114 * <i>length</i> bytes of data.
115 * @param b the buffer into which the data is read
116 * @param off the start offset of the data
117 * @param length the maximum number of bytes read
118 * @return the actual number of bytes read, -1 is
119 * returned when the end of the stream is reached.
120 * @exception IOException If an I/O error has occurred.
121 */
122 public int read(byte b[], int off, int length) throws IOException {
123 return read(b, off, length, impl.getTimeout());
124 }
125
126 int read(byte b[], int off, int length, int timeout) throws IOException {
127 int n = 0;
128
129 // EOF already encountered
130 if (eof) {
131 return -1;
132 }
133
134 // connection reset
135 if (impl.isConnectionReset()) {
136 throw new SocketException("Connection reset");
137 }
138
139 // bounds check
140 if (length <= 0 || off < 0 || off + length > b.length) {
141 if (length == 0) {
142 return 0;
143 }
144 throw new ArrayIndexOutOfBoundsException();
145 }
146
147 boolean gotReset = false;
148
149 IoTraceContext traceContext = IoTrace.socketReadBegin(impl.address, impl.port, timeout);
150 // acquire file descriptor and do the read
151 FileDescriptor fd = impl.acquireFD();
152 try {
153 n = socketRead0(fd, b, off, length, timeout);
154 if (n > 0) {
155 return n;
156 }
157 } catch (ConnectionResetException rstExc) {
158 gotReset = true;
159 } finally {
160 impl.releaseFD();
161 IoTrace.socketReadEnd(traceContext, n > 0 ? n : 0);
162 }
163
164 /*
165 * We receive a "connection reset" but there may be bytes still
166 * buffered on the socket
167 */
168 if (gotReset) {
169 traceContext = IoTrace.socketReadBegin(impl.address, impl.port, timeout);
170 impl.setConnectionResetPending();
171 impl.acquireFD();
172 try {
173 n = socketRead0(fd, b, off, length, timeout);
174 if (n > 0) {
175 return n;
176 }
177 } catch (ConnectionResetException rstExc) {
178 } finally {
179 impl.releaseFD();
180 IoTrace.socketReadEnd(traceContext, n > 0 ? n : 0);
181 }
182 }
183
184 /*
185 * If we get here we are at EOF, the socket has been closed,
186 * or the connection has been reset.
187 */
188 if (impl.isClosedOrPending()) {
189 throw new SocketException("Socket closed");
190 }
191 if (impl.isConnectionResetPending()) {
192 impl.setConnectionReset();
193 }
194 if (impl.isConnectionReset()) {
195 throw new SocketException("Connection reset");
196 }
197 eof = true;
198 return -1;
199 }
200
|