1 package org.apache.lucene.store;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with this
6 * work for additional information regarding copyright ownership. The ASF
7 * licenses this file to You under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations under
20 import java.io.EOFException;
22 import java.io.IOException;
24 import org.apache.lucene.store.Directory; // javadoc
25 import org.apache.lucene.store.NativeFSLockFactory; // javadoc
28 * Native {@link Directory} implementation for Microsoft Windows.
32 * <li>Compile the source code to create WindowsDirectory.dll:
34 * c:\mingw\bin\g++ -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at
35 * -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -static-libgcc
36 * -static-libstdc++ -shared WindowsDirectory.cpp -o WindowsDirectory.dll
38 * For 64-bit JREs, use mingw64, with the -m64 option.
39 * <li>Put WindowsDirectory.dll into some directory in your windows PATH
40 * <li>Open indexes with WindowsDirectory and use it.
42 * @lucene.experimental
44 public class WindowsDirectory extends FSDirectory {
45 private static final int DEFAULT_BUFFERSIZE = 4096; /* default pgsize on ia32/amd64 */
48 System.loadLibrary("WindowsDirectory");
51 /** Create a new WindowsDirectory for the named location.
53 * @param path the path of the directory
54 * @param lockFactory the lock factory to use, or null for the default
55 * ({@link NativeFSLockFactory});
58 public WindowsDirectory(File path, LockFactory lockFactory) throws IOException {
59 super(path, lockFactory);
62 /** Create a new WindowsDirectory for the named location and {@link NativeFSLockFactory}.
64 * @param path the path of the directory
67 public WindowsDirectory(File path) throws IOException {
72 public IndexInput openInput(String name, int bufferSize) throws IOException {
74 return new WindowsIndexInput(new File(getDirectory(), name), Math.max(bufferSize, DEFAULT_BUFFERSIZE));
77 protected static class WindowsIndexInput extends BufferedIndexInput {
78 private final long fd;
79 private final long length;
83 public WindowsIndexInput(File file, int bufferSize) throws IOException {
84 super("WindowsIndexInput(path=\"" + file.getPath() + "\")", bufferSize);
85 fd = WindowsDirectory.open(file.getPath());
86 length = WindowsDirectory.length(fd);
91 protected void readInternal(byte[] b, int offset, int length) throws IOException {
94 bytesRead = WindowsDirectory.read(fd, b, offset, length, getFilePointer());
95 } catch (IOException ioe) {
96 IOException newIOE = new IOException(ioe.getMessage() + ": " + this);
97 newIOE.initCause(ioe);
101 if (bytesRead != length) {
102 throw new EOFException("Read past EOF (resource: " + this + ")");
107 protected void seekInternal(long pos) throws IOException {
111 public synchronized void close() throws IOException {
112 // NOTE: we synchronize and track "isOpen" because Lucene sometimes closes IIs twice!
113 if (!isClone && isOpen) {
114 WindowsDirectory.close(fd);
120 public long length() {
125 public Object clone() {
126 WindowsIndexInput clone = (WindowsIndexInput)super.clone();
127 clone.isClone = true;
132 /** Opens a handle to a file. */
133 private static native long open(String filename) throws IOException;
135 /** Reads data from a file at pos into bytes */
136 private static native int read(long fd, byte bytes[], int offset, int length, long pos) throws IOException;
138 /** Closes a handle to a file */
139 private static native void close(long fd) throws IOException;
141 /** Returns the length of a file */
142 private static native long length(long fd) throws IOException;