Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/nio/channels/FileChannel/TransferOverlappedFileChannel.java
66645 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
/* @test
25
* @bug 8140241
26
* @summary Test transferring to and from same file channel
27
*/
28
import java.io.BufferedOutputStream;
29
import java.io.File;
30
import java.io.FileOutputStream;
31
import java.io.OutputStream;
32
import java.io.RandomAccessFile;
33
import java.io.IOException;
34
import java.nio.channels.FileChannel;
35
import java.util.Random;
36
37
public class TransferOverlappedFileChannel {
38
39
public static void main(String[] args) throws Exception {
40
File file = File.createTempFile("readingin", null);
41
file.deleteOnExit();
42
generateBigFile(file);
43
RandomAccessFile raf = new RandomAccessFile(file, "rw");
44
try (FileChannel channel = raf.getChannel()) {
45
transferToNoOverlap(file, channel);
46
transferToOverlap(file, channel);
47
transferFromNoOverlap(file, channel);
48
transferFromOverlap(file, channel);
49
} finally {
50
file.delete();
51
}
52
}
53
54
private static void transferToNoOverlap(File file, FileChannel channel)
55
throws IOException {
56
final long length = file.length();
57
58
// position at three quarters
59
channel.position(length*3/4);
60
// copy last quarter to third quarter
61
// (copied and overwritten regions do NOT overlap)
62
// So: 1 2 3 4 -> 1 2 4 4
63
channel.transferTo(length / 2, length / 4, channel);
64
System.out.println("transferToNoOverlap: OK");
65
}
66
67
private static void transferToOverlap(File file, FileChannel channel)
68
throws IOException {
69
final long length = file.length();
70
71
// position at half
72
channel.position(length/2);
73
// copy last half to second quarter
74
// (copied and overwritten regions DO overlap)
75
// So: 1 2 3 4 -> 1 3 4 4
76
channel.transferTo(length / 4, length / 2, channel);
77
System.out.println("transferToOverlap: OK");
78
}
79
80
private static void transferFromNoOverlap(File file, FileChannel channel)
81
throws IOException {
82
final long length = file.length();
83
84
// position at three quarters
85
channel.position(length*3/4);
86
// copy last quarter to third quarter
87
// (copied and overwritten regions do NOT overlap)
88
// So: 1 2 3 4 -> 1 2 4 4
89
channel.transferFrom(channel, length / 2, length / 4);
90
System.out.println("transferFromNoOverlap: OK");
91
}
92
93
private static void transferFromOverlap(File file, FileChannel channel)
94
throws IOException {
95
final long length = file.length();
96
97
// position at half
98
channel.position(length/2);
99
// copy last half to second quarter
100
// (copied and overwritten regions DO overlap)
101
// So: 1 2 3 4 -> 1 3 4 4
102
channel.transferFrom(channel, length / 4, length / 2);
103
System.out.println("transferFromOverlap: OK");
104
}
105
106
private static void generateBigFile(File file) throws Exception {
107
try (OutputStream out = new BufferedOutputStream(
108
new FileOutputStream(file))) {
109
byte[] randomBytes = new byte[1024];
110
Random rand = new Random(0);
111
rand.nextBytes(randomBytes);
112
for (int i = 0; i < 1024; i++) {
113
out.write(randomBytes);
114
}
115
out.flush();
116
}
117
}
118
}
119
120