Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/FileChannel/TransferToChannel.java
41154 views
1
/*
2
* Copyright (c) 2002, 2010, 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 4652496
26
* @summary Test transferTo with different target channels
27
* @run main TransferToChannel
28
* @run main/othervm -Djdk.nio.enableFastFileTransfer TransferToChannel
29
*/
30
31
import java.nio.channels.FileChannel;
32
import java.nio.channels.WritableByteChannel;
33
import java.nio.ByteBuffer;
34
import java.io.*;
35
import java.util.Random;
36
37
public class TransferToChannel {
38
39
static File file;
40
static File outFile;
41
static FileChannel in;
42
// Chunk size should be larger than FileChannelImpl.TRANSFER_SIZE for good test
43
static int CHUNK_SIZE = 1024 * 9;
44
45
public static void main(String[] args) throws Exception {
46
file = File.createTempFile("readingin", null);
47
outFile = File.createTempFile("writingout", null);
48
file.deleteOnExit();
49
outFile.deleteOnExit();
50
generateBigFile(file);
51
FileInputStream fis = new FileInputStream(file);
52
in = fis.getChannel();
53
test1();
54
test2();
55
in.close();
56
file.delete();
57
outFile.delete();
58
}
59
60
static void test1() throws Exception {
61
for (int i=0; i<10; i++) {
62
transferFileToUserChannel();
63
System.gc();
64
System.err.println("Transferred file...");
65
}
66
}
67
68
static void test2() throws Exception {
69
for (int i=0; i<10; i++) {
70
transferFileToTrustedChannel();
71
System.gc();
72
System.err.println("Transferred file...");
73
}
74
}
75
76
static void transferFileToUserChannel() throws Exception {
77
long remainingBytes = in.size();
78
long size = remainingBytes;
79
WritableByteChannel wbc = new WritableByteChannel() {
80
Random rand = new Random(0);
81
public int write(ByteBuffer src) throws IOException {
82
int read = src.remaining();
83
byte[] incoming = new byte[read];
84
src.get(incoming);
85
checkData(incoming, read);
86
return read == 0 ? -1 : read;
87
}
88
public boolean isOpen() {
89
return true;
90
}
91
public void close() throws IOException {
92
}
93
void checkData(byte[] incoming, int size) {
94
byte[] expected = new byte[size];
95
rand.nextBytes(expected);
96
for (int i=0; i<size; i++)
97
if (incoming[i] != expected[i])
98
throw new RuntimeException("Data corrupted");
99
}
100
};
101
while (remainingBytes > 0) {
102
long bytesTransferred = in.transferTo(size - remainingBytes,
103
Math.min(CHUNK_SIZE, remainingBytes), wbc);
104
if (bytesTransferred >= 0)
105
remainingBytes -= bytesTransferred;
106
else
107
throw new Exception("transfer failed");
108
}
109
}
110
111
static void transferFileToTrustedChannel() throws Exception {
112
long remainingBytes = in.size();
113
long size = remainingBytes;
114
FileOutputStream fos = new FileOutputStream(outFile);
115
FileChannel out = fos.getChannel();
116
while (remainingBytes > 0) {
117
long bytesTransferred = in.transferTo(size - remainingBytes,
118
CHUNK_SIZE, out);
119
if (bytesTransferred >= 0)
120
remainingBytes -= bytesTransferred;
121
else
122
throw new Exception("transfer failed");
123
}
124
out.close();
125
}
126
127
static void generateBigFile(File file) throws Exception {
128
OutputStream out = new BufferedOutputStream(
129
new FileOutputStream(file));
130
byte[] randomBytes = new byte[1024];
131
Random rand = new Random(0);
132
for (int i = 0; i < 1000; i++) {
133
rand.nextBytes(randomBytes);
134
out.write(randomBytes);
135
}
136
out.flush();
137
out.close();
138
}
139
}
140
141