src/java.base/windows/native/libnio/ch/FileChannelImpl.c
author bpb
Wed, 27 Mar 2019 13:00:57 -0700
changeset 54313 440cbcf3b268
parent 49897 117501815bed
child 57804 9b7b9f16dfd9
permissions -rw-r--r--
8221531: Incorrect copyright header in src/java.base/windows/native/libnio/ch/FileChannelImpl.c Reviewed-by: lancea, bchristi
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54313
440cbcf3b268 8221531: Incorrect copyright header in src/java.base/windows/native/libnio/ch/FileChannelImpl.c
bpb
parents: 49897
diff changeset
     2
 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    23
 * questions.
2
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"
27937
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
    34
#include "java_lang_Integer.h"
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
    35
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
    36
#include <Mswsock.h>
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
    37
#pragma comment(lib, "Mswsock.lib")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
static jfieldID chan_fd; /* id for jobject 'fd' in java.io.FileChannel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * static method to store field ID's in initializers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * and retrieve the allocation granularity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
Java_sun_nio_ch_FileChannelImpl_initIDs(JNIEnv *env, jclass clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    SYSTEM_INFO si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    jint align;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    GetSystemInfo(&si);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    align = si.dwAllocationGranularity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    chan_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    return align;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/**************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                               jint prot, jlong off, jlong len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    void *mapAddress = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    jint lowOffset = (jint)off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    jint highOffset = (jint)(off >> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    jlong maxSize = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    jint lowLen = (jint)(maxSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    jint highLen = (jint)(maxSize >> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    jobject fdo = (*env)->GetObjectField(env, this, chan_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    HANDLE fileHandle = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    HANDLE mapping;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    DWORD mapAccess = FILE_MAP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    DWORD fileProtect = PAGE_READONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    DWORD mapError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    BOOL result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        fileProtect = PAGE_READONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        mapAccess = FILE_MAP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    } else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        fileProtect = PAGE_READWRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        mapAccess = FILE_MAP_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    } else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        fileProtect = PAGE_WRITECOPY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        mapAccess = FILE_MAP_COPY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    mapping = CreateFileMapping(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        fileHandle,      /* Handle of file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        NULL,            /* Not inheritable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        fileProtect,     /* Read and write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        highLen,         /* High word of max size */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        lowLen,          /* Low word of max size */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        NULL);           /* No name for object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    if (mapping == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        JNU_ThrowIOExceptionWithLastError(env, "Map failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    mapAddress = MapViewOfFile(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        mapping,             /* Handle of file mapping object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        mapAccess,           /* Read and write access */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        highOffset,          /* High word of offset */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        lowOffset,           /* Low word of offset */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        (DWORD)len);         /* Number of bytes to map */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    mapError = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    result = CloseHandle(mapping);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        JNU_ThrowIOExceptionWithLastError(env, "Map failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    if (mapAddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (mapError == ERROR_NOT_ENOUGH_MEMORY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            JNU_ThrowOutOfMemoryError(env, "Map failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            JNU_ThrowIOExceptionWithLastError(env, "Map failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    return ptr_to_jlong(mapAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
Java_sun_nio_ch_FileChannelImpl_unmap0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                 jlong address, jlong len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    BOOL result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    void *a = (void *) jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    result = UnmapViewOfFile(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        JNU_ThrowIOExceptionWithLastError(env, "Unmap failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
27937
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   145
                                            jobject srcFD,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                            jlong position, jlong count,
27937
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   147
                                            jobject dstFD)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
{
27937
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   149
    const int PACKET_SIZE = 524288;
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   150
49897
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47216
diff changeset
   151
    LARGE_INTEGER where;
27937
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   152
    HANDLE src = (HANDLE)(handleval(env, srcFD));
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   153
    SOCKET dst = (SOCKET)(fdval(env, dstFD));
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   154
    DWORD chunkSize = (count > java_lang_Integer_MAX_VALUE) ?
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   155
        java_lang_Integer_MAX_VALUE : (DWORD)count;
49897
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47216
diff changeset
   156
    BOOL result;
27937
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   157
49897
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47216
diff changeset
   158
    where.QuadPart = position;
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47216
diff changeset
   159
    result = SetFilePointerEx(src, where, &where, FILE_BEGIN);
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47216
diff changeset
   160
    if (result == 0) {
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47216
diff changeset
   161
        JNU_ThrowIOExceptionWithLastError(env, "SetFilePointerEx failed");
27937
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   162
        return IOS_THROWN;
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   163
    }
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   164
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   165
    result = TransmitFile(
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   166
        dst,
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   167
        src,
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   168
        chunkSize,
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   169
        PACKET_SIZE,
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   170
        NULL,
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   171
        NULL,
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   172
        TF_USE_KERNEL_APC
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   173
    );
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   174
    if (!result) {
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   175
        int error = WSAGetLastError();
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   176
        if (WSAEINVAL == error && count >= 0) {
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   177
            return IOS_UNSUPPORTED_CASE;
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   178
        }
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   179
        if (WSAENOTSOCK == error) {
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   180
            return IOS_UNSUPPORTED_CASE;
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   181
        }
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   182
        JNU_ThrowIOExceptionWithLastError(env, "transfer failed");
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   183
        return IOS_THROWN;
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   184
    }
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 26732
diff changeset
   185
    return chunkSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}