|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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 | +/* @test |
| 25 | + * @bug 8292562 |
| 26 | + * @summary Test transferTo and transferFrom when target is appending |
| 27 | + * @library /test/lib |
| 28 | + * @build jdk.test.lib.RandomFactory |
| 29 | + * @run main TransferToAppending |
| 30 | + * @key randomness |
| 31 | + */ |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.nio.ByteBuffer; |
| 35 | +import java.nio.file.Files; |
| 36 | +import java.nio.file.Path; |
| 37 | +import java.nio.channels.FileChannel; |
| 38 | +import java.util.Random; |
| 39 | +import jdk.test.lib.RandomFactory; |
| 40 | + |
| 41 | +import static java.nio.file.StandardOpenOption.*; |
| 42 | + |
| 43 | +public class TransferToAppending { |
| 44 | + private static final int MIN_SIZE = 128; |
| 45 | + private static final int MAX_SIZE = 32768; |
| 46 | + private static final Random RND = RandomFactory.getRandom(); |
| 47 | + |
| 48 | + public static void main(String... args) throws IOException { |
| 49 | + // Create files in size range [MIN_SIZE,MAX_SIZE) |
| 50 | + // filled with random bytes |
| 51 | + Path source = createFile("src"); |
| 52 | + Path target = createFile("tgt"); |
| 53 | + |
| 54 | + try (FileChannel src = FileChannel.open(source, READ, WRITE); |
| 55 | + FileChannel tgt = FileChannel.open(target, WRITE, APPEND);) { |
| 56 | + // Set source range to a subset of the source |
| 57 | + long size = Files.size(source); |
| 58 | + long position = RND.nextInt((int)size); |
| 59 | + long count = RND.nextInt((int)(size - position)); |
| 60 | + long tgtSize = Files.size(target); |
| 61 | + |
| 62 | + // Transfer subrange to target |
| 63 | + long nbytes = src.transferTo(position, count, tgt); |
| 64 | + |
| 65 | + long expectedSize = tgtSize + nbytes; |
| 66 | + |
| 67 | + if (Files.size(target) != expectedSize) { |
| 68 | + String msg = String.format("Bad size: expected %d, actual %d%n", |
| 69 | + expectedSize, Files.size(target)); |
| 70 | + throw new RuntimeException(msg); |
| 71 | + } |
| 72 | + |
| 73 | + tgt.close(); |
| 74 | + |
| 75 | + // Load subrange of source |
| 76 | + ByteBuffer bufSrc = ByteBuffer.allocate((int)nbytes); |
| 77 | + src.read(bufSrc, position); |
| 78 | + |
| 79 | + try (FileChannel res = FileChannel.open(target, READ, WRITE)) { |
| 80 | + // Load appended range of target |
| 81 | + ByteBuffer bufTgt = ByteBuffer.allocate((int)nbytes); |
| 82 | + res.read(bufTgt, tgtSize); |
| 83 | + |
| 84 | + // Subranges of values should be equal |
| 85 | + if (bufSrc.mismatch(bufTgt) != -1) { |
| 86 | + throw new RuntimeException("Range of values unequal"); |
| 87 | + } |
| 88 | + } |
| 89 | + } finally { |
| 90 | + Files.delete(source); |
| 91 | + Files.delete(target); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static Path createFile(String name) throws IOException { |
| 96 | + Path path = Files.createTempFile(name, ".dat"); |
| 97 | + try (FileChannel fc = FileChannel.open(path, CREATE, READ, WRITE)) { |
| 98 | + int size = Math.max(RND.nextInt(MAX_SIZE), 128); |
| 99 | + byte[] b = new byte[size]; |
| 100 | + RND.nextBytes(b); |
| 101 | + fc.write(ByteBuffer.wrap(b)); |
| 102 | + } |
| 103 | + return path; |
| 104 | + } |
| 105 | +} |
0 commit comments