/* * Copyright (c) 2022, 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. *
*/
/** * @test * @summary Verify that the outputstream created for zip file entries, through the ZipFileSystem * works fine for varying sizes of the zip file entries * @bug 8190753 8011146 8279536 * @run testng/timeout=300 ZipFSOutputStreamTest
*/ publicclass ZipFSOutputStreamTest { // List of files to be added to the ZIP file along with their sizes in bytes privatestaticfinal Map<String, Long> ZIP_ENTRIES = Map.of( "f1", Integer.MAX_VALUE + 1L, // a value which when cast to an integer, becomes a negative value "f2", 25L * 1024L * 1024L, // 25 MB "d1/f3", 1234L, "d1/d2/f4", 0L);
/** * Create a zip filesystem and write out entries of varying sizes using the outputstream returned * by the ZipFileSystem. Then verify that the generated zip file entries are as expected, * both in size and content
*/
@Test(dataProvider = "zipFSCreationEnv") publicvoid testOutputStream(final Map<String, ?> env) throws Exception { finalbyte[] chunk = newbyte[1024]; // fill it with some fixed content (the fixed content will later on help ease // the verification of the content written out)
Arrays.fill(chunk, (byte) 42); try (final FileSystem zipfs = FileSystems.newFileSystem(ZIP_FILE, env)) { // create the zip with varying sized entries for (final Map.Entry<String, Long> entry : ZIP_ENTRIES.entrySet()) { final Path entryPath = zipfs.getPath(entry.getKey()); if (entryPath.getParent() != null) {
Files.createDirectories(entryPath.getParent());
} long start = System.currentTimeMillis(); try (final OutputStream os = Files.newOutputStream(entryPath)) {
writeAsChunks(os, chunk, entry.getValue());
}
System.out.println("Wrote entry " + entryPath + " of bytes " + entry.getValue()
+ " in " + (System.currentTimeMillis() - start) + " milli seconds");
}
} // now verify the written content try (final FileSystem zipfs = FileSystems.newFileSystem(ZIP_FILE)) { for (final Map.Entry<String, Long> entry : ZIP_ENTRIES.entrySet()) { final Path entryPath = zipfs.getPath(entry.getKey()); try (final InputStream is = Files.newInputStream(entryPath)) { finalbyte[] buf = newbyte[chunk.length]; int numRead; long totalRead = 0; long start = System.currentTimeMillis(); while ((numRead = is.read(buf)) != -1) {
totalRead += numRead; // verify the content Assert.assertEquals(Arrays.mismatch(buf, 0, numRead, chunk, 0, numRead), -1, "Unexpected content in " + entryPath);
}
System.out.println("Read entry " + entryPath + " of bytes " + totalRead
+ " in " + (System.currentTimeMillis() - start) + " milli seconds"); Assert.assertEquals(totalRead, (long) entry.getValue(), "Unexpected number of bytes read from zip entry " + entryPath);
}
}
}
}
/** * Repeatedly writes out to the outputstream, the chunk of data, till the number of bytes * written to the stream equals the totalSize
*/ privatestaticvoid writeAsChunks(final OutputStream os, finalbyte[] chunk, finallong totalSize) throws IOException { long remaining = totalSize; for (long l = 0; l < totalSize; l += chunk.length) { finalint numToWrite = (int) Math.min(remaining, chunk.length);
os.write(chunk, 0, numToWrite);
remaining -= numToWrite;
}
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.16 Sekunden
(vorverarbeitet)
¤
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.