Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/nio/channels/FileChannel/Transfer2GPlus.java
66646 views
1
/*
2
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 8271308
27
* @summary Verify that transferTo() copies more than Integer.MAX_VALUE bytes
28
* @library .. /test/lib
29
* @build jdk.test.lib.Platform
30
* @run main Transfer2GPlus
31
*/
32
33
import java.io.File;
34
import java.io.DataOutputStream;
35
import java.io.FileOutputStream;
36
import java.io.IOException;
37
import java.io.RandomAccessFile;
38
import java.nio.ByteBuffer;
39
import java.nio.channels.Channels;
40
import java.nio.channels.FileChannel;
41
import java.nio.channels.WritableByteChannel;
42
import java.nio.file.Files;
43
import java.nio.file.Path;
44
import java.nio.file.StandardOpenOption;
45
import java.util.Arrays;
46
import java.util.Random;
47
import jdk.test.lib.Platform;
48
49
public class Transfer2GPlus {
50
private static final long BASE = (long)Integer.MAX_VALUE;
51
private static final int EXTRA = 1024;
52
private static final long LENGTH = BASE + EXTRA;
53
54
public static void main(String[] args) throws IOException {
55
Path src = Files.createTempFile("src", ".dat");
56
src.toFile().deleteOnExit();
57
byte[] b = createSrcFile(src);
58
testToFileChannel(src, b);
59
testToWritableByteChannel(src, b);
60
}
61
62
// Create a file of size LENGTH with EXTRA random bytes at offset BASE.
63
private static byte[] createSrcFile(Path src)
64
throws IOException {
65
RandomAccessFile raf = new RandomAccessFile(src.toString(), "rw");
66
raf.setLength(LENGTH);
67
raf.seek(BASE);
68
Random r = new Random(System.nanoTime());
69
byte[] b = new byte[EXTRA];
70
r.nextBytes(b);
71
raf.write(b);
72
return b;
73
}
74
75
// Exercises transferToDirectly() on Linux and transferToTrustedChannel()
76
// on macOS and Windows.
77
private static void testToFileChannel(Path src, byte[] expected)
78
throws IOException {
79
Path dst = Files.createTempFile("dst", ".dat");
80
dst.toFile().deleteOnExit();
81
try (FileChannel srcCh = FileChannel.open(src)) {
82
try (FileChannel dstCh = FileChannel.open(dst,
83
StandardOpenOption.READ, StandardOpenOption.WRITE)) {
84
long total = 0L;
85
if ((total = srcCh.transferTo(0, LENGTH, dstCh)) < LENGTH) {
86
if (!Platform.isLinux())
87
throw new RuntimeException("Transfer too small: " + total);
88
89
// If this point is reached we're on Linux which cannot
90
// transfer all LENGTH bytes in one call to sendfile(2),
91
// so loop to get the rest.
92
do {
93
long n = srcCh.transferTo(total, LENGTH, dstCh);
94
if (n == 0)
95
break;
96
total += n;
97
} while (total < LENGTH);
98
}
99
100
if (dstCh.size() < LENGTH)
101
throw new RuntimeException("Target file too small: " +
102
dstCh.size() + " < " + LENGTH);
103
104
System.out.println("Transferred " + total + " bytes");
105
106
dstCh.position(BASE);
107
ByteBuffer bb = ByteBuffer.allocate(EXTRA);
108
dstCh.read(bb);
109
if (!Arrays.equals(bb.array(), expected))
110
throw new RuntimeException("Unexpected values");
111
}
112
}
113
}
114
115
// Exercises transferToArbitraryChannel() on all platforms.
116
private static void testToWritableByteChannel(Path src, byte[] expected)
117
throws IOException {
118
File file = File.createTempFile("dst", ".dat");
119
file.deleteOnExit();
120
try (FileChannel srcCh = FileChannel.open(src)) {
121
// The FileOutputStream is wrapped so that newChannel() does not
122
// return a FileChannelImpl and so make a faster path be taken.
123
try (DataOutputStream stream =
124
new DataOutputStream(new FileOutputStream(file))) {
125
try (WritableByteChannel wbc = Channels.newChannel(stream)) {
126
long n;
127
if ((n = srcCh.transferTo(0, LENGTH, wbc)) < LENGTH)
128
throw new RuntimeException("Too few bytes transferred: " +
129
n + " < " + LENGTH);
130
131
System.out.println("Transferred " + n + " bytes");
132
133
RandomAccessFile raf = new RandomAccessFile(file, "r");
134
raf.seek(BASE);
135
byte[] b = new byte[EXTRA];
136
raf.read(b);
137
if (!Arrays.equals(b, expected))
138
throw new RuntimeException("Unexpected values");
139
}
140
}
141
}
142
}
143
}
144
145