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