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 public class IOUtil {
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;
106 if (rem == 0)
107 return 0;
108 if (position != -1) {
109 written = nd.pwrite(fd,
110 ((DirectBuffer)bb).address() + pos,
111 rem, position);
112 } else {
113 written = nd.write(fd, ((DirectBuffer)bb).address() + pos, rem);
114 }
115 if (written > 0)
116 bb.position(pos + written);
117 return written;
118 }
119
120 static long write(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd)
121 throws IOException
122 {
123 return write(fd, bufs, 0, bufs.length, false, -1, nd);
124 }
125
126 static long write(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
127 NativeDispatcher nd)
128 throws IOException
129 {
130 return write(fd, bufs, offset, length, false, -1, nd);
131 }
132
133 static long write(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
134 boolean directIO, int alignment, NativeDispatcher nd)
135 throws IOException
136 {
137 IOVecWrapper vec = IOVecWrapper.get(length);
138
139 boolean completed = false;
140 int iov_len = 0;
141 try {
142
143 // Iterate over buffers to populate native iovec array.
144 int count = offset + length;
145 int i = offset;
146 while (i < count && iov_len < IOV_MAX) {
147 ByteBuffer buf = bufs[i];
199 vec.clearRefs(j);
200 }
201
202 completed = true;
203 return bytesWritten;
204
205 } finally {
206 // if an error occurred then clear refs to buffers and return any shadow
207 // buffers to cache
208 if (!completed) {
209 for (int j=0; j<iov_len; j++) {
210 ByteBuffer shadow = vec.getShadow(j);
211 if (shadow != null)
212 Util.offerLastTemporaryDirectBuffer(shadow);
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);
269
270 if (rem == 0)
271 return 0;
272 int n = 0;
273 if (position != -1) {
274 n = nd.pread(fd, ((DirectBuffer)bb).address() + pos, rem, position);
275 } else {
276 n = nd.read(fd, ((DirectBuffer)bb).address() + pos, rem);
277 }
278 if (n > 0)
279 bb.position(pos + n);
280 return n;
281 }
282
283 static long read(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd)
284 throws IOException
285 {
286 return read(fd, bufs, 0, bufs.length, false, -1, nd);
287 }
288
289 static long read(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
290 NativeDispatcher nd)
291 throws IOException
292 {
293 return read(fd, bufs, offset, length, false, -1, nd);
294 }
295
296 static long read(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
297 boolean directIO, int alignment, NativeDispatcher nd)
298 throws IOException
299 {
300 IOVecWrapper vec = IOVecWrapper.get(length);
301
302 boolean completed = false;
303 int iov_len = 0;
304 try {
305
306 // Iterate over buffers to populate native iovec array.
307 int count = offset + length;
308 int i = offset;
309 while (i < count && iov_len < IOV_MAX) {
310 ByteBuffer buf = bufs[i];
|
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 public class IOUtil {
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 public 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;
106 if (rem == 0)
107 return 0;
108 if (position != -1) {
109 written = nd.pwrite(fd,
110 ((DirectBuffer)bb).address() + pos,
111 rem, position);
112 } else {
113 written = nd.write(fd, ((DirectBuffer)bb).address() + pos, rem);
114 }
115 if (written > 0)
116 bb.position(pos + written);
117 return written;
118 }
119
120 static long write(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd)
121 throws IOException
122 {
123 return write(fd, bufs, 0, bufs.length, false, -1, nd);
124 }
125
126 public static long write(FileDescriptor fd, ByteBuffer[] bufs, int offset,
127 int length, NativeDispatcher nd)
128 throws IOException
129 {
130 return write(fd, bufs, offset, length, false, -1, nd);
131 }
132
133 static long write(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
134 boolean directIO, int alignment, NativeDispatcher nd)
135 throws IOException
136 {
137 IOVecWrapper vec = IOVecWrapper.get(length);
138
139 boolean completed = false;
140 int iov_len = 0;
141 try {
142
143 // Iterate over buffers to populate native iovec array.
144 int count = offset + length;
145 int i = offset;
146 while (i < count && iov_len < IOV_MAX) {
147 ByteBuffer buf = bufs[i];
199 vec.clearRefs(j);
200 }
201
202 completed = true;
203 return bytesWritten;
204
205 } finally {
206 // if an error occurred then clear refs to buffers and return any shadow
207 // buffers to cache
208 if (!completed) {
209 for (int j=0; j<iov_len; j++) {
210 ByteBuffer shadow = vec.getShadow(j);
211 if (shadow != null)
212 Util.offerLastTemporaryDirectBuffer(shadow);
213 vec.clearRefs(j);
214 }
215 }
216 }
217 }
218
219 public 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);
269
270 if (rem == 0)
271 return 0;
272 int n = 0;
273 if (position != -1) {
274 n = nd.pread(fd, ((DirectBuffer)bb).address() + pos, rem, position);
275 } else {
276 n = nd.read(fd, ((DirectBuffer)bb).address() + pos, rem);
277 }
278 if (n > 0)
279 bb.position(pos + n);
280 return n;
281 }
282
283 static long read(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd)
284 throws IOException
285 {
286 return read(fd, bufs, 0, bufs.length, false, -1, nd);
287 }
288
289 public static long read(FileDescriptor fd, ByteBuffer[] bufs, int offset,
290 int length, NativeDispatcher nd)
291 throws IOException
292 {
293 return read(fd, bufs, offset, length, false, -1, nd);
294 }
295
296 static long read(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
297 boolean directIO, int alignment, NativeDispatcher nd)
298 throws IOException
299 {
300 IOVecWrapper vec = IOVecWrapper.get(length);
301
302 boolean completed = false;
303 int iov_len = 0;
304 try {
305
306 // Iterate over buffers to populate native iovec array.
307 int count = offset + length;
308 int i = offset;
309 while (i < count && iov_len < IOV_MAX) {
310 ByteBuffer buf = bufs[i];
|