2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with this
4 * work for additional information regarding copyright ownership. The ASF
5 * licenses this file to You under the Apache License, Version 2.0 (the
6 * "License"); you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
22 * Windows Native IO methods.
27 * Utility to format a Windows system error code into an exception.
29 void throwIOException(JNIEnv *env, DWORD error)
34 ioex = env->FindClass("java/io/IOException");
37 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
38 NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &msg, 0, NULL );
39 env->ThrowNew(ioex, msg);
45 * Utility to throw Exceptions on bad input
47 void throwException(JNIEnv *env, const char *clazz, const char *msg)
49 jclass exc = env->FindClass(clazz);
52 env->ThrowNew(exc, msg);
57 * Opens a handle to a file.
59 * Class: org_apache_lucene_store_WindowsDirectory
61 * Signature: (Ljava/lang/String;)J
63 JNIEXPORT jlong JNICALL Java_org_apache_lucene_store_WindowsDirectory_open
64 (JNIEnv *env, jclass ignored, jstring filename)
69 if (filename == NULL) {
70 throwException(env, "java/lang/NullPointerException", "filename cannot be null");
74 fname = (char *) env->GetStringUTFChars(filename, NULL);
77 throwException(env, "java/lang/IllegalArgumentException", "invalid filename");
81 handle = CreateFile(fname, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
82 NULL, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, NULL);
84 env->ReleaseStringUTFChars(filename, fname);
86 if (handle == INVALID_HANDLE_VALUE) {
87 throwIOException(env, GetLastError());
91 return (jlong) handle;
95 * Reads data into the byte array, starting at offset, for length characters.
96 * The read is positioned at pos.
98 * Class: org_apache_lucene_store_WindowsDirectory
100 * Signature: (J[BIIJ)I
102 JNIEXPORT jint JNICALL Java_org_apache_lucene_store_WindowsDirectory_read
103 (JNIEnv *env, jclass ignored, jlong fd, jbyteArray bytes, jint offset, jint length, jlong pos)
105 OVERLAPPED io = { 0 };
108 io.Offset = (DWORD) (pos & 0xFFFFFFFF);
109 io.OffsetHigh = (DWORD) ((pos >> 0x20) & 0x7FFFFFFF);
112 throwException(env, "java/lang/NullPointerException", "bytes cannot be null");
116 if (length <= 4096) { /* For small buffers, avoid GetByteArrayElements' copy */
119 if (ReadFile((HANDLE) fd, &buffer, length, &numRead, &io)) {
120 env->SetByteArrayRegion(bytes, offset, numRead, (const jbyte *) buffer);
122 throwIOException(env, GetLastError());
127 jbyte *buffer = env->GetByteArrayElements (bytes, NULL);
129 if (!ReadFile((HANDLE) fd, (void *)(buffer+offset), length, &numRead, &io)) {
130 throwIOException(env, GetLastError());
134 env->ReleaseByteArrayElements(bytes, buffer, numRead == 0 || numRead == -1 ? JNI_ABORT : 0);
141 * Closes a handle to a file
143 * Class: org_apache_lucene_store_WindowsDirectory
147 JNIEXPORT void JNICALL Java_org_apache_lucene_store_WindowsDirectory_close
148 (JNIEnv *env, jclass ignored, jlong fd)
150 if (!CloseHandle((HANDLE) fd)) {
151 throwIOException(env, GetLastError());
156 * Returns the length in bytes of a file.
158 * Class: org_apache_lucene_store_WindowsDirectory
162 JNIEXPORT jlong JNICALL Java_org_apache_lucene_store_WindowsDirectory_length
163 (JNIEnv *env, jclass ignored, jlong fd)
165 BY_HANDLE_FILE_INFORMATION info;
167 if (GetFileInformationByHandle((HANDLE) fd, (LPBY_HANDLE_FILE_INFORMATION) &info)) {
168 return (jlong) (((DWORDLONG) info.nFileSizeHigh << 0x20) + info.nFileSizeLow);
170 throwIOException(env, GetLastError());