Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/ch/ChannelInputStream.java
38918 views
/*1* Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.ch;2627import java.io.*;28import java.nio.*;29import java.nio.channels.*;30import java.nio.channels.spi.*;313233/**34* This class is defined here rather than in java.nio.channels.Channels35* so that code can be shared with SocketAdaptor.36*37* @author Mike McCloskey38* @author Mark Reinhold39* @since 1.440*/4142public class ChannelInputStream43extends InputStream44{4546public static int read(ReadableByteChannel ch, ByteBuffer bb,47boolean block)48throws IOException49{50if (ch instanceof SelectableChannel) {51SelectableChannel sc = (SelectableChannel)ch;52synchronized (sc.blockingLock()) {53boolean bm = sc.isBlocking();54if (!bm)55throw new IllegalBlockingModeException();56if (bm != block)57sc.configureBlocking(block);58int n = ch.read(bb);59if (bm != block)60sc.configureBlocking(bm);61return n;62}63} else {64return ch.read(bb);65}66}6768protected final ReadableByteChannel ch;69private ByteBuffer bb = null;70private byte[] bs = null; // Invoker's previous array71private byte[] b1 = null;7273public ChannelInputStream(ReadableByteChannel ch) {74this.ch = ch;75}7677public synchronized int read() throws IOException {78if (b1 == null)79b1 = new byte[1];80int n = this.read(b1);81if (n == 1)82return b1[0] & 0xff;83return -1;84}8586public synchronized int read(byte[] bs, int off, int len)87throws IOException88{89if ((off < 0) || (off > bs.length) || (len < 0) ||90((off + len) > bs.length) || ((off + len) < 0)) {91throw new IndexOutOfBoundsException();92} else if (len == 0)93return 0;9495ByteBuffer bb = ((this.bs == bs)96? this.bb97: ByteBuffer.wrap(bs));98bb.limit(Math.min(off + len, bb.capacity()));99bb.position(off);100this.bb = bb;101this.bs = bs;102return read(bb);103}104105protected int read(ByteBuffer bb)106throws IOException107{108return ChannelInputStream.read(ch, bb, true);109}110111public int available() throws IOException {112// special case where the channel is to a file113if (ch instanceof SeekableByteChannel) {114SeekableByteChannel sbc = (SeekableByteChannel)ch;115long rem = Math.max(0, sbc.size() - sbc.position());116return (rem > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int)rem;117}118return 0;119}120121public void close() throws IOException {122ch.close();123}124125}126127128