src/java.base/share/classes/sun/nio/ch/IOUtil.java
author rriggs
Fri, 01 Dec 2017 16:40:08 -0500
changeset 48224 be0df5ab3093
parent 47726 a85bb15efb57
child 48750 ffbb784a8873
permissions -rw-r--r--
8080225: FileInput/OutputStream/FileChannel cleanup should be improved Reviewed-by: mchung, plevart, bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2017, 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,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
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)
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   164
                            shadow = Util.getTemporaryAlignedDirectBuffer(rem,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   165
                                    alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   166
                        else
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   167
                            shadow = Util.getTemporaryDirectBuffer(rem);
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   168
                        shadow.put(buf);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   169
                        shadow.flip();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   170
                        vec.setShadow(iov_len, shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   171
                        buf.position(pos);  // temporarily restore position in user buffer
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   172
                        buf = shadow;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   173
                        pos = shadow.position();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   174
                    }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   175
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   176
                    vec.putBase(iov_len, ((DirectBuffer)buf).address() + pos);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   177
                    vec.putLen(iov_len, rem);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   178
                    iov_len++;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   179
                }
13024
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   180
                i++;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   181
            }
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   182
            if (iov_len == 0)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   183
                return 0L;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   184
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   185
            long bytesWritten = nd.writev(fd, vec.address, iov_len);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   186
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   187
            // Notify the buffers how many bytes were taken
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   188
            long left = bytesWritten;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   189
            for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   190
                if (left > 0) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   191
                    ByteBuffer buf = vec.getBuffer(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   192
                    int pos = vec.getPosition(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   193
                    int rem = vec.getRemaining(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   194
                    int n = (left > rem) ? rem : (int)left;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   195
                    buf.position(pos + n);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   196
                    left -= n;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   197
                }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   198
                // return shadow buffers to buffer pool
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   199
                ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   200
                if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   201
                    Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   202
                vec.clearRefs(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   203
            }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   204
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   205
            completed = true;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   206
            return bytesWritten;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   207
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   208
        } finally {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   209
            // 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
   210
            // buffers to cache
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   211
            if (!completed) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   212
                for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   213
                    ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   214
                    if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   215
                        Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   216
                    vec.clearRefs(j);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   217
                }
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    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
   223
                    NativeDispatcher nd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    {
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   226
        return read(fd, dst, position, false, -1, nd);
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
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   229
    static int read(FileDescriptor fd, ByteBuffer dst, long position,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   230
                    boolean directIO, int alignment, NativeDispatcher nd)
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   231
        throws IOException
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   232
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (dst.isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw new IllegalArgumentException("Read-only buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (dst instanceof DirectBuffer)
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   236
            return readIntoNativeBuffer(fd, dst, position,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   237
                    directIO, alignment, nd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // Substitute a native buffer
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   240
        ByteBuffer bb;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   241
        int rem = dst.remaining();
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   242
        if (directIO) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   243
            Util.checkRemainingBufferSizeAligned(rem, alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   244
            bb = Util.getTemporaryAlignedDirectBuffer(rem,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   245
                                                      alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   246
        } else {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   247
            bb = Util.getTemporaryDirectBuffer(rem);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   248
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        try {
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   250
            int n = readIntoNativeBuffer(fd, bb, position,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   251
                    directIO, alignment,nd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                dst.put(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } finally {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   257
            Util.offerFirstTemporaryDirectBuffer(bb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private static int readIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb,
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   262
                                            long position, boolean directIO,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   263
                                            int alignment, NativeDispatcher nd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   271
        if (directIO) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   272
            Util.checkBufferPositionAligned(bb, pos, alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   273
            Util.checkRemainingBufferSizeAligned(rem, alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   274
        }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   275
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (rem == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (position != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            n = nd.pread(fd, ((DirectBuffer)bb).address() + pos,
16921
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents: 14342
diff changeset
   281
                         rem, position);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            n = nd.read(fd, ((DirectBuffer)bb).address() + pos, rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            bb.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    static long read(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    {
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   293
        return read(fd, bufs, 0, bufs.length, false, -1, nd);
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   294
    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   295
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   296
    static long read(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   297
                     NativeDispatcher nd)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   298
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   299
    {
47726
a85bb15efb57 8191025: (ch) Scattering reads to a subsequence of buffers ignores length
bpb
parents: 47428
diff changeset
   300
        return read(fd, bufs, offset, length, false, -1, nd);
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   301
    }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   302
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   303
    static long read(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   304
                     boolean directIO, int alignment, NativeDispatcher nd)
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   305
        throws IOException
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   306
    {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   307
        IOVecWrapper vec = IOVecWrapper.get(length);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   308
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   309
        boolean completed = false;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   310
        int iov_len = 0;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   311
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   313
            // Iterate over buffers to populate native iovec array.
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   314
            int count = offset + length;
13024
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   315
            int i = offset;
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   316
            while (i < count && iov_len < IOV_MAX) {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   317
                ByteBuffer buf = bufs[i];
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   318
                if (buf.isReadOnly())
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   319
                    throw new IllegalArgumentException("Read-only buffer");
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   320
                int pos = buf.position();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   321
                int lim = buf.limit();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   322
                assert (pos <= lim);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   323
                int rem = (pos <= lim ? lim - pos : 0);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   324
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   325
                if (directIO)
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   326
                    Util.checkRemainingBufferSizeAligned(rem, alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   327
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   328
                if (rem > 0) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   329
                    vec.setBuffer(iov_len, buf, pos, rem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   331
                    // allocate shadow buffer to ensure I/O is done with direct buffer
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   332
                    if (!(buf instanceof DirectBuffer)) {
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   333
                        ByteBuffer shadow;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   334
                        if (directIO) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   335
                            shadow = Util.getTemporaryAlignedDirectBuffer(rem,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   336
                                    alignment);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   337
                        } else {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   338
                            shadow = Util.getTemporaryDirectBuffer(rem);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
   339
                        }
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   340
                        vec.setShadow(iov_len, shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   341
                        buf = shadow;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   342
                        pos = shadow.position();
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   343
                    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   344
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   345
                    vec.putBase(iov_len, ((DirectBuffer)buf).address() + pos);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   346
                    vec.putLen(iov_len, rem);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   347
                    iov_len++;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   348
                }
13024
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   349
                i++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   351
            if (iov_len == 0)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   352
                return 0L;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   353
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   354
            long bytesRead = nd.readv(fd, vec.address, iov_len);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   355
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   356
            // Notify the buffers how many bytes were read
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   357
            long left = bytesRead;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   358
            for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   359
                ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   360
                if (left > 0) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   361
                    ByteBuffer buf = vec.getBuffer(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   362
                    int rem = vec.getRemaining(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   363
                    int n = (left > rem) ? rem : (int)left;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   364
                    if (shadow == null) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   365
                        int pos = vec.getPosition(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   366
                        buf.position(pos + n);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   367
                    } else {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   368
                        shadow.limit(shadow.position() + n);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   369
                        buf.put(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   370
                    }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   371
                    left -= n;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   372
                }
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   373
                if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   374
                    Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   375
                vec.clearRefs(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   376
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   378
            completed = true;
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   379
            return bytesRead;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   381
        } finally {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   382
            // 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
   383
            // buffers to cache
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   384
            if (!completed) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   385
                for (int j=0; j<iov_len; j++) {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   386
                    ByteBuffer shadow = vec.getShadow(j);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   387
                    if (shadow != null)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   388
                        Util.offerLastTemporaryDirectBuffer(shadow);
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   389
                    vec.clearRefs(j);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   390
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 9237
diff changeset
   395
    public static FileDescriptor newFD(int i) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        FileDescriptor fd = new FileDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        setfdVal(fd, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    static native boolean randomBytes(byte[] someBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
6520
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
     * 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
   405
     * 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
   406
     * 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
   407
     */
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 6301
diff changeset
   408
    static native long makePipe(boolean blocking);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    static native boolean drain(int fd) 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 void configureBlocking(FileDescriptor fd,
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 9237
diff changeset
   413
                                                boolean blocking)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 9237
diff changeset
   416
    public static native int fdVal(FileDescriptor fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    static native void setfdVal(FileDescriptor fd, int value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
12872
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 11823
diff changeset
   420
    static native int fdLimit();
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 11823
diff changeset
   421
13024
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   422
    static native int iovMax();
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   423
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   426
    /**
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   427
     * Used to trigger loading of native libraries
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   428
     */
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   429
    public static void load() { }
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   430
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    static {
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   432
        java.security.AccessController.doPrivileged(
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   433
                new java.security.PrivilegedAction<Void>() {
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   434
                    public Void run() {
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   435
                        System.loadLibrary("net");
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   436
                        System.loadLibrary("nio");
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   437
                        return null;
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   438
                    }
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   439
                });
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   440
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   441
        initIDs();
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16921
diff changeset
   442
13024
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   443
        IOV_MAX = iovMax();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
}