author | alanb |
Sun, 15 Feb 2009 12:25:54 +0000 | |
changeset 2057 | 3acf8e5e2ca0 |
parent 2 | 90ce3da70b43 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
2 |
* Copyright 2000-2009 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.nio.ch; |
|
27 |
||
28 |
import java.io.FileDescriptor; |
|
29 |
import java.io.IOException; |
|
30 |
import java.nio.ByteBuffer; |
|
31 |
||
32 |
||
33 |
/** |
|
34 |
* File-descriptor based I/O utilities that are shared by NIO classes. |
|
35 |
*/ |
|
36 |
||
37 |
class IOUtil { |
|
38 |
||
39 |
private IOUtil() { } // No instantiation |
|
40 |
||
41 |
/* |
|
42 |
* Returns the index of first buffer in bufs with remaining, |
|
43 |
* or -1 if there is nothing left |
|
44 |
*/ |
|
45 |
private static int remaining(ByteBuffer[] bufs) { |
|
46 |
int numBufs = bufs.length; |
|
47 |
for (int i=0; i<numBufs; i++) { |
|
48 |
if (bufs[i].hasRemaining()) { |
|
49 |
return i; |
|
50 |
} |
|
51 |
} |
|
52 |
return -1; |
|
53 |
} |
|
54 |
||
55 |
/* |
|
56 |
* Returns a new ByteBuffer array with only unfinished buffers in it |
|
57 |
*/ |
|
58 |
private static ByteBuffer[] skipBufs(ByteBuffer[] bufs, |
|
59 |
int nextWithRemaining) |
|
60 |
{ |
|
61 |
int newSize = bufs.length - nextWithRemaining; |
|
62 |
ByteBuffer[] temp = new ByteBuffer[newSize]; |
|
63 |
for (int i=0; i<newSize; i++) { |
|
64 |
temp[i] = bufs[i + nextWithRemaining]; |
|
65 |
} |
|
66 |
return temp; |
|
67 |
} |
|
68 |
||
69 |
static int write(FileDescriptor fd, ByteBuffer src, long position, |
|
70 |
NativeDispatcher nd, Object lock) |
|
71 |
throws IOException |
|
72 |
{ |
|
73 |
if (src instanceof DirectBuffer) |
|
74 |
return writeFromNativeBuffer(fd, src, position, nd, lock); |
|
75 |
||
76 |
// Substitute a native buffer |
|
77 |
int pos = src.position(); |
|
78 |
int lim = src.limit(); |
|
79 |
assert (pos <= lim); |
|
80 |
int rem = (pos <= lim ? lim - pos : 0); |
|
81 |
ByteBuffer bb = null; |
|
82 |
try { |
|
83 |
bb = Util.getTemporaryDirectBuffer(rem); |
|
84 |
bb.put(src); |
|
85 |
bb.flip(); |
|
86 |
// Do not update src until we see how many bytes were written |
|
87 |
src.position(pos); |
|
88 |
||
89 |
int n = writeFromNativeBuffer(fd, bb, position, nd, lock); |
|
90 |
if (n > 0) { |
|
91 |
// now update src |
|
92 |
src.position(pos + n); |
|
93 |
} |
|
94 |
return n; |
|
95 |
} finally { |
|
96 |
Util.releaseTemporaryDirectBuffer(bb); |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
private static int writeFromNativeBuffer(FileDescriptor fd, ByteBuffer bb, |
|
101 |
long position, NativeDispatcher nd, |
|
102 |
Object lock) |
|
103 |
throws IOException |
|
104 |
{ |
|
105 |
int pos = bb.position(); |
|
106 |
int lim = bb.limit(); |
|
107 |
assert (pos <= lim); |
|
108 |
int rem = (pos <= lim ? lim - pos : 0); |
|
109 |
||
110 |
int written = 0; |
|
111 |
if (rem == 0) |
|
112 |
return 0; |
|
113 |
if (position != -1) { |
|
114 |
written = nd.pwrite(fd, |
|
115 |
((DirectBuffer)bb).address() + pos, |
|
116 |
rem, position, lock); |
|
117 |
} else { |
|
118 |
written = nd.write(fd, ((DirectBuffer)bb).address() + pos, rem); |
|
119 |
} |
|
120 |
if (written > 0) |
|
121 |
bb.position(pos + written); |
|
122 |
return written; |
|
123 |
} |
|
124 |
||
125 |
static long write(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd) |
|
126 |
throws IOException |
|
127 |
{ |
|
128 |
int nextWithRemaining = remaining(bufs); |
|
129 |
// if all bufs are empty we should return immediately |
|
130 |
if (nextWithRemaining < 0) |
|
131 |
return 0; |
|
132 |
// If some bufs are empty we should skip them |
|
133 |
if (nextWithRemaining > 0) |
|
134 |
bufs = skipBufs(bufs, nextWithRemaining); |
|
135 |
||
136 |
int numBufs = bufs.length; |
|
137 |
||
138 |
// Create shadow to ensure DirectByteBuffers are used |
|
139 |
ByteBuffer[] shadow = new ByteBuffer[numBufs]; |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
140 |
try { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
141 |
for (int i=0; i<numBufs; i++) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
142 |
if (!(bufs[i] instanceof DirectBuffer)) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
143 |
int pos = bufs[i].position(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
144 |
int lim = bufs[i].limit(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
145 |
assert (pos <= lim); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
146 |
int rem = (pos <= lim ? lim - pos : 0); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
147 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
148 |
ByteBuffer bb = Util.getTemporaryDirectBuffer(rem); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
149 |
shadow[i] = bb; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
150 |
// Leave slow buffer position untouched; it will be updated |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
151 |
// after we see how many bytes were really written out |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
152 |
bb.put(bufs[i]); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
153 |
bufs[i].position(pos); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
154 |
bb.flip(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
155 |
} else { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
156 |
shadow[i] = bufs[i]; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
157 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
158 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
159 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
160 |
IOVecWrapper vec = null; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
161 |
long bytesWritten = 0; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
162 |
try { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
163 |
// Create a native iovec array |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
164 |
vec= new IOVecWrapper(numBufs); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
165 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
166 |
// Fill in the iovec array with appropriate data |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
167 |
for (int i=0; i<numBufs; i++) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
168 |
ByteBuffer nextBuffer = shadow[i]; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
169 |
// put in the buffer addresses |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
170 |
long pos = nextBuffer.position(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
171 |
long len = nextBuffer.limit() - pos; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
172 |
vec.putBase(i, ((DirectBuffer)nextBuffer).address() + pos); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
173 |
vec.putLen(i, len); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
174 |
} |
2 | 175 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
176 |
// Invoke native call to fill the buffers |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
177 |
bytesWritten = nd.writev(fd, vec.address, numBufs); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
178 |
} finally { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
179 |
vec.free(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
180 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
181 |
long returnVal = bytesWritten; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
182 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
183 |
// Notify the buffers how many bytes were taken |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
184 |
for (int i=0; i<numBufs; i++) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
185 |
ByteBuffer nextBuffer = bufs[i]; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
186 |
int pos = nextBuffer.position(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
187 |
int lim = nextBuffer.limit(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
188 |
assert (pos <= lim); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
189 |
int len = (pos <= lim ? lim - pos : lim); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
190 |
if (bytesWritten >= len) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
191 |
bytesWritten -= len; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
192 |
int newPosition = pos + len; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
193 |
nextBuffer.position(newPosition); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
194 |
} else { // Buffers not completely filled |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
195 |
if (bytesWritten > 0) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
196 |
assert(pos + bytesWritten < (long)Integer.MAX_VALUE); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
197 |
int newPosition = (int)(pos + bytesWritten); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
198 |
nextBuffer.position(newPosition); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
199 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
200 |
break; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
201 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
202 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
203 |
return returnVal; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
204 |
} finally { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
205 |
// return any substituted buffers to cache |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
206 |
for (int i=0; i<numBufs; i++) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
207 |
ByteBuffer bb = shadow[i]; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
208 |
if (bb != null && bb != bufs[i]) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
209 |
Util.releaseTemporaryDirectBuffer(bb); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
210 |
} |
2 | 211 |
} |
212 |
} |
|
213 |
} |
|
214 |
||
215 |
static int read(FileDescriptor fd, ByteBuffer dst, long position, |
|
216 |
NativeDispatcher nd, Object lock) |
|
217 |
throws IOException |
|
218 |
{ |
|
219 |
if (dst.isReadOnly()) |
|
220 |
throw new IllegalArgumentException("Read-only buffer"); |
|
221 |
if (dst instanceof DirectBuffer) |
|
222 |
return readIntoNativeBuffer(fd, dst, position, nd, lock); |
|
223 |
||
224 |
// Substitute a native buffer |
|
225 |
ByteBuffer bb = null; |
|
226 |
try { |
|
227 |
bb = Util.getTemporaryDirectBuffer(dst.remaining()); |
|
228 |
int n = readIntoNativeBuffer(fd, bb, position, nd, lock); |
|
229 |
bb.flip(); |
|
230 |
if (n > 0) |
|
231 |
dst.put(bb); |
|
232 |
return n; |
|
233 |
} finally { |
|
234 |
Util.releaseTemporaryDirectBuffer(bb); |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
private static int readIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb, |
|
239 |
long position, NativeDispatcher nd, |
|
240 |
Object lock) |
|
241 |
throws IOException |
|
242 |
{ |
|
243 |
int pos = bb.position(); |
|
244 |
int lim = bb.limit(); |
|
245 |
assert (pos <= lim); |
|
246 |
int rem = (pos <= lim ? lim - pos : 0); |
|
247 |
||
248 |
if (rem == 0) |
|
249 |
return 0; |
|
250 |
int n = 0; |
|
251 |
if (position != -1) { |
|
252 |
n = nd.pread(fd, ((DirectBuffer)bb).address() + pos, |
|
253 |
rem, position, lock); |
|
254 |
} else { |
|
255 |
n = nd.read(fd, ((DirectBuffer)bb).address() + pos, rem); |
|
256 |
} |
|
257 |
if (n > 0) |
|
258 |
bb.position(pos + n); |
|
259 |
return n; |
|
260 |
} |
|
261 |
||
262 |
static long read(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd) |
|
263 |
throws IOException |
|
264 |
{ |
|
265 |
int nextWithRemaining = remaining(bufs); |
|
266 |
// if all bufs are empty we should return immediately |
|
267 |
if (nextWithRemaining < 0) |
|
268 |
return 0; |
|
269 |
// If some bufs are empty we should skip them |
|
270 |
if (nextWithRemaining > 0) |
|
271 |
bufs = skipBufs(bufs, nextWithRemaining); |
|
272 |
||
273 |
int numBufs = bufs.length; |
|
274 |
||
275 |
// Read into the shadow to ensure DirectByteBuffers are used |
|
276 |
ByteBuffer[] shadow = new ByteBuffer[numBufs]; |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
277 |
boolean usingSlowBuffers = false; |
2 | 278 |
try { |
279 |
for (int i=0; i<numBufs; i++) { |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
280 |
if (bufs[i].isReadOnly()) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
281 |
throw new IllegalArgumentException("Read-only buffer"); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
282 |
if (!(bufs[i] instanceof DirectBuffer)) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
283 |
shadow[i] = Util.getTemporaryDirectBuffer(bufs[i].remaining()); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
284 |
usingSlowBuffers = true; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
285 |
} else { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
286 |
shadow[i] = bufs[i]; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
287 |
} |
2 | 288 |
} |
289 |
||
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
290 |
IOVecWrapper vec = null; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
291 |
long bytesRead = 0; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
292 |
try { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
293 |
// Create a native iovec array |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
294 |
vec = new IOVecWrapper(numBufs); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
295 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
296 |
// Fill in the iovec array with appropriate data |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
297 |
for (int i=0; i<numBufs; i++) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
298 |
ByteBuffer nextBuffer = shadow[i]; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
299 |
// put in the buffer addresses |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
300 |
long pos = nextBuffer.position(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
301 |
long len = nextBuffer.remaining(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
302 |
vec.putBase(i, ((DirectBuffer)nextBuffer).address() + pos); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
303 |
vec.putLen(i, len); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
304 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
305 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
306 |
// Invoke native call to fill the buffers |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
307 |
bytesRead = nd.readv(fd, vec.address, numBufs); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
308 |
} finally { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
309 |
vec.free(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
310 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
311 |
long returnVal = bytesRead; |
2 | 312 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
313 |
// Notify the buffers how many bytes were read |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
314 |
for (int i=0; i<numBufs; i++) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
315 |
ByteBuffer nextBuffer = shadow[i]; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
316 |
// Note: should this have been cached from above? |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
317 |
int pos = nextBuffer.position(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
318 |
int len = nextBuffer.remaining(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
319 |
if (bytesRead >= len) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
320 |
bytesRead -= len; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
321 |
int newPosition = pos + len; |
2 | 322 |
nextBuffer.position(newPosition); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
323 |
} else { // Buffers not completely filled |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
324 |
if (bytesRead > 0) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
325 |
assert(pos + bytesRead < (long)Integer.MAX_VALUE); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
326 |
int newPosition = (int)(pos + bytesRead); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
327 |
nextBuffer.position(newPosition); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
328 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
329 |
break; |
2 | 330 |
} |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
331 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
332 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
333 |
// Put results from shadow into the slow buffers |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
334 |
if (usingSlowBuffers) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
335 |
for (int i=0; i<numBufs; i++) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
336 |
if (!(bufs[i] instanceof DirectBuffer)) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
337 |
shadow[i].flip(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
338 |
bufs[i].put(shadow[i]); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
339 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
340 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
341 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
342 |
return returnVal; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
343 |
} finally { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
344 |
// return any substituted buffers to cache |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
345 |
if (usingSlowBuffers) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
346 |
for (int i=0; i<numBufs; i++) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
347 |
ByteBuffer bb = shadow[i]; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
348 |
if (bb != null && bb != bufs[i]) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
349 |
Util.releaseTemporaryDirectBuffer(bb); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
350 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
351 |
} |
2 | 352 |
} |
353 |
} |
|
354 |
} |
|
355 |
||
356 |
static FileDescriptor newFD(int i) { |
|
357 |
FileDescriptor fd = new FileDescriptor(); |
|
358 |
setfdVal(fd, i); |
|
359 |
return fd; |
|
360 |
} |
|
361 |
||
362 |
static native boolean randomBytes(byte[] someBytes); |
|
363 |
||
364 |
static native void initPipe(int[] fda, boolean blocking); |
|
365 |
||
366 |
static native boolean drain(int fd) throws IOException; |
|
367 |
||
368 |
static native void configureBlocking(FileDescriptor fd, boolean blocking) |
|
369 |
throws IOException; |
|
370 |
||
371 |
static native int fdVal(FileDescriptor fd); |
|
372 |
||
373 |
static native void setfdVal(FileDescriptor fd, int value); |
|
374 |
||
375 |
static native void initIDs(); |
|
376 |
||
377 |
static { |
|
378 |
// Note that IOUtil.initIDs is called from within Util.load. |
|
379 |
Util.load(); |
|
380 |
} |
|
381 |
||
382 |
} |