src/java.base/share/classes/sun/nio/ch/IOUtil.java
author alanb
Wed, 28 Feb 2018 09:54:38 +0000
changeset 49001 ce06058197a4
parent 48750 ffbb784a8873
child 49290 07779973cbe2
permissions -rw-r--r--
8198562: (ch) Separate blocking and non-blocking code paths (part 1) 8198754: (ch) Separate blocking and non-blocking code paths (part 2) Reviewed-by: bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47726
diff changeset
     2
 * Copyright (c) 2000, 2018, 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
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 9237
diff changeset
    37
public class IOUtil {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
13024
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
    39
    /**
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
    40
     * Max number of iovec structures that readv/writev supports
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
    41
     */
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
    42
    static final int IOV_MAX;
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
    43
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private IOUtil() { }                // No instantiation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static int write(FileDescriptor fd, ByteBuffer src, long position,
16921
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents: 14342
diff changeset
    47
                     NativeDispatcher nd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    {
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    50
        return write(fd, src, position, false, -1, nd);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    51
    }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    52
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    53
    static int write(FileDescriptor fd, ByteBuffer src, long position,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    54
                     boolean directIO, int alignment, NativeDispatcher nd)
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    55
        throws IOException
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    56
    {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    57
        if (src instanceof DirectBuffer) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48750
diff changeset
    58
            return writeFromNativeBuffer(fd, src, position, directIO, alignment, nd);
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    59
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        // Substitute a native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        int pos = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        int lim = src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        int rem = (pos <= lim ? lim - pos : 0);
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    66
        ByteBuffer bb;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    67
        if (directIO) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    68
            Util.checkRemainingBufferSizeAligned(rem, alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    69
            bb = Util.getTemporaryAlignedDirectBuffer(rem, alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    70
        } else {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    71
            bb = Util.getTemporaryDirectBuffer(rem);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    72
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            bb.put(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            // Do not update src until we see how many bytes were written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            src.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48750
diff changeset
    79
            int n = writeFromNativeBuffer(fd, bb, position, directIO, alignment, nd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                // now update src
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                src.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } finally {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
    86
            Util.offerFirstTemporaryDirectBuffer(bb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static int writeFromNativeBuffer(FileDescriptor fd, ByteBuffer bb,
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    91
                                             long position, boolean directIO,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    92
                                             int alignment, NativeDispatcher nd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   100
        if (directIO) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   101
            Util.checkBufferPositionAligned(bb, pos, alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   102
            Util.checkRemainingBufferSizeAligned(rem, alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   103
        }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   104
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        int written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (rem == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (position != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            written = nd.pwrite(fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                ((DirectBuffer)bb).address() + pos,
16921
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents: 14342
diff changeset
   111
                                rem, position);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            written = nd.write(fd, ((DirectBuffer)bb).address() + pos, rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (written > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            bb.position(pos + written);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static long write(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    {
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   123
        return write(fd, bufs, 0, bufs.length, false, -1, nd);
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   124
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   126
    static long write(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   127
                      NativeDispatcher nd)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   128
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   129
    {
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   130
        return write(fd, bufs, offset, length, false, -1, nd);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   131
    }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   132
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   133
    static long write(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   134
                      boolean directIO, int alignment, NativeDispatcher nd)
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   135
        throws IOException
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   136
    {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   137
        IOVecWrapper vec = IOVecWrapper.get(length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   139
        boolean completed = false;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   140
        int iov_len = 0;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   141
        try {
6301
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
            // Iterate over buffers to populate native iovec array.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   144
            int count = offset + length;
13024
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   145
            int i = offset;
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   146
            while (i < count && iov_len < IOV_MAX) {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   147
                ByteBuffer buf = bufs[i];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   148
                int pos = buf.position();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   149
                int lim = buf.limit();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   150
                assert (pos <= lim);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   151
                int rem = (pos <= lim ? lim - pos : 0);
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   152
                if (directIO)
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   153
                    Util.checkRemainingBufferSizeAligned(rem, alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   154
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   155
                if (rem > 0) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   156
                    vec.setBuffer(iov_len, buf, pos, rem);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   157
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   158
                    // allocate shadow buffer to ensure I/O is done with direct buffer
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   159
                    if (!(buf instanceof DirectBuffer)) {
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   160
                        ByteBuffer shadow;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   161
                        if (directIO)
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47726
diff changeset
   162
                            shadow = Util.getTemporaryAlignedDirectBuffer(rem, alignment);
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   163
                        else
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   164
                            shadow = Util.getTemporaryDirectBuffer(rem);
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   165
                        shadow.put(buf);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   166
                        shadow.flip();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   167
                        vec.setShadow(iov_len, shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   168
                        buf.position(pos);  // temporarily restore position in user buffer
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   169
                        buf = shadow;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   170
                        pos = shadow.position();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   171
                    }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   172
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   173
                    vec.putBase(iov_len, ((DirectBuffer)buf).address() + pos);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   174
                    vec.putLen(iov_len, rem);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   175
                    iov_len++;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   176
                }
13024
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   177
                i++;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   178
            }
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   179
            if (iov_len == 0)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   180
                return 0L;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   181
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   182
            long bytesWritten = nd.writev(fd, vec.address, iov_len);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   183
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   184
            // Notify the buffers how many bytes were taken
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   185
            long left = bytesWritten;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   186
            for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   187
                if (left > 0) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   188
                    ByteBuffer buf = vec.getBuffer(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   189
                    int pos = vec.getPosition(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   190
                    int rem = vec.getRemaining(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   191
                    int n = (left > rem) ? rem : (int)left;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   192
                    buf.position(pos + n);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   193
                    left -= n;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   194
                }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   195
                // return shadow buffers to buffer pool
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   196
                ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   197
                if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   198
                    Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   199
                vec.clearRefs(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   200
            }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   201
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   202
            completed = true;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   203
            return bytesWritten;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   204
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   205
        } finally {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   206
            // 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
   207
            // buffers to cache
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   208
            if (!completed) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   209
                for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   210
                    ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   211
                    if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   212
                        Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   213
                    vec.clearRefs(j);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   214
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    static int read(FileDescriptor fd, ByteBuffer dst, long position,
16921
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents: 14342
diff changeset
   220
                    NativeDispatcher nd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    {
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   223
        return read(fd, dst, position, false, -1, nd);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   224
    }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   225
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   226
    static int read(FileDescriptor fd, ByteBuffer dst, long position,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   227
                    boolean directIO, int alignment, NativeDispatcher nd)
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   228
        throws IOException
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   229
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (dst.isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new IllegalArgumentException("Read-only buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (dst instanceof DirectBuffer)
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48750
diff changeset
   233
            return readIntoNativeBuffer(fd, dst, position, directIO, alignment, nd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // Substitute a native buffer
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   236
        ByteBuffer bb;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   237
        int rem = dst.remaining();
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   238
        if (directIO) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   239
            Util.checkRemainingBufferSizeAligned(rem, alignment);
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47726
diff changeset
   240
            bb = Util.getTemporaryAlignedDirectBuffer(rem, alignment);
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   241
        } else {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   242
            bb = Util.getTemporaryDirectBuffer(rem);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   243
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48750
diff changeset
   245
            int n = readIntoNativeBuffer(fd, bb, position, directIO, alignment,nd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                dst.put(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        } finally {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   251
            Util.offerFirstTemporaryDirectBuffer(bb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private static int readIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb,
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   256
                                            long position, boolean directIO,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   257
                                            int alignment, NativeDispatcher nd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   265
        if (directIO) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   266
            Util.checkBufferPositionAligned(bb, pos, alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   267
            Util.checkRemainingBufferSizeAligned(rem, alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   268
        }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   269
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (rem == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (position != -1) {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47726
diff changeset
   274
            n = nd.pread(fd, ((DirectBuffer)bb).address() + pos, rem, position);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            n = nd.read(fd, ((DirectBuffer)bb).address() + pos, rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            bb.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    static long read(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    {
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   286
        return read(fd, bufs, 0, bufs.length, false, -1, nd);
6301
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
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   289
    static long read(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   290
                     NativeDispatcher nd)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   291
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   292
    {
47726
a85bb15efb57 8191025: (ch) Scattering reads to a subsequence of buffers ignores length
bpb
parents: 47428
diff changeset
   293
        return read(fd, bufs, offset, length, false, -1, nd);
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   294
    }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   295
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   296
    static long read(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   297
                     boolean directIO, int alignment, NativeDispatcher nd)
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   298
        throws IOException
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   299
    {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   300
        IOVecWrapper vec = IOVecWrapper.get(length);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   302
        boolean completed = false;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   303
        int iov_len = 0;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   304
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   306
            // Iterate over buffers to populate native iovec array.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   307
            int count = offset + length;
13024
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   308
            int i = offset;
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   309
            while (i < count && iov_len < IOV_MAX) {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   310
                ByteBuffer buf = bufs[i];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   311
                if (buf.isReadOnly())
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   312
                    throw new IllegalArgumentException("Read-only buffer");
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   313
                int pos = buf.position();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   314
                int lim = buf.limit();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   315
                assert (pos <= lim);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   316
                int rem = (pos <= lim ? lim - pos : 0);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   317
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   318
                if (directIO)
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   319
                    Util.checkRemainingBufferSizeAligned(rem, alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   320
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   321
                if (rem > 0) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   322
                    vec.setBuffer(iov_len, buf, pos, rem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   324
                    // allocate shadow buffer to ensure I/O is done with direct buffer
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   325
                    if (!(buf instanceof DirectBuffer)) {
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   326
                        ByteBuffer shadow;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   327
                        if (directIO) {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47726
diff changeset
   328
                            shadow = Util.getTemporaryAlignedDirectBuffer(rem, alignment);
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   329
                        } else {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   330
                            shadow = Util.getTemporaryDirectBuffer(rem);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   331
                        }
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   332
                        vec.setShadow(iov_len, shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   333
                        buf = shadow;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   334
                        pos = shadow.position();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   335
                    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   336
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   337
                    vec.putBase(iov_len, ((DirectBuffer)buf).address() + pos);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   338
                    vec.putLen(iov_len, rem);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   339
                    iov_len++;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   340
                }
13024
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   341
                i++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   343
            if (iov_len == 0)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   344
                return 0L;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   345
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   346
            long bytesRead = nd.readv(fd, vec.address, iov_len);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   347
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   348
            // Notify the buffers how many bytes were read
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   349
            long left = bytesRead;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   350
            for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   351
                ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   352
                if (left > 0) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   353
                    ByteBuffer buf = vec.getBuffer(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   354
                    int rem = vec.getRemaining(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   355
                    int n = (left > rem) ? rem : (int)left;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   356
                    if (shadow == null) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   357
                        int pos = vec.getPosition(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   358
                        buf.position(pos + n);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   359
                    } else {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   360
                        shadow.limit(shadow.position() + n);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   361
                        buf.put(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   362
                    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   363
                    left -= n;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   364
                }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   365
                if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   366
                    Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   367
                vec.clearRefs(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   368
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   370
            completed = true;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   371
            return bytesRead;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   373
        } finally {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   374
            // 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
   375
            // buffers to cache
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   376
            if (!completed) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   377
                for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   378
                    ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   379
                    if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   380
                        Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   381
                    vec.clearRefs(j);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   382
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 9237
diff changeset
   387
    public static FileDescriptor newFD(int i) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        FileDescriptor fd = new FileDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        setfdVal(fd, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    static native boolean randomBytes(byte[] someBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 6301
diff changeset
   395
    /**
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 6301
diff changeset
   396
     * 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
   397
     * 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
   398
     * 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
   399
     */
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 6301
diff changeset
   400
    static native long makePipe(boolean blocking);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    static native boolean drain(int fd) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 9237
diff changeset
   404
    public static native void configureBlocking(FileDescriptor fd,
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 9237
diff changeset
   405
                                                boolean blocking)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 9237
diff changeset
   408
    public static native int fdVal(FileDescriptor fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    static native void setfdVal(FileDescriptor fd, int value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
12872
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 11823
diff changeset
   412
    static native int fdLimit();
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 11823
diff changeset
   413
13024
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   414
    static native int iovMax();
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   415
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   418
    /**
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   419
     * Used to trigger loading of native libraries
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   420
     */
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   421
    public static void load() { }
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   422
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    static {
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   424
        java.security.AccessController.doPrivileged(
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   425
                new java.security.PrivilegedAction<Void>() {
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   426
                    public Void run() {
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   427
                        System.loadLibrary("net");
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   428
                        System.loadLibrary("nio");
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   429
                        return null;
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   430
                    }
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   431
                });
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   432
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   433
        initIDs();
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   434
13024
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   435
        IOV_MAX = iovMax();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
}