📄 fsdirectory.java
字号:
delete() and the call to renameTo() then we're screwed, but I've been unable to figure out how else to do this... */ if (nu.exists()) if (!nu.delete()) throw new IOException("Cannot delete " + nu); // Rename the old file to the new one. Unfortunately, the renameTo() // method does not work reliably under some JVMs. Therefore, if the // rename fails, we manually rename by copying the old file to the new one if (!old.renameTo(nu)) { java.io.InputStream in = null; java.io.OutputStream out = null; try { in = new FileInputStream(old); out = new FileOutputStream(nu); // see if the buffer needs to be initialized. Initialization is // only done on-demand since many VM's will never run into the renameTo // bug and hence shouldn't waste 1K of mem for no reason. if (buffer == null) { buffer = new byte[1024]; } int len; while ((len = in.read(buffer)) >= 0) { out.write(buffer, 0, len); } // delete the old file. old.delete(); } catch (IOException ioe) { IOException newExc = new IOException("Cannot rename " + old + " to " + nu); newExc.initCause(ioe); throw newExc; } finally { if (in != null) { try { in.close(); } catch (IOException e) { throw new RuntimeException("Cannot close input stream: " + e.toString(), e); } } if (out != null) { try { out.close(); } catch (IOException e) { throw new RuntimeException("Cannot close output stream: " + e.toString(), e); } } } } } /** Creates a new, empty file in the directory with the given name. Returns a stream writing this file. */ public IndexOutput createOutput(String name) throws IOException { File file = new File(directory, name); if (file.exists() && !file.delete()) // delete existing, if any throw new IOException("Cannot overwrite: " + file); return new FSIndexOutput(file); } /** Returns a stream reading an existing file. */ public IndexInput openInput(String name) throws IOException { return new FSIndexInput(new File(directory, name)); } /** * So we can do some byte-to-hexchar conversion below */ private static final char[] HEX_DIGITS = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; /** Constructs a {@link Lock} with the specified name. Locks are implemented * with {@link File#createNewFile()}. * * @param name the name of the lock file * @return an instance of <code>Lock</code> holding the lock */ public Lock makeLock(String name) { StringBuffer buf = getLockPrefix(); buf.append("-"); buf.append(name); // create a lock file final File lockFile = new File(lockDir, buf.toString()); return new Lock() { public boolean obtain() throws IOException { if (disableLocks) return true; if (!lockDir.exists()) { if (!lockDir.mkdirs()) { throw new IOException("Cannot create lock directory: " + lockDir); } } return lockFile.createNewFile(); } public void release() { if (disableLocks) return; lockFile.delete(); } public boolean isLocked() { if (disableLocks) return false; return lockFile.exists(); } public String toString() { return "Lock@" + lockFile; } }; } private StringBuffer getLockPrefix() { String dirName; // name to be hashed try { dirName = directory.getCanonicalPath(); } catch (IOException e) { throw new RuntimeException(e.toString(), e); } byte digest[]; synchronized (DIGESTER) { digest = DIGESTER.digest(dirName.getBytes()); } StringBuffer buf = new StringBuffer(); buf.append("lucene-"); for (int i = 0; i < digest.length; i++) { int b = digest[i]; buf.append(HEX_DIGITS[(b >> 4) & 0xf]); buf.append(HEX_DIGITS[b & 0xf]); } return buf; } /** Closes the store to future operations. */ public synchronized void close() { if (--refCount <= 0) { synchronized (DIRECTORIES) { DIRECTORIES.remove(directory); } } } public File getFile() { return directory; } /** For debug output. */ public String toString() { return this.getClass().getName() + "@" + directory; }}class FSIndexInput extends BufferedIndexInput { private class Descriptor extends RandomAccessFile { public long position; public Descriptor(File file, String mode) throws IOException { super(file, mode); } } private Descriptor file = null; boolean isClone; private long length; public FSIndexInput(File path) throws IOException { file = new Descriptor(path, "r"); length = file.length(); } /** IndexInput methods */ protected void readInternal(byte[] b, int offset, int len) throws IOException { synchronized (file) { long position = getFilePointer(); if (position != file.position) { file.seek(position); file.position = position; } int total = 0; do { int i = file.read(b, offset+total, len-total); if (i == -1) throw new IOException("read past EOF"); file.position += i; total += i; } while (total < len); } } public void close() throws IOException { if (!isClone) file.close(); } protected void seekInternal(long position) { } public long length() { return length; } protected void finalize() throws IOException { close(); // close the file } public Object clone() { FSIndexInput clone = (FSIndexInput)super.clone(); clone.isClone = true; return clone; } /** Method used for testing. Returns true if the underlying * file descriptor is valid. */ boolean isFDValid() throws IOException { return file.getFD().valid(); }}class FSIndexOutput extends BufferedIndexOutput { RandomAccessFile file = null; public FSIndexOutput(File path) throws IOException { file = new RandomAccessFile(path, "rw"); } /** output methods: */ public void flushBuffer(byte[] b, int size) throws IOException { file.write(b, 0, size); } public void close() throws IOException { super.close(); file.close(); } /** Random-access methods */ public void seek(long pos) throws IOException { super.seek(pos); file.seek(pos); } public long length() throws IOException { return file.length(); } protected void finalize() throws IOException { file.close(); // close the file }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -