Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/FileChannel/Write.java
41154 views
1
/*
2
* Copyright (c) 2001, 2020, 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 4475533 4698138 4638365 4796221
27
* @summary Test FileChannel write
28
* @run main/othervm Write
29
*/
30
31
import java.nio.channels.*;
32
import java.nio.*;
33
import java.io.*;
34
35
public class Write {
36
37
public static void main(String[] args) throws Exception {
38
test1(); // for bug 4475533
39
test2();
40
test3(); // for bug 4698138
41
}
42
43
// Test to see that offset > length does not throw exception
44
static void test1() throws Exception {
45
ByteBuffer[] dsts = new ByteBuffer[4];
46
for (int i=0; i<4; i++)
47
dsts[i] = ByteBuffer.allocateDirect(10);
48
49
File testFile = File.createTempFile("test1", null);
50
try {
51
FileOutputStream fos = new FileOutputStream(testFile);
52
FileChannel fc = fos.getChannel();
53
fc.write(dsts, 2, 1);
54
fos.close();
55
} finally {
56
testFile.delete();
57
}
58
}
59
60
// Test to see that the appropriate buffers are updated
61
static void test2() throws Exception {
62
File testFile = File.createTempFile("test2", null);
63
testFile.delete();
64
ByteBuffer[] srcs = new ByteBuffer[4];
65
for (int i=0; i<4; i++)
66
srcs[i] = ByteBuffer.allocateDirect(10);
67
68
srcs[0].put((byte)1); srcs[0].flip();
69
srcs[1].put((byte)2); srcs[1].flip();
70
srcs[2].put((byte)3); srcs[2].flip();
71
srcs[3].put((byte)4); srcs[3].flip();
72
73
FileOutputStream fos = new FileOutputStream(testFile);
74
FileChannel fc = fos.getChannel();
75
try {
76
fc.write(srcs, 1, 2);
77
} finally {
78
fc.close();
79
}
80
81
FileInputStream fis = new FileInputStream(testFile);
82
fc = fis.getChannel();
83
try {
84
ByteBuffer bb = ByteBuffer.allocateDirect(10);
85
fc.read(bb);
86
bb.flip();
87
if (bb.get() != 2)
88
throw new RuntimeException("Write failure");
89
if (bb.get() != 3)
90
throw new RuntimeException("Write failure");
91
try {
92
bb.get();
93
throw new RuntimeException("Write failure");
94
} catch (BufferUnderflowException bufe) {
95
// correct result
96
}
97
} finally {
98
fc.close();
99
}
100
101
// eagerly clean-up
102
testFile.delete();
103
}
104
105
// Test write to a negative position (bug 4698138).
106
static void test3() throws Exception {
107
File testFile = File.createTempFile("test1", null);
108
testFile.deleteOnExit();
109
ByteBuffer dst = ByteBuffer.allocate(10);
110
FileOutputStream fos = new FileOutputStream(testFile);
111
FileChannel fc = fos.getChannel();
112
try {
113
fc.write(dst, -1);
114
throw new RuntimeException("Expected IAE not thrown");
115
} catch (IllegalArgumentException iae) {
116
// Correct result
117
} finally {
118
fos.close();
119
}
120
}
121
}
122
123