1. /*
  2. * @(#)SocketOutputStream.java 1.17 00/02/02
  3. *
  4. * Copyright 1995-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.net;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. /**
  14. * This stream extends FileOutputStream to implement a
  15. * SocketOutputStream. Note that this class should <b>NOT</b> be
  16. * public.
  17. *
  18. * @version 1.17, 02/02/00
  19. * @author Jonathan Payne
  20. * @author Arthur van Hoff
  21. */
  22. class SocketOutputStream extends FileOutputStream
  23. {
  24. static {
  25. init();
  26. }
  27. private SocketImpl impl;
  28. private byte temp[] = new byte[1];
  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(SocketImpl impl) throws IOException {
  36. super(impl.getFileDescriptor());
  37. this.impl = impl;
  38. }
  39. /**
  40. * Writes to the socket.
  41. * @param b the data to be written
  42. * @param off the start offset in the data
  43. * @param len the number of bytes that are written
  44. * @exception IOException If an I/O error has occurred.
  45. */
  46. private native void socketWrite(byte b[], int off, int len)
  47. throws IOException;
  48. /**
  49. * Writes a byte to the socket.
  50. * @param b the data to be written
  51. * @exception IOException If an I/O error has occurred.
  52. */
  53. public void write(int b) throws IOException {
  54. temp[0] = (byte)b;
  55. socketWrite(temp, 0, 1);
  56. }
  57. /**
  58. * Writes the contents of the buffer <i>b</i> to the socket.
  59. * @param b the data to be written
  60. * @exception SocketException If an I/O error has occurred.
  61. */
  62. public void write(byte b[]) throws IOException {
  63. socketWrite(b, 0, b.length);
  64. }
  65. /**
  66. * Writes <i>length</i> bytes from buffer <i>b</i> starting at
  67. * offset <i>len</i>.
  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 SocketException If an I/O error has occurred.
  72. */
  73. public void write(byte b[], int off, int len) throws IOException {
  74. socketWrite(b, off, len);
  75. }
  76. /**
  77. * Closes the stream.
  78. */
  79. public void close() throws IOException {
  80. impl.close();
  81. }
  82. /**
  83. * Overrides finalize, the fd is closed by the Socket.
  84. */
  85. protected void finalize() {}
  86. /**
  87. * Perform class load-time initializations.
  88. */
  89. private native static void init();
  90. }