• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.  Oracle designates this
9  * particular file as subject to the "Classpath" exception as provided
10  * by Oracle in the LICENSE file that accompanied this code.
11  *
12  * This code is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * version 2 for more details (a copy is included in the LICENSE file that
16  * accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License version
19  * 2 along with this work; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23  * or visit www.oracle.com if you need additional information or have any
24  * questions.
25  */
26 
27 package sun.nio.ch;
28 
29 import java.io.*;
30 import java.nio.*;
31 import java.nio.channels.*;
32 import java.nio.channels.spi.*;
33 
34 
35 /**
36  * This class is defined here rather than in java.nio.channels.Channels
37  * so that code can be shared with SocketAdaptor.
38  *
39  * @author Mike McCloskey
40  * @author Mark Reinhold
41  * @since 1.4
42  */
43 
44 public class ChannelInputStream
45     extends InputStream
46 {
47 
read(ReadableByteChannel ch, ByteBuffer bb, boolean block)48     public static int read(ReadableByteChannel ch, ByteBuffer bb,
49                            boolean block)
50         throws IOException
51     {
52         if (ch instanceof SelectableChannel) {
53             SelectableChannel sc = (SelectableChannel)ch;
54             synchronized (sc.blockingLock()) {
55                 boolean bm = sc.isBlocking();
56                 if (!bm)
57                     throw new IllegalBlockingModeException();
58                 if (bm != block)
59                     sc.configureBlocking(block);
60                 int n = ch.read(bb);
61                 if (bm != block)
62                     sc.configureBlocking(bm);
63                 return n;
64             }
65         } else {
66             return ch.read(bb);
67         }
68     }
69 
70     protected final ReadableByteChannel ch;
71     private ByteBuffer bb = null;
72     private byte[] bs = null;           // Invoker's previous array
73     private byte[] b1 = null;
74 
ChannelInputStream(ReadableByteChannel ch)75     public ChannelInputStream(ReadableByteChannel ch) {
76         this.ch = ch;
77     }
78 
read()79     public synchronized int read() throws IOException {
80         if (b1 == null)
81             b1 = new byte[1];
82         int n = this.read(b1);
83         if (n == 1)
84             return b1[0] & 0xff;
85         return -1;
86     }
87 
read(byte[] bs, int off, int len)88     public synchronized int read(byte[] bs, int off, int len)
89         throws IOException
90     {
91         if ((off < 0) || (off > bs.length) || (len < 0) ||
92             ((off + len) > bs.length) || ((off + len) < 0)) {
93             throw new IndexOutOfBoundsException();
94         } else if (len == 0)
95             return 0;
96 
97         ByteBuffer bb = ((this.bs == bs)
98                          ? this.bb
99                          : ByteBuffer.wrap(bs));
100         bb.limit(Math.min(off + len, bb.capacity()));
101         bb.position(off);
102         this.bb = bb;
103         this.bs = bs;
104         return read(bb);
105     }
106 
read(ByteBuffer bb)107     protected int read(ByteBuffer bb)
108         throws IOException
109     {
110         return ChannelInputStream.read(ch, bb, true);
111     }
112 
available()113     public int available() throws IOException {
114         // special case where the channel is to a file
115         if (ch instanceof SeekableByteChannel) {
116             SeekableByteChannel sbc = (SeekableByteChannel)ch;
117             long rem = Math.max(0, sbc.size() - sbc.position());
118             return (rem > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int)rem;
119         }
120         return 0;
121     }
122 
close()123     public void close() throws IOException {
124         ch.close();
125     }
126 
127 }
128