jdk/src/windows/native/sun/nio/ch/FileChannelImpl.c
author alanb
Sun, 15 Feb 2009 12:25:54 +0000
changeset 2057 3acf8e5e2ca0
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99 4313887: New I/O: Improved filesystem interface 4607272: New I/O: Support asynchronous I/O Reviewed-by: sherman, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
     2
 * Copyright 2000-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <io.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "nio.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "nio_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "sun_nio_ch_FileChannelImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
static jfieldID chan_fd; /* id for jobject 'fd' in java.io.FileChannel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * static method to store field ID's in initializers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * and retrieve the allocation granularity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
Java_sun_nio_ch_FileChannelImpl_initIDs(JNIEnv *env, jclass clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    SYSTEM_INFO si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    jint align;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    GetSystemInfo(&si);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    align = si.dwAllocationGranularity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    chan_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    return align;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
/**************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                               jint prot, jlong off, jlong len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    void *mapAddress = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    jint lowOffset = (jint)off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    jint highOffset = (jint)(off >> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    jlong maxSize = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    jint lowLen = (jint)(maxSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    jint highLen = (jint)(maxSize >> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    jobject fdo = (*env)->GetObjectField(env, this, chan_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    HANDLE fileHandle = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    HANDLE mapping;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    DWORD mapAccess = FILE_MAP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    DWORD fileProtect = PAGE_READONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    DWORD mapError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    BOOL result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        fileProtect = PAGE_READONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        mapAccess = FILE_MAP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    } else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        fileProtect = PAGE_READWRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        mapAccess = FILE_MAP_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    } else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        fileProtect = PAGE_WRITECOPY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        mapAccess = FILE_MAP_COPY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    mapping = CreateFileMapping(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        fileHandle,      /* Handle of file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        NULL,            /* Not inheritable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        fileProtect,     /* Read and write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        highLen,         /* High word of max size */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        lowLen,          /* Low word of max size */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        NULL);           /* No name for object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    if (mapping == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        JNU_ThrowIOExceptionWithLastError(env, "Map failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    mapAddress = MapViewOfFile(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        mapping,             /* Handle of file mapping object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        mapAccess,           /* Read and write access */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        highOffset,          /* High word of offset */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        lowOffset,           /* Low word of offset */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        (DWORD)len);         /* Number of bytes to map */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    mapError = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    result = CloseHandle(mapping);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        JNU_ThrowIOExceptionWithLastError(env, "Map failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    if (mapAddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (mapError == ERROR_NOT_ENOUGH_MEMORY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            JNU_ThrowOutOfMemoryError(env, "Map failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            JNU_ThrowIOExceptionWithLastError(env, "Map failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    return ptr_to_jlong(mapAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
Java_sun_nio_ch_FileChannelImpl_unmap0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                 jlong address, jlong len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    BOOL result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    void *a = (void *) jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    result = UnmapViewOfFile(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        JNU_ThrowIOExceptionWithLastError(env, "Unmap failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
Java_sun_nio_ch_FileChannelImpl_position0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                          jobject fdo, jlong offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    DWORD lowPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    long highPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    if (offset < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        lowPos = SetFilePointer(h, 0, &highPos, FILE_CURRENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        lowPos = (DWORD)offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        highPos = (long)(offset >> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    if (lowPos == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (GetLastError() != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    return (((jlong)highPos) << 32) | lowPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
Java_sun_nio_ch_FileChannelImpl_close0(JNIEnv *env, jobject this, jobject fdo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        jint result = CloseHandle(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (result < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            JNU_ThrowIOExceptionWithLastError(env, "Close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                            jint srcFD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                            jlong position, jlong count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                            jint dstFD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    return IOS_UNSUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}