1. /*
  2. * @(#)SocketOutputStream.java 1.16 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.FileOutputStream;
  9. import java.io.IOException;
  10. /**
  11. * This stream extends FileOutputStream to implement a
  12. * SocketOutputStream. Note that this class should <b>NOT</b> be
  13. * public.
  14. *
  15. * @version 1.16, 11/29/01
  16. * @author Jonathan Payne
  17. * @author Arthur van Hoff
  18. */
  19. class SocketOutputStream extends FileOutputStream
  20. {
  21. static {
  22. init();
  23. }
  24. private SocketImpl impl;
  25. private byte temp[] = new byte[1];
  26. /**
  27. * Creates a new SocketOutputStream. Can only be called
  28. * by a Socket. This method needs to hang on to the owner Socket so
  29. * that the fd will not be closed.
  30. * @param impl the socket output stream inplemented
  31. */
  32. SocketOutputStream(SocketImpl impl) throws IOException {
  33. super(impl.getFileDescriptor());
  34. this.impl = impl;
  35. }
  36. /**
  37. * Writes to the socket.
  38. * @param b the data to be written
  39. * @param off the start offset in the data
  40. * @param len the number of bytes that are written
  41. * @exception IOException If an I/O error has occurred.
  42. */
  43. private native void socketWrite(byte b[], int off, int len)
  44. throws IOException;
  45. /**
  46. * Writes a byte to the socket.
  47. * @param b the data to be written
  48. * @exception IOException If an I/O error has occurred.
  49. */
  50. public void write(int b) throws IOException {
  51. temp[0] = (byte)b;
  52. socketWrite(temp, 0, 1);
  53. }
  54. /**
  55. * Writes the contents of the buffer <i>b</i> to the socket.
  56. * @param b the data to be written
  57. * @exception SocketException If an I/O error has occurred.
  58. */
  59. public void write(byte b[]) throws IOException {
  60. socketWrite(b, 0, b.length);
  61. }
  62. /**
  63. * Writes <i>length</i> bytes from buffer <i>b</i> starting at
  64. * offset <i>len</i>.
  65. * @param b the data to be written
  66. * @param off the start offset in the data
  67. * @param len the number of bytes that are written
  68. * @exception SocketException If an I/O error has occurred.
  69. */
  70. public void write(byte b[], int off, int len) throws IOException {
  71. socketWrite(b, off, len);
  72. }
  73. /**
  74. * Closes the stream.
  75. */
  76. public void close() throws IOException {
  77. impl.close();
  78. }
  79. /**
  80. * Overrides finalize, the fd is closed by the Socket.
  81. */
  82. protected void finalize() {}
  83. /**
  84. * Perform class load-time initializations.
  85. */
  86. private native static void init();
  87. }