Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/RandomAccessFile/SetLength.java
41149 views
1
/*
2
* Copyright (c) 1997, 2018, 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 8204310
26
* @summary General tests of the setLength method
27
* @library /test/lib
28
* @build jdk.test.lib.RandomFactory
29
* @run main SetLength
30
* @key randomness
31
*/
32
33
import java.io.IOException;
34
import java.io.File;
35
import java.io.RandomAccessFile;
36
37
import jdk.test.lib.RandomFactory;
38
39
public class SetLength {
40
41
static void checkState(RandomAccessFile f, long expectedFilePointer,
42
long expectedLength)
43
throws IOException
44
{
45
long filePointer = f.getFilePointer();
46
long length = f.length();
47
if (length != expectedLength) {
48
throw new RuntimeException("File length " + length + " != expected "
49
+ expectedLength);
50
}
51
if (filePointer != expectedFilePointer) {
52
throw new RuntimeException("File pointer " + filePointer
53
+ " != expected " + expectedFilePointer);
54
}
55
}
56
57
static void test(RandomAccessFile f, long quarterLength)
58
throws IOException
59
{
60
long halfLength = 2 * quarterLength;
61
long threeQuarterLength = 3 * quarterLength;
62
long fullLength = 4 * quarterLength;
63
64
// initially, empty file
65
checkState(f, 0, 0);
66
67
// extending the file size
68
f.setLength(halfLength);
69
checkState(f, 0, halfLength);
70
71
// writing from the begining
72
f.write(new byte[(int)fullLength]);
73
checkState(f, fullLength, fullLength);
74
75
// setting to the same length
76
f.setLength(fullLength);
77
checkState(f, fullLength, fullLength);
78
79
// truncating the file
80
f.setLength(threeQuarterLength);
81
checkState(f, threeQuarterLength, threeQuarterLength);
82
83
// changing the file pointer
84
f.seek(quarterLength);
85
checkState(f, quarterLength, threeQuarterLength);
86
87
// truncating the file again
88
f.setLength(halfLength);
89
checkState(f, quarterLength, halfLength);
90
91
// writing from the middle with extending the file
92
f.write(new byte[(int)halfLength]);
93
checkState(f, threeQuarterLength, threeQuarterLength);
94
95
// changing the file pointer
96
f.seek(quarterLength);
97
checkState(f, quarterLength, threeQuarterLength);
98
99
// writing from the middle without extending the file
100
f.write(new byte[(int)quarterLength]);
101
checkState(f, halfLength, threeQuarterLength);
102
103
// changing the file pointer to the end of file
104
f.seek(threeQuarterLength);
105
checkState(f, threeQuarterLength, threeQuarterLength);
106
107
// writing to the end of file
108
f.write(new byte[(int)quarterLength]);
109
checkState(f, fullLength, fullLength);
110
111
// truncating the file to zero
112
f.setLength(0);
113
checkState(f, 0, 0);
114
115
// changing the file pointer beyond the end of file
116
f.seek(threeQuarterLength);
117
checkState(f, threeQuarterLength, 0);
118
119
// writing beyont the end of file
120
f.write(new byte[(int)quarterLength]);
121
checkState(f, fullLength, fullLength);
122
123
// negative file pointer
124
try {
125
f.seek(-1);
126
throw new RuntimeException("IOE not thrown");
127
} catch (IOException expected) {
128
}
129
checkState(f, fullLength, fullLength);
130
131
// truncating the file after failed seek
132
f.setLength(halfLength);
133
checkState(f, halfLength, halfLength);
134
135
// truncating after closing
136
f.close();
137
try {
138
f.setLength(halfLength);
139
throw new RuntimeException("IOE not thrown");
140
} catch (IOException expected) {
141
}
142
}
143
144
public static void main(String[] args) throws IOException {
145
File f28b = new File("f28b");
146
File f28K = new File("f28K");
147
File frnd = new File("frnd");
148
149
try (RandomAccessFile raf28b = new RandomAccessFile(f28b, "rw");
150
RandomAccessFile raf28K = new RandomAccessFile(f28K, "rw");
151
RandomAccessFile rafrnd = new RandomAccessFile(frnd, "rw")) {
152
test(raf28b, 7);
153
test(raf28K, 7 * 1024);
154
test(rafrnd, 1 + RandomFactory.getRandom().nextInt(16000));
155
}
156
157
// truncating read-only file
158
try (RandomAccessFile raf28b = new RandomAccessFile(f28b, "r")) {
159
raf28b.setLength(42);
160
throw new RuntimeException("IOE not thrown");
161
} catch (IOException expected) {
162
}
163
}
164
165
}
166
167