jdk/src/share/classes/sun/nio/ch/IOUtil.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2057 3acf8e5e2ca0
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2002 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * File-descriptor based I/O utilities that are shared by NIO classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
class IOUtil {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private IOUtil() { }                // No instantiation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Returns the index of first buffer in bufs with remaining,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * or -1 if there is nothing left
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static int remaining(ByteBuffer[] bufs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        int numBufs = bufs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        boolean remaining = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        for (int i=0; i<numBufs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            if (bufs[i].hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Returns a new ByteBuffer array with only unfinished buffers in it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static ByteBuffer[] skipBufs(ByteBuffer[] bufs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                         int nextWithRemaining)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        int newSize = bufs.length - nextWithRemaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        ByteBuffer[] temp = new ByteBuffer[newSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        for (int i=0; i<newSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            temp[i] = bufs[i + nextWithRemaining];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static int write(FileDescriptor fd, ByteBuffer src, long position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                     NativeDispatcher nd, Object lock)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (src instanceof DirectBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return writeFromNativeBuffer(fd, src, position, nd, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // Substitute a native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        int pos = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int lim = src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            bb = Util.getTemporaryDirectBuffer(rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            bb.put(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            // Do not update src until we see how many bytes were written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            src.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            int n = writeFromNativeBuffer(fd, bb, position, nd, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                // now update src
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                src.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static int writeFromNativeBuffer(FileDescriptor fd, ByteBuffer bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                           long position, NativeDispatcher nd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                             Object lock)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        int written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (rem == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (position != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            written = nd.pwrite(fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                ((DirectBuffer)bb).address() + pos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                rem, position, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            written = nd.write(fd, ((DirectBuffer)bb).address() + pos, rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (written > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            bb.position(pos + written);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    static long write(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int nextWithRemaining = remaining(bufs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // if all bufs are empty we should return immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (nextWithRemaining < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // If some bufs are empty we should skip them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (nextWithRemaining > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            bufs = skipBufs(bufs, nextWithRemaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        int numBufs = bufs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        int bytesReadyToWrite = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // Create shadow to ensure DirectByteBuffers are used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        ByteBuffer[] shadow = new ByteBuffer[numBufs];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        for (int i=0; i<numBufs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (!(bufs[i] instanceof DirectBuffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                int pos = bufs[i].position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                int lim = bufs[i].limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                ByteBuffer bb = ByteBuffer.allocateDirect(rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                shadow[i] = bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                // Leave slow buffer position untouched; it will be updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                // after we see how many bytes were really written out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                bb.put(bufs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                bufs[i].position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                shadow[i] = bufs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        IOVecWrapper vec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        long bytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // Create a native iovec array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            vec= new IOVecWrapper(numBufs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // Fill in the iovec array with appropriate data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            for (int i=0; i<numBufs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                ByteBuffer nextBuffer = shadow[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                // put in the buffer addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                long pos = nextBuffer.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                long len = nextBuffer.limit() - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                bytesReadyToWrite += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                vec.putBase(i, ((DirectBuffer)nextBuffer).address() + pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                vec.putLen(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            // Invoke native call to fill the buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            bytesWritten = nd.writev(fd, vec.address, numBufs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            vec.free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        long returnVal = bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // Notify the buffers how many bytes were taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        for (int i=0; i<numBufs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            ByteBuffer nextBuffer = bufs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            int pos = nextBuffer.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            int lim = nextBuffer.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            int len = (pos <= lim ? lim - pos : lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (bytesWritten >= len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                bytesWritten -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                int newPosition = pos + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                nextBuffer.position(newPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            } else { // Buffers not completely filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                if (bytesWritten > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    assert(pos + bytesWritten < (long)Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    int newPosition = (int)(pos + bytesWritten);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    nextBuffer.position(newPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return returnVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    static int read(FileDescriptor fd, ByteBuffer dst, long position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    NativeDispatcher nd, Object lock)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (dst.isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throw new IllegalArgumentException("Read-only buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (dst instanceof DirectBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return readIntoNativeBuffer(fd, dst, position, nd, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // Substitute a native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            bb = Util.getTemporaryDirectBuffer(dst.remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            int n = readIntoNativeBuffer(fd, bb, position, nd, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                dst.put(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private static int readIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                            long position, NativeDispatcher nd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                            Object lock)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (rem == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (position != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            n = nd.pread(fd, ((DirectBuffer)bb).address() + pos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                         rem, position, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            n = nd.read(fd, ((DirectBuffer)bb).address() + pos, rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            bb.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    static long read(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        int nextWithRemaining = remaining(bufs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // if all bufs are empty we should return immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (nextWithRemaining < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // If some bufs are empty we should skip them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (nextWithRemaining > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            bufs = skipBufs(bufs, nextWithRemaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        int numBufs = bufs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // Read into the shadow to ensure DirectByteBuffers are used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        ByteBuffer[] shadow = new ByteBuffer[numBufs];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        for (int i=0; i<numBufs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (bufs[i].isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                throw new IllegalArgumentException("Read-only buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (!(bufs[i] instanceof DirectBuffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                shadow[i] = ByteBuffer.allocateDirect(bufs[i].remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                shadow[i] = bufs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        IOVecWrapper vec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        long bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // Create a native iovec array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            vec = new IOVecWrapper(numBufs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // Fill in the iovec array with appropriate data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            for (int i=0; i<numBufs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                ByteBuffer nextBuffer = shadow[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                // put in the buffer addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                long pos = nextBuffer.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                long len = nextBuffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                vec.putBase(i, ((DirectBuffer)nextBuffer).address() + pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                vec.putLen(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            // Invoke native call to fill the buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            bytesRead = nd.readv(fd, vec.address, numBufs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            vec.free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        long returnVal = bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // Notify the buffers how many bytes were read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        for (int i=0; i<numBufs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            ByteBuffer nextBuffer = shadow[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            // Note: should this have been cached from above?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            int pos = nextBuffer.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            int len = nextBuffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (bytesRead >= len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                bytesRead -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                int newPosition = pos + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                nextBuffer.position(newPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            } else { // Buffers not completely filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                if (bytesRead > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    assert(pos + bytesRead < (long)Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    int newPosition = (int)(pos + bytesRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    nextBuffer.position(newPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // Put results from shadow into the slow buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        for (int i=0; i<numBufs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            if (!(bufs[i] instanceof DirectBuffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                shadow[i].flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                bufs[i].put(shadow[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return returnVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    static FileDescriptor newFD(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        FileDescriptor fd = new FileDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        setfdVal(fd, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    static native boolean randomBytes(byte[] someBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    static native void initPipe(int[] fda, boolean blocking);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    static native boolean drain(int fd) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    static native void configureBlocking(FileDescriptor fd, boolean blocking)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    static native int fdVal(FileDescriptor fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    static native void setfdVal(FileDescriptor fd, int value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        // Note that IOUtil.initIDs is called from within Util.load.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        Util.load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
}