author | xuelei |
Sat, 24 Nov 2012 04:09:19 -0800 | |
changeset 14664 | e71aa0962e70 |
parent 5506 | 202f599c92aa |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 2009, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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 |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.ssl; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.nio.*; |
|
30 |
||
31 |
/** |
|
32 |
* A simple InputStream which uses ByteBuffers as it's backing store. |
|
33 |
* <P> |
|
34 |
* The only IOException should come if the InputStream has been closed. |
|
35 |
* All other IOException should not occur because all the data is local. |
|
36 |
* Data reads on an exhausted ByteBuffer returns a -1. |
|
37 |
* |
|
38 |
* @author Brad Wetmore |
|
39 |
*/ |
|
40 |
class ByteBufferInputStream extends InputStream { |
|
41 |
||
42 |
ByteBuffer bb; |
|
43 |
||
44 |
ByteBufferInputStream(ByteBuffer bb) { |
|
45 |
this.bb = bb; |
|
46 |
} |
|
47 |
||
48 |
/** |
|
49 |
* Returns a byte from the ByteBuffer. |
|
50 |
* |
|
51 |
* Increments position(). |
|
52 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
5506
diff
changeset
|
53 |
@Override |
2 | 54 |
public int read() throws IOException { |
55 |
||
56 |
if (bb == null) { |
|
57 |
throw new IOException("read on a closed InputStream"); |
|
58 |
} |
|
59 |
||
60 |
if (bb.remaining() == 0) { |
|
61 |
return -1; |
|
62 |
} |
|
63 |
return bb.get(); |
|
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* Returns a byte array from the ByteBuffer. |
|
68 |
* |
|
69 |
* Increments position(). |
|
70 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
5506
diff
changeset
|
71 |
@Override |
2 | 72 |
public int read(byte b[]) throws IOException { |
73 |
||
74 |
if (bb == null) { |
|
75 |
throw new IOException("read on a closed InputStream"); |
|
76 |
} |
|
77 |
||
78 |
return read(b, 0, b.length); |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Returns a byte array from the ByteBuffer. |
|
83 |
* |
|
84 |
* Increments position(). |
|
85 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
5506
diff
changeset
|
86 |
@Override |
2 | 87 |
public int read(byte b[], int off, int len) throws IOException { |
88 |
||
89 |
if (bb == null) { |
|
90 |
throw new IOException("read on a closed InputStream"); |
|
91 |
} |
|
92 |
||
93 |
if (b == null) { |
|
94 |
throw new NullPointerException(); |
|
2061 | 95 |
} else if (off < 0 || len < 0 || len > b.length - off) { |
2 | 96 |
throw new IndexOutOfBoundsException(); |
97 |
} else if (len == 0) { |
|
98 |
return 0; |
|
99 |
} |
|
100 |
||
101 |
int length = Math.min(bb.remaining(), len); |
|
102 |
if (length == 0) { |
|
103 |
return -1; |
|
104 |
} |
|
105 |
||
106 |
bb.get(b, off, length); |
|
107 |
return length; |
|
108 |
} |
|
109 |
||
110 |
/** |
|
111 |
* Skips over and discards <code>n</code> bytes of data from this input |
|
112 |
* stream. |
|
113 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
5506
diff
changeset
|
114 |
@Override |
2 | 115 |
public long skip(long n) throws IOException { |
116 |
||
117 |
if (bb == null) { |
|
118 |
throw new IOException("skip on a closed InputStream"); |
|
119 |
} |
|
120 |
||
121 |
if (n <= 0) { |
|
122 |
return 0; |
|
123 |
} |
|
124 |
||
125 |
/* |
|
126 |
* ByteBuffers have at most an int, so lose the upper bits. |
|
127 |
* The contract allows this. |
|
128 |
*/ |
|
129 |
int nInt = (int) n; |
|
130 |
int skip = Math.min(bb.remaining(), nInt); |
|
131 |
||
132 |
bb.position(bb.position() + skip); |
|
133 |
||
134 |
return nInt; |
|
135 |
} |
|
136 |
||
137 |
/** |
|
138 |
* Returns the number of bytes that can be read (or skipped over) |
|
139 |
* from this input stream without blocking by the next caller of a |
|
140 |
* method for this input stream. |
|
141 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
5506
diff
changeset
|
142 |
@Override |
2 | 143 |
public int available() throws IOException { |
144 |
||
145 |
if (bb == null) { |
|
146 |
throw new IOException("available on a closed InputStream"); |
|
147 |
} |
|
148 |
||
149 |
return bb.remaining(); |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Closes this input stream and releases any system resources associated |
|
154 |
* with the stream. |
|
155 |
* |
|
156 |
* @exception IOException if an I/O error occurs. |
|
157 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
5506
diff
changeset
|
158 |
@Override |
2 | 159 |
public void close() throws IOException { |
160 |
bb = null; |
|
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
* Marks the current position in this input stream. |
|
165 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
5506
diff
changeset
|
166 |
@Override |
2 | 167 |
public synchronized void mark(int readlimit) {} |
168 |
||
169 |
/** |
|
170 |
* Repositions this stream to the position at the time the |
|
171 |
* <code>mark</code> method was last called on this input stream. |
|
172 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
5506
diff
changeset
|
173 |
@Override |
2 | 174 |
public synchronized void reset() throws IOException { |
175 |
throw new IOException("mark/reset not supported"); |
|
176 |
} |
|
177 |
||
178 |
/** |
|
179 |
* Tests if this input stream supports the <code>mark</code> and |
|
180 |
* <code>reset</code> methods. |
|
181 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
5506
diff
changeset
|
182 |
@Override |
2 | 183 |
public boolean markSupported() { |
184 |
return false; |
|
185 |
} |
|
186 |
} |