1. /*
  2. * @(#)SocketOutputStream.java 1.28 03/01/23
  3. *
  4. * Copyright 2003 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.FileDescriptor;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.nio.channels.FileChannel;
  12. /**
  13. * This stream extends FileOutputStream to implement a
  14. * SocketOutputStream. Note that this class should <b>NOT</b> be
  15. * public.
  16. *
  17. * @version 1.28, 01/23/03
  18. * @author Jonathan Payne
  19. * @author Arthur van Hoff
  20. */
  21. class SocketOutputStream extends FileOutputStream
  22. {
  23. static {
  24. init();
  25. }
  26. private PlainSocketImpl impl = null;
  27. private byte temp[] = new byte[1];
  28. private Socket socket = null;
  29. /**
  30. * Creates a new SocketOutputStream. Can only be called
  31. * by a Socket. This method needs to hang on to the owner Socket so
  32. * that the fd will not be closed.
  33. * @param impl the socket output stream inplemented
  34. */
  35. SocketOutputStream(PlainSocketImpl impl) throws IOException {
  36. super(impl.getFileDescriptor());
  37. this.impl = impl;
  38. socket = impl.getSocket();
  39. }
  40. /**
  41. * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
  42. * object associated with this file output stream. </p>
  43. *
  44. * The <code>getChannel</code> method of <code>SocketOutputStream</code>
  45. * returns <code>null</code> since it is a socket based stream.</p>
  46. *
  47. * @return the file channel associated with this file output stream
  48. *
  49. * @since 1.4
  50. * @spec JSR-51
  51. */
  52. public final FileChannel getChannel() {
  53. return null;
  54. }
  55. /**
  56. * Writes to the socket.
  57. * @param fd the FileDescriptor
  58. * @param b the data to be written
  59. * @param off the start offset in the data
  60. * @param len the number of bytes that are written
  61. * @exception IOException If an I/O error has occurred.
  62. */
  63. private native void socketWrite0(FileDescriptor fd, byte[] b, int off,
  64. int len) throws IOException;
  65. /**
  66. * Writes to the socket with appropriate locking of the
  67. * FileDescriptor.
  68. * @param b the data to be written
  69. * @param off the start offset in the data
  70. * @param len the number of bytes that are written
  71. * @exception IOException If an I/O error has occurred.
  72. */
  73. private void socketWrite(byte b[], int off, int len) throws IOException {
  74. if (len <= 0 || off < 0 | off + len > b.length) {
  75. if (len == 0) {
  76. return;
  77. }
  78. throw new ArrayIndexOutOfBoundsException();
  79. }
  80. FileDescriptor fd = impl.acquireFD();
  81. try {
  82. socketWrite0(fd, b, off, len);
  83. } catch (SocketException se) {
  84. if (se instanceof sun.net.ConnectionResetException) {
  85. impl.setConnectionResetPending();
  86. se = new SocketException("Connection reset");
  87. }
  88. if (impl.isClosedOrPending()) {
  89. throw new SocketException("Socket closed");
  90. } else {
  91. throw se;
  92. }
  93. } finally {
  94. impl.releaseFD();
  95. }
  96. }
  97. /**
  98. * Writes a byte to the socket.
  99. * @param b the data to be written
  100. * @exception IOException If an I/O error has occurred.
  101. */
  102. public void write(int b) throws IOException {
  103. temp[0] = (byte)b;
  104. socketWrite(temp, 0, 1);
  105. }
  106. /**
  107. * Writes the contents of the buffer <i>b</i> to the socket.
  108. * @param b the data to be written
  109. * @exception SocketException If an I/O error has occurred.
  110. */
  111. public void write(byte b[]) throws IOException {
  112. socketWrite(b, 0, b.length);
  113. }
  114. /**
  115. * Writes <i>length</i> bytes from buffer <i>b</i> starting at
  116. * offset <i>len</i>.
  117. * @param b the data to be written
  118. * @param off the start offset in the data
  119. * @param len the number of bytes that are written
  120. * @exception SocketException If an I/O error has occurred.
  121. */
  122. public void write(byte b[], int off, int len) throws IOException {
  123. socketWrite(b, off, len);
  124. }
  125. /**
  126. * Closes the stream.
  127. */
  128. private boolean closing = false;
  129. public void close() throws IOException {
  130. // Prevent recursion. See BugId 4484411
  131. if (closing)
  132. return;
  133. closing = true;
  134. if (socket != null) {
  135. if (!socket.isClosed())
  136. socket.close();
  137. } else
  138. impl.close();
  139. closing = false;
  140. }
  141. /**
  142. * Overrides finalize, the fd is closed by the Socket.
  143. */
  144. protected void finalize() {}
  145. /**
  146. * Perform class load-time initializations.
  147. */
  148. private native static void init();
  149. }