8294437: java/nio/channels/FileChannel tests slow on Windows

Reviewed-by: alanb, bpb
This commit is contained in:
Daniel Jeliński 2022-10-03 17:42:56 +00:00
parent c6e3daa5fa
commit a4f2078bd6
6 changed files with 348 additions and 273 deletions

View File

@ -0,0 +1,44 @@
/*
* 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.
*/
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import static java.nio.file.StandardOpenOption.*;
/**
* Common library for various test file utility functions.
*/
public final class FileChannelUtils {
public static Path createSparseTempFile(String prefix, String suffix) throws IOException {
Path file = Files.createTempFile(prefix, suffix);
Files.delete(file); // need CREATE_NEW to make the file sparse
FileChannel fc = FileChannel.open(file, CREATE_NEW, SPARSE, WRITE);
fc.close();
return file;
}
}

View File

@ -21,14 +21,18 @@
* questions. * questions.
*/ */
import jdk.test.lib.RandomFactory;
import java.lang.foreign.MemorySegment; import java.lang.foreign.MemorySegment;
import java.lang.foreign.MemorySession; import java.lang.foreign.MemorySession;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import static java.nio.file.StandardOpenOption.*; import static java.nio.file.StandardOpenOption.*;
/* /*
@ -38,42 +42,46 @@ import static java.nio.file.StandardOpenOption.*;
* @summary Ensure that memory mapping beyond 32-bit range does not cause an * @summary Ensure that memory mapping beyond 32-bit range does not cause an
* EXCEPTION_ACCESS_VIOLATION. * EXCEPTION_ACCESS_VIOLATION.
* @requires vm.bits == 64 * @requires vm.bits == 64
* @run main/othervm/timeout=240 LargeMapTest * @library /test/lib
* @build jdk.test.lib.RandomFactory FileChannelUtils
* @run main/othervm LargeMapTest
* @key randomness
*/ */
public class LargeMapTest { public class LargeMapTest {
private static final String FILE = "test.dat"; private static final long LENGTH = (1L << 32) + 512;
private static final long LENGTH = 8000000000L; private static final int EXTRA = 1024;
private static final long OFFSET = 3704800000L; private static final long BASE = LENGTH - EXTRA;
private static final int BUFSIZ = 100000; private static final Random GEN = RandomFactory.getRandom();
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
System.out.println(System.getProperty("sun.arch.data.model")); System.out.println(System.getProperty("sun.arch.data.model"));
System.out.println(System.getProperty("os.arch")); System.out.println(System.getProperty("os.arch"));
System.out.println(System.getProperty("java.version")); System.out.println(System.getProperty("java.version"));
Path p = Path.of(FILE); System.out.println(" Writing large file...");
long t0 = System.nanoTime();
Path p = FileChannelUtils.createSparseTempFile("test", ".dat");
p.toFile().deleteOnExit(); p.toFile().deleteOnExit();
try (FileChannel fc = FileChannel.open(p, CREATE, WRITE)) { ByteBuffer bb;
fc.position(LENGTH - 1); try (FileChannel fc = FileChannel.open(p, WRITE)) {
fc.write(ByteBuffer.wrap(new byte[] {27})); fc.position(BASE);
byte[] b = new byte[EXTRA];
GEN.nextBytes(b);
bb = ByteBuffer.wrap(b);
fc.write(bb);
long t1 = System.nanoTime();
System.out.printf(" Wrote large file in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
} }
bb.rewind();
long offset = OFFSET; try (FileChannel fc = FileChannel.open(p, READ, WRITE)) {
ByteBuffer bb = ByteBuffer.allocateDirect(BUFSIZ);
try (FileChannel fc = FileChannel.open(p, READ, WRITE);) {
MemorySegment mbb = MemorySegment.ofBuffer(bb);
MemorySegment mappedMemorySegment = MemorySegment mappedMemorySegment =
fc.map(FileChannel.MapMode.READ_WRITE, 0, p.toFile().length(), fc.map(FileChannel.MapMode.READ_WRITE, 0, p.toFile().length(),
MemorySession.openImplicit()); MemorySession.openImplicit());
MemorySegment target = mappedMemorySegment.asSlice(BASE, EXTRA);
final int interval = BUFSIZ*1000; if (!target.asByteBuffer().equals(bb)) {
while (offset < LENGTH) { throw new RuntimeException("Expected buffers to be equal");
if (offset % interval == 0)
System.out.println("offset: " + offset);
MemorySegment target = mappedMemorySegment.asSlice(offset, BUFSIZ);
offset = offset + BUFSIZ;
target.copyFrom(mbb);
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -36,6 +36,7 @@ import java.nio.file.Files;
import static java.nio.file.StandardOpenOption.*; import static java.nio.file.StandardOpenOption.*;
import static java.nio.charset.StandardCharsets.*; import static java.nio.charset.StandardCharsets.*;
import java.util.Random; import java.util.Random;
import java.util.concurrent.TimeUnit;
/** /**
@ -56,21 +57,42 @@ public class MapTest {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
blah = File.createTempFile("blah", null); blah = File.createTempFile("blah", null);
blah.deleteOnExit(); blah.deleteOnExit();
long t0 = System.nanoTime();
initTestFile(blah); initTestFile(blah);
long t1 = System.nanoTime();
out.printf("Test file %s initialized in %d ns (%d ms) %n",
blah, t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
try { try {
out.println("Test file " + blah + " initialized");
testZero(); testZero();
out.println("Zero size: OK"); t1 = System.nanoTime();
out.printf("Zero size: done in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
testRead(); testRead();
out.println("Read: OK"); t1 = System.nanoTime();
out.printf("Read: done in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
testWrite(); testWrite();
out.println("Write: OK"); t1 = System.nanoTime();
out.printf("Write: done in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
testHighOffset(); testHighOffset();
out.println("High offset: OK"); t1 = System.nanoTime();
out.printf("High offset: done in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
testForce(); testForce();
out.println("Force: OK"); t1 = System.nanoTime();
out.printf("Force: done in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
testExceptions(); testExceptions();
out.println("Exceptions: OK"); t1 = System.nanoTime();
out.printf("Exceptions: done in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
} finally { } finally {
blah.delete(); blah.delete();
} }
@ -195,7 +217,6 @@ public class MapTest {
* the data exercising various valid and invalid writeback ranges. * the data exercising various valid and invalid writeback ranges.
*/ */
private static void testForce() throws Exception { private static void testForce() throws Exception {
for (int x=0; x<50; x++) {
try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) { try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
FileChannel fc = raf.getChannel(); FileChannel fc = raf.getChannel();
final int BLOCK_SIZE = 64; final int BLOCK_SIZE = 64;
@ -235,7 +256,6 @@ public class MapTest {
} }
} }
} }
}
/** /**
* Test exceptions specified by map method * Test exceptions specified by map method

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,51 +26,67 @@
* @bug 8271308 * @bug 8271308
* @summary Verify that transferTo() copies more than Integer.MAX_VALUE bytes * @summary Verify that transferTo() copies more than Integer.MAX_VALUE bytes
* @library .. /test/lib * @library .. /test/lib
* @build jdk.test.lib.Platform * @build jdk.test.lib.Platform jdk.test.lib.RandomFactory FileChannelUtils
* @run main/othervm/timeout=300 Transfer2GPlus * @run main/othervm/timeout=300 Transfer2GPlus
* @key randomness
*/ */
import java.io.File; import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.FilterOutputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel; import java.nio.channels.WritableByteChannel;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random; import java.util.Random;
import java.util.concurrent.TimeUnit;
import jdk.test.lib.Platform; import jdk.test.lib.Platform;
import jdk.test.lib.RandomFactory;
import static java.nio.file.StandardOpenOption.*;
public class Transfer2GPlus { public class Transfer2GPlus {
private static final long BASE = (long)Integer.MAX_VALUE; private static final long BASE = (long)Integer.MAX_VALUE;
private static final int EXTRA = 1024; private static final int EXTRA = 1024;
private static final long LENGTH = BASE + EXTRA; private static final long LENGTH = BASE + EXTRA;
private static final Random GEN = RandomFactory.getRandom();
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
Path src = Files.createTempFile("src", ".dat"); Path src = FileChannelUtils.createSparseTempFile("src", ".dat");
src.toFile().deleteOnExit(); src.toFile().deleteOnExit();
long t0 = System.nanoTime();
byte[] b = createSrcFile(src); byte[] b = createSrcFile(src);
long t1 = System.nanoTime();
System.out.printf(" Wrote large file in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
testToFileChannel(src, b); testToFileChannel(src, b);
t1 = System.nanoTime();
System.out.printf(" Copied to file channel in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
testToWritableByteChannel(src, b); testToWritableByteChannel(src, b);
t1 = System.nanoTime();
System.out.printf(" Copied to byte channel in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
} }
// Create a file of size LENGTH with EXTRA random bytes at offset BASE. // Create a file of size LENGTH with EXTRA random bytes at offset BASE.
private static byte[] createSrcFile(Path src) private static byte[] createSrcFile(Path src)
throws IOException { throws IOException {
RandomAccessFile raf = new RandomAccessFile(src.toString(), "rw"); try (FileChannel fc = FileChannel.open(src, WRITE)) {
raf.setLength(LENGTH); fc.position(BASE);
raf.seek(BASE);
Random r = new Random(System.nanoTime());
byte[] b = new byte[EXTRA]; byte[] b = new byte[EXTRA];
r.nextBytes(b); GEN.nextBytes(b);
raf.write(b); fc.write(ByteBuffer.wrap(b));
return b; return b;
} }
}
// Exercises transferToDirectly() on Linux and transferToTrustedChannel() // Exercises transferToDirectly() on Linux and transferToTrustedChannel()
// on macOS and Windows. // on macOS and Windows.
@ -79,8 +95,7 @@ public class Transfer2GPlus {
Path dst = Files.createTempFile("dst", ".dat"); Path dst = Files.createTempFile("dst", ".dat");
dst.toFile().deleteOnExit(); dst.toFile().deleteOnExit();
try (FileChannel srcCh = FileChannel.open(src)) { try (FileChannel srcCh = FileChannel.open(src)) {
try (FileChannel dstCh = FileChannel.open(dst, try (FileChannel dstCh = FileChannel.open(dst, READ, WRITE)) {
StandardOpenOption.READ, StandardOpenOption.WRITE)) {
long total = 0L; long total = 0L;
if ((total = srcCh.transferTo(0, LENGTH, dstCh)) < LENGTH) { if ((total = srcCh.transferTo(0, LENGTH, dstCh)) < LENGTH) {
if (!Platform.isLinux()) if (!Platform.isLinux())
@ -115,14 +130,12 @@ public class Transfer2GPlus {
// Exercises transferToArbitraryChannel() on all platforms. // Exercises transferToArbitraryChannel() on all platforms.
private static void testToWritableByteChannel(Path src, byte[] expected) private static void testToWritableByteChannel(Path src, byte[] expected)
throws IOException { throws IOException {
File file = File.createTempFile("dst", ".dat"); // transfer src to channel that is not FileChannelImpl
file.deleteOnExit(); try (FileChannel srcCh = FileChannel.open(src);
try (FileChannel srcCh = FileChannel.open(src)) { ByteArrayOutputStream baos = new ByteArrayOutputStream(EXTRA);
// The FileOutputStream is wrapped so that newChannel() does not OutputStream os = new SkipBytesStream(baos, BASE);
// return a FileChannelImpl and so make a faster path be taken. WritableByteChannel wbc = Channels.newChannel(os)){
try (DataOutputStream stream =
new DataOutputStream(new FileOutputStream(file))) {
try (WritableByteChannel wbc = Channels.newChannel(stream)) {
long n; long n;
if ((n = srcCh.transferTo(0, LENGTH, wbc)) < LENGTH) if ((n = srcCh.transferTo(0, LENGTH, wbc)) < LENGTH)
throw new RuntimeException("Too few bytes transferred: " + throw new RuntimeException("Too few bytes transferred: " +
@ -130,14 +143,46 @@ public class Transfer2GPlus {
System.out.println("Transferred " + n + " bytes"); System.out.println("Transferred " + n + " bytes");
RandomAccessFile raf = new RandomAccessFile(file, "r"); byte[] b = baos.toByteArray();
raf.seek(BASE);
byte[] b = new byte[EXTRA];
raf.read(b);
if (!Arrays.equals(b, expected)) if (!Arrays.equals(b, expected))
throw new RuntimeException("Unexpected values"); throw new RuntimeException("Unexpected values");
} }
} }
/**
* Stream that discards the first bytesToSkip bytes, then passes through
*/
static class SkipBytesStream extends FilterOutputStream {
private long bytesToSkip;
public SkipBytesStream(OutputStream out, long bytesToSkip) {
super(out);
this.bytesToSkip = bytesToSkip;
}
@Override
public void write(int b) throws IOException {
if (bytesToSkip > 0) {
bytesToSkip--;
} else {
super.write(b);
}
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
// check copied from FilterOutputStream
if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
throw new IndexOutOfBoundsException();
if (bytesToSkip >= len) {
bytesToSkip -= len;
} else {
int skip = (int)bytesToSkip;
bytesToSkip = 0;
super.write(b, off + skip, len - skip);
}
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,25 +24,24 @@
/* @test /* @test
* @bug 4638365 * @bug 4638365
* @summary Test FileChannel.transferFrom and transferTo for 4GB files * @summary Test FileChannel.transferFrom and transferTo for 4GB files
* @build FileChannelUtils
* @run testng/timeout=300 Transfer4GBFile * @run testng/timeout=300 Transfer4GBFile
*/ */
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption; import java.nio.charset.StandardCharsets;
import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static java.nio.file.StandardOpenOption.*;
public class Transfer4GBFile { public class Transfer4GBFile {
private static PrintStream err = System.err; private static PrintStream err = System.err;
@ -51,30 +50,24 @@ public class Transfer4GBFile {
// Test transferTo with large file // Test transferTo with large file
@Test @Test
public void xferTest04() throws Exception { // for bug 4638365 public void xferTest04() throws Exception { // for bug 4638365
File source = File.createTempFile("blah", null); Path source = FileChannelUtils.createSparseTempFile("blah", null);
source.deleteOnExit(); source.toFile().deleteOnExit();
long testSize = ((long)Integer.MAX_VALUE) * 2; long testSize = ((long)Integer.MAX_VALUE) * 2;
initTestFile(source, 10);
RandomAccessFile raf = new RandomAccessFile(source, "rw");
FileChannel fc = raf.getChannel();
out.println(" Writing large file..."); out.println(" Writing large file...");
long t0 = System.nanoTime(); long t0 = System.nanoTime();
try (FileChannel fc = FileChannel.open(source, READ, WRITE)) {
fc.write(ByteBuffer.wrap("Use the source!".getBytes()), testSize - 40); fc.write(ByteBuffer.wrap("Use the source!".getBytes()), testSize - 40);
long t1 = System.nanoTime(); long t1 = System.nanoTime();
out.printf(" Wrote large file in %d ns (%d ms) %n", out.printf(" Wrote large file in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0)); t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
}
fc.close(); Path sink = Files.createTempFile("sink", null);
raf.close(); sink.toFile().deleteOnExit();
File sink = File.createTempFile("sink", null); try (FileChannel sourceChannel = FileChannel.open(source, READ);
sink.deleteOnExit(); FileChannel sinkChannel = FileChannel.open(sink, WRITE)) {
FileInputStream fis = new FileInputStream(source);
FileChannel sourceChannel = fis.getChannel();
raf = new RandomAccessFile(sink, "rw");
FileChannel sinkChannel = raf.getChannel();
long bytesWritten = sourceChannel.transferTo(testSize - 40, 10, long bytesWritten = sourceChannel.transferTo(testSize - 40, 10,
sinkChannel); sinkChannel);
@ -82,41 +75,25 @@ public class Transfer4GBFile {
throw new RuntimeException("Transfer test 4 failed " + throw new RuntimeException("Transfer test 4 failed " +
bytesWritten); bytesWritten);
} }
sourceChannel.close(); }
sinkChannel.close();
source.delete(); Files.delete(source);
sink.delete(); Files.delete(sink);
} }
// Test transferFrom with large file // Test transferFrom with large file
@Test @Test
public void xferTest05() throws Exception { // for bug 4638365 public void xferTest05() throws Exception { // for bug 4638365
// Create a source file & large sink file for the test // Create a source file & large sink file for the test
File source = File.createTempFile("blech", null); Path source = Files.createTempFile("blech", null);
source.deleteOnExit(); source.toFile().deleteOnExit();
initTestFile(source, 100); initTestFile(source, 100);
// Create the sink file as a sparse file if possible // Create the sink file as a sparse file if possible
File sink = null; Path sink = FileChannelUtils.createSparseTempFile("sink", null);
FileChannel fc = null; sink.toFile().deleteOnExit();
while (fc == null) {
sink = File.createTempFile("sink", null);
// re-create as a sparse file
sink.delete();
try {
fc = FileChannel.open(sink.toPath(),
StandardOpenOption.CREATE_NEW,
StandardOpenOption.WRITE,
StandardOpenOption.SPARSE);
} catch (FileAlreadyExistsException ignore) {
// someone else got it
}
}
sink.deleteOnExit();
long testSize = ((long)Integer.MAX_VALUE) * 2; long testSize = ((long)Integer.MAX_VALUE) * 2;
try { try (FileChannel fc = FileChannel.open(sink, WRITE)){
out.println(" Writing large file..."); out.println(" Writing large file...");
long t0 = System.nanoTime(); long t0 = System.nanoTime();
fc.write(ByteBuffer.wrap("Use the source!".getBytes()), fc.write(ByteBuffer.wrap("Use the source!".getBytes()),
@ -128,46 +105,33 @@ public class Transfer4GBFile {
// Can't set up the test, abort it // Can't set up the test, abort it
err.println("xferTest05 was aborted."); err.println("xferTest05 was aborted.");
return; return;
} finally {
fc.close();
} }
// Get new channels for the source and sink and attempt transfer // Get new channels for the source and sink and attempt transfer
FileChannel sourceChannel = new FileInputStream(source).getChannel(); try (FileChannel sourceChannel = FileChannel.open(source, READ);
try { FileChannel sinkChannel = FileChannel.open(sink, WRITE)) {
FileChannel sinkChannel = new RandomAccessFile(sink, "rw").getChannel();
try {
long bytesWritten = sinkChannel.transferFrom(sourceChannel, long bytesWritten = sinkChannel.transferFrom(sourceChannel,
testSize - 40, 10); testSize - 40, 10);
if (bytesWritten != 10) { if (bytesWritten != 10) {
throw new RuntimeException("Transfer test 5 failed " + throw new RuntimeException("Transfer test 5 failed " +
bytesWritten); bytesWritten);
} }
} finally {
sinkChannel.close();
}
} finally {
sourceChannel.close();
} }
source.delete(); Files.delete(source);
sink.delete(); Files.delete(sink);
} }
/** /**
* Creates file blah of specified size in bytes. * Creates file blah of specified size in bytes.
*/ */
private static void initTestFile(File blah, long size) throws Exception { private static void initTestFile(Path blah, long size) throws Exception {
if (blah.exists()) try (BufferedWriter awriter = Files.newBufferedWriter(blah,
blah.delete(); StandardCharsets.ISO_8859_1)) {
FileOutputStream fos = new FileOutputStream(blah);
BufferedWriter awriter
= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
awriter.write("e"); awriter.write("e");
} }
awriter.flush(); }
awriter.close();
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,23 +24,26 @@
/* @test /* @test
* @bug 6253145 * @bug 6253145
* @summary Test FileChannel.transferTo with file positions up to 8GB * @summary Test FileChannel.transferTo with file positions up to 8GB
* @build FileChannelUtils
* @run testng/timeout=300 TransferTo6GBFile * @run testng/timeout=300 TransferTo6GBFile
*/ */
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel; import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel; import java.nio.channels.SocketChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static java.nio.file.StandardOpenOption.*;
public class TransferTo6GBFile { public class TransferTo6GBFile {
private static PrintStream err = System.err; private static PrintStream err = System.err;
@ -52,27 +55,18 @@ public class TransferTo6GBFile {
final long G = 1024L * 1024L * 1024L; final long G = 1024L * 1024L * 1024L;
// Create 6GB file // Create 6GB file
Path file = FileChannelUtils.createSparseTempFile("source", null);
File file = File.createTempFile("source", null); file.toFile().deleteOnExit();
file.deleteOnExit();
RandomAccessFile raf = new RandomAccessFile(file, "rw");
FileChannel fc = raf.getChannel();
out.println(" Writing large file..."); out.println(" Writing large file...");
long t0 = System.nanoTime(); long t0 = System.nanoTime();
try { try (FileChannel fc = FileChannel.open(file, READ, WRITE)) {
fc.write(ByteBuffer.wrap("0123456789012345".getBytes("UTF-8")), 6*G); fc.write(ByteBuffer.wrap("0123456789012345".getBytes("UTF-8")), 6*G);
long t1 = System.nanoTime(); long t1 = System.nanoTime();
out.printf(" Wrote large file in %d ns (%d ms) %n", out.printf(" Wrote large file in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0)); t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
} catch (IOException x) {
err.println(" Unable to create test file:" + x);
fc.close();
return;
}
// Setup looback connection and echo server // Setup loopback connection and echo server
ServerSocketChannel ssc = ServerSocketChannel.open(); ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(0)); ssc.socket().bind(new InetSocketAddress(0));
@ -148,10 +142,10 @@ public class TransferTo6GBFile {
} finally { } finally {
source.close(); source.close();
ssc.close(); ssc.close();
fc.close();
file.delete();
} }
} }
Files.delete(file);
}
/** /**
* Simple in-process server to echo bytes read by a given socket channel * Simple in-process server to echo bytes read by a given socket channel