2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2000, 2009, 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"
|
|
34 |
|
|
35 |
static jfieldID chan_fd; /* id for jobject 'fd' in java.io.FileChannel */
|
|
36 |
|
|
37 |
/**************************************************************
|
|
38 |
* static method to store field ID's in initializers
|
|
39 |
* and retrieve the allocation granularity
|
|
40 |
*/
|
|
41 |
JNIEXPORT jlong JNICALL
|
|
42 |
Java_sun_nio_ch_FileChannelImpl_initIDs(JNIEnv *env, jclass clazz)
|
|
43 |
{
|
|
44 |
SYSTEM_INFO si;
|
|
45 |
jint align;
|
|
46 |
GetSystemInfo(&si);
|
|
47 |
align = si.dwAllocationGranularity;
|
|
48 |
chan_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;");
|
|
49 |
return align;
|
|
50 |
}
|
|
51 |
|
|
52 |
|
|
53 |
/**************************************************************
|
|
54 |
* Channel
|
|
55 |
*/
|
|
56 |
|
|
57 |
JNIEXPORT jlong JNICALL
|
|
58 |
Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this,
|
|
59 |
jint prot, jlong off, jlong len)
|
|
60 |
{
|
|
61 |
void *mapAddress = 0;
|
|
62 |
jint lowOffset = (jint)off;
|
|
63 |
jint highOffset = (jint)(off >> 32);
|
|
64 |
jlong maxSize = off + len;
|
|
65 |
jint lowLen = (jint)(maxSize);
|
|
66 |
jint highLen = (jint)(maxSize >> 32);
|
|
67 |
jobject fdo = (*env)->GetObjectField(env, this, chan_fd);
|
|
68 |
HANDLE fileHandle = (HANDLE)(handleval(env, fdo));
|
|
69 |
HANDLE mapping;
|
|
70 |
DWORD mapAccess = FILE_MAP_READ;
|
|
71 |
DWORD fileProtect = PAGE_READONLY;
|
|
72 |
DWORD mapError;
|
|
73 |
BOOL result;
|
|
74 |
|
|
75 |
if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) {
|
|
76 |
fileProtect = PAGE_READONLY;
|
|
77 |
mapAccess = FILE_MAP_READ;
|
|
78 |
} else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) {
|
|
79 |
fileProtect = PAGE_READWRITE;
|
|
80 |
mapAccess = FILE_MAP_WRITE;
|
|
81 |
} else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) {
|
|
82 |
fileProtect = PAGE_WRITECOPY;
|
|
83 |
mapAccess = FILE_MAP_COPY;
|
|
84 |
}
|
|
85 |
|
|
86 |
mapping = CreateFileMapping(
|
|
87 |
fileHandle, /* Handle of file */
|
|
88 |
NULL, /* Not inheritable */
|
|
89 |
fileProtect, /* Read and write */
|
|
90 |
highLen, /* High word of max size */
|
|
91 |
lowLen, /* Low word of max size */
|
|
92 |
NULL); /* No name for object */
|
|
93 |
|
|
94 |
if (mapping == NULL) {
|
|
95 |
JNU_ThrowIOExceptionWithLastError(env, "Map failed");
|
|
96 |
return IOS_THROWN;
|
|
97 |
}
|
|
98 |
|
|
99 |
mapAddress = MapViewOfFile(
|
|
100 |
mapping, /* Handle of file mapping object */
|
|
101 |
mapAccess, /* Read and write access */
|
|
102 |
highOffset, /* High word of offset */
|
|
103 |
lowOffset, /* Low word of offset */
|
|
104 |
(DWORD)len); /* Number of bytes to map */
|
|
105 |
mapError = GetLastError();
|
|
106 |
|
|
107 |
result = CloseHandle(mapping);
|
|
108 |
if (result == 0) {
|
|
109 |
JNU_ThrowIOExceptionWithLastError(env, "Map failed");
|
|
110 |
return IOS_THROWN;
|
|
111 |
}
|
|
112 |
|
|
113 |
if (mapAddress == NULL) {
|
|
114 |
if (mapError == ERROR_NOT_ENOUGH_MEMORY)
|
|
115 |
JNU_ThrowOutOfMemoryError(env, "Map failed");
|
|
116 |
else
|
|
117 |
JNU_ThrowIOExceptionWithLastError(env, "Map failed");
|
|
118 |
return IOS_THROWN;
|
|
119 |
}
|
|
120 |
|
|
121 |
return ptr_to_jlong(mapAddress);
|
|
122 |
}
|
|
123 |
|
|
124 |
JNIEXPORT jint JNICALL
|
|
125 |
Java_sun_nio_ch_FileChannelImpl_unmap0(JNIEnv *env, jobject this,
|
|
126 |
jlong address, jlong len)
|
|
127 |
{
|
|
128 |
BOOL result;
|
|
129 |
void *a = (void *) jlong_to_ptr(address);
|
|
130 |
|
|
131 |
result = UnmapViewOfFile(a);
|
|
132 |
if (result == 0) {
|
|
133 |
JNU_ThrowIOExceptionWithLastError(env, "Unmap failed");
|
|
134 |
return IOS_THROWN;
|
|
135 |
}
|
|
136 |
return 0;
|
|
137 |
}
|
|
138 |
|
|
139 |
JNIEXPORT jlong JNICALL
|
|
140 |
Java_sun_nio_ch_FileChannelImpl_position0(JNIEnv *env, jobject this,
|
|
141 |
jobject fdo, jlong offset)
|
|
142 |
{
|
|
143 |
DWORD lowPos = 0;
|
|
144 |
long highPos = 0;
|
|
145 |
HANDLE h = (HANDLE)(handleval(env, fdo));
|
|
146 |
|
|
147 |
if (offset < 0) {
|
|
148 |
lowPos = SetFilePointer(h, 0, &highPos, FILE_CURRENT);
|
|
149 |
} else {
|
|
150 |
lowPos = (DWORD)offset;
|
|
151 |
highPos = (long)(offset >> 32);
|
|
152 |
lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN);
|
|
153 |
}
|
|
154 |
if (lowPos == ((DWORD)-1)) {
|
|
155 |
if (GetLastError() != ERROR_SUCCESS) {
|
|
156 |
JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
|
|
157 |
return IOS_THROWN;
|
|
158 |
}
|
|
159 |
}
|
|
160 |
return (((jlong)highPos) << 32) | lowPos;
|
|
161 |
}
|
|
162 |
|
|
163 |
JNIEXPORT void JNICALL
|
|
164 |
Java_sun_nio_ch_FileChannelImpl_close0(JNIEnv *env, jobject this, jobject fdo)
|
|
165 |
{
|
|
166 |
HANDLE h = (HANDLE)(handleval(env, fdo));
|
|
167 |
if (h != INVALID_HANDLE_VALUE) {
|
|
168 |
jint result = CloseHandle(h);
|
|
169 |
if (result < 0) {
|
|
170 |
JNU_ThrowIOExceptionWithLastError(env, "Close failed");
|
|
171 |
}
|
|
172 |
}
|
|
173 |
}
|
|
174 |
|
|
175 |
JNIEXPORT jlong JNICALL
|
|
176 |
Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
|
|
177 |
jint srcFD,
|
|
178 |
jlong position, jlong count,
|
|
179 |
jint dstFD)
|
|
180 |
{
|
|
181 |
return IOS_UNSUPPORTED;
|
|
182 |
}
|