Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/sun/nio/ch/WindowsAsynchronousFileChannelImpl.c
32288 views
/*1* Copyright (c) 2008, 2010, 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 <windows.h>2627#include "jni.h"28#include "jni_util.h"29#include "jlong.h"30#include "nio.h"31#include "nio_util.h"3233#include "sun_nio_ch_WindowsAsynchronousFileChannelImpl.h"343536JNIEXPORT jint JNICALL37Java_sun_nio_ch_WindowsAsynchronousFileChannelImpl_readFile(JNIEnv* env, jclass this,38jlong handle, jlong address, jint len, jlong offset, jlong ov)39{40BOOL res;4142OVERLAPPED* lpOverlapped = (OVERLAPPED*)jlong_to_ptr(ov);43lpOverlapped->Offset = (DWORD)offset;44lpOverlapped->OffsetHigh = (DWORD)((long)(offset >> 32));45lpOverlapped->hEvent = NULL;4647res = ReadFile((HANDLE) jlong_to_ptr(handle),48(LPVOID) jlong_to_ptr(address),49(DWORD)len,50NULL,51lpOverlapped);5253if (res == 0) {54int error = GetLastError();55if (error == ERROR_IO_PENDING)56return IOS_UNAVAILABLE;57if (error == ERROR_HANDLE_EOF)58return IOS_EOF;59JNU_ThrowIOExceptionWithLastError(env, "ReadFile failed");60return IOS_THROWN;61}6263return IOS_UNAVAILABLE;64}6566JNIEXPORT jint JNICALL67Java_sun_nio_ch_WindowsAsynchronousFileChannelImpl_writeFile(JNIEnv* env, jclass this,68jlong handle, jlong address, jint len, jlong offset, jlong ov)69{70BOOL res;7172OVERLAPPED* lpOverlapped = (OVERLAPPED*)jlong_to_ptr(ov);73lpOverlapped->Offset = (DWORD)offset;74lpOverlapped->OffsetHigh = (DWORD)((long)(offset >> 32));75lpOverlapped->hEvent = NULL;7677res = WriteFile((HANDLE)jlong_to_ptr(handle),78(LPVOID) jlong_to_ptr(address),79(DWORD)len,80NULL,81lpOverlapped);8283if (res == 0) {84int error = GetLastError();85if (error == ERROR_IO_PENDING)86return IOS_UNAVAILABLE;87JNU_ThrowIOExceptionWithLastError(env, "WriteFile failed");88return IOS_THROWN;89}9091return IOS_UNAVAILABLE;92}9394JNIEXPORT jint JNICALL95Java_sun_nio_ch_WindowsAsynchronousFileChannelImpl_lockFile(JNIEnv *env, jobject this, jlong handle,96jlong pos, jlong size, jboolean shared, jlong ov)97{98BOOL res;99HANDLE h = jlong_to_ptr(handle);100DWORD lowPos = (DWORD)pos;101long highPos = (long)(pos >> 32);102DWORD lowNumBytes = (DWORD)size;103DWORD highNumBytes = (DWORD)(size >> 32);104DWORD flags = (shared == JNI_TRUE) ? 0 : LOCKFILE_EXCLUSIVE_LOCK;105OVERLAPPED* lpOverlapped = (OVERLAPPED*)jlong_to_ptr(ov);106107lpOverlapped->Offset = lowPos;108lpOverlapped->OffsetHigh = highPos;109lpOverlapped->hEvent = NULL;110111res = LockFileEx(h, flags, 0, lowNumBytes, highNumBytes, lpOverlapped);112if (res == 0) {113int error = GetLastError();114if (error == ERROR_IO_PENDING) {115return IOS_UNAVAILABLE;116}117JNU_ThrowIOExceptionWithLastError(env, "WriteFile failed");118return IOS_THROWN;119}120return 0;121}122123JNIEXPORT void JNICALL124Java_sun_nio_ch_WindowsAsynchronousFileChannelImpl_close0(JNIEnv* env, jclass this,125jlong handle)126{127HANDLE h = (HANDLE)jlong_to_ptr(handle);128CloseHandle(h);129}130131132