mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 23:04:50 +02:00
8271308: (fc) FileChannel.transferTo() transfers no more than Integer.MAX_VALUE bytes in one call
Reviewed-by: alanb, vtewari
This commit is contained in:
parent
7234a433f8
commit
e2c5bfe083
4 changed files with 183 additions and 16 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2000, 2021, 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
|
||||
|
@ -65,6 +65,9 @@ public class FileChannelImpl
|
|||
private static final JavaIOFileDescriptorAccess fdAccess =
|
||||
SharedSecrets.getJavaIOFileDescriptorAccess();
|
||||
|
||||
// Maximum direct transfer size
|
||||
private static final int MAX_DIRECT_TRANSFER_SIZE;
|
||||
|
||||
// Used to make native read and write calls
|
||||
private final FileDispatcher nd;
|
||||
|
||||
|
@ -622,18 +625,18 @@ public class FileChannelImpl
|
|||
return count - remaining;
|
||||
}
|
||||
|
||||
private long transferToArbitraryChannel(long position, int icount,
|
||||
private long transferToArbitraryChannel(long position, long count,
|
||||
WritableByteChannel target)
|
||||
throws IOException
|
||||
{
|
||||
// Untrusted target: Use a newly-erased buffer
|
||||
int c = Math.min(icount, TRANSFER_SIZE);
|
||||
int c = (int)Math.min(count, TRANSFER_SIZE);
|
||||
ByteBuffer bb = ByteBuffer.allocate(c);
|
||||
long tw = 0; // Total bytes written
|
||||
long pos = position;
|
||||
try {
|
||||
while (tw < icount) {
|
||||
bb.limit(Math.min((int)(icount - tw), TRANSFER_SIZE));
|
||||
while (tw < count) {
|
||||
bb.limit((int)Math.min(count - tw, TRANSFER_SIZE));
|
||||
int nr = read(bb, pos);
|
||||
if (nr <= 0)
|
||||
break;
|
||||
|
@ -672,22 +675,23 @@ public class FileChannelImpl
|
|||
long sz = size();
|
||||
if (position > sz)
|
||||
return 0;
|
||||
int icount = (int)Math.min(count, Integer.MAX_VALUE);
|
||||
if ((sz - position) < icount)
|
||||
icount = (int)(sz - position);
|
||||
|
||||
if ((sz - position) < count)
|
||||
count = (int)(sz - position);
|
||||
|
||||
// Attempt a direct transfer, if the kernel supports it, limiting
|
||||
// the number of bytes according to which platform
|
||||
int icount = (int)Math.min(count, MAX_DIRECT_TRANSFER_SIZE);
|
||||
long n;
|
||||
|
||||
// Attempt a direct transfer, if the kernel supports it
|
||||
if ((n = transferToDirectly(position, icount, target)) >= 0)
|
||||
return n;
|
||||
|
||||
// Attempt a mapped transfer, but only to trusted channel types
|
||||
if ((n = transferToTrustedChannel(position, icount, target)) >= 0)
|
||||
if ((n = transferToTrustedChannel(position, count, target)) >= 0)
|
||||
return n;
|
||||
|
||||
// Slow path for untrusted targets
|
||||
return transferToArbitraryChannel(position, icount, target);
|
||||
return transferToArbitraryChannel(position, count, target);
|
||||
}
|
||||
|
||||
private long transferFromFileChannel(FileChannelImpl src,
|
||||
|
@ -1348,11 +1352,15 @@ public class FileChannelImpl
|
|||
private native long transferTo0(FileDescriptor src, long position,
|
||||
long count, FileDescriptor dst);
|
||||
|
||||
// Retrieves the maximum size of a transfer
|
||||
private static native int maxDirectTransferSize0();
|
||||
|
||||
// Caches fieldIDs
|
||||
private static native long initIDs();
|
||||
|
||||
static {
|
||||
IOUtil.load();
|
||||
allocationGranularity = initIDs();
|
||||
MAX_DIRECT_TRANSFER_SIZE = maxDirectTransferSize0();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue