Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/RandomAccessFile/SetLength.java
38811 views
1
/*
2
* Copyright (c) 1997, 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
@summary General tests of the setLength method -- Should migrate to 1.2 JCK
26
*/
27
28
import java.io.*;
29
30
31
public class SetLength {
32
33
static void fail(String s) {
34
throw new RuntimeException(s);
35
}
36
37
static void go(File fn, int max) throws IOException {
38
int chunk = max / 4;
39
long i;
40
RandomAccessFile f;
41
42
f = new RandomAccessFile(fn, "rw");
43
f.setLength(2 * chunk);
44
if (f.length() != 2 * chunk) fail("Length not increased to " + (2 * chunk));
45
if ((i = f.getFilePointer()) != 0) fail("File pointer shifted to " + i);
46
byte[] buf = new byte[max];
47
f.write(buf);
48
if (f.length() != max) fail("Write didn't work");
49
if (f.getFilePointer() != max) fail("File pointer inconsistent");
50
f.setLength(3 * chunk);
51
if (f.length() != 3 * chunk) fail("Length not reduced to " + 3 * chunk);
52
if (f.getFilePointer() != 3 * chunk) fail("File pointer not shifted to " + (3 * chunk));
53
f.seek(1 * chunk);
54
if (f.getFilePointer() != 1 * chunk) fail("File pointer not shifted to " + (1 * chunk));
55
f.setLength(2 * chunk);
56
if (f.length() != 2 * chunk) fail("Length not reduced to " + (2 * chunk));
57
if (f.getFilePointer() != 1 * chunk) fail("File pointer not shifted to " + (1 * chunk));
58
f.close();
59
}
60
61
public static void main(String[] args) throws IOException {
62
File fn = new File("x.SetLength");
63
try {
64
go(fn, 20);
65
fn.delete();
66
go(fn, 64 * 1024);
67
RandomAccessFile f = new RandomAccessFile(fn, "r");
68
boolean thrown = false;
69
try {
70
f.setLength(3);
71
} catch (IOException x) {
72
thrown = true;
73
}
74
if (!thrown) fail("setLength succeeded on a file opened read-only");
75
f.close();
76
}
77
finally {
78
fn.delete();
79
}
80
}
81
82
}
83
84