69 *
70 * @return the file channel associated with this file input stream
71 *
72 * @since 1.4
73 * @spec JSR-51
74 */
75 public final FileChannel getChannel() {
76 return null;
77 }
78
79 /**
80 * Reads into an array of bytes at the specified offset using
81 * the received socket primitive.
82 * @param fd the FileDescriptor
83 * @param b the buffer into which the data is read
84 * @param off the start offset of the data
85 * @param len the maximum number of bytes read
86 * @param timeout the read timeout in ms
87 * @return the actual number of bytes read, -1 is
88 * returned when the end of the stream is reached.
89 * @exception IOException If an I/O error has occurred.
90 */
91 private native int socketRead0(FileDescriptor fd,
92 byte b[], int off, int len,
93 int timeout)
94 throws IOException;
95
96 // wrap native call to allow instrumentation
97 /**
98 * Reads into an array of bytes at the specified offset using
99 * the received socket primitive.
100 * @param fd the FileDescriptor
101 * @param b the buffer into which the data is read
102 * @param off the start offset of the data
103 * @param len the maximum number of bytes read
104 * @param timeout the read timeout in ms
105 * @return the actual number of bytes read, -1 is
106 * returned when the end of the stream is reached.
107 * @exception IOException If an I/O error has occurred.
108 */
109 private int socketRead(FileDescriptor fd,
110 byte b[], int off, int len,
111 int timeout)
112 throws IOException {
113 return socketRead0(fd, b, off, len, timeout);
114 }
115
116 /**
117 * Reads into a byte array data from the socket.
118 * @param b the buffer into which the data is read
119 * @return the actual number of bytes read, -1 is
120 * returned when the end of the stream is reached.
121 * @exception IOException If an I/O error has occurred.
122 */
123 public int read(byte b[]) throws IOException {
124 return read(b, 0, b.length);
125 }
126
127 /**
128 * Reads into a byte array <i>b</i> at offset <i>off</i>,
129 * <i>length</i> bytes of data.
130 * @param b the buffer into which the data is read
131 * @param off the start offset of the data
132 * @param length the maximum number of bytes read
133 * @return the actual number of bytes read, -1 is
134 * returned when the end of the stream is reached.
135 * @exception IOException If an I/O error has occurred.
136 */
137 public int read(byte b[], int off, int length) throws IOException {
138 return read(b, off, length, impl.getTimeout());
139 }
140
141 int read(byte b[], int off, int length, int timeout) throws IOException {
142 int n;
143
144 // EOF already encountered
145 if (eof) {
146 return -1;
147 }
148
149 // connection reset
150 if (impl.isConnectionReset()) {
151 throw new SocketException("Connection reset");
152 }
153
154 // bounds check
155 if (length <= 0 || off < 0 || length > b.length - off) {
189
190 /**
191 * Reads a single byte from the socket.
192 */
193 public int read() throws IOException {
194 if (eof) {
195 return -1;
196 }
197 temp = new byte[1];
198 int n = read(temp, 0, 1);
199 if (n <= 0) {
200 return -1;
201 }
202 return temp[0] & 0xff;
203 }
204
205 /**
206 * Skips n bytes of input.
207 * @param numbytes the number of bytes to skip
208 * @return the actual number of bytes skipped.
209 * @exception IOException If an I/O error has occurred.
210 */
211 public long skip(long numbytes) throws IOException {
212 if (numbytes <= 0) {
213 return 0;
214 }
215 long n = numbytes;
216 int buflen = (int) Math.min(1024, n);
217 byte data[] = new byte[buflen];
218 while (n > 0) {
219 int r = read(data, 0, (int) Math.min((long) buflen, n));
220 if (r < 0) {
221 break;
222 }
223 n -= r;
224 }
225 return numbytes - n;
226 }
227
228 /**
229 * Returns the number of bytes that can be read without blocking.
|
69 *
70 * @return the file channel associated with this file input stream
71 *
72 * @since 1.4
73 * @spec JSR-51
74 */
75 public final FileChannel getChannel() {
76 return null;
77 }
78
79 /**
80 * Reads into an array of bytes at the specified offset using
81 * the received socket primitive.
82 * @param fd the FileDescriptor
83 * @param b the buffer into which the data is read
84 * @param off the start offset of the data
85 * @param len the maximum number of bytes read
86 * @param timeout the read timeout in ms
87 * @return the actual number of bytes read, -1 is
88 * returned when the end of the stream is reached.
89 * @throws IOException If an I/O error has occurred.
90 */
91 private native int socketRead0(FileDescriptor fd,
92 byte b[], int off, int len,
93 int timeout)
94 throws IOException;
95
96 // wrap native call to allow instrumentation
97 /**
98 * Reads into an array of bytes at the specified offset using
99 * the received socket primitive.
100 * @param fd the FileDescriptor
101 * @param b the buffer into which the data is read
102 * @param off the start offset of the data
103 * @param len the maximum number of bytes read
104 * @param timeout the read timeout in ms
105 * @return the actual number of bytes read, -1 is
106 * returned when the end of the stream is reached.
107 * @throws IOException If an I/O error has occurred.
108 */
109 private int socketRead(FileDescriptor fd,
110 byte b[], int off, int len,
111 int timeout)
112 throws IOException {
113 return socketRead0(fd, b, off, len, timeout);
114 }
115
116 /**
117 * Reads into a byte array data from the socket.
118 * @param b the buffer into which the data is read
119 * @return the actual number of bytes read, -1 is
120 * returned when the end of the stream is reached.
121 * @throws IOException If an I/O error has occurred.
122 */
123 public int read(byte b[]) throws IOException {
124 return read(b, 0, b.length);
125 }
126
127 /**
128 * Reads into a byte array <i>b</i> at offset <i>off</i>,
129 * <i>length</i> bytes of data.
130 * @param b the buffer into which the data is read
131 * @param off the start offset of the data
132 * @param length the maximum number of bytes read
133 * @return the actual number of bytes read, -1 is
134 * returned when the end of the stream is reached.
135 * @throws IOException If an I/O error has occurred.
136 */
137 public int read(byte b[], int off, int length) throws IOException {
138 return read(b, off, length, impl.getTimeout());
139 }
140
141 int read(byte b[], int off, int length, int timeout) throws IOException {
142 int n;
143
144 // EOF already encountered
145 if (eof) {
146 return -1;
147 }
148
149 // connection reset
150 if (impl.isConnectionReset()) {
151 throw new SocketException("Connection reset");
152 }
153
154 // bounds check
155 if (length <= 0 || off < 0 || length > b.length - off) {
189
190 /**
191 * Reads a single byte from the socket.
192 */
193 public int read() throws IOException {
194 if (eof) {
195 return -1;
196 }
197 temp = new byte[1];
198 int n = read(temp, 0, 1);
199 if (n <= 0) {
200 return -1;
201 }
202 return temp[0] & 0xff;
203 }
204
205 /**
206 * Skips n bytes of input.
207 * @param numbytes the number of bytes to skip
208 * @return the actual number of bytes skipped.
209 * @throws IOException If an I/O error has occurred.
210 */
211 public long skip(long numbytes) throws IOException {
212 if (numbytes <= 0) {
213 return 0;
214 }
215 long n = numbytes;
216 int buflen = (int) Math.min(1024, n);
217 byte data[] = new byte[buflen];
218 while (n > 0) {
219 int r = read(data, 0, (int) Math.min((long) buflen, n));
220 if (r < 0) {
221 break;
222 }
223 n -= r;
224 }
225 return numbytes - n;
226 }
227
228 /**
229 * Returns the number of bytes that can be read without blocking.
|