1. /*
  2. * @(#)SocketInputStream.java 1.21 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.net;
  8. import java.io.IOException;
  9. import java.io.FileInputStream;
  10. /**
  11. * This stream extends FileInputStream to implement a
  12. * SocketInputStream. Note that this class should <b>NOT</b> be
  13. * public.
  14. *
  15. * @version 1.21, 11/29/01
  16. * @author Jonathan Payne
  17. * @author Arthur van Hoff
  18. */
  19. class SocketInputStream extends FileInputStream
  20. {
  21. static {
  22. init();
  23. }
  24. private boolean eof;
  25. private SocketImpl impl;
  26. private byte temp[] = new byte[1];
  27. /**
  28. * Creates a new SocketInputStream. Can only be called
  29. * by a Socket. This method needs to hang on to the owner Socket so
  30. * that the fd will not be closed.
  31. * @param impl the implemented socket input stream
  32. */
  33. SocketInputStream(SocketImpl impl) throws IOException {
  34. super(impl.getFileDescriptor());
  35. this.impl = impl;
  36. }
  37. /**
  38. * Reads into an array of bytes at the specified offset using
  39. * the received socket primitive.
  40. * @param b the buffer into which the data is read
  41. * @param off the start offset of the data
  42. * @param len the maximum number of bytes read
  43. * @return the actual number of bytes read, -1 is
  44. * returned when the end of the stream is reached.
  45. * @exception IOException If an I/O error has occurred.
  46. */
  47. private native int socketRead(byte b[], int off, int len)
  48. throws IOException;
  49. /**
  50. * Reads into a byte array data from the socket.
  51. * @param b the buffer into which the data is read
  52. * @return the actual number of bytes read, -1 is
  53. * returned when the end of the stream is reached.
  54. * @exception IOException If an I/O error has occurred.
  55. */
  56. public int read(byte b[]) throws IOException {
  57. return read(b, 0, b.length);
  58. }
  59. /**
  60. * Reads into a byte array <i>b</i> at offset <i>off</i>,
  61. * <i>length</i> bytes of data.
  62. * @param b the buffer into which the data is read
  63. * @param off the start offset of the data
  64. * @param len the maximum number of bytes read
  65. * @return the actual number of bytes read, -1 is
  66. * returned when the end of the stream is reached.
  67. * @exception IOException If an I/O error has occurred.
  68. */
  69. public int read(byte b[], int off, int length) throws IOException {
  70. if (eof) {
  71. return -1;
  72. }
  73. if (length == 0)
  74. return 0;
  75. int n = socketRead(b, off, length);
  76. if (n <= 0) {
  77. eof = true;
  78. return -1;
  79. }
  80. return n;
  81. }
  82. /**
  83. * Reads a single byte from the socket.
  84. */
  85. public int read() throws IOException {
  86. if (eof) {
  87. return -1;
  88. }
  89. int n = read(temp, 0, 1);
  90. if (n <= 0) {
  91. return -1;
  92. }
  93. return temp[0] & 0xff;
  94. }
  95. /**
  96. * Skips n bytes of input.
  97. * @param n the number of bytes to skip
  98. * @return the actual number of bytes skipped.
  99. * @exception IOException If an I/O error has occurred.
  100. */
  101. public long skip(long numbytes) throws IOException {
  102. if (numbytes <= 0) {
  103. return 0;
  104. }
  105. long n = numbytes;
  106. int buflen = (int) Math.min(1024, n);
  107. byte data[] = new byte[buflen];
  108. while (n > 0) {
  109. int r = read(data, 0, (int) Math.min((long) buflen, n));
  110. if (r < 0) {
  111. break;
  112. }
  113. n -= r;
  114. }
  115. return numbytes - n;
  116. }
  117. /**
  118. * Returns the number of bytes that can be read without blocking.
  119. * @return the number of immediately available bytes
  120. */
  121. public int available() throws IOException {
  122. return impl.available();
  123. }
  124. /**
  125. * Closes the stream.
  126. */
  127. public void close() throws IOException {
  128. impl.close();
  129. }
  130. /**
  131. * Overrides finalize, the fd is closed by the Socket.
  132. */
  133. protected void finalize() {}
  134. /**
  135. * Perform class load-time initializations.
  136. */
  137. private native static void init();
  138. }