Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/sun/nio/ch/FileChannelImpl.c
32288 views
/*1* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "jni.h"26#include "jni_util.h"27#include "jvm.h"28#include "jlong.h"29#include <io.h>30#include "nio.h"31#include "nio_util.h"32#include "sun_nio_ch_FileChannelImpl.h"33#include "java_lang_Integer.h"3435#include <Mswsock.h>36#pragma comment(lib, "Mswsock.lib")3738static jfieldID chan_fd; /* id for jobject 'fd' in java.io.FileChannel */3940/**************************************************************41* static method to store field ID's in initializers42* and retrieve the allocation granularity43*/44JNIEXPORT jlong JNICALL45Java_sun_nio_ch_FileChannelImpl_initIDs(JNIEnv *env, jclass clazz)46{47SYSTEM_INFO si;48jint align;49GetSystemInfo(&si);50align = si.dwAllocationGranularity;51chan_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;");52return align;53}545556/**************************************************************57* Channel58*/5960JNIEXPORT jlong JNICALL61Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this,62jint prot, jlong off, jlong len)63{64void *mapAddress = 0;65jint lowOffset = (jint)off;66jint highOffset = (jint)(off >> 32);67jlong maxSize = off + len;68jint lowLen = (jint)(maxSize);69jint highLen = (jint)(maxSize >> 32);70jobject fdo = (*env)->GetObjectField(env, this, chan_fd);71HANDLE fileHandle = (HANDLE)(handleval(env, fdo));72HANDLE mapping;73DWORD mapAccess = FILE_MAP_READ;74DWORD fileProtect = PAGE_READONLY;75DWORD mapError;76BOOL result;7778if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) {79fileProtect = PAGE_READONLY;80mapAccess = FILE_MAP_READ;81} else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) {82fileProtect = PAGE_READWRITE;83mapAccess = FILE_MAP_WRITE;84} else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) {85fileProtect = PAGE_WRITECOPY;86mapAccess = FILE_MAP_COPY;87}8889mapping = CreateFileMapping(90fileHandle, /* Handle of file */91NULL, /* Not inheritable */92fileProtect, /* Read and write */93highLen, /* High word of max size */94lowLen, /* Low word of max size */95NULL); /* No name for object */9697if (mapping == NULL) {98JNU_ThrowIOExceptionWithLastError(env, "Map failed");99return IOS_THROWN;100}101102mapAddress = MapViewOfFile(103mapping, /* Handle of file mapping object */104mapAccess, /* Read and write access */105highOffset, /* High word of offset */106lowOffset, /* Low word of offset */107(DWORD)len); /* Number of bytes to map */108mapError = GetLastError();109110result = CloseHandle(mapping);111if (result == 0) {112JNU_ThrowIOExceptionWithLastError(env, "Map failed");113return IOS_THROWN;114}115116if (mapAddress == NULL) {117if (mapError == ERROR_NOT_ENOUGH_MEMORY)118JNU_ThrowOutOfMemoryError(env, "Map failed");119else120JNU_ThrowIOExceptionWithLastError(env, "Map failed");121return IOS_THROWN;122}123124return ptr_to_jlong(mapAddress);125}126127JNIEXPORT jint JNICALL128Java_sun_nio_ch_FileChannelImpl_unmap0(JNIEnv *env, jobject this,129jlong address, jlong len)130{131BOOL result;132void *a = (void *) jlong_to_ptr(address);133134result = UnmapViewOfFile(a);135if (result == 0) {136JNU_ThrowIOExceptionWithLastError(env, "Unmap failed");137return IOS_THROWN;138}139return 0;140}141142JNIEXPORT void JNICALL143Java_sun_nio_ch_FileChannelImpl_close0(JNIEnv *env, jobject this, jobject fdo)144{145HANDLE h = (HANDLE)(handleval(env, fdo));146if (h != INVALID_HANDLE_VALUE) {147jint result = CloseHandle(h);148if (result < 0) {149JNU_ThrowIOExceptionWithLastError(env, "Close failed");150}151}152}153154JNIEXPORT jlong JNICALL155Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,156jobject srcFD,157jlong position, jlong count,158jobject dstFD)159{160const int PACKET_SIZE = 524288;161162LARGE_INTEGER where;163HANDLE src = (HANDLE)(handleval(env, srcFD));164SOCKET dst = (SOCKET)(fdval(env, dstFD));165DWORD chunkSize = (count > java_lang_Integer_MAX_VALUE) ?166java_lang_Integer_MAX_VALUE : (DWORD)count;167BOOL result;168169where.QuadPart = position;170result = SetFilePointerEx(src, where, &where, FILE_BEGIN);171if (result == 0) {172JNU_ThrowIOExceptionWithLastError(env, "SetFilePointerEx failed");173return IOS_THROWN;174}175176result = TransmitFile(177dst,178src,179chunkSize,180PACKET_SIZE,181NULL,182NULL,183TF_USE_KERNEL_APC184);185if (!result) {186int error = WSAGetLastError();187if (WSAEINVAL == error && count >= 0) {188return IOS_UNSUPPORTED_CASE;189}190if (WSAENOTSOCK == error) {191return IOS_UNSUPPORTED_CASE;192}193JNU_ThrowIOExceptionWithLastError(env, "transfer failed");194return IOS_THROWN;195}196return chunkSize;197}198199200