jdk/src/share/classes/sun/nio/ch/IOUtil.java
author martin
Wed, 01 Sep 2010 09:45:08 -0700
changeset 6520 0e7cf575332e
parent 6301 c90a67d75c9f
child 7668 d4a77089c587
permissions -rw-r--r--
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups Summary: Avoid *Critical; fix compile warnings; improve readability Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
     2
 * Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * File-descriptor based I/O utilities that are shared by NIO classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
class IOUtil {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private IOUtil() { }                // No instantiation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static int write(FileDescriptor fd, ByteBuffer src, long position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                     NativeDispatcher nd, Object lock)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if (src instanceof DirectBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            return writeFromNativeBuffer(fd, src, position, nd, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        // Substitute a native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        int pos = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        int lim = src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            bb = Util.getTemporaryDirectBuffer(rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            bb.put(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            // Do not update src until we see how many bytes were written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            src.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            int n = writeFromNativeBuffer(fd, bb, position, nd, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                // now update src
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                src.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        } finally {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    68
            Util.offerFirstTemporaryDirectBuffer(bb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static int writeFromNativeBuffer(FileDescriptor fd, ByteBuffer bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                           long position, NativeDispatcher nd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                             Object lock)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (rem == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (position != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            written = nd.pwrite(fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                ((DirectBuffer)bb).address() + pos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                rem, position, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            written = nd.write(fd, ((DirectBuffer)bb).address() + pos, rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (written > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            bb.position(pos + written);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static long write(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   100
        return write(fd, bufs, 0, bufs.length, nd);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   101
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   103
    static long write(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   104
                      NativeDispatcher nd)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   105
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   106
    {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   107
        IOVecWrapper vec = IOVecWrapper.get(length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   109
        boolean completed = false;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   110
        int iov_len = 0;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   111
        try {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   112
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   113
            // Iterate over buffers to populate native iovec array.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   114
            int count = offset + length;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   115
            for (int i=offset; i<count; i++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   116
                ByteBuffer buf = bufs[i];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   117
                int pos = buf.position();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   118
                int lim = buf.limit();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   119
                assert (pos <= lim);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   120
                int rem = (pos <= lim ? lim - pos : 0);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   121
                if (rem > 0) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   122
                    vec.setBuffer(iov_len, buf, pos, rem);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   123
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   124
                    // allocate shadow buffer to ensure I/O is done with direct buffer
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   125
                    if (!(buf instanceof DirectBuffer)) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   126
                        ByteBuffer shadow = Util.getTemporaryDirectBuffer(rem);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   127
                        shadow.put(buf);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   128
                        shadow.flip();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   129
                        vec.setShadow(iov_len, shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   130
                        buf.position(pos);  // temporarily restore position in user buffer
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   131
                        buf = shadow;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   132
                        pos = shadow.position();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   133
                    }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   134
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   135
                    vec.putBase(iov_len, ((DirectBuffer)buf).address() + pos);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   136
                    vec.putLen(iov_len, rem);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   137
                    iov_len++;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   138
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   139
            }
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   140
            if (iov_len == 0)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   141
                return 0L;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   142
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   143
            long bytesWritten = nd.writev(fd, vec.address, iov_len);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   144
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   145
            // Notify the buffers how many bytes were taken
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   146
            long left = bytesWritten;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   147
            for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   148
                if (left > 0) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   149
                    ByteBuffer buf = vec.getBuffer(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   150
                    int pos = vec.getPosition(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   151
                    int rem = vec.getRemaining(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   152
                    int n = (left > rem) ? rem : (int)left;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   153
                    buf.position(pos + n);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   154
                    left -= n;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   155
                }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   156
                // return shadow buffers to buffer pool
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   157
                ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   158
                if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   159
                    Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   160
                vec.clearRefs(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   161
            }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   162
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   163
            completed = true;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   164
            return bytesWritten;
2057
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
        } finally {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   167
            // if an error occurred then clear refs to buffers and return any shadow
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   168
            // buffers to cache
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   169
            if (!completed) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   170
                for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   171
                    ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   172
                    if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   173
                        Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   174
                    vec.clearRefs(j);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   175
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    static int read(FileDescriptor fd, ByteBuffer dst, long position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    NativeDispatcher nd, Object lock)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (dst.isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            throw new IllegalArgumentException("Read-only buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (dst instanceof DirectBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return readIntoNativeBuffer(fd, dst, position, nd, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // Substitute a native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            bb = Util.getTemporaryDirectBuffer(dst.remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            int n = readIntoNativeBuffer(fd, bb, position, nd, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                dst.put(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } finally {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   199
            Util.offerFirstTemporaryDirectBuffer(bb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private static int readIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                            long position, NativeDispatcher nd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                            Object lock)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (rem == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (position != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            n = nd.pread(fd, ((DirectBuffer)bb).address() + pos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                         rem, position, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            n = nd.read(fd, ((DirectBuffer)bb).address() + pos, rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            bb.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    static long read(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   230
        return read(fd, bufs, 0, bufs.length, nd);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   231
    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   232
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   233
    static long read(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   234
                     NativeDispatcher nd)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   235
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   236
    {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   237
        IOVecWrapper vec = IOVecWrapper.get(length);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   238
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   239
        boolean completed = false;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   240
        int iov_len = 0;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   241
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   243
            // Iterate over buffers to populate native iovec array.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   244
            int count = offset + length;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   245
            for (int i=offset; i<count; i++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   246
                ByteBuffer buf = bufs[i];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   247
                if (buf.isReadOnly())
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   248
                    throw new IllegalArgumentException("Read-only buffer");
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   249
                int pos = buf.position();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   250
                int lim = buf.limit();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   251
                assert (pos <= lim);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   252
                int rem = (pos <= lim ? lim - pos : 0);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   253
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   254
                if (rem > 0) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   255
                    vec.setBuffer(iov_len, buf, pos, rem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   257
                    // allocate shadow buffer to ensure I/O is done with direct buffer
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   258
                    if (!(buf instanceof DirectBuffer)) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   259
                        ByteBuffer shadow = Util.getTemporaryDirectBuffer(rem);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   260
                        vec.setShadow(iov_len, shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   261
                        buf = shadow;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   262
                        pos = shadow.position();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   263
                    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   264
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   265
                    vec.putBase(iov_len, ((DirectBuffer)buf).address() + pos);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   266
                    vec.putLen(iov_len, rem);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   267
                    iov_len++;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   268
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   270
            if (iov_len == 0)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   271
                return 0L;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   272
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   273
            long bytesRead = nd.readv(fd, vec.address, iov_len);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   274
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   275
            // Notify the buffers how many bytes were read
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   276
            long left = bytesRead;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   277
            for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   278
                ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   279
                if (left > 0) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   280
                    ByteBuffer buf = vec.getBuffer(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   281
                    int rem = vec.getRemaining(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   282
                    int n = (left > rem) ? rem : (int)left;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   283
                    if (shadow == null) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   284
                        int pos = vec.getPosition(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   285
                        buf.position(pos + n);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   286
                    } else {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   287
                        shadow.limit(shadow.position() + n);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   288
                        buf.put(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   289
                    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   290
                    left -= n;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   291
                }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   292
                if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   293
                    Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   294
                vec.clearRefs(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   295
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   297
            completed = true;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   298
            return bytesRead;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   300
        } finally {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   301
            // if an error occurred then clear refs to buffers and return any shadow
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   302
            // buffers to cache
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   303
            if (!completed) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   304
                for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   305
                    ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   306
                    if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   307
                        Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   308
                    vec.clearRefs(j);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   309
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    static FileDescriptor newFD(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        FileDescriptor fd = new FileDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        setfdVal(fd, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    static native boolean randomBytes(byte[] someBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 6301
diff changeset
   322
    /**
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 6301
diff changeset
   323
     * Returns two file descriptors for a pipe encoded in a long.
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 6301
diff changeset
   324
     * The read end of the pipe is returned in the high 32 bits,
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 6301
diff changeset
   325
     * while the write end is returned in the low 32 bits.
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 6301
diff changeset
   326
     */
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 6301
diff changeset
   327
    static native long makePipe(boolean blocking);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    static native boolean drain(int fd) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    static native void configureBlocking(FileDescriptor fd, boolean blocking)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    static native int fdVal(FileDescriptor fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    static native void setfdVal(FileDescriptor fd, int value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        // Note that IOUtil.initIDs is called from within Util.load.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        Util.load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
}