home *** CD-ROM | disk | FTP | other *** search
/ Chip: Shareware for Win 95 / Chip-Shareware-Win95.bin / ostatni / powerj / java.z / SocketInputStream.java < prev    next >
Encoding:
Java Source  |  1996-05-03  |  4.4 KB  |  156 lines

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