jdk/src/share/classes/sun/nio/ch/IOUtil.java
author alanb
Wed, 06 Apr 2011 20:51:55 +0100
changeset 9237 cbb5753e87e7
parent 7668 d4a77089c587
child 11823 ee83ae88512d
permissions -rw-r--r--
7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown Reviewed-by: forax
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6520
diff changeset
     2
 * Copyright (c) 2000, 2010, 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);
9237
cbb5753e87e7 7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents: 7668
diff changeset
    53
        ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            bb.put(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            // Do not update src until we see how many bytes were written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            src.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            int n = writeFromNativeBuffer(fd, bb, position, nd, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                // now update src
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                src.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        } finally {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    67
            Util.offerFirstTemporaryDirectBuffer(bb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static int writeFromNativeBuffer(FileDescriptor fd, ByteBuffer bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                           long position, NativeDispatcher nd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                             Object lock)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        int written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (rem == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (position != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            written = nd.pwrite(fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                ((DirectBuffer)bb).address() + pos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                rem, position, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            written = nd.write(fd, ((DirectBuffer)bb).address() + pos, rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (written > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            bb.position(pos + written);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static long write(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    99
        return write(fd, bufs, 0, bufs.length, nd);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   100
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   102
    static long write(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   103
                      NativeDispatcher nd)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   104
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   105
    {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   106
        IOVecWrapper vec = IOVecWrapper.get(length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   108
        boolean completed = false;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   109
        int iov_len = 0;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   110
        try {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   111
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   112
            // Iterate over buffers to populate native iovec array.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   113
            int count = offset + length;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   114
            for (int i=offset; i<count; i++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   115
                ByteBuffer buf = bufs[i];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   116
                int pos = buf.position();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   117
                int lim = buf.limit();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   118
                assert (pos <= lim);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   119
                int rem = (pos <= lim ? lim - pos : 0);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   120
                if (rem > 0) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   121
                    vec.setBuffer(iov_len, buf, pos, rem);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   122
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   123
                    // allocate shadow buffer to ensure I/O is done with direct buffer
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   124
                    if (!(buf instanceof DirectBuffer)) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   125
                        ByteBuffer shadow = Util.getTemporaryDirectBuffer(rem);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   126
                        shadow.put(buf);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   127
                        shadow.flip();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   128
                        vec.setShadow(iov_len, shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   129
                        buf.position(pos);  // temporarily restore position in user buffer
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   130
                        buf = shadow;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   131
                        pos = shadow.position();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   132
                    }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   133
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   134
                    vec.putBase(iov_len, ((DirectBuffer)buf).address() + pos);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   135
                    vec.putLen(iov_len, rem);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   136
                    iov_len++;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   137
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   138
            }
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   139
            if (iov_len == 0)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   140
                return 0L;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   141
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   142
            long bytesWritten = nd.writev(fd, vec.address, iov_len);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   143
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   144
            // Notify the buffers how many bytes were taken
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   145
            long left = bytesWritten;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   146
            for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   147
                if (left > 0) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   148
                    ByteBuffer buf = vec.getBuffer(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   149
                    int pos = vec.getPosition(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   150
                    int rem = vec.getRemaining(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   151
                    int n = (left > rem) ? rem : (int)left;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   152
                    buf.position(pos + n);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   153
                    left -= n;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   154
                }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   155
                // return shadow buffers to buffer pool
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   156
                ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   157
                if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   158
                    Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   159
                vec.clearRefs(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   160
            }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   161
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   162
            completed = true;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   163
            return bytesWritten;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   164
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   165
        } finally {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   166
            // 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
   167
            // buffers to cache
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   168
            if (!completed) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   169
                for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   170
                    ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   171
                    if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   172
                        Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   173
                    vec.clearRefs(j);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   174
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
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
    static int read(FileDescriptor fd, ByteBuffer dst, long position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    NativeDispatcher nd, Object lock)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (dst.isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw new IllegalArgumentException("Read-only buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (dst instanceof DirectBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return readIntoNativeBuffer(fd, dst, position, nd, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // Substitute a native buffer
9237
cbb5753e87e7 7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents: 7668
diff changeset
   189
        ByteBuffer bb = Util.getTemporaryDirectBuffer(dst.remaining());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            int n = readIntoNativeBuffer(fd, bb, position, nd, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                dst.put(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } finally {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   197
            Util.offerFirstTemporaryDirectBuffer(bb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private static int readIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                            long position, NativeDispatcher nd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                            Object lock)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (rem == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (position != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            n = nd.pread(fd, ((DirectBuffer)bb).address() + pos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                         rem, position, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            n = nd.read(fd, ((DirectBuffer)bb).address() + pos, rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            bb.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    static long read(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   228
        return read(fd, bufs, 0, bufs.length, nd);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   229
    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   230
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   231
    static long read(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   232
                     NativeDispatcher nd)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   233
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   234
    {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   235
        IOVecWrapper vec = IOVecWrapper.get(length);
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
        boolean completed = false;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   238
        int iov_len = 0;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   239
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   241
            // Iterate over buffers to populate native iovec array.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   242
            int count = offset + length;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   243
            for (int i=offset; i<count; i++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   244
                ByteBuffer buf = bufs[i];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   245
                if (buf.isReadOnly())
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   246
                    throw new IllegalArgumentException("Read-only buffer");
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   247
                int pos = buf.position();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   248
                int lim = buf.limit();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   249
                assert (pos <= lim);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   250
                int rem = (pos <= lim ? lim - pos : 0);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   251
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   252
                if (rem > 0) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   253
                    vec.setBuffer(iov_len, buf, pos, rem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   255
                    // allocate shadow buffer to ensure I/O is done with direct buffer
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   256
                    if (!(buf instanceof DirectBuffer)) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   257
                        ByteBuffer shadow = Util.getTemporaryDirectBuffer(rem);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   258
                        vec.setShadow(iov_len, shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   259
                        buf = shadow;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   260
                        pos = shadow.position();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   261
                    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   262
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   263
                    vec.putBase(iov_len, ((DirectBuffer)buf).address() + pos);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   264
                    vec.putLen(iov_len, rem);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   265
                    iov_len++;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   266
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   268
            if (iov_len == 0)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   269
                return 0L;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   270
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   271
            long bytesRead = nd.readv(fd, vec.address, iov_len);
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
            // Notify the buffers how many bytes were read
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   274
            long left = bytesRead;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   275
            for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   276
                ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   277
                if (left > 0) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   278
                    ByteBuffer buf = vec.getBuffer(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   279
                    int rem = vec.getRemaining(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   280
                    int n = (left > rem) ? rem : (int)left;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   281
                    if (shadow == null) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   282
                        int pos = vec.getPosition(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   283
                        buf.position(pos + n);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   284
                    } else {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   285
                        shadow.limit(shadow.position() + n);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   286
                        buf.put(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   287
                    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   288
                    left -= n;
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
                if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   291
                    Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   292
                vec.clearRefs(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   293
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   295
            completed = true;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   296
            return bytesRead;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   298
        } finally {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   299
            // 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
   300
            // buffers to cache
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   301
            if (!completed) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   302
                for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   303
                    ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   304
                    if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   305
                        Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   306
                    vec.clearRefs(j);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   307
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    static FileDescriptor newFD(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        FileDescriptor fd = new FileDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        setfdVal(fd, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    static native boolean randomBytes(byte[] someBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 6301
diff changeset
   320
    /**
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 6301
diff changeset
   321
     * 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
   322
     * 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
   323
     * 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
   324
     */
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 6301
diff changeset
   325
    static native long makePipe(boolean blocking);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    static native boolean drain(int fd) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    static native void configureBlocking(FileDescriptor fd, boolean blocking)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    static native int fdVal(FileDescriptor fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    static native void setfdVal(FileDescriptor fd, int value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        // Note that IOUtil.initIDs is called from within Util.load.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        Util.load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
}