Path: blob/master/src/java.base/share/classes/sun/nio/ch/ChannelInputStream.java
41159 views
/*1* Copyright (c) 2001, 2019, 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.*;31import java.util.Objects;3233/**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{89Objects.checkFromIndexSize(off, len, bs.length);90if (len == 0)91return 0;9293ByteBuffer bb = ((this.bs == bs)94? this.bb95: ByteBuffer.wrap(bs));96bb.limit(Math.min(off + len, bb.capacity()));97bb.position(off);98this.bb = bb;99this.bs = bs;100return read(bb);101}102103protected int read(ByteBuffer bb)104throws IOException105{106return ChannelInputStream.read(ch, bb, true);107}108109public int available() throws IOException {110// special case where the channel is to a file111if (ch instanceof SeekableByteChannel) {112SeekableByteChannel sbc = (SeekableByteChannel)ch;113long rem = Math.max(0, sbc.size() - sbc.position());114return (rem > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int)rem;115}116return 0;117}118119public synchronized long skip(long n) throws IOException {120// special case where the channel is to a file121if (ch instanceof SeekableByteChannel) {122SeekableByteChannel sbc = (SeekableByteChannel)ch;123long pos = sbc.position();124long newPos;125if (n > 0) {126newPos = pos + n;127long size = sbc.size();128if (newPos < 0 || newPos > size) {129newPos = size;130}131} else {132newPos = Long.max(pos + n, 0);133}134sbc.position(newPos);135return newPos - pos;136}137return super.skip(n);138}139140public void close() throws IOException {141ch.close();142}143144}145146147