/* * Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
// clone a fs from fs0 and test on it
Path tmpfsPath = getTempPath();
Map<String, Object> env = new HashMap<String, Object>();
env.put("create", "true"); try (FileSystem copy = newZipFileSystem(tmpfsPath, env)) {
z2zcopy(fs0, copy, "/", 0);
// copy the test jar itself in
Files.copy(Paths.get(fs0.toString()), copy.getPath("/foo.jar"));
Path zpath = copy.getPath("/foo.jar"); try (FileSystem zzfs = FileSystems.newFileSystem(zpath)) {
Files.copy(src, zzfs.getPath("/srcInjarjar"));
}
}
try (FileSystem fs = newZipFileSystem(tmpfsPath, new HashMap<String, Object>())) {
FileSystemProvider provider = fs.provider(); // newFileSystem(path...) should not throw exception try (FileSystem fsPath = provider.newFileSystem(tmpfsPath, new HashMap<String, Object>())){} try (FileSystem fsUri = provider.newFileSystem( new URI("jar", tmpfsPath.toUri().toString(), null), new HashMap<String, Object>()))
{ thrownew RuntimeException("newFileSystem(URI...) does not throw exception");
} catch (FileSystemAlreadyExistsException fsaee) {}
try {
provider.newFileSystem(new File(System.getProperty("test.src", ".")).toPath(), new HashMap<String, Object>()); thrownew RuntimeException("newFileSystem() opens a directory as zipfs");
} catch (UnsupportedOperationException uoe) {}
try {
provider.newFileSystem(src, new HashMap<String, Object>()); thrownew RuntimeException("newFileSystem() opens a non-zip file as zipfs");
} catch (UnsupportedOperationException uoe) {}
// newFileChannel() copy in, out and verify via fch
fchCopy(src, dst); // in
checkEqual(src, dst);
Path tmp = Paths.get(tmpName + "_Tmp");
fchCopy(dst, tmp); // out
checkEqual(src, tmp);
Files.delete(tmp);
// test channels
channel(fs, dst);
Files.delete(dst);
System.out.println("open fs0 as fs1");
env = new HashMap<String, Object>(); final FileSystem fs1 = newZipFileSystem(fs1Path, env);
System.out.println("listing..."); final ArrayList<String> files = new ArrayList<>(); final ArrayList<String> dirs = new ArrayList<>();
list(fs1.getPath("/"), files, dirs);
// channels -- via sun.nio.ch.ChannelInputStream try (SeekableByteChannel sbc = Files.newByteChannel(path);
InputStream is = Channels.newInputStream(sbc)) {
// check all bytes match if (!Arrays.equals(is.readAllBytes(), expected)) {
System.out.printf(" newByteChannel <%s> failed...%n", path.toString()); thrownew RuntimeException("CHECK FAILED!");
}
// Check if read position is at the end if (sbc.position() != expected.length) {
System.out.printf("pos [%s]: size=%d, position=%d%n",
path.toString(), expected.length, sbc.position()); thrownew RuntimeException("CHECK FAILED!");
}
// Check position(x) + read() at the random/specific pos/len byte[] buf = newbyte[1024];
ByteBuffer bb = ByteBuffer.wrap(buf); for (int i = 0; i < 10; i++) { int pos = 0; int len = 0; if (expected.length > 0) {
pos = rdm.nextInt((int) sbc.size());
len = rdm.nextInt(Math.min(buf.length, expected.length - pos));
} // System.out.printf(" --> %d, %d%n", pos, len);
bb.position(0).limit(len); // bb.flip().limit(len); int expectedReadResult = sbc.size() == 0 ? -1 : len; if (sbc.position(pos).position() != pos ||
sbc.read(bb) != expectedReadResult ||
!Arrays.equals(buf, 0, bb.position(), expected, pos, pos + len)) {
System.out.printf("read()/position() failed%n"); thrownew RuntimeException("CHECK FAILED!");
}
}
} catch (IOException x) {
x.printStackTrace(); thrownew RuntimeException("CHECK FAILED!");
}
}
// test entry stream/channel reading staticvoid testStreamChannel() throws Exception {
Path zpath = getTempPath(); try { var crc = new CRC32();
Object[][] entries = getEntries();
// [1] create zip via ZipOutputStream try (var os = Files.newOutputStream(zpath); var zos = new ZipOutputStream(os)) { for (Object[] entry : entries) { var ze = new ZipEntry((String)entry[0]); int method = (int)entry[1]; byte[] bytes = (byte[])entry[2]; if (method == METHOD_STORED) {
ze.setSize(bytes.length);
crc.reset();
crc.update(bytes);
ze.setCrc(crc.getValue());
}
ze.setMethod(method);
zos.putNextEntry(ze);
zos.write(bytes);
zos.closeEntry();
}
} try (var zfs = newZipFileSystem(zpath, Collections.emptyMap())) { for (Object[] e : entries) {
Path path = zfs.getPath((String)e[0]); byte[] bytes = (byte[])e[2];
checkRead(path, bytes);
}
}
Files.deleteIfExists(zpath);
// [2] create zip via zfs.newByteChannel try (var zfs = newZipFileSystem(zpath, Map.of("create", "true"))) { for (Object[] e : entries) { // tbd: method is not used try (var sbc = Files.newByteChannel(zfs.getPath((String)e[0]),
CREATE_NEW, WRITE)) {
sbc.write(ByteBuffer.wrap((byte[])e[2]));
}
}
} try (var zfs = newZipFileSystem(zpath, Collections.emptyMap())) { for (Object[] e : entries) {
checkRead(zfs.getPath((String)e[0]), (byte[])e[2]);
}
}
Files.deleteIfExists(zpath);
// [3] create zip via Files.write()/newoutputStream/ try (var zfs = newZipFileSystem(zpath, Map.of("create", "true"))) { for (Object[] e : entries) {
Files.write(zfs.getPath((String)e[0]), (byte[])e[2]);
}
} try (var zfs = newZipFileSystem(zpath, Collections.emptyMap())) { for (Object[] e : entries) {
checkRead(zfs.getPath((String)e[0]), (byte[])e[2]);
}
}
Files.deleteIfExists(zpath);
// [4] create zip via zfs.newByteChannel, with "method_stored" try (var zfs = newZipFileSystem(zpath,
Map.of("create", true, "noCompression", true))) { for (Object[] e : entries) { try (var sbc = Files.newByteChannel(zfs.getPath((String)e[0]),
CREATE_NEW, WRITE)) {
sbc.write(ByteBuffer.wrap((byte[])e[2]));
}
}
} try (var zfs = newZipFileSystem(zpath, Collections.emptyMap())) { for (Object[] e : entries) {
checkRead(zfs.getPath((String)e[0]), (byte[])e[2]);
}
}
Files.deleteIfExists(zpath);
// file name with space character for URI to quote it
File tmp = File.createTempFile("test zipfs", "zip");
tmp.delete(); // we need a clean path, no file
Path fsPath = tmp.toPath(); try (FileSystem fs = newZipFileSystem(fsPath, env);) {
Files.write(fs.getPath("/foo"), "hello".getBytes());
URI fooUri = fs.getPath("/foo").toUri(); if (!Arrays.equals(Files.readAllBytes(Paths.get(fooUri)), "hello".getBytes())) { thrownew RuntimeException("entry close() failed");
}
} finally {
Files.delete(fsPath);
}
}
privatestatic FileSystem newZipFileSystem(Path path, Map<String, ?> env) throws Exception
{ // Use URLDecoder (for test only) to remove the double escaped space // character. For the path which is not encoded by UTF-8, we need to // replace "+" by "%2b" manually before feeding into the decoder. // Otherwise, the URLDecoder would replace "+" by " ", which may // raise NoSuchFileException. // // eg. var path = "file:///jdk-18+9/basic.jar"; // URLDecoder.decode(path, "utf8") -> file:///jdk-18 9/basic.jar // // Also, we should not use URLEncoder in case of the path has been // encoded. return FileSystems.newFileSystem( new URI("jar", URLDecoder.decode(path.toUri().toString()
.replace("+", "%2b"), "utf8"), null), env, null);
}
privatestatic Path getTempPath() throws IOException
{
File tmp = File.createTempFile("testzipfs_", "zip");
tmp.delete(); // we need a clean path, no file return tmp.toPath();
}
// check the content of two paths are equal privatestaticvoid checkEqual(Path src, Path dst) throws IOException
{
System.out.printf("checking <%s> vs <%s>...%n",
src.toString(), dst.toString());
//streams byte[] bufSrc = newbyte[8192]; byte[] bufDst = newbyte[8192]; try (InputStream isSrc = Files.newInputStream(src);
InputStream isDst = Files.newInputStream(dst))
{ int nSrc = 0; while ((nSrc = isSrc.read(bufSrc)) != -1) { int nDst = 0; while (nDst < nSrc) { int n = isDst.read(bufDst, nDst, nSrc - nDst); if (n == -1) {
System.out.printf("checking <%s> vs <%s>...%n",
src.toString(), dst.toString()); thrownew RuntimeException("CHECK FAILED!");
}
nDst += n;
} while (--nSrc >= 0) { if (bufSrc[nSrc] != bufDst[nSrc]) {
System.out.printf("checking <%s> vs <%s>...%n",
src.toString(), dst.toString()); thrownew RuntimeException("CHECK FAILED!");
}
nSrc--;
}
}
}
int nSrc = 0; while ((nSrc = chSrc.read(bbSrc)) != -1) { int nDst = chDst.read(bbDst); if (nSrc != nDst) {
System.out.printf("checking <%s> vs <%s>...%n",
src.toString(), dst.toString()); thrownew RuntimeException("CHECK FAILED!");
} while (--nSrc >= 0) { if (bbSrc.get(nSrc) != bbDst.get(nSrc)) {
System.out.printf("checking <%s> vs <%s>...%n",
src.toString(), dst.toString()); thrownew RuntimeException("CHECK FAILED!");
}
nSrc--;
}
bbSrc.flip();
bbDst.flip();
}
// Check if source read position is at the end if (chSrc.position() != chSrc.size()) {
System.out.printf("src[%s]: size=%d, position=%d%n",
chSrc.toString(), chSrc.size(), chSrc.position()); thrownew RuntimeException("CHECK FAILED!");
}
// Check if destination read position is at the end if (chDst.position() != chDst.size()) {
System.out.printf("dst[%s]: size=%d, position=%d%n",
chDst.toString(), chDst.size(), chDst.position()); thrownew RuntimeException("CHECK FAILED!");
}
// Check position(x) + read() at the specific pos/len for (int i = 0; i < 10; i++) { int pos = rdm.nextInt((int)chSrc.size()); int limit = rdm.nextInt(1024); if (chSrc.position(pos).position() != chDst.position(pos).position()) {
System.out.printf("dst/src.position(pos failed%n");
}
bbSrc.clear().limit(limit);
bbDst.clear().limit(limit); if (chSrc.read(bbSrc) != chDst.read(bbDst) ||
!bbSrc.flip().equals(bbDst.flip())) {
System.out.printf("dst/src.read() failed%n");
}
}
} catch (IOException x) {
x.printStackTrace();
}
}
// Check if source read position is at the end if (srcCh.position() != srcCh.size()) {
System.out.printf("src[%s]: size=%d, position=%d%n",
srcCh.toString(), srcCh.size(), srcCh.position()); thrownew RuntimeException("CHECK FAILED!");
}
// Check if destination write position is at the end if (dstCh.position() != dstCh.size()) {
System.out.printf("dst[%s]: size=%d, position=%d%n",
dstCh.toString(), dstCh.size(), dstCh.position()); thrownew RuntimeException("CHECK FAILED!");
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.