test/jdk/java/nio/channels/FileChannel/Transfers.java
author jbhateja
Tue, 26 Nov 2019 16:09:25 +0300
changeset 59278 8375560db76b
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234394: C2: Dynamic register class support in ADLC Reviewed-by: vlivanov, sviswanathan, thartmann, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44266
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
     2
 * Copyright (c) 2002, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @summary Comprehensive test for FileChannel.transfer{From,To}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4708120
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Mark Reinhold
44266
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    28
 * @run main/timeout=300 Transfers
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class Transfers {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static PrintStream out = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static class Failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        extends RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        Failure(Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            super(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        Failure(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            super(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // -- Writing and reading random bytes --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static void writeBytes(byte[] ba, FileChannel fc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                   int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        fc.position(off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (fc.write(ByteBuffer.wrap(ba, 0, len)) != len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throw new IOException("Incomplete write");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        fc.position(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static void writeRandomBytes(long seed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                         FileChannel fc, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        Random r = new Random(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        byte[] ba = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        r.nextBytes(ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        writeBytes(ba, fc, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static void writeZeroBytes(FileChannel fc, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        byte[] ba = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        writeBytes(ba, fc, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static void checkBytes(FileChannel fc, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                   byte[] bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        ByteBuffer bb = ByteBuffer.allocate(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        fc.position(off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (fc.read(bb) != len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throw new IOException("Incomplete read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        ByteBuffer bab = ByteBuffer.wrap(bytes, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (!bb.equals(bab))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new Failure("Wrong data written");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static void checkRandomBytes(FileChannel fc, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                         long seed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        byte[] ba = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        Random r = new Random(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        r.nextBytes(ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        checkBytes(fc, off, len, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static void checkZeroBytes(FileChannel fc, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        byte[] ba = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        checkBytes(fc, off, len, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // For debugging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static void dump(FileChannel fc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int sz = (int)fc.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        ByteBuffer bb = ByteBuffer.allocate(sz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        fc.position(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (fc.read(bb) != sz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new IOException("Incomplete read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        byte prev = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int r = 0;                      // Repeats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        while (bb.hasRemaining() && (n < 32)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            byte b = bb.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (b == prev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                r++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (r > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                int c = prev & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (c < 0x10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    out.print('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                out.print(Integer.toHexString(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (r > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    out.print("[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    out.print(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    out.print("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            prev = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            r = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (r > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            int c = prev & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (c < 0x10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                out.print('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            out.print(Integer.toHexString(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (r > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                out.print("[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                out.print(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                out.print("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (bb.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            out.print("...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    static File sourceFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    static File targetFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    // -- Self-verifying sources and targets --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    static abstract class Source {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        protected final int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        protected final long seed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        Source(int size, long seed, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            this.seed = seed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        abstract ReadableByteChannel channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        abstract void verify() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    static class FileSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        extends Source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        private final File fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        private final RandomAccessFile raf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        private final FileChannel fc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        FileSource(int size, long seed) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            super(size, seed, "FileChannel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            fn = sourceFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            raf = new RandomAccessFile(fn, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            fc = raf.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            fc.position(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            writeRandomBytes(seed, fc, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        ReadableByteChannel channel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return fc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        void verify() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (fc.position() != size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                throw new Failure("Wrong position: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                  + fc.position() + " (expected " + size +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                  ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            checkRandomBytes(fc, 0, size, seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            fc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            raf.close();                // Bug in 1.4.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    static class UserSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        extends Source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        private ReadableByteChannel ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        private final ByteBuffer src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        UserSource(int size, long seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            super(size, seed, "UserChannel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            final byte[] bytes = new byte[size + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            Random r = new Random(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            r.nextBytes(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            src = ByteBuffer.wrap(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            ch = new ReadableByteChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    public int read(ByteBuffer dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        if (!src.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        int nr = Math.min(src.remaining(), dst.remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        ByteBuffer s = src.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        s.limit(s.position() + nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        dst.put(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        src.position(src.position() + nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        return nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    public boolean isOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    public void close() { }
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
        ReadableByteChannel channel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        void verify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (src.remaining() != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                throw new Failure("Source has " + src.remaining()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                  + " bytes remaining (expected 1)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    static abstract class Target {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        protected final int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        protected final long seed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        Target(int size, long seed, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            this.seed = seed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        abstract WritableByteChannel channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        abstract void verify() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    static class FileTarget
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        extends Target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        private final File fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        private final RandomAccessFile raf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        private final FileChannel fc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        FileTarget(int size, long seed) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            super(size, seed, "FileChannel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            fn = targetFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            raf = new RandomAccessFile(fn, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            fc = raf.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            fc.position(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        WritableByteChannel channel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return fc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        void verify() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (fc.position() != size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                throw new Failure("Wrong position: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                  + fc.position() + " (expected " + size + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            checkRandomBytes(fc, 0, size, seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            fc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            raf.close();                // Bug in 1.4.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    static class UserTarget
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        extends Target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        private WritableByteChannel ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        private final ByteBuffer dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        UserTarget(int size, long seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            super(size, seed, "UserChannel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            dst = ByteBuffer.wrap(new byte[size + 1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            ch = new WritableByteChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    public int write(ByteBuffer src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        int nr = Math.min(src.remaining(), dst.remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        ByteBuffer s = src.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        s.limit(s.position() + nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        dst.put(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        src.position(src.position() + nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        return nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    public boolean isOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    public void close() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        WritableByteChannel channel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        void verify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if (dst.remaining() != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                throw new Failure("Destination has " + dst.remaining()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                  + " bytes remaining (expected 1)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            byte[] ba = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            Random r = new Random(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            r.nextBytes(ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            dst.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            ByteBuffer rbb = ByteBuffer.wrap(ba, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            if (!dst.equals(rbb))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                throw new Failure("Wrong data written");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    // Generates a sequence of ints of the form 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    // 15, 16, 17, 31, 32, 33, ..., 2^i-1, 2^i, 2^i+1, ..., max.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    static class IntGenerator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        private int max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        private int cur = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        private int p2 = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        IntGenerator(int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            this.max = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            return cur < max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        int next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (cur >= max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            if (cur < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                cur++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                return cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            if (cur == p2 + 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                p2 <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                cur = p2 - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                return cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            cur++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            return cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    // -- Tests --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    private static final int MAX_XFER_SIZE = 1 << 14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    private static final int MAX_FILE_SIZE = MAX_XFER_SIZE << 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    private static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    private static boolean verbose = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    static void show(String dir, String channelName, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if (!verbose)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        out.println(dir + " " + channelName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    ": offset " + off + ", length " + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    static void testTo(long seed, FileChannel fc, int off, int len, Target tgt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        show("To", tgt.name(), off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        // Clear source, then randomize just the source region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        writeZeroBytes(fc, 0, MAX_FILE_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        writeRandomBytes(seed, fc, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        // Randomize position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        int pos = (int)seed & 0xfff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        fc.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        int n = (int)fc.transferTo(off, len, tgt.channel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (n != len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            throw new Failure("Incorrect transfer length: " + n
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                              + " (expected " + len + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        // Check that source wasn't changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        if (fc.position() != pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            throw new Failure("Position changed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            dump(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        checkRandomBytes(fc, off, len, seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        writeZeroBytes(fc, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        checkZeroBytes(fc, 0, MAX_FILE_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // Check that target was updated correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        tgt.verify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    static void testFrom(long seed, Source src, FileChannel fc, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        show("From", src.name(), off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        // Clear target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        writeZeroBytes(fc, 0, MAX_FILE_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        // Randomize position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        int pos = (int)seed & 0xfff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        fc.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        int n = (int)fc.transferFrom(src.channel(), off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (n != len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            throw new Failure("Incorrect transfer length: " + n
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                              + " (expected " + len + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        // Check that source didn't change, and was read correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        src.verify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        // Check that target was updated correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (fc.position() != pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            throw new Failure("Position changed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            dump(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        checkRandomBytes(fc, off, len, seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        writeZeroBytes(fc, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        checkZeroBytes(fc, 0, MAX_FILE_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    public static void main(String[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            if (args[0].indexOf('v') >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                verbose = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            if (args[0].indexOf('d') >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                debug = verbose = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
39641
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   493
        File testDir = new File(System.getProperty("test.dir", "."));
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   494
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   495
        sourceFile = File.createTempFile("xfer.src.", "", testDir);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        sourceFile.deleteOnExit();
39641
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   497
        targetFile = File.createTempFile("xfer.tgt.", "", testDir);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        targetFile.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
39641
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   500
        File fn = File.createTempFile("xfer.fch.", "", testDir);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        fn.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
39641
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   506
        try (FileChannel fc = new RandomAccessFile(fn, "rw").getChannel()) {
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   507
            for (boolean to = false;; to = true) {
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   508
                for (boolean user = false;; user = true) {
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   509
                    if (!verbose)
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   510
                        out.print((to ? "To " : "From ") +
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   511
                                  (user ? "user channel" : "file channel")
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   512
                                  + ":");
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   513
                    IntGenerator offGen = new IntGenerator(MAX_XFER_SIZE + 2);
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   514
                    while (offGen.hasNext()) {
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   515
                        int off = offGen.next();
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   516
                        if (!verbose) out.print(" " + off);
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   517
                        IntGenerator lenGen = new IntGenerator(MAX_XFER_SIZE + 2);
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   518
                        while (lenGen.hasNext()) {
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   519
                            int len = lenGen.next();
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   520
                            long s = rnd.nextLong();
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   521
                            String chName = null;
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   522
                            try {
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   523
                                if (to) {
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   524
                                    Target tgt;
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   525
                                    if (user)
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   526
                                        tgt = new UserTarget(len, s);
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   527
                                    else
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   528
                                        tgt = new FileTarget(len, s);
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   529
                                    chName = tgt.name();
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   530
                                    testTo(s, fc, off, len, tgt);
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   531
                                }
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   532
                                else {
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   533
                                    Source src;
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   534
                                    if (user)
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   535
                                        src = new UserSource(len, s);
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   536
                                    else
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   537
                                        src = new FileSource(len, s);
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   538
                                    chName = src.name();
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   539
                                    testFrom(s, src, fc, off, len);
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   540
                                }
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   541
                            } catch (Failure x) {
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   542
                                out.println();
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   543
                                out.println("FAILURE: " + chName
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   544
                                            + ", offset " + off
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   545
                                            + ", length " + len);
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   546
                                x.printStackTrace(out);
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   547
                                failures++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    }
39641
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   551
                    if (!verbose)
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   552
                        out.println();
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   553
                    if (user)
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   554
                        break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                }
39641
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   556
                if (to)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   561
        sourceFile.delete();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   562
        targetFile.delete();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   563
        fn.delete();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   564
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (failures > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            throw new RuntimeException("Some tests failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
39641
8769812a294f 8160221: jdk/test/java/nio/channels/FileChannel/Transfers.java leaving files behind
bpb
parents: 7668
diff changeset
   570
        out.println("Test succeeded.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
}