home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / java.z / SocketOutputStream.java < prev    next >
Text File  |  1996-05-03  |  3KB  |  103 lines

  1. /*
  2.  * @(#)SocketOutputStream.java    1.9 95/11/06 Jonathan Payne
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.net;
  21.  
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24.  
  25. /**
  26.  * This stream extends FileOutputStream to implement a
  27.  * SocketOutputStream. Note that this class should <b>NOT</b> be
  28.  * public.
  29.  *
  30.  * @version     1.9, 06 Nov 1995
  31.  * @author     Jonathan Payne
  32.  * @author    Arthur van Hoff
  33.  */
  34. class SocketOutputStream extends FileOutputStream
  35. {
  36.     private SocketImpl impl;
  37.     private byte temp[] = new byte[1];
  38.     
  39.     /**
  40.      * Creates a new SocketOutputStream. Can only be called
  41.      * by a Socket. This method needs to hang on to the owner Socket so
  42.      * that the fd will not be closed.
  43.      * @param impl the socket output stream inplemented
  44.      */
  45.     SocketOutputStream(SocketImpl impl) throws IOException {
  46.     super(impl.getFileDescriptor());
  47.     this.impl = impl;
  48.     }
  49.  
  50.     /**
  51.      * Writes to the socket.
  52.      * @param b the data to be written
  53.      * @param off the start offset in the data
  54.      * @param len the number of bytes that are written
  55.      * @exception IOException If an I/O error has occurred.
  56.      */
  57.     private native void socketWrite(byte b[], int off, int len)
  58.     throws IOException;
  59.  
  60.     /** 
  61.      * Writes a byte to the socket. 
  62.      * @param b the data to be written
  63.      * @exception IOException If an I/O error has occurred. 
  64.      */
  65.     public void write(int b) throws IOException {
  66.     temp[0] = (byte)b;
  67.     socketWrite(temp, 0, 1);
  68.     }
  69.  
  70.     /** 
  71.      * Writes the contents of the buffer <i>b</i> to the socket.
  72.      * @param b the data to be written
  73.      * @exception SocketException If an I/O error has occurred. 
  74.      */
  75.     public void write(byte b[]) throws IOException {
  76.     socketWrite(b, 0, b.length);
  77.     }
  78.  
  79.     /** 
  80.      * Writes <i>length</i> bytes from buffer <i>b</i> starting at 
  81.      * offset <i>len</i>.
  82.      * @param b the data to be written
  83.      * @param off the start offset in the data
  84.      * @param len the number of bytes that are written
  85.      * @exception SocketException If an I/O error has occurred.
  86.      */
  87.     public void write(byte b[], int off, int len) throws IOException {
  88.     socketWrite(b, off, len);
  89.     }
  90.  
  91.     /**
  92.      * Closes the stream.
  93.      */
  94.     public void close() throws IOException {
  95.     impl.close();
  96.     }
  97.  
  98.     /** 
  99.      * Overrides finalize, the fd is closed by the Socket.
  100.      */
  101.     protected void finalize() {}
  102. }
  103.