8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space

Reviewed-by: bpb
This commit is contained in:
Alan Bateman 2018-04-26 09:04:18 +01:00
parent ccc74fdd60
commit 2df7aa7b9f
9 changed files with 101 additions and 144 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2018, 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
@ -78,6 +78,10 @@ class FileDispatcherImpl extends FileDispatcher {
return writev0(fd, address, len, fdAccess.getAppend(fd));
}
long seek(FileDescriptor fd, long offset) throws IOException {
return seek0(fd, offset);
}
int force(FileDescriptor fd, boolean metaData) throws IOException {
return force0(fd, metaData);
}
@ -86,11 +90,6 @@ class FileDispatcherImpl extends FileDispatcher {
return truncate0(fd, size);
}
int allocate(FileDescriptor fd, long size) throws IOException {
// truncate0() works for extending and truncating file size
return truncate0(fd, size);
}
long size(FileDescriptor fd) throws IOException {
return size0(fd);
}
@ -126,8 +125,7 @@ class FileDispatcherImpl extends FileDispatcher {
return true;
}
int setDirectIO(FileDescriptor fd, String path)
{
int setDirectIO(FileDescriptor fd, String path) {
int result = -1;
String filePath = path.substring(0, path.lastIndexOf(File.separator));
CharBuffer buffer = CharBuffer.allocate(filePath.length());
@ -178,6 +176,8 @@ class FileDispatcherImpl extends FileDispatcher {
static native long writev0(FileDescriptor fd, long address, int len, boolean append)
throws IOException;
static native long seek0(FileDescriptor fd, long offset) throws IOException;
static native int force0(FileDescriptor fd, boolean metaData)
throws IOException;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2018 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
@ -140,31 +140,6 @@ Java_sun_nio_ch_FileChannelImpl_unmap0(JNIEnv *env, jobject this,
return 0;
}
JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileChannelImpl_position0(JNIEnv *env, jobject this,
jobject fdo, jlong offset)
{
BOOL result = 0;
HANDLE h = (HANDLE)(handleval(env, fdo));
LARGE_INTEGER where;
DWORD whence;
if (offset < 0) {
where.QuadPart = 0;
whence = FILE_CURRENT;
} else {
where.QuadPart = offset;
whence = FILE_BEGIN;
}
result = SetFilePointerEx(h, where, &where, whence);
if (result == 0) {
JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
return IOS_THROWN;
}
return (jlong)where.QuadPart;
}
JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
jobject srcFD,
@ -173,14 +148,17 @@ Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
{
const int PACKET_SIZE = 524288;
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;
BOOL result = 0;
BOOL result;
jlong pos = Java_sun_nio_ch_FileChannelImpl_position0(env, this, srcFD, position);
if (pos == IOS_THROWN) {
where.QuadPart = position;
result = SetFilePointerEx(src, where, &where, FILE_BEGIN);
if (result == 0) {
JNU_ThrowIOExceptionWithLastError(env, "SetFilePointerEx failed");
return IOS_THROWN;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2018, 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
@ -298,6 +298,31 @@ Java_sun_nio_ch_FileDispatcherImpl_pwrite0(JNIEnv *env, jclass clazz, jobject fd
return convertReturnVal(env, (jint)written, JNI_FALSE);
}
JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileDispatcherImpl_seek0(JNIEnv *env, jclass clazz,
jobject fdo, jlong offset)
{
BOOL result = 0;
HANDLE h = (HANDLE)(handleval(env, fdo));
LARGE_INTEGER where;
DWORD whence;
if (offset < 0) {
where.QuadPart = 0;
whence = FILE_CURRENT;
} else {
where.QuadPart = offset;
whence = FILE_BEGIN;
}
result = SetFilePointerEx(h, where, &where, whence);
if (result == 0) {
JNU_ThrowIOExceptionWithLastError(env, "SetFilePointerEx failed");
return IOS_THROWN;
}
return (jlong)where.QuadPart;
}
JNIEXPORT jint JNICALL
Java_sun_nio_ch_FileDispatcherImpl_force0(JNIEnv *env, jobject this,
jobject fdo, jboolean md)