File: Transfer2GPlus.java

package info (click to toggle)
openjdk-17 17.0.12%2B7-2~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 761,492 kB
  • sloc: java: 5,260,864; xml: 1,291,612; cpp: 1,195,623; ansic: 417,064; asm: 404,978; objc: 20,747; sh: 15,482; javascript: 10,900; python: 6,402; makefile: 2,378; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (143 lines) | stat: -rw-r--r-- 5,698 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8271308
 * @summary Verify that transferTo() copies more than Integer.MAX_VALUE bytes
 * @library .. /test/lib
 * @build jdk.test.lib.Platform
 * @run main Transfer2GPlus
 */

import java.io.File;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Random;
import jdk.test.lib.Platform;

public class Transfer2GPlus {
    private static final long BASE   = (long)Integer.MAX_VALUE;
    private static final int  EXTRA  = 1024;
    private static final long LENGTH = BASE + EXTRA;

    public static void main(String[] args) throws IOException {
        Path src = Files.createTempFile("src", ".dat");
        src.toFile().deleteOnExit();
        byte[] b = createSrcFile(src);
        testToFileChannel(src, b);
        testToWritableByteChannel(src, b);
    }

    // Create a file of size LENGTH with EXTRA random bytes at offset BASE.
    private static byte[] createSrcFile(Path src)
        throws IOException {
        RandomAccessFile raf = new RandomAccessFile(src.toString(), "rw");
        raf.setLength(LENGTH);
        raf.seek(BASE);
        Random r = new Random(System.nanoTime());
        byte[] b = new byte[EXTRA];
        r.nextBytes(b);
        raf.write(b);
        return b;
    }

    // Exercises transferToDirectly() on Linux and transferToTrustedChannel()
    // on macOS and Windows.
    private static void testToFileChannel(Path src, byte[] expected)
        throws IOException {
        Path dst = Files.createTempFile("dst", ".dat");
        dst.toFile().deleteOnExit();
        try (FileChannel srcCh = FileChannel.open(src)) {
            try (FileChannel dstCh = FileChannel.open(dst,
                 StandardOpenOption.READ, StandardOpenOption.WRITE)) {
                long total = 0L;
                if ((total = srcCh.transferTo(0, LENGTH, dstCh)) < LENGTH) {
                    if (!Platform.isLinux())
                        throw new RuntimeException("Transfer too small: " + total);

                    // If this point is reached we're on Linux which cannot
                    // transfer all LENGTH bytes in one call to sendfile(2),
                    // so loop to get the rest.
                    do {
                        long n = srcCh.transferTo(total, LENGTH, dstCh);
                        if (n == 0)
                            break;
                        total += n;
                    } while (total < LENGTH);
                }

                if (dstCh.size() < LENGTH)
                    throw new RuntimeException("Target file too small: " +
                        dstCh.size() + " < " + LENGTH);

                System.out.println("Transferred " + total + " bytes");

                dstCh.position(BASE);
                ByteBuffer bb = ByteBuffer.allocate(EXTRA);
                dstCh.read(bb);
                if (!Arrays.equals(bb.array(), expected))
                    throw new RuntimeException("Unexpected values");
            }
        }
    }

    // Exercises transferToArbitraryChannel() on all platforms.
    private static void testToWritableByteChannel(Path src, byte[] expected)
        throws IOException {
        File file = File.createTempFile("dst", ".dat");
        file.deleteOnExit();
        try (FileChannel srcCh = FileChannel.open(src)) {
            // The FileOutputStream is wrapped so that newChannel() does not
            // return a FileChannelImpl and so make a faster path be taken.
            try (DataOutputStream stream =
                new DataOutputStream(new FileOutputStream(file))) {
                try (WritableByteChannel wbc = Channels.newChannel(stream)) {
                    long n;
                    if ((n = srcCh.transferTo(0, LENGTH, wbc)) < LENGTH)
                        throw new RuntimeException("Too few bytes transferred: " +
                            n + " < " + LENGTH);

                    System.out.println("Transferred " + n + " bytes");

                    RandomAccessFile raf = new RandomAccessFile(file, "r");
                    raf.seek(BASE);
                    byte[] b = new byte[EXTRA];
                    raf.read(b);
                    if (!Arrays.equals(b, expected))
                        throw new RuntimeException("Unexpected values");
                }
            }
        }
    }
}