Skip to content

Commit 6b7c971

Browse files
author
Brian Burkhalter
committed
8325382: (fc) FileChannel.transferTo throws IOException when position equals size
Reviewed-by: alanb
1 parent 13d9e8f commit 6b7c971

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -857,20 +857,18 @@ public long transferTo(long position, long count, WritableByteChannel target)
857857
if (position > sz)
858858
return 0;
859859

860-
// Now position <= sz so remaining >= 0 and
861-
// remaining == 0 if and only if sz == 0
862-
long remaining = sz - position;
863-
864-
// Adjust count only if remaining > 0, i.e.,
865-
// sz > position which means sz > 0
866-
if (remaining > 0 && remaining < count)
867-
count = remaining;
868-
869860
// System calls supporting fast transfers might not work on files
870861
// which advertise zero size such as those in Linux /proc
871862
if (sz > 0) {
872-
// Attempt a direct transfer, if the kernel supports it, limiting
873-
// the number of bytes according to which platform
863+
// Now sz > 0 and position <= sz so remaining >= 0 and
864+
// remaining == 0 if and only if sz == position
865+
long remaining = sz - position;
866+
867+
if (remaining >= 0 && remaining < count)
868+
count = remaining;
869+
870+
// Attempt a direct transfer, if the kernel supports it,
871+
// limiting the number of bytes according to which platform
874872
int icount = (int) Math.min(count, nd.maxDirectTransferSize());
875873
long n;
876874
if ((n = transferToDirect(position, icount, target)) >= 0)

test/jdk/java/nio/channels/FileChannel/Transfer.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
2222
*/
2323

2424
/* @test
25-
* @bug 4434723 4482726 4559072 4795550 5081340 5103988 6984545
25+
* @bug 4434723 4482726 4559072 4795550 5081340 5103988 6984545 8325382
2626
* @summary Test FileChannel.transferFrom and transferTo (use -Dseed=X to set PRNG seed)
2727
* @library ..
2828
* @library /test/lib
@@ -48,11 +48,13 @@
4848
import java.nio.channels.ServerSocketChannel;
4949
import java.nio.channels.SocketChannel;
5050
import java.nio.channels.spi.SelectorProvider;
51+
import java.nio.file.Files;
5152
import java.util.Random;
5253
import java.util.concurrent.TimeUnit;
5354

5455
import jdk.test.lib.RandomFactory;
5556

57+
import org.testng.Assert;
5658
import org.testng.annotations.Test;
5759

5860
public class Transfer {
@@ -158,6 +160,33 @@ public void testReadableByteChannel() throws Exception {
158160
}
159161
}
160162

163+
@Test
164+
public void transferToNoThrow() throws IOException { // for bug 8325382
165+
File source = File.createTempFile("before", "after");
166+
source.deleteOnExit();
167+
168+
CharSequence csq = "Reality is greater than the sum of its parts.";
169+
Files.writeString(source.toPath(), csq);
170+
final long length = csq.length();
171+
Assert.assertEquals(source.length(), length);
172+
173+
File target = File.createTempFile("before", "after");
174+
target.deleteOnExit();
175+
176+
try (FileInputStream in = new FileInputStream(source);
177+
FileOutputStream out = new FileOutputStream(target);
178+
FileChannel chSource = in.getChannel();
179+
FileChannel chTarget = out.getChannel()) {
180+
// The count of bytes requested to transfer must exceed
181+
// FileChannelImpl.MAPPED_TRANSFER_THRESHOLD which is
182+
// currently 16384
183+
long n = chSource.transferTo(length, 16385, chTarget);
184+
185+
// At the end of the input so no bytes should be transferred
186+
Assert.assertEquals(n, 0);
187+
}
188+
}
189+
161190
@Test
162191
public void xferTest02() throws Exception { // for bug 4482726
163192
byte[] srcData = new byte[5000];

0 commit comments

Comments
 (0)