1 |
/* PlainSocketImpl.java -- Default socket implementation |
/* PlainSocketImpl.java -- Default socket implementation |
2 |
Copyright (C) 1998, 1999, 2003 Free Software Foundation, Inc. |
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 |
3 |
|
Free Software Foundation, Inc. |
4 |
|
|
5 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
6 |
|
|
321 |
|
|
322 |
return out; |
return out; |
323 |
} |
} |
324 |
} |
|
325 |
|
/** |
326 |
|
* This class contains an implementation of <code>InputStream</code> for |
327 |
|
* sockets. It in an internal only class used by <code>PlainSocketImpl</code>. |
328 |
|
*/ |
329 |
|
class SocketInputStream extends InputStream |
330 |
|
{ |
331 |
|
/** |
332 |
|
* The PlainSocketImpl object this stream is associated with |
333 |
|
*/ |
334 |
|
private PlainSocketImpl impl; |
335 |
|
|
336 |
|
/** |
337 |
|
* Builds an instance of this class from a PlainSocketImpl object |
338 |
|
*/ |
339 |
|
protected SocketInputStream (PlainSocketImpl impl) |
340 |
|
{ |
341 |
|
this.impl = impl; |
342 |
|
} |
343 |
|
|
344 |
|
/** |
345 |
|
* Returns the number of bytes available to be read before blocking |
346 |
|
*/ |
347 |
|
public int available() throws IOException |
348 |
|
{ |
349 |
|
return impl.available(); |
350 |
|
} |
351 |
|
|
352 |
|
/** |
353 |
|
* Determines if "mark" functionality is supported on this stream. For |
354 |
|
* sockets, this is always false. Note that the superclass default is |
355 |
|
* false, but it is overridden out of safety concerns and/or paranoia. |
356 |
|
*/ |
357 |
|
public boolean markSupported() |
358 |
|
{ |
359 |
|
return false; |
360 |
|
} |
361 |
|
|
362 |
|
/** |
363 |
|
* Do nothing mark method since we don't support this functionality. Again, |
364 |
|
* overriding out of paranoia. |
365 |
|
* |
366 |
|
* @param readlimit In theory, the number of bytes we can read before the mark becomes invalid |
367 |
|
*/ |
368 |
|
public void mark (int readlimit) |
369 |
|
{ |
370 |
|
} |
371 |
|
|
372 |
|
/** |
373 |
|
* Since we don't support mark, this method always throws an exception |
374 |
|
* |
375 |
|
* @exception IOException Everytime since we don't support this functionality |
376 |
|
*/ |
377 |
|
public void reset() throws IOException |
378 |
|
{ |
379 |
|
throw new IOException ("Socket InputStreams do not support mark/reset"); |
380 |
|
} |
381 |
|
|
382 |
|
/** |
383 |
|
* This method not only closes the stream, it closes the underlying socket |
384 |
|
* (and thus any connection) and invalidates any other Input/Output streams |
385 |
|
* for the underlying impl object |
386 |
|
*/ |
387 |
|
public void close() throws IOException |
388 |
|
{ |
389 |
|
impl.close(); |
390 |
|
} |
391 |
|
|
392 |
|
/** |
393 |
|
* Reads the next byte of data and returns it as an int. |
394 |
|
* |
395 |
|
* @return The byte read (as an int) or -1 if end of stream); |
396 |
|
* |
397 |
|
* @exception IOException If an error occurs. |
398 |
|
*/ |
399 |
|
public int read() throws IOException |
400 |
|
{ |
401 |
|
byte buf[] = new byte [1]; |
402 |
|
int bytes_read = read (buf, 0, buf.length); |
403 |
|
|
404 |
|
if (bytes_read != -1) |
405 |
|
return buf[0] & 0xFF; |
406 |
|
else |
407 |
|
return -1; |
408 |
|
} |
409 |
|
|
410 |
|
/** |
411 |
|
* Reads up to buf.length bytes of data into the caller supplied buffer. |
412 |
|
* |
413 |
|
* @return The actual number of bytes read or -1 if end of stream |
414 |
|
* |
415 |
|
* @exception IOException If an error occurs. |
416 |
|
*/ |
417 |
|
public int read (byte[] buf) throws IOException |
418 |
|
{ |
419 |
|
return read (buf, 0, buf.length); |
420 |
|
} |
421 |
|
|
422 |
|
/** |
423 |
|
* Reads up to len bytes of data into the caller supplied buffer starting |
424 |
|
* at offset bytes from the start of the buffer |
425 |
|
* |
426 |
|
* @return The number of bytes actually read or -1 if end of stream |
427 |
|
* |
428 |
|
* @exception IOException If an error occurs. |
429 |
|
*/ |
430 |
|
public int read (byte[] buf, int offset, int len) throws IOException |
431 |
|
{ |
432 |
|
int bytes_read = impl.read (buf, offset, len); |
433 |
|
|
434 |
|
if (bytes_read == 0) |
435 |
|
return -1; |
436 |
|
|
437 |
|
return bytes_read; |
438 |
|
} |
439 |
|
|
440 |
|
} // class SocketInputStream |
441 |
|
|
442 |
|
/** |
443 |
|
* This class is used internally by <code>PlainSocketImpl</code> to be the |
444 |
|
* <code>OutputStream</code> subclass returned by its |
445 |
|
* <code>getOutputStream method</code>. It expects only to be used in that |
446 |
|
* context. |
447 |
|
*/ |
448 |
|
class SocketOutputStream extends OutputStream |
449 |
|
{ |
450 |
|
/** |
451 |
|
* The PlainSocketImpl object this stream is associated with |
452 |
|
*/ |
453 |
|
private PlainSocketImpl impl; |
454 |
|
|
455 |
|
/** |
456 |
|
* Build an instance of this class from a PlainSocketImpl object |
457 |
|
*/ |
458 |
|
protected SocketOutputStream (PlainSocketImpl impl) |
459 |
|
{ |
460 |
|
this.impl = impl; |
461 |
|
} |
462 |
|
|
463 |
|
/** |
464 |
|
* This method closes the stream and the underlying socket connection. This |
465 |
|
* action also effectively closes any other InputStream or OutputStream |
466 |
|
* object associated with the connection. |
467 |
|
* |
468 |
|
* @exception IOException If an error occurs |
469 |
|
*/ |
470 |
|
public void close() throws IOException |
471 |
|
{ |
472 |
|
impl.close(); |
473 |
|
} |
474 |
|
|
475 |
|
/** |
476 |
|
* Hmmm, we don't seem to have a flush() method in Socket impl, so just |
477 |
|
* return for now, but this might need to be looked at later. |
478 |
|
* |
479 |
|
* @exception IOException Can't happen |
480 |
|
*/ |
481 |
|
public void flush() throws IOException |
482 |
|
{ |
483 |
|
} |
484 |
|
|
485 |
|
/** |
486 |
|
* Writes a byte (passed in as an int) to the given output stream |
487 |
|
* |
488 |
|
* @param b The byte to write |
489 |
|
* |
490 |
|
* @exception IOException If an error occurs |
491 |
|
*/ |
492 |
|
public void write (int b) throws IOException |
493 |
|
{ |
494 |
|
byte buf[] = new byte [1]; |
495 |
|
Integer i = new Integer (b); |
496 |
|
|
497 |
|
buf [0] = i.byteValue(); |
498 |
|
write (buf, 0, buf.length); |
499 |
|
} |
500 |
|
|
501 |
|
/** |
502 |
|
* Write an array of bytes to the output stream |
503 |
|
* |
504 |
|
* @param buf The array of bytes to write |
505 |
|
* |
506 |
|
* @exception IOException If an error occurs |
507 |
|
*/ |
508 |
|
public void write (byte[] buf) throws IOException |
509 |
|
{ |
510 |
|
write (buf, 0, buf.length); |
511 |
|
} |
512 |
|
|
513 |
|
/** |
514 |
|
* Writes len number of bytes from the array buf to the stream starting |
515 |
|
* at offset bytes into the buffer. |
516 |
|
* |
517 |
|
* @param buf The buffer |
518 |
|
* @param offset Offset into the buffer to start writing from |
519 |
|
* @param len The number of bytes to write |
520 |
|
*/ |
521 |
|
public void write (byte[] buf, int offset, int len) throws IOException |
522 |
|
{ |
523 |
|
impl.write (buf, offset, len); |
524 |
|
} |
525 |
|
|
526 |
|
} // class SocketOutputStream |
527 |
|
|
528 |
|
} // class PlainSocketImpl |