/* * Copyright (c) 2021, 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.
*/
privatestaticfinal Random RAND = RandomFactory.getRandom();
// The largest source from which to read all bytes privatestaticfinalint BIG_LENGTH = ArraysSupport.SOFT_MAX_ARRAY_LENGTH;
// A length greater than a 32-bit integer can accommodate privatestaticfinallong HUGE_LENGTH = Integer.MAX_VALUE + 27L;
// Current directory privatestaticfinal Path DIR = Path.of(System.getProperty("test.dir", "."));
// --- Framework ---
// Create a temporary file path static Path createFilePath() {
String name = String.format("ReadXBytes%d.tmp", System.nanoTime()); return DIR.resolve(name);
}
// Creates a temporary file of a specified length with undefined content static Path createFile(long length) throws IOException {
Path path = createFilePath();
path.toFile().deleteOnExit(); try (FileChannel fc = FileChannel.open(path, CREATE_NEW, SPARSE, WRITE)) { if (length > 0) {
fc.position(length - 1);
fc.write(ByteBuffer.wrap(newbyte[] {27}));
}
} return path;
}
// Creates a temporary file of a specified length with random content static Path createFileWithRandomContent(long length) throws IOException {
Path file = createFile(length); try (FileChannel fc = FileChannel.open(file, WRITE)) { if (length < 65536) { // if the length is less than 64k, write the entire file byte[] buf = newbyte[(int)length];
RAND.nextBytes(buf);
ByteBuffer bb = ByteBuffer.wrap(buf); while (bb.hasRemaining()) {
fc.write(bb);
}
} else { // write the first and the last 32k only byte[] buf = newbyte[32768];
RAND.nextBytes(buf);
ByteBuffer bb = ByteBuffer.wrap(buf); while (bb.hasRemaining()) {
fc.write(bb);
}
bb.clear();
fc.position(length - buf.length);
RAND.nextBytes(buf); while (bb.hasRemaining()) {
fc.write(bb);
}
}
} return file;
}
// Creates a file of a specified length
@FunctionalInterface interface FileCreator {
Path create(long length) throws IOException;
}
// Performs a test for checking edge cases
@FunctionalInterface interface EdgeTest { void test(long length, InputStream source) throws IOException;
}
// Performs a test for evaluating correctness of content
@FunctionalInterface interface DataTest { void test(long length, InputStream source, InputStream reference) throws IOException;
}
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 ist noch experimentell.