893N/A/*
3909N/A * Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Apackage sun.nio.ch;
893N/A
893N/Aimport java.io.*;
893N/Aimport java.nio.*;
893N/Aimport java.nio.channels.*;
893N/Aimport java.nio.channels.spi.*;
893N/A
893N/A
893N/A/**
893N/A * This class is defined here rather than in java.nio.channels.Channels
893N/A * so that code can be shared with SocketAdaptor.
893N/A *
893N/A * @author Mike McCloskey
893N/A * @author Mark Reinhold
893N/A * @since 1.4
893N/A */
893N/A
893N/Apublic class ChannelInputStream
893N/A extends InputStream
893N/A{
893N/A
893N/A public static int read(ReadableByteChannel ch, ByteBuffer bb,
893N/A boolean block)
893N/A throws IOException
893N/A {
893N/A if (ch instanceof SelectableChannel) {
893N/A SelectableChannel sc = (SelectableChannel)ch;
893N/A synchronized (sc.blockingLock()) {
893N/A boolean bm = sc.isBlocking();
893N/A if (!bm)
893N/A throw new IllegalBlockingModeException();
893N/A if (bm != block)
893N/A sc.configureBlocking(block);
893N/A int n = ch.read(bb);
893N/A if (bm != block)
893N/A sc.configureBlocking(bm);
893N/A return n;
893N/A }
893N/A } else {
893N/A return ch.read(bb);
893N/A }
893N/A }
893N/A
893N/A protected final ReadableByteChannel ch;
893N/A private ByteBuffer bb = null;
893N/A private byte[] bs = null; // Invoker's previous array
893N/A private byte[] b1 = null;
893N/A
893N/A public ChannelInputStream(ReadableByteChannel ch) {
893N/A this.ch = ch;
893N/A }
893N/A
893N/A public synchronized int read() throws IOException {
893N/A if (b1 == null)
893N/A b1 = new byte[1];
893N/A int n = this.read(b1);
893N/A if (n == 1)
893N/A return b1[0] & 0xff;
893N/A return -1;
893N/A }
893N/A
893N/A public synchronized int read(byte[] bs, int off, int len)
893N/A throws IOException
893N/A {
893N/A if ((off < 0) || (off > bs.length) || (len < 0) ||
893N/A ((off + len) > bs.length) || ((off + len) < 0)) {
893N/A throw new IndexOutOfBoundsException();
893N/A } else if (len == 0)
893N/A return 0;
893N/A
893N/A ByteBuffer bb = ((this.bs == bs)
893N/A ? this.bb
893N/A : ByteBuffer.wrap(bs));
893N/A bb.limit(Math.min(off + len, bb.capacity()));
1319N/A bb.position(off);
893N/A this.bb = bb;
893N/A this.bs = bs;
1319N/A return read(bb);
1319N/A }
1319N/A
1319N/A protected int read(ByteBuffer bb)
893N/A throws IOException
893N/A {
893N/A return ChannelInputStream.read(ch, bb, true);
893N/A }
893N/A
893N/A public int available() throws IOException {
893N/A // special case where the channel is to a file
893N/A if (ch instanceof SeekableByteChannel) {
893N/A SeekableByteChannel sbc = (SeekableByteChannel)ch;
1319N/A long rem = Math.max(0, sbc.size() - sbc.position());
1319N/A return (rem > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int)rem;
893N/A }
893N/A return 0;
893N/A }
1319N/A
1319N/A public void close() throws IOException {
893N/A ch.close();
893N/A }
893N/A
1319N/A}
3471N/A