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