home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / net / SocketOutputStream.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  2.9 KB  |  108 lines

  1. /*
  2.  * @(#)SocketOutputStream.java    1.14 98/03/18
  3.  *
  4.  * Copyright 1995-1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.net;
  16.  
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19.  
  20. /**
  21.  * This stream extends FileOutputStream to implement a
  22.  * SocketOutputStream. Note that this class should <b>NOT</b> be
  23.  * public.
  24.  *
  25.  * @version     1.14, 03/18/98
  26.  * @author     Jonathan Payne
  27.  * @author    Arthur van Hoff
  28.  */
  29. class SocketOutputStream extends FileOutputStream
  30. {
  31.     static {
  32.         init();
  33.     }
  34.  
  35.     private SocketImpl impl;
  36.     private byte temp[] = new byte[1];
  37.     
  38.     /**
  39.      * Creates a new SocketOutputStream. Can only be called
  40.      * by a Socket. This method needs to hang on to the owner Socket so
  41.      * that the fd will not be closed.
  42.      * @param impl the socket output stream inplemented
  43.      */
  44.     SocketOutputStream(SocketImpl impl) throws IOException {
  45.     super(impl.getFileDescriptor());
  46.     this.impl = impl;
  47.     }
  48.  
  49.     /**
  50.      * Writes to the socket.
  51.      * @param b the data to be written
  52.      * @param off the start offset in the data
  53.      * @param len the number of bytes that are written
  54.      * @exception IOException If an I/O error has occurred.
  55.      */
  56.     private native void socketWrite(byte b[], int off, int len)
  57.     throws IOException;
  58.  
  59.     /** 
  60.      * Writes a byte to the socket. 
  61.      * @param b the data to be written
  62.      * @exception IOException If an I/O error has occurred. 
  63.      */
  64.     public void write(int b) throws IOException {
  65.     temp[0] = (byte)b;
  66.     socketWrite(temp, 0, 1);
  67.     }
  68.  
  69.     /** 
  70.      * Writes the contents of the buffer <i>b</i> to the socket.
  71.      * @param b the data to be written
  72.      * @exception SocketException If an I/O error has occurred. 
  73.      */
  74.     public void write(byte b[]) throws IOException {
  75.     socketWrite(b, 0, b.length);
  76.     }
  77.  
  78.     /** 
  79.      * Writes <i>length</i> bytes from buffer <i>b</i> starting at 
  80.      * offset <i>len</i>.
  81.      * @param b the data to be written
  82.      * @param off the start offset in the data
  83.      * @param len the number of bytes that are written
  84.      * @exception SocketException If an I/O error has occurred.
  85.      */
  86.     public void write(byte b[], int off, int len) throws IOException {
  87.     socketWrite(b, off, len);
  88.     }
  89.  
  90.     /**
  91.      * Closes the stream.
  92.      */
  93.     public void close() throws IOException {
  94.     impl.close();
  95.     }
  96.  
  97.     /** 
  98.      * Overrides finalize, the fd is closed by the Socket.
  99.      */
  100.     protected void finalize() {}
  101.  
  102.     /**
  103.      * Perform class load-time initializations.
  104.      */
  105.     private native static void init();
  106.  
  107. }
  108.