Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/demo/zipfs/ZipFSTester.java
38833 views
1
/*
2
* Copyright (c) 2010, 2015, 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
import java.io.*;
25
import java.nio.*;
26
import java.nio.channels.*;
27
import java.nio.file.*;
28
import java.nio.file.spi.*;
29
import java.nio.file.attribute.*;
30
import java.net.*;
31
import java.util.*;
32
import java.util.concurrent.TimeUnit;
33
import java.util.zip.*;
34
35
import static java.nio.file.StandardOpenOption.*;
36
import static java.nio.file.StandardCopyOption.*;
37
38
/*
39
* Tests various zipfs operations.
40
*/
41
42
public class ZipFSTester {
43
44
public static void main(String[] args) throws Throwable {
45
46
try (FileSystem fs = newZipFileSystem(Paths.get(args[0]),
47
new HashMap<String, Object>()))
48
{
49
test0(fs);
50
test1(fs);
51
test2(fs); // more tests
52
testTime(Paths.get(args[0]));
53
test8069211();
54
}
55
}
56
57
static void test0(FileSystem fs)
58
throws Exception
59
{
60
List<String> list = new LinkedList<>();
61
try (ZipFile zf = new ZipFile(fs.toString())) {
62
Enumeration<? extends ZipEntry> zes = zf.entries();
63
while (zes.hasMoreElements()) {
64
list.add(zes.nextElement().getName());
65
}
66
for (String pname : list) {
67
Path path = fs.getPath(pname);
68
if (!Files.exists(path))
69
throw new RuntimeException("path existence check failed!");
70
while ((path = path.getParent()) != null) {
71
if (!Files.exists(path))
72
throw new RuntimeException("parent existence check failed!");
73
}
74
}
75
}
76
}
77
78
static void test1(FileSystem fs0)
79
throws Exception
80
{
81
Random rdm = new Random();
82
// clone a fs and test on it
83
Path tmpfsPath = getTempPath();
84
Map<String, Object> env = new HashMap<String, Object>();
85
env.put("create", "true");
86
try (FileSystem copy = newZipFileSystem(tmpfsPath, env)) {
87
z2zcopy(fs0, copy, "/", 0);
88
}
89
90
try (FileSystem fs = newZipFileSystem(tmpfsPath, new HashMap<String, Object>())) {
91
92
FileSystemProvider provider = fs.provider();
93
// newFileSystem(path...) should not throw exception
94
try (FileSystem fsPath = provider.newFileSystem(tmpfsPath, new HashMap<String, Object>())){}
95
try (FileSystem fsUri = provider.newFileSystem(
96
new URI("jar", tmpfsPath.toUri().toString(), null),
97
new HashMap<String, Object>()))
98
{
99
throw new RuntimeException("newFileSystem(uri...) does not throw exception");
100
} catch (FileSystemAlreadyExistsException fsaee) {}
101
102
// prepare a src
103
Path src = getTempPath();
104
String tmpName = src.toString();
105
OutputStream os = Files.newOutputStream(src);
106
byte[] bits = new byte[12345];
107
rdm.nextBytes(bits);
108
os.write(bits);
109
os.close();
110
111
try {
112
provider.newFileSystem(new File(System.getProperty("test.src", ".")).toPath(),
113
new HashMap<String, Object>());
114
throw new RuntimeException("newFileSystem() opens a directory as zipfs");
115
} catch (UnsupportedOperationException uoe) {}
116
117
try {
118
provider.newFileSystem(src, new HashMap<String, Object>());
119
throw new RuntimeException("newFileSystem() opens a non-zip file as zipfs");
120
} catch (UnsupportedOperationException uoe) {}
121
122
123
// copyin
124
Path dst = getPathWithParents(fs, tmpName);
125
Files.copy(src, dst);
126
checkEqual(src, dst);
127
128
// copy
129
Path dst2 = getPathWithParents(fs, "/xyz" + rdm.nextInt(100) +
130
"/efg" + rdm.nextInt(100) + "/foo.class");
131
Files.copy(dst, dst2);
132
//dst.moveTo(dst2);
133
checkEqual(src, dst2);
134
135
// delete
136
Files.delete(dst);
137
if (Files.exists(dst))
138
throw new RuntimeException("Failed!");
139
140
// moveout
141
Path dst3 = Paths.get(tmpName + "_Tmp");
142
Files.move(dst2, dst3);
143
checkEqual(src, dst3);
144
if (Files.exists(dst2))
145
throw new RuntimeException("Failed!");
146
147
// copyback + move
148
Files.copy(dst3, dst);
149
Path dst4 = getPathWithParents(fs, tmpName + "_Tmp0");
150
Files.move(dst, dst4);
151
checkEqual(src, dst4);
152
153
// delete
154
Files.delete(dst4);
155
if (Files.exists(dst4))
156
throw new RuntimeException("Failed!");
157
Files.delete(dst3);
158
if (Files.exists(dst3))
159
throw new RuntimeException("Failed!");
160
161
// move (existing entry)
162
Path dst5 = fs.getPath("META-INF/MANIFEST.MF");
163
if (Files.exists(dst5)) {
164
Path dst6 = fs.getPath("META-INF/MANIFEST.MF_TMP");
165
Files.move(dst5, dst6);
166
walk(fs.getPath("/"));
167
}
168
169
// newInputStream on dir
170
Path parent = dst2.getParent();
171
try {
172
Files.newInputStream(parent);
173
throw new RuntimeException("Failed");
174
} catch (FileSystemException e) {
175
e.printStackTrace(); // expected fse
176
}
177
178
// rmdirs
179
try {
180
rmdirs(parent);
181
} catch (IOException x) {
182
x.printStackTrace();
183
}
184
185
// newFileChannel() copy in, out and verify via fch
186
fchCopy(src, dst); // in
187
checkEqual(src, dst);
188
Path tmp = Paths.get(tmpName + "_Tmp");
189
fchCopy(dst, tmp); // out
190
checkEqual(src, tmp);
191
Files.delete(tmp);
192
193
// test channels
194
channel(fs, dst);
195
Files.delete(dst);
196
Files.delete(src);
197
} finally {
198
if (Files.exists(tmpfsPath))
199
Files.delete(tmpfsPath);
200
}
201
}
202
203
static void test2(FileSystem fs) throws Exception {
204
205
Path fs1Path = getTempPath();
206
Path fs2Path = getTempPath();
207
Path fs3Path = getTempPath();
208
209
// create a new filesystem, copy everything from fs
210
Map<String, Object> env = new HashMap<String, Object>();
211
env.put("create", "true");
212
FileSystem fs0 = newZipFileSystem(fs1Path, env);
213
214
final FileSystem fs2 = newZipFileSystem(fs2Path, env);
215
final FileSystem fs3 = newZipFileSystem(fs3Path, env);
216
217
System.out.println("copy src: fs -> fs0...");
218
z2zcopy(fs, fs0, "/", 0); // copy fs -> fs1
219
fs0.close(); // dump to file
220
221
System.out.println("open fs0 as fs1");
222
env = new HashMap<String, Object>();
223
final FileSystem fs1 = newZipFileSystem(fs1Path, env);
224
225
System.out.println("listing...");
226
final ArrayList<String> files = new ArrayList<>();
227
final ArrayList<String> dirs = new ArrayList<>();
228
list(fs1.getPath("/"), files, dirs);
229
230
Thread t0 = new Thread(new Runnable() {
231
public void run() {
232
List<String> list = new ArrayList<>(dirs);
233
Collections.shuffle(list);
234
for (String path : list) {
235
try {
236
z2zcopy(fs1, fs2, path, 0);
237
} catch (Exception x) {
238
x.printStackTrace();
239
}
240
}
241
}
242
243
});
244
245
Thread t1 = new Thread(new Runnable() {
246
public void run() {
247
List<String> list = new ArrayList<>(dirs);
248
Collections.shuffle(list);
249
for (String path : list) {
250
try {
251
z2zcopy(fs1, fs2, path, 1);
252
} catch (Exception x) {
253
x.printStackTrace();
254
}
255
}
256
}
257
258
});
259
260
Thread t2 = new Thread(new Runnable() {
261
public void run() {
262
List<String> list = new ArrayList<>(dirs);
263
Collections.shuffle(list);
264
for (String path : list) {
265
try {
266
z2zcopy(fs1, fs2, path, 2);
267
} catch (Exception x) {
268
x.printStackTrace();
269
}
270
}
271
}
272
273
});
274
275
Thread t3 = new Thread(new Runnable() {
276
public void run() {
277
List<String> list = new ArrayList<>(files);
278
Collections.shuffle(list);
279
while (!list.isEmpty()) {
280
Iterator<String> itr = list.iterator();
281
while (itr.hasNext()) {
282
String path = itr.next();
283
try {
284
if (Files.exists(fs2.getPath(path))) {
285
z2zmove(fs2, fs3, path);
286
itr.remove();
287
}
288
} catch (FileAlreadyExistsException x){
289
itr.remove();
290
} catch (Exception x) {
291
x.printStackTrace();
292
}
293
}
294
}
295
}
296
297
});
298
299
System.out.println("copying/removing...");
300
t0.start(); t1.start(); t2.start(); t3.start();
301
t0.join(); t1.join(); t2.join(); t3.join();
302
303
System.out.println("closing: fs1, fs2");
304
fs1.close();
305
fs2.close();
306
307
int failed = 0;
308
System.out.println("checkEqual: fs vs fs3");
309
for (String path : files) {
310
try {
311
checkEqual(fs.getPath(path), fs3.getPath(path));
312
} catch (IOException x) {
313
//x.printStackTrace();
314
failed++;
315
}
316
}
317
System.out.println("closing: fs3");
318
fs3.close();
319
320
System.out.println("opening: fs3 as fs4");
321
FileSystem fs4 = newZipFileSystem(fs3Path, env);
322
323
324
ArrayList<String> files2 = new ArrayList<>();
325
ArrayList<String> dirs2 = new ArrayList<>();
326
list(fs4.getPath("/"), files2, dirs2);
327
328
System.out.println("checkEqual: fs vs fs4");
329
for (String path : files2) {
330
checkEqual(fs.getPath(path), fs4.getPath(path));
331
}
332
System.out.println("walking: fs4");
333
walk(fs4.getPath("/"));
334
System.out.println("closing: fs4");
335
fs4.close();
336
System.out.printf("failed=%d%n", failed);
337
338
Files.delete(fs1Path);
339
Files.delete(fs2Path);
340
Files.delete(fs3Path);
341
}
342
343
// test file stamp
344
static void testTime(Path src) throws Exception {
345
BasicFileAttributes attrs = Files
346
.getFileAttributeView(src, BasicFileAttributeView.class)
347
.readAttributes();
348
// create a new filesystem, copy this file into it
349
Map<String, Object> env = new HashMap<String, Object>();
350
env.put("create", "true");
351
Path fsPath = getTempPath();
352
FileSystem fs = newZipFileSystem(fsPath, env);
353
354
System.out.println("test copy with timestamps...");
355
// copyin
356
Path dst = getPathWithParents(fs, "me");
357
Files.copy(src, dst, COPY_ATTRIBUTES);
358
checkEqual(src, dst);
359
System.out.println("mtime: " + attrs.lastModifiedTime());
360
System.out.println("ctime: " + attrs.creationTime());
361
System.out.println("atime: " + attrs.lastAccessTime());
362
System.out.println(" ==============>");
363
BasicFileAttributes dstAttrs = Files
364
.getFileAttributeView(dst, BasicFileAttributeView.class)
365
.readAttributes();
366
System.out.println("mtime: " + dstAttrs.lastModifiedTime());
367
System.out.println("ctime: " + dstAttrs.creationTime());
368
System.out.println("atime: " + dstAttrs.lastAccessTime());
369
370
// 1-second granularity
371
if (attrs.lastModifiedTime().to(TimeUnit.SECONDS) !=
372
dstAttrs.lastModifiedTime().to(TimeUnit.SECONDS) ||
373
attrs.lastAccessTime().to(TimeUnit.SECONDS) !=
374
dstAttrs.lastAccessTime().to(TimeUnit.SECONDS) ||
375
attrs.creationTime().to(TimeUnit.SECONDS) !=
376
dstAttrs.creationTime().to(TimeUnit.SECONDS)) {
377
throw new RuntimeException("Timestamp Copy Failed!");
378
}
379
Files.delete(fsPath);
380
}
381
382
static void test8069211() throws Exception {
383
// create a new filesystem, copy this file into it
384
Map<String, Object> env = new HashMap<String, Object>();
385
env.put("create", "true");
386
Path fsPath = getTempPath();
387
try (FileSystem fs = newZipFileSystem(fsPath, env);) {
388
OutputStream out = Files.newOutputStream(fs.getPath("/foo"));
389
out.write("hello".getBytes());
390
out.close();
391
out.close();
392
}
393
try (FileSystem fs = newZipFileSystem(fsPath, new HashMap<String, Object>())) {
394
if (!Arrays.equals(Files.readAllBytes(fs.getPath("/foo")),
395
"hello".getBytes())) {
396
throw new RuntimeException("entry close() failed");
397
}
398
} catch (Exception x) {
399
throw new RuntimeException("entry close() failed", x);
400
} finally {
401
Files.delete(fsPath);
402
}
403
}
404
405
private static FileSystem newZipFileSystem(Path path, Map<String, ?> env)
406
throws Exception
407
{
408
return FileSystems.newFileSystem(
409
new URI("jar", path.toUri().toString(), null), env, null);
410
}
411
412
private static Path getTempPath() throws IOException
413
{
414
File tmp = File.createTempFile("testzipfs_", "zip");
415
tmp.delete(); // we need a clean path, no file
416
return tmp.toPath();
417
}
418
419
private static void list(Path path, List<String> files, List<String> dirs )
420
throws IOException
421
{
422
if (Files.isDirectory(path)) {
423
try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
424
for (Path child : ds)
425
list(child, files, dirs);
426
}
427
dirs.add(path.toString());
428
} else {
429
files.add(path.toString());
430
}
431
}
432
433
private static void z2zcopy(FileSystem src, FileSystem dst, String path,
434
int method)
435
throws IOException
436
{
437
Path srcPath = src.getPath(path);
438
Path dstPath = dst.getPath(path);
439
440
if (Files.isDirectory(srcPath)) {
441
if (!Files.exists(dstPath)) {
442
try {
443
mkdirs(dstPath);
444
} catch (FileAlreadyExistsException x) {}
445
}
446
try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
447
for (Path child : ds) {
448
z2zcopy(src, dst,
449
path + (path.endsWith("/")?"":"/") + child.getFileName(),
450
method);
451
}
452
}
453
} else {
454
try {
455
if (Files.exists(dstPath))
456
return;
457
switch (method) {
458
case 0:
459
Files.copy(srcPath, dstPath);
460
break;
461
case 1:
462
chCopy(srcPath, dstPath);
463
break;
464
case 2:
465
//fchCopy(srcPath, dstPath);
466
streamCopy(srcPath, dstPath);
467
break;
468
}
469
} catch (FileAlreadyExistsException x) {}
470
}
471
}
472
473
private static void z2zmove(FileSystem src, FileSystem dst, String path)
474
throws IOException
475
{
476
Path srcPath = src.getPath(path);
477
Path dstPath = dst.getPath(path);
478
479
if (Files.isDirectory(srcPath)) {
480
if (!Files.exists(dstPath))
481
mkdirs(dstPath);
482
try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
483
for (Path child : ds) {
484
z2zmove(src, dst,
485
path + (path.endsWith("/")?"":"/") + child.getFileName());
486
}
487
}
488
} else {
489
//System.out.println("moving..." + path);
490
Path parent = dstPath.getParent();
491
if (parent != null && Files.notExists(parent))
492
mkdirs(parent);
493
Files.move(srcPath, dstPath);
494
}
495
}
496
497
private static void walk(Path path) throws IOException
498
{
499
Files.walkFileTree(
500
path,
501
new SimpleFileVisitor<Path>() {
502
private int indent = 0;
503
private void indent() {
504
int n = 0;
505
while (n++ < indent)
506
System.out.printf(" ");
507
}
508
509
@Override
510
public FileVisitResult visitFile(Path file,
511
BasicFileAttributes attrs)
512
{
513
indent();
514
System.out.printf("%s%n", file.getFileName().toString());
515
return FileVisitResult.CONTINUE;
516
}
517
518
@Override
519
public FileVisitResult preVisitDirectory(Path dir,
520
BasicFileAttributes attrs)
521
{
522
indent();
523
System.out.printf("[%s]%n", dir.toString());
524
indent += 2;
525
return FileVisitResult.CONTINUE;
526
}
527
528
@Override
529
public FileVisitResult postVisitDirectory(Path dir,
530
IOException ioe)
531
throws IOException
532
{
533
indent -= 2;
534
return FileVisitResult.CONTINUE;
535
}
536
});
537
}
538
539
private static void mkdirs(Path path) throws IOException {
540
if (Files.exists(path))
541
return;
542
path = path.toAbsolutePath();
543
Path parent = path.getParent();
544
if (parent != null) {
545
if (Files.notExists(parent))
546
mkdirs(parent);
547
}
548
Files.createDirectory(path);
549
}
550
551
private static void rmdirs(Path path) throws IOException {
552
while (path != null && path.getNameCount() != 0) {
553
Files.delete(path);
554
path = path.getParent();
555
}
556
}
557
558
// check the content of two paths are equal
559
private static void checkEqual(Path src, Path dst) throws IOException
560
{
561
//System.out.printf("checking <%s> vs <%s>...%n",
562
// src.toString(), dst.toString());
563
564
//streams
565
byte[] bufSrc = new byte[8192];
566
byte[] bufDst = new byte[8192];
567
try (InputStream isSrc = Files.newInputStream(src);
568
InputStream isDst = Files.newInputStream(dst))
569
{
570
int nSrc = 0;
571
while ((nSrc = isSrc.read(bufSrc)) != -1) {
572
int nDst = 0;
573
while (nDst < nSrc) {
574
int n = isDst.read(bufDst, nDst, nSrc - nDst);
575
if (n == -1) {
576
System.out.printf("checking <%s> vs <%s>...%n",
577
src.toString(), dst.toString());
578
throw new RuntimeException("CHECK FAILED!");
579
}
580
nDst += n;
581
}
582
while (--nSrc >= 0) {
583
if (bufSrc[nSrc] != bufDst[nSrc]) {
584
System.out.printf("checking <%s> vs <%s>...%n",
585
src.toString(), dst.toString());
586
throw new RuntimeException("CHECK FAILED!");
587
}
588
nSrc--;
589
}
590
}
591
}
592
593
// channels
594
try (SeekableByteChannel chSrc = Files.newByteChannel(src);
595
SeekableByteChannel chDst = Files.newByteChannel(dst))
596
{
597
if (chSrc.size() != chDst.size()) {
598
System.out.printf("src[%s].size=%d, dst[%s].size=%d%n",
599
chSrc.toString(), chSrc.size(),
600
chDst.toString(), chDst.size());
601
throw new RuntimeException("CHECK FAILED!");
602
}
603
ByteBuffer bbSrc = ByteBuffer.allocate(8192);
604
ByteBuffer bbDst = ByteBuffer.allocate(8192);
605
606
int nSrc = 0;
607
while ((nSrc = chSrc.read(bbSrc)) != -1) {
608
int nDst = chDst.read(bbDst);
609
if (nSrc != nDst) {
610
System.out.printf("checking <%s> vs <%s>...%n",
611
src.toString(), dst.toString());
612
throw new RuntimeException("CHECK FAILED!");
613
}
614
while (--nSrc >= 0) {
615
if (bbSrc.get(nSrc) != bbDst.get(nSrc)) {
616
System.out.printf("checking <%s> vs <%s>...%n",
617
src.toString(), dst.toString());
618
throw new RuntimeException("CHECK FAILED!");
619
}
620
nSrc--;
621
}
622
bbSrc.flip();
623
bbDst.flip();
624
}
625
626
// Check if source read position is at the end
627
if (chSrc.position() != chSrc.size()) {
628
System.out.printf("src[%s]: size=%d, position=%d%n",
629
chSrc.toString(), chSrc.size(), chSrc.position());
630
throw new RuntimeException("CHECK FAILED!");
631
}
632
633
// Check if destination read position is at the end
634
if (chDst.position() != chDst.size()) {
635
System.out.printf("dst[%s]: size=%d, position=%d%n",
636
chDst.toString(), chDst.size(), chDst.position());
637
throw new RuntimeException("CHECK FAILED!");
638
}
639
} catch (IOException x) {
640
x.printStackTrace();
641
}
642
}
643
644
private static void fchCopy(Path src, Path dst) throws IOException
645
{
646
Set<OpenOption> read = new HashSet<>();
647
read.add(READ);
648
Set<OpenOption> openwrite = new HashSet<>();
649
openwrite.add(CREATE_NEW);
650
openwrite.add(WRITE);
651
652
try (FileChannel srcFc = src.getFileSystem()
653
.provider()
654
.newFileChannel(src, read);
655
FileChannel dstFc = dst.getFileSystem()
656
.provider()
657
.newFileChannel(dst, openwrite))
658
{
659
ByteBuffer bb = ByteBuffer.allocate(8192);
660
while (srcFc.read(bb) >= 0) {
661
bb.flip();
662
dstFc.write(bb);
663
bb.clear();
664
}
665
}
666
}
667
668
private static void chCopy(Path src, Path dst) throws IOException
669
{
670
Set<OpenOption> read = new HashSet<>();
671
read.add(READ);
672
Set<OpenOption> openwrite = new HashSet<>();
673
openwrite.add(CREATE_NEW);
674
openwrite.add(WRITE);
675
676
try (SeekableByteChannel srcCh = Files.newByteChannel(src, read);
677
SeekableByteChannel dstCh = Files.newByteChannel(dst, openwrite))
678
{
679
680
ByteBuffer bb = ByteBuffer.allocate(8192);
681
while (srcCh.read(bb) >= 0) {
682
bb.flip();
683
dstCh.write(bb);
684
bb.clear();
685
}
686
687
// Check if source read position is at the end
688
if (srcCh.position() != srcCh.size()) {
689
System.out.printf("src[%s]: size=%d, position=%d%n",
690
srcCh.toString(), srcCh.size(), srcCh.position());
691
throw new RuntimeException("CHECK FAILED!");
692
}
693
694
// Check if destination write position is at the end
695
if (dstCh.position() != dstCh.size()) {
696
System.out.printf("dst[%s]: size=%d, position=%d%n",
697
dstCh.toString(), dstCh.size(), dstCh.position());
698
throw new RuntimeException("CHECK FAILED!");
699
}
700
}
701
}
702
703
private static void streamCopy(Path src, Path dst) throws IOException
704
{
705
byte[] buf = new byte[8192];
706
try (InputStream isSrc = Files.newInputStream(src);
707
OutputStream osDst = Files.newOutputStream(dst))
708
{
709
int n = 0;
710
while ((n = isSrc.read(buf)) != -1) {
711
osDst.write(buf, 0, n);
712
}
713
}
714
}
715
716
static void channel(FileSystem fs, Path path)
717
throws Exception
718
{
719
System.out.println("test ByteChannel...");
720
Set<OpenOption> read = new HashSet<>();
721
read.add(READ);
722
int n = 0;
723
ByteBuffer bb = null;
724
ByteBuffer bb2 = null;
725
int N = 120;
726
727
try (SeekableByteChannel sbc = Files.newByteChannel(path)) {
728
System.out.printf(" sbc[0]: pos=%d, size=%d%n", sbc.position(), sbc.size());
729
if (sbc.position() != 0) {
730
throw new RuntimeException("CHECK FAILED!");
731
}
732
733
bb = ByteBuffer.allocate((int)sbc.size());
734
n = sbc.read(bb);
735
System.out.printf(" sbc[1]: read=%d, pos=%d, size=%d%n",
736
n, sbc.position(), sbc.size());
737
if (sbc.position() != sbc.size()) {
738
throw new RuntimeException("CHECK FAILED!");
739
}
740
bb2 = ByteBuffer.allocate((int)sbc.size());
741
}
742
743
// sbc.position(pos) is not supported in current version
744
// try the FileChannel
745
try (SeekableByteChannel sbc = fs.provider().newFileChannel(path, read)) {
746
sbc.position(N);
747
System.out.printf(" sbc[2]: pos=%d, size=%d%n",
748
sbc.position(), sbc.size());
749
if (sbc.position() != N) {
750
throw new RuntimeException("CHECK FAILED!");
751
}
752
bb2.limit(100);
753
n = sbc.read(bb2);
754
System.out.printf(" sbc[3]: read=%d, pos=%d, size=%d%n",
755
n, sbc.position(), sbc.size());
756
if (n < 0 || sbc.position() != (N + n)) {
757
throw new RuntimeException("CHECK FAILED!");
758
}
759
System.out.printf(" sbc[4]: bb[%d]=%d, bb1[0]=%d%n",
760
N, bb.get(N) & 0xff, bb2.get(0) & 0xff);
761
}
762
}
763
764
// create parents if does not exist
765
static Path getPathWithParents(FileSystem fs, String name)
766
throws Exception
767
{
768
Path path = fs.getPath(name);
769
Path parent = path.getParent();
770
if (parent != null && Files.notExists(parent))
771
mkdirs(parent);
772
return path;
773
}
774
}
775
776