0N/A/*
2362N/A * Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include "jni.h"
0N/A#include "jni_util.h"
0N/A#include "jvm.h"
0N/A#include "jlong.h"
0N/A#include <io.h>
0N/A#include "nio.h"
0N/A#include "nio_util.h"
0N/A#include "sun_nio_ch_FileChannelImpl.h"
0N/A
0N/Astatic jfieldID chan_fd; /* id for jobject 'fd' in java.io.FileChannel */
0N/A
0N/A/**************************************************************
0N/A * static method to store field ID's in initializers
0N/A * and retrieve the allocation granularity
0N/A */
0N/AJNIEXPORT jlong JNICALL
0N/AJava_sun_nio_ch_FileChannelImpl_initIDs(JNIEnv *env, jclass clazz)
0N/A{
0N/A SYSTEM_INFO si;
0N/A jint align;
0N/A GetSystemInfo(&si);
0N/A align = si.dwAllocationGranularity;
0N/A chan_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;");
0N/A return align;
0N/A}
0N/A
0N/A
0N/A/**************************************************************
0N/A * Channel
0N/A */
0N/A
0N/AJNIEXPORT jlong JNICALL
0N/AJava_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this,
0N/A jint prot, jlong off, jlong len)
0N/A{
0N/A void *mapAddress = 0;
0N/A jint lowOffset = (jint)off;
0N/A jint highOffset = (jint)(off >> 32);
0N/A jlong maxSize = off + len;
0N/A jint lowLen = (jint)(maxSize);
0N/A jint highLen = (jint)(maxSize >> 32);
0N/A jobject fdo = (*env)->GetObjectField(env, this, chan_fd);
0N/A HANDLE fileHandle = (HANDLE)(handleval(env, fdo));
0N/A HANDLE mapping;
0N/A DWORD mapAccess = FILE_MAP_READ;
0N/A DWORD fileProtect = PAGE_READONLY;
0N/A DWORD mapError;
0N/A BOOL result;
0N/A
0N/A if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) {
0N/A fileProtect = PAGE_READONLY;
0N/A mapAccess = FILE_MAP_READ;
0N/A } else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) {
0N/A fileProtect = PAGE_READWRITE;
0N/A mapAccess = FILE_MAP_WRITE;
0N/A } else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) {
0N/A fileProtect = PAGE_WRITECOPY;
0N/A mapAccess = FILE_MAP_COPY;
0N/A }
0N/A
0N/A mapping = CreateFileMapping(
0N/A fileHandle, /* Handle of file */
0N/A NULL, /* Not inheritable */
0N/A fileProtect, /* Read and write */
0N/A highLen, /* High word of max size */
0N/A lowLen, /* Low word of max size */
0N/A NULL); /* No name for object */
0N/A
0N/A if (mapping == NULL) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "Map failed");
0N/A return IOS_THROWN;
0N/A }
0N/A
0N/A mapAddress = MapViewOfFile(
0N/A mapping, /* Handle of file mapping object */
0N/A mapAccess, /* Read and write access */
0N/A highOffset, /* High word of offset */
0N/A lowOffset, /* Low word of offset */
0N/A (DWORD)len); /* Number of bytes to map */
0N/A mapError = GetLastError();
0N/A
0N/A result = CloseHandle(mapping);
0N/A if (result == 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "Map failed");
0N/A return IOS_THROWN;
0N/A }
0N/A
0N/A if (mapAddress == NULL) {
0N/A if (mapError == ERROR_NOT_ENOUGH_MEMORY)
0N/A JNU_ThrowOutOfMemoryError(env, "Map failed");
0N/A else
0N/A JNU_ThrowIOExceptionWithLastError(env, "Map failed");
0N/A return IOS_THROWN;
0N/A }
0N/A
0N/A return ptr_to_jlong(mapAddress);
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_FileChannelImpl_unmap0(JNIEnv *env, jobject this,
0N/A jlong address, jlong len)
0N/A{
0N/A BOOL result;
0N/A void *a = (void *) jlong_to_ptr(address);
0N/A
0N/A result = UnmapViewOfFile(a);
0N/A if (result == 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "Unmap failed");
0N/A return IOS_THROWN;
0N/A }
0N/A return 0;
0N/A}
0N/A
0N/AJNIEXPORT jlong JNICALL
0N/AJava_sun_nio_ch_FileChannelImpl_position0(JNIEnv *env, jobject this,
0N/A jobject fdo, jlong offset)
0N/A{
0N/A DWORD lowPos = 0;
0N/A long highPos = 0;
0N/A HANDLE h = (HANDLE)(handleval(env, fdo));
0N/A
0N/A if (offset < 0) {
0N/A lowPos = SetFilePointer(h, 0, &highPos, FILE_CURRENT);
0N/A } else {
0N/A lowPos = (DWORD)offset;
0N/A highPos = (long)(offset >> 32);
0N/A lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN);
0N/A }
0N/A if (lowPos == ((DWORD)-1)) {
0N/A if (GetLastError() != ERROR_SUCCESS) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
0N/A return IOS_THROWN;
0N/A }
0N/A }
0N/A return (((jlong)highPos) << 32) | lowPos;
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_FileChannelImpl_close0(JNIEnv *env, jobject this, jobject fdo)
0N/A{
0N/A HANDLE h = (HANDLE)(handleval(env, fdo));
0N/A if (h != INVALID_HANDLE_VALUE) {
0N/A jint result = CloseHandle(h);
0N/A if (result < 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "Close failed");
0N/A }
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT jlong JNICALL
0N/AJava_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
0N/A jint srcFD,
0N/A jlong position, jlong count,
0N/A jint dstFD)
0N/A{
0N/A return IOS_UNSUPPORTED;
0N/A}