8271308: (fc) FileChannel.transferTo() transfers no more than Integer.MAX_VALUE bytes in one call

Reviewed-by: alanb, vtewari
This commit is contained in:
Brian Burkhalter 2021-08-05 16:10:04 +00:00
parent 7234a433f8
commit e2c5bfe083
4 changed files with 183 additions and 16 deletions

View file

@ -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();
}
}

View file

@ -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
@ -252,3 +252,12 @@ Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
#endif
}
JNIEXPORT jint JNICALL
Java_sun_nio_ch_FileChannelImpl_maxDirectTransferSize0(JNIEnv* env, jobject this)
{
#if defined(LINUX)
return 0x7ffff000; // 2,147,479,552 maximum for sendfile()
#else
return java_lang_Integer_MAX_VALUE;
#endif
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, 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
@ -145,6 +145,9 @@ Java_sun_nio_ch_FileChannelImpl_unmap0(JNIEnv *env, jobject this,
return 0;
}
// Integer.MAX_VALUE - 1 is the maximum transfer size for TransmitFile()
#define MAX_TRANSMIT_SIZE (java_lang_Integer_MAX_VALUE - 1)
JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
jobject srcFD,
@ -156,8 +159,8 @@ Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
LARGE_INTEGER where;
HANDLE src = (HANDLE)(handleval(env, srcFD));
SOCKET dst = (SOCKET)(fdval(env, dstFD));
DWORD chunkSize = (count > java_lang_Integer_MAX_VALUE) ?
java_lang_Integer_MAX_VALUE : (DWORD)count;
DWORD chunkSize = (count > MAX_TRANSMIT_SIZE) ?
MAX_TRANSMIT_SIZE : (DWORD)count;
BOOL result;
where.QuadPart = position;
@ -189,3 +192,10 @@ Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
}
return chunkSize;
}
JNIEXPORT jint JNICALL
Java_sun_nio_ch_FileChannelImpl_maxDirectTransferSize0(JNIEnv* env, jobject this)
{
return MAX_TRANSMIT_SIZE;
}