home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / net / SocketInputStream.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  4.2 KB  |  162 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)SocketInputStream.java    1.20 98/09/21
  3.  *
  4.  * Copyright 1995-1998 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.20, 09/21/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.         if (length == 0)
  89.             return 0;
  90.     int n = socketRead(b, off, length);
  91.     if (n <= 0) {
  92.         eof = true;
  93.         return -1;
  94.     }
  95.     return n;
  96.     }
  97.  
  98.     /** 
  99.      * Reads a single byte from the socket. 
  100.      */
  101.     public int read() throws IOException {
  102.     if (eof) {
  103.         return -1;
  104.     }
  105.  
  106.      int n = read(temp, 0, 1);
  107.     if (n <= 0) {
  108.         return -1;
  109.     }
  110.     return temp[0] & 0xff;
  111.     }
  112.  
  113.     /** 
  114.      * Skips n bytes of input.
  115.      * @param n the number of bytes to skip
  116.      * @return    the actual number of bytes skipped.
  117.      * @exception IOException If an I/O error has occurred.
  118.      */
  119.     public long skip(long numbytes) throws IOException {
  120.     if (numbytes <= 0) {
  121.         return 0;
  122.     }
  123.     long n = numbytes;
  124.     int buflen = (int) Math.min(1024, n);
  125.     byte data[] = new byte[buflen];
  126.     while (n > 0) {
  127.         int r = read(data, 0, (int) Math.min((long) buflen, n));
  128.         if (r < 0) {
  129.         break;
  130.         }
  131.         n -= r;
  132.     }
  133.     return numbytes - n;
  134.     }
  135.  
  136.     /**
  137.      * Returns the number of bytes that can be read without blocking.
  138.      * @return the number of immediately available bytes
  139.      */
  140.     public int available() throws IOException {
  141.     return impl.available();
  142.     }
  143.  
  144.     /**
  145.      * Closes the stream.
  146.      */
  147.     public void close() throws IOException {
  148.     impl.close();
  149.     }
  150.  
  151.     /** 
  152.      * Overrides finalize, the fd is closed by the Socket.
  153.      */
  154.     protected void finalize() {}
  155.  
  156.     /**
  157.      * Perform class load-time initializations.
  158.      */
  159.     private native static void init();
  160. }
  161.  
  162.