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

  1. /*
  2.  * @(#)SocketInputStream.java    1.18 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.IOException;
  18. import java.io.FileInputStream;
  19.  
  20. /**
  21.  * This stream extends FileInputStream to implement a
  22.  * SocketInputStream. Note that this class should <b>NOT</b> be
  23.  * public.
  24.  *
  25.  * @version     1.18, 03/18/98
  26.  * @author    Jonathan Payne
  27.  * @author    Arthur van Hoff
  28.  */
  29. class SocketInputStream extends FileInputStream
  30. {
  31.     static {
  32.         init();
  33.     }
  34.     
  35.     private boolean eof;
  36.     private SocketImpl impl;
  37.     private byte temp[] = new byte[1];
  38.  
  39.     /**
  40.      * Creates a new SocketInputStream. 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 implemented socket input stream
  44.      */
  45.     SocketInputStream(SocketImpl impl) throws IOException {
  46.     super(impl.getFileDescriptor());
  47.     this.impl = impl;
  48.     }
  49.  
  50.     /** 
  51.      * Reads into an array of bytes at the specified offset using
  52.      * the received socket primitive. 
  53.      * @param b the buffer into which the data is read
  54.      * @param off the start offset of the data
  55.      * @param len the maximum number of bytes read
  56.      * @return the actual number of bytes read, -1 is
  57.      *          returned when the end of the stream is reached. 
  58.      * @exception IOException If an I/O error has occurred.
  59.      */
  60.     private native int socketRead(byte b[], int off, int len)
  61.     throws IOException;
  62.  
  63.     /** 
  64.      * Reads into a byte array data from the socket. 
  65.      * @param b the buffer into which the data is read
  66.      * @return the actual number of bytes read, -1 is
  67.      *          returned when the end of the stream is reached. 
  68.      * @exception IOException If an I/O error has occurred. 
  69.      */
  70.     public int read(byte b[]) throws IOException {
  71.     return read(b, 0, b.length);
  72.     }
  73.  
  74.     /** 
  75.      * Reads into a byte array <i>b</i> at offset <i>off</i>, 
  76.      * <i>length</i> bytes of data.
  77.      * @param b the buffer into which the data is read
  78.      * @param off the start offset of the data
  79.      * @param len the maximum number of bytes read
  80.      * @return the actual number of bytes read, -1 is
  81.      *          returned when the end of the stream is reached. 
  82.      * @exception IOException If an I/O error has occurred.
  83.      */
  84.     public int read(byte b[], int off, int length) throws IOException {
  85.     if (eof) {
  86.         return -1;
  87.     }
  88.     int n = socketRead(b, off, length);
  89.     if (n <= 0) {
  90.         eof = true;
  91.         return -1;
  92.     }
  93.     return n;
  94.     }
  95.  
  96.     /** 
  97.      * Reads a single byte from the socket. 
  98.      */
  99.     public int read() throws IOException {
  100.     if (eof) {
  101.         return -1;
  102.     }
  103.  
  104.      int n = read(temp, 0, 1);
  105.     if (n <= 0) {
  106.         return -1;
  107.     }
  108.     return temp[0] & 0xff;
  109.     }
  110.  
  111.     /** 
  112.      * Skips n bytes of input.
  113.      * @param n the number of bytes to skip
  114.      * @return    the actual number of bytes skipped.
  115.      * @exception IOException If an I/O error has occurred.
  116.      */
  117.     public long skip(long numbytes) throws IOException {
  118.     if (numbytes <= 0) {
  119.         return 0;
  120.     }
  121.     long n = numbytes;
  122.     int buflen = (int) Math.min(1024, n);
  123.     byte data[] = new byte[buflen];
  124.     while (n > 0) {
  125.         int r = read(data, 0, (int) Math.min((long) buflen, n));
  126.         if (r < 0) {
  127.         break;
  128.         }
  129.         n -= r;
  130.     }
  131.     return numbytes - n;
  132.     }
  133.  
  134.     /**
  135.      * Returns the number of bytes that can be read without blocking.
  136.      * @return the number of immediately available bytes
  137.      */
  138.     public int available() throws IOException {
  139.     return impl.available();
  140.     }
  141.  
  142.     /**
  143.      * Closes the stream.
  144.      */
  145.     public void close() throws IOException {
  146.     impl.close();
  147.     }
  148.  
  149.     /** 
  150.      * Overrides finalize, the fd is closed by the Socket.
  151.      */
  152.     protected void finalize() {}
  153.  
  154.     /**
  155.      * Perform class load-time initializations.
  156.      */
  157.     private native static void init();
  158. }
  159.  
  160.