author | adinn |
Tue, 20 Aug 2019 10:11:53 +0100 | |
changeset 57804 | 9b7b9f16dfd9 |
parent 54313 | 440cbcf3b268 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57804 | 2 |
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
#include "jni.h" |
|
27 |
#include "jni_util.h" |
|
28 |
#include "jvm.h" |
|
29 |
#include "jlong.h" |
|
30 |
#include <io.h> |
|
31 |
#include "nio.h" |
|
32 |
#include "nio_util.h" |
|
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 | 38 |
|
39 |
static jfieldID chan_fd; /* id for jobject 'fd' in java.io.FileChannel */ |
|
40 |
||
41 |
/************************************************************** |
|
42 |
* static method to store field ID's in initializers |
|
43 |
* and retrieve the allocation granularity |
|
44 |
*/ |
|
45 |
JNIEXPORT jlong JNICALL |
|
46 |
Java_sun_nio_ch_FileChannelImpl_initIDs(JNIEnv *env, jclass clazz) |
|
47 |
{ |
|
48 |
SYSTEM_INFO si; |
|
49 |
jint align; |
|
50 |
GetSystemInfo(&si); |
|
51 |
align = si.dwAllocationGranularity; |
|
52 |
chan_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;"); |
|
53 |
return align; |
|
54 |
} |
|
55 |
||
56 |
||
57 |
/************************************************************** |
|
58 |
* Channel |
|
59 |
*/ |
|
60 |
||
61 |
JNIEXPORT jlong JNICALL |
|
62 |
Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this, |
|
57804 | 63 |
jint prot, jlong off, jlong len, jboolean map_sync) |
2 | 64 |
{ |
65 |
void *mapAddress = 0; |
|
66 |
jint lowOffset = (jint)off; |
|
67 |
jint highOffset = (jint)(off >> 32); |
|
68 |
jlong maxSize = off + len; |
|
69 |
jint lowLen = (jint)(maxSize); |
|
70 |
jint highLen = (jint)(maxSize >> 32); |
|
71 |
jobject fdo = (*env)->GetObjectField(env, this, chan_fd); |
|
72 |
HANDLE fileHandle = (HANDLE)(handleval(env, fdo)); |
|
73 |
HANDLE mapping; |
|
74 |
DWORD mapAccess = FILE_MAP_READ; |
|
75 |
DWORD fileProtect = PAGE_READONLY; |
|
76 |
DWORD mapError; |
|
77 |
BOOL result; |
|
78 |
||
79 |
if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) { |
|
80 |
fileProtect = PAGE_READONLY; |
|
81 |
mapAccess = FILE_MAP_READ; |
|
82 |
} else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) { |
|
83 |
fileProtect = PAGE_READWRITE; |
|
84 |
mapAccess = FILE_MAP_WRITE; |
|
85 |
} else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) { |
|
86 |
fileProtect = PAGE_WRITECOPY; |
|
87 |
mapAccess = FILE_MAP_COPY; |
|
88 |
} |
|
89 |
||
57804 | 90 |
if (map_sync) { |
91 |
JNU_ThrowInternalError(env, "should never call map on platform where MAP_SYNC is unimplemented"); |
|
92 |
return IOS_THROWN; |
|
93 |
} |
|
94 |
||
2 | 95 |
mapping = CreateFileMapping( |
96 |
fileHandle, /* Handle of file */ |
|
97 |
NULL, /* Not inheritable */ |
|
98 |
fileProtect, /* Read and write */ |
|
99 |
highLen, /* High word of max size */ |
|
100 |
lowLen, /* Low word of max size */ |
|
101 |
NULL); /* No name for object */ |
|
102 |
||
103 |
if (mapping == NULL) { |
|
104 |
JNU_ThrowIOExceptionWithLastError(env, "Map failed"); |
|
105 |
return IOS_THROWN; |
|
106 |
} |
|
107 |
||
108 |
mapAddress = MapViewOfFile( |
|
109 |
mapping, /* Handle of file mapping object */ |
|
110 |
mapAccess, /* Read and write access */ |
|
111 |
highOffset, /* High word of offset */ |
|
112 |
lowOffset, /* Low word of offset */ |
|
113 |
(DWORD)len); /* Number of bytes to map */ |
|
114 |
mapError = GetLastError(); |
|
115 |
||
116 |
result = CloseHandle(mapping); |
|
117 |
if (result == 0) { |
|
118 |
JNU_ThrowIOExceptionWithLastError(env, "Map failed"); |
|
119 |
return IOS_THROWN; |
|
120 |
} |
|
121 |
||
122 |
if (mapAddress == NULL) { |
|
123 |
if (mapError == ERROR_NOT_ENOUGH_MEMORY) |
|
124 |
JNU_ThrowOutOfMemoryError(env, "Map failed"); |
|
125 |
else |
|
126 |
JNU_ThrowIOExceptionWithLastError(env, "Map failed"); |
|
127 |
return IOS_THROWN; |
|
128 |
} |
|
129 |
||
130 |
return ptr_to_jlong(mapAddress); |
|
131 |
} |
|
132 |
||
133 |
JNIEXPORT jint JNICALL |
|
134 |
Java_sun_nio_ch_FileChannelImpl_unmap0(JNIEnv *env, jobject this, |
|
135 |
jlong address, jlong len) |
|
136 |
{ |
|
137 |
BOOL result; |
|
138 |
void *a = (void *) jlong_to_ptr(address); |
|
139 |
||
140 |
result = UnmapViewOfFile(a); |
|
141 |
if (result == 0) { |
|
142 |
JNU_ThrowIOExceptionWithLastError(env, "Unmap failed"); |
|
143 |
return IOS_THROWN; |
|
144 |
} |
|
145 |
return 0; |
|
146 |
} |
|
147 |
||
148 |
JNIEXPORT jlong JNICALL |
|
149 |
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
|
150 |
jobject srcFD, |
2 | 151 |
jlong position, jlong count, |
27937
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
152 |
jobject dstFD) |
2 | 153 |
{ |
27937
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
154 |
const int PACKET_SIZE = 524288; |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
155 |
|
49897
117501815bed
8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents:
47216
diff
changeset
|
156 |
LARGE_INTEGER where; |
27937
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
157 |
HANDLE src = (HANDLE)(handleval(env, srcFD)); |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
158 |
SOCKET dst = (SOCKET)(fdval(env, dstFD)); |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
159 |
DWORD chunkSize = (count > java_lang_Integer_MAX_VALUE) ? |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
160 |
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
|
161 |
BOOL result; |
27937
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
162 |
|
49897
117501815bed
8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents:
47216
diff
changeset
|
163 |
where.QuadPart = position; |
117501815bed
8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents:
47216
diff
changeset
|
164 |
result = SetFilePointerEx(src, where, &where, FILE_BEGIN); |
117501815bed
8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents:
47216
diff
changeset
|
165 |
if (result == 0) { |
117501815bed
8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents:
47216
diff
changeset
|
166 |
JNU_ThrowIOExceptionWithLastError(env, "SetFilePointerEx failed"); |
27937
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
167 |
return IOS_THROWN; |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
168 |
} |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
169 |
|
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
170 |
result = TransmitFile( |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
171 |
dst, |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
172 |
src, |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
173 |
chunkSize, |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
174 |
PACKET_SIZE, |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
175 |
NULL, |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
176 |
NULL, |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
177 |
TF_USE_KERNEL_APC |
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 (!result) { |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
180 |
int error = WSAGetLastError(); |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
181 |
if (WSAEINVAL == error && count >= 0) { |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
182 |
return IOS_UNSUPPORTED_CASE; |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
183 |
} |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
184 |
if (WSAENOTSOCK == error) { |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
185 |
return IOS_UNSUPPORTED_CASE; |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
186 |
} |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
187 |
JNU_ThrowIOExceptionWithLastError(env, "transfer failed"); |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
188 |
return IOS_THROWN; |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
189 |
} |
0c9f63e42e91
8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents:
26732
diff
changeset
|
190 |
return chunkSize; |
2 | 191 |
} |