home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 February / VPR9802A.ISO / APP_DEMO / VC / MAIN.BIN / SocketInputStream.java < prev    next >
Text File  |  1997-10-27  |  4KB  |  159 lines

  1. /*
  2.  * @(#)SocketInputStream.java    1.15 97/01/26
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.net;
  24.  
  25. import java.io.IOException;
  26. import java.io.FileInputStream;
  27.  
  28. /**
  29.  * This stream extends FileInputStream to implement a
  30.  * SocketInputStream. Note that this class should <b>NOT</b> be
  31.  * public.
  32.  *
  33.  * @version     1.15, 01/26/97
  34.  * @author    Jonathan Payne
  35.  * @author    Arthur van Hoff
  36.  */
  37. class SocketInputStream extends FileInputStream
  38. {
  39.     private boolean eof;
  40.     private SocketImpl impl;
  41.     private byte temp[] = new byte[1];
  42.  
  43.     /**
  44.      * Creates a new SocketInputStream. Can only be called
  45.      * by a Socket. This method needs to hang on to the owner Socket so
  46.      * that the fd will not be closed.
  47.      * @param impl the implemented socket input stream
  48.      */
  49.     SocketInputStream(SocketImpl impl) throws IOException {
  50.     super(impl.getFileDescriptor());
  51.     this.impl = impl;
  52.     }
  53.  
  54.     /** 
  55.      * Reads into an array of bytes at the specified offset using
  56.      * the received socket primitive. 
  57.      * @param b the buffer into which the data is read
  58.      * @param off the start offset of the data
  59.      * @param len the maximum number of bytes read
  60.      * @return the actual number of bytes read, -1 is
  61.      *          returned when the end of the stream is reached. 
  62.      * @exception IOException If an I/O error has occurred.
  63.      */
  64.     private native int socketRead(byte b[], int off, int len)
  65.     throws IOException;
  66.  
  67.     /** 
  68.      * Reads into a byte array data from the socket. 
  69.      * @param b the buffer into which the data is read
  70.      * @return the actual number of bytes read, -1 is
  71.      *          returned when the end of the stream is reached. 
  72.      * @exception IOException If an I/O error has occurred. 
  73.      */
  74.     public int read(byte b[]) throws IOException {
  75.     return read(b, 0, b.length);
  76.     }
  77.  
  78.     /** 
  79.      * Reads into a byte array <i>b</i> at offset <i>off</i>, 
  80.      * <i>length</i> bytes of data.
  81.      * @param b the buffer into which the data is read
  82.      * @param off the start offset of the data
  83.      * @param len the maximum number of bytes read
  84.      * @return the actual number of bytes read, -1 is
  85.      *          returned when the end of the stream is reached. 
  86.      * @exception IOException If an I/O error has occurred.
  87.      */
  88.     public int read(byte b[], int off, int length) throws IOException {
  89.     if (eof) {
  90.         return -1;
  91.     }
  92.     int n = socketRead(b, off, length);
  93.     if (n <= 0) {
  94.         eof = true;
  95.         return -1;
  96.     }
  97.     return n;
  98.     }
  99.  
  100.     /** 
  101.      * Reads a single byte from the socket. 
  102.      */
  103.     public int read() throws IOException {
  104.     if (eof) {
  105.         return -1;
  106.     }
  107.  
  108.      int n = read(temp, 0, 1);
  109.     if (n <= 0) {
  110.         return -1;
  111.     }
  112.     return temp[0] & 0xff;
  113.     }
  114.  
  115.     /** 
  116.      * Skips n bytes of input.
  117.      * @param n the number of bytes to skip
  118.      * @return    the actual number of bytes skipped.
  119.      * @exception IOException If an I/O error has occurred.
  120.      */
  121.     public long skip(long numbytes) throws IOException {
  122.     if (numbytes <= 0) {
  123.         return 0;
  124.     }
  125.     long n = numbytes;
  126.     int buflen = (int) Math.min(1024, n);
  127.     byte data[] = new byte[buflen];
  128.     while (n > 0) {
  129.         int r = read(data, 0, (int) Math.min((long) buflen, n));
  130.         if (r < 0) {
  131.         break;
  132.         }
  133.         n -= r;
  134.     }
  135.     return numbytes - n;
  136.     }
  137.  
  138.     /**
  139.      * Returns the number of bytes that can be read without blocking.
  140.      * @return the number of immediately available bytes
  141.      */
  142.     public int available() throws IOException {
  143.     return impl.available();
  144.     }
  145.  
  146.     /**
  147.      * Closes the stream.
  148.      */
  149.     public void close() throws IOException {
  150.     impl.close();
  151.     }
  152.  
  153.     /** 
  154.      * Overrides finalize, the fd is closed by the Socket.
  155.      */
  156.     protected void finalize() {}
  157. }
  158.  
  159.