< prev index next >

src/java.base/share/classes/java/net/SocketInputStream.java

Print this page


   1 /*
   2  * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  31 import java.nio.channels.FileChannel;
  32 
  33 import sun.net.ConnectionResetException;
  34 
  35 /**
  36  * This stream extends FileInputStream to implement a
  37  * SocketInputStream. Note that this class should <b>NOT</b> be
  38  * public.
  39  *
  40  * @author      Jonathan Payne
  41  * @author      Arthur van Hoff
  42  */
  43 class SocketInputStream extends FileInputStream {
  44     static {
  45         init();
  46     }
  47 
  48     private boolean eof;
  49     private AbstractPlainSocketImpl impl = null;
  50     private byte temp[];
  51     private Socket socket = null;
  52 
  53     /**
  54      * Creates a new SocketInputStream. Can only be called
  55      * by a Socket. This method needs to hang on to the owner Socket so
  56      * that the fd will not be closed.
  57      * @param impl the implemented socket input stream
  58      */
  59     SocketInputStream(AbstractPlainSocketImpl impl) throws IOException {
  60         super(impl.getFileDescriptor());
  61         this.impl = impl;
  62         socket = impl.getSocket();
  63     }
  64 
  65     /**
  66      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
  67      * object associated with this file input stream.</p>
  68      *
  69      * The {@code getChannel} method of {@code SocketInputStream}
  70      * returns {@code null} since it is a socket based stream.</p>
  71      *
  72      * @return  the file channel associated with this file input stream
  73      *
  74      * @since 1.4
  75      * @spec JSR-51
  76      */
  77     public final FileChannel getChannel() {
  78         return null;
  79     }
  80 
  81     /**
  82      * Reads into an array of bytes at the specified offset using


 219         byte data[] = new byte[buflen];
 220         while (n > 0) {
 221             int r = read(data, 0, (int) Math.min((long) buflen, n));
 222             if (r < 0) {
 223                 break;
 224             }
 225             n -= r;
 226         }
 227         return numbytes - n;
 228     }
 229 
 230     /**
 231      * Returns the number of bytes that can be read without blocking.
 232      * @return the number of immediately available bytes
 233      */
 234     public int available() throws IOException {
 235         int available = impl.available();
 236         return eof ? 0 : available;
 237     }
 238 
 239     /**
 240      * Closes the stream.
 241      */
 242     private boolean closing = false;
 243     public void close() throws IOException {
 244         // Prevent recursion. See BugId 4484411
 245         if (closing)
 246             return;
 247         closing = true;
 248         if (socket != null) {
 249             if (!socket.isClosed())
 250                 socket.close();
 251         } else
 252             impl.close();
 253         closing = false;
 254     }
 255 
 256     void setEOF(boolean eof) {
 257         this.eof = eof;
 258     }
 259 
 260     /**
 261      * Overrides finalize, the fd is closed by the Socket.
 262      */
 263     @SuppressWarnings({"deprecation", "removal"})
 264     protected void finalize() {}
 265 
 266     /**
 267      * Perform class load-time initializations.
 268      */
 269     private static native void init();
 270 }
   1 /*
   2  * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  31 import java.nio.channels.FileChannel;
  32 
  33 import sun.net.ConnectionResetException;
  34 
  35 /**
  36  * This stream extends FileInputStream to implement a
  37  * SocketInputStream. Note that this class should <b>NOT</b> be
  38  * public.
  39  *
  40  * @author      Jonathan Payne
  41  * @author      Arthur van Hoff
  42  */
  43 class SocketInputStream extends FileInputStream {
  44     static {
  45         init();
  46     }
  47 
  48     private boolean eof;
  49     private AbstractPlainSocketImpl impl = null;
  50     private byte temp[];

  51 
  52     /**
  53      * Creates a new SocketInputStream. Can only be called
  54      * by a Socket. This method needs to hang on to the owner Socket so
  55      * that the fd will not be closed.
  56      * @param impl the implemented socket input stream
  57      */
  58     SocketInputStream(AbstractPlainSocketImpl impl) throws IOException {
  59         super(impl.getFileDescriptor());
  60         this.impl = impl;

  61     }
  62 
  63     /**
  64      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
  65      * object associated with this file input stream.</p>
  66      *
  67      * The {@code getChannel} method of {@code SocketInputStream}
  68      * returns {@code null} since it is a socket based stream.</p>
  69      *
  70      * @return  the file channel associated with this file input stream
  71      *
  72      * @since 1.4
  73      * @spec JSR-51
  74      */
  75     public final FileChannel getChannel() {
  76         return null;
  77     }
  78 
  79     /**
  80      * Reads into an array of bytes at the specified offset using


 217         byte data[] = new byte[buflen];
 218         while (n > 0) {
 219             int r = read(data, 0, (int) Math.min((long) buflen, n));
 220             if (r < 0) {
 221                 break;
 222             }
 223             n -= r;
 224         }
 225         return numbytes - n;
 226     }
 227 
 228     /**
 229      * Returns the number of bytes that can be read without blocking.
 230      * @return the number of immediately available bytes
 231      */
 232     public int available() throws IOException {
 233         int available = impl.available();
 234         return eof ? 0 : available;
 235     }
 236 

















 237     void setEOF(boolean eof) {
 238         this.eof = eof;
 239     }
 240 
 241     /**
 242      * Overrides finalize, the fd is closed by the Socket.
 243      */
 244     @SuppressWarnings({"deprecation", "removal"})
 245     protected void finalize() {}
 246 
 247     /**
 248      * Perform class load-time initializations.
 249      */
 250     private static native void init();
 251 }
< prev index next >