38
39 /**
40 * Max number of iovec structures that readv/writev supports
41 */
42 static final int IOV_MAX;
43
44 private IOUtil() { } // No instantiation
45
46 static int write(FileDescriptor fd, ByteBuffer src, long position,
47 NativeDispatcher nd)
48 throws IOException
49 {
50 return write(fd, src, position, false, -1, nd);
51 }
52
53 static int write(FileDescriptor fd, ByteBuffer src, long position,
54 boolean directIO, int alignment, NativeDispatcher nd)
55 throws IOException
56 {
57 if (src instanceof DirectBuffer) {
58 return writeFromNativeBuffer(fd, src, position,
59 directIO, alignment, nd);
60 }
61
62 // Substitute a native buffer
63 int pos = src.position();
64 int lim = src.limit();
65 assert (pos <= lim);
66 int rem = (pos <= lim ? lim - pos : 0);
67 ByteBuffer bb;
68 if (directIO) {
69 Util.checkRemainingBufferSizeAligned(rem, alignment);
70 bb = Util.getTemporaryAlignedDirectBuffer(rem, alignment);
71 } else {
72 bb = Util.getTemporaryDirectBuffer(rem);
73 }
74 try {
75 bb.put(src);
76 bb.flip();
77 // Do not update src until we see how many bytes were written
78 src.position(pos);
79
80 int n = writeFromNativeBuffer(fd, bb, position,
81 directIO, alignment, nd);
82 if (n > 0) {
83 // now update src
84 src.position(pos + n);
85 }
86 return n;
87 } finally {
88 Util.offerFirstTemporaryDirectBuffer(bb);
89 }
90 }
91
92 private static int writeFromNativeBuffer(FileDescriptor fd, ByteBuffer bb,
93 long position, boolean directIO,
94 int alignment, NativeDispatcher nd)
95 throws IOException
96 {
97 int pos = bb.position();
98 int lim = bb.limit();
99 assert (pos <= lim);
100 int rem = (pos <= lim ? lim - pos : 0);
101
215 vec.clearRefs(j);
216 }
217 }
218 }
219 }
220
221 static int read(FileDescriptor fd, ByteBuffer dst, long position,
222 NativeDispatcher nd)
223 throws IOException
224 {
225 return read(fd, dst, position, false, -1, nd);
226 }
227
228 static int read(FileDescriptor fd, ByteBuffer dst, long position,
229 boolean directIO, int alignment, NativeDispatcher nd)
230 throws IOException
231 {
232 if (dst.isReadOnly())
233 throw new IllegalArgumentException("Read-only buffer");
234 if (dst instanceof DirectBuffer)
235 return readIntoNativeBuffer(fd, dst, position,
236 directIO, alignment, nd);
237
238 // Substitute a native buffer
239 ByteBuffer bb;
240 int rem = dst.remaining();
241 if (directIO) {
242 Util.checkRemainingBufferSizeAligned(rem, alignment);
243 bb = Util.getTemporaryAlignedDirectBuffer(rem, alignment);
244 } else {
245 bb = Util.getTemporaryDirectBuffer(rem);
246 }
247 try {
248 int n = readIntoNativeBuffer(fd, bb, position,
249 directIO, alignment,nd);
250 bb.flip();
251 if (n > 0)
252 dst.put(bb);
253 return n;
254 } finally {
255 Util.offerFirstTemporaryDirectBuffer(bb);
256 }
257 }
258
259 private static int readIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb,
260 long position, boolean directIO,
261 int alignment, NativeDispatcher nd)
262 throws IOException
263 {
264 int pos = bb.position();
265 int lim = bb.limit();
266 assert (pos <= lim);
267 int rem = (pos <= lim ? lim - pos : 0);
268
269 if (directIO) {
|
38
39 /**
40 * Max number of iovec structures that readv/writev supports
41 */
42 static final int IOV_MAX;
43
44 private IOUtil() { } // No instantiation
45
46 static int write(FileDescriptor fd, ByteBuffer src, long position,
47 NativeDispatcher nd)
48 throws IOException
49 {
50 return write(fd, src, position, false, -1, nd);
51 }
52
53 static int write(FileDescriptor fd, ByteBuffer src, long position,
54 boolean directIO, int alignment, NativeDispatcher nd)
55 throws IOException
56 {
57 if (src instanceof DirectBuffer) {
58 return writeFromNativeBuffer(fd, src, position, directIO, alignment, nd);
59 }
60
61 // Substitute a native buffer
62 int pos = src.position();
63 int lim = src.limit();
64 assert (pos <= lim);
65 int rem = (pos <= lim ? lim - pos : 0);
66 ByteBuffer bb;
67 if (directIO) {
68 Util.checkRemainingBufferSizeAligned(rem, alignment);
69 bb = Util.getTemporaryAlignedDirectBuffer(rem, alignment);
70 } else {
71 bb = Util.getTemporaryDirectBuffer(rem);
72 }
73 try {
74 bb.put(src);
75 bb.flip();
76 // Do not update src until we see how many bytes were written
77 src.position(pos);
78
79 int n = writeFromNativeBuffer(fd, bb, position, directIO, alignment, nd);
80 if (n > 0) {
81 // now update src
82 src.position(pos + n);
83 }
84 return n;
85 } finally {
86 Util.offerFirstTemporaryDirectBuffer(bb);
87 }
88 }
89
90 private static int writeFromNativeBuffer(FileDescriptor fd, ByteBuffer bb,
91 long position, boolean directIO,
92 int alignment, NativeDispatcher nd)
93 throws IOException
94 {
95 int pos = bb.position();
96 int lim = bb.limit();
97 assert (pos <= lim);
98 int rem = (pos <= lim ? lim - pos : 0);
99
213 vec.clearRefs(j);
214 }
215 }
216 }
217 }
218
219 static int read(FileDescriptor fd, ByteBuffer dst, long position,
220 NativeDispatcher nd)
221 throws IOException
222 {
223 return read(fd, dst, position, false, -1, nd);
224 }
225
226 static int read(FileDescriptor fd, ByteBuffer dst, long position,
227 boolean directIO, int alignment, NativeDispatcher nd)
228 throws IOException
229 {
230 if (dst.isReadOnly())
231 throw new IllegalArgumentException("Read-only buffer");
232 if (dst instanceof DirectBuffer)
233 return readIntoNativeBuffer(fd, dst, position, directIO, alignment, nd);
234
235 // Substitute a native buffer
236 ByteBuffer bb;
237 int rem = dst.remaining();
238 if (directIO) {
239 Util.checkRemainingBufferSizeAligned(rem, alignment);
240 bb = Util.getTemporaryAlignedDirectBuffer(rem, alignment);
241 } else {
242 bb = Util.getTemporaryDirectBuffer(rem);
243 }
244 try {
245 int n = readIntoNativeBuffer(fd, bb, position, directIO, alignment,nd);
246 bb.flip();
247 if (n > 0)
248 dst.put(bb);
249 return n;
250 } finally {
251 Util.offerFirstTemporaryDirectBuffer(bb);
252 }
253 }
254
255 private static int readIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb,
256 long position, boolean directIO,
257 int alignment, NativeDispatcher nd)
258 throws IOException
259 {
260 int pos = bb.position();
261 int lim = bb.limit();
262 assert (pos <= lim);
263 int rem = (pos <= lim ? lim - pos : 0);
264
265 if (directIO) {
|