Apache Solr open-source search software
Updated FSDirectory and RAMDirectory to no longer use deprecated methods.
Doug Cuttingb32c045d3535
diff --git a/src/java/org/apache/lucene/store/BufferedIndexOutput.java b/src/java/org/apache/lucene/store/BufferedIndexOutput.javaindex 1bb48e8aca4..9a20efff454 100644--- a/src/java/org/apache/lucene/store/BufferedIndexOutput.java+++ b/src/java/org/apache/lucene/store/BufferedIndexOutput.java@@ -27,7 +27,7 @@ public abstract class BufferedIndexOutput extends IndexOutput {private int bufferPosition = 0; // position in buffer/** Writes a single byte.- * @see InputStream#readByte()+ * @see IndexInput#readByte()*/public void writeByte(byte b) throws IOException {if (bufferPosition >= BUFFER_SIZE)@@ -38,7 +38,7 @@ public abstract class BufferedIndexOutput extends IndexOutput {/** Writes an array of bytes.* @param b the bytes to write* @param length the number of bytes to write- * @see InputStream#readBytes(byte[],int,int)+ * @see IndexInput#readBytes(byte[],int,int)*/public void writeBytes(byte[] b, int length) throws IOException {for (int i = 0; i < length; i++)diff --git a/src/java/org/apache/lucene/store/FSDirectory.java b/src/java/org/apache/lucene/store/FSDirectory.javaindex 40f2a9fa3a9..82e3ed5ed6a 100644--- a/src/java/org/apache/lucene/store/FSDirectory.java+++ b/src/java/org/apache/lucene/store/FSDirectory.java@@ -278,13 +278,13 @@ public class FSDirectory extends Directory {/** Creates a new, empty file in the directory with the given name.Returns a stream writing this file. */- public OutputStream createFile(String name) throws IOException {- return new FSOutputStream(new File(directory, name));+ public IndexOutput createOutput(String name) throws IOException {+ return new FSIndexOutput(new File(directory, name));}/** Returns a stream reading an existing file. */- public InputStream openFile(String name) throws IOException {- return new FSInputStream(new File(directory, name));+ public IndexInput openInput(String name) throws IOException {+ return new FSIndexInput(new File(directory, name));}/**@@ -385,47 +385,25 @@ public class FSDirectory extends Directory {}-class FSInputStream extends InputStream {+class FSIndexInput extends BufferedIndexInput {+private class Descriptor extends RandomAccessFile {- /* DEBUG */- //private String name;- /* DEBUG */public long position;public Descriptor(File file, String mode) throws IOException {super(file, mode);- /* DEBUG */- //name = file.toString();- //debug_printInfo("OPEN");- /* DEBUG */}+ }- /* DEBUG */- //public void close() throws IOException {- // debug_printInfo("CLOSE");- // super.close();- //}- //- //private void debug_printInfo(String op) {- // try { throw new Exception(op + " <" + name + ">");- // } catch (Exception e) {- // java.io.StringWriter sw = new java.io.StringWriter();- // java.io.PrintWriter pw = new java.io.PrintWriter(sw);- // e.printStackTrace(pw);- // System.out.println(sw.getBuffer().toString());- // }- //}- /* DEBUG */- }-- Descriptor file = null;+ private Descriptor file = null;boolean isClone;+ private long length;- public FSInputStream(File path) throws IOException {+ public FSIndexInput(File path) throws IOException {file = new Descriptor(path, "r");length = file.length();}- /** InputStream methods */+ /** IndexInput methods */protected void readInternal(byte[] b, int offset, int len)throws IOException {synchronized (file) {@@ -450,16 +428,19 @@ class FSInputStream extends InputStream {file.close();}- /** Random-access methods */protected void seekInternal(long position) {}+ public long length() {+ return length;+ }+protected void finalize() throws IOException {close(); // close the file}public Object clone() {- FSInputStream clone = (FSInputStream)super.clone();+ FSIndexInput clone = (FSIndexInput)super.clone();clone.isClone = true;return clone;}@@ -473,10 +454,10 @@ class FSInputStream extends InputStream {}-class FSOutputStream extends OutputStream {+class FSIndexOutput extends BufferedIndexOutput {RandomAccessFile file = null;- public FSOutputStream(File path) throws IOException {+ public FSIndexOutput(File path) throws IOException {file = new RandomAccessFile(path, "rw");}diff --git a/src/java/org/apache/lucene/store/IndexOutput.java b/src/java/org/apache/lucene/store/IndexOutput.javaindex 8c652dc93b2..7547fa62f62 100644--- a/src/java/org/apache/lucene/store/IndexOutput.java+++ b/src/java/org/apache/lucene/store/IndexOutput.java@@ -21,24 +21,24 @@ import java.io.IOException;/** Abstract base class for output to a file in a Directory. A random-access* output stream. Used for all Lucene index output operations.* @see Directory- * @see InputStream+ * @see IndexInput*/public abstract class IndexOutput {/** Writes a single byte.- * @see InputStream#readByte()+ * @see IndexInput#readByte()*/public abstract void writeByte(byte b) throws IOException;/** Writes an array of bytes.* @param b the bytes to write* @param length the number of bytes to write- * @see InputStream#readBytes(byte[],int,int)+ * @see IndexInput#readBytes(byte[],int,int)*/public abstract void writeBytes(byte[] b, int length) throws IOException;/** Writes an int as four bytes.- * @see InputStream#readInt()+ * @see IndexInput#readInt()*/public void writeInt(int i) throws IOException {writeByte((byte)(i >> 24));@@ -50,7 +50,7 @@ public abstract class IndexOutput {/** Writes an int in a variable-length format. Writes between one and* five bytes. Smaller values take fewer bytes. Negative numbers are not* supported.- * @see InputStream#readVInt()+ * @see IndexInput#readVInt()*/public void writeVInt(int i) throws IOException {while ((i & ~0x7F) != 0) {@@ -61,7 +61,7 @@ public abstract class IndexOutput {}/** Writes a long as eight bytes.- * @see InputStream#readLong()+ * @see IndexInput#readLong()*/public void writeLong(long i) throws IOException {writeInt((int) (i >> 32));@@ -71,7 +71,7 @@ public abstract class IndexOutput {/** Writes an long in a variable-length format. Writes between one and five* bytes. Smaller values take fewer bytes. Negative numbers are not* supported.- * @see InputStream#readVLong()+ * @see IndexInput#readVLong()*/public void writeVLong(long i) throws IOException {while ((i & ~0x7F) != 0) {@@ -82,7 +82,7 @@ public abstract class IndexOutput {}/** Writes a string.- * @see InputStream#readString()+ * @see IndexInput#readString()*/public void writeString(String s) throws IOException {int length = s.length();@@ -94,7 +94,7 @@ public abstract class IndexOutput {* @param s the source of the characters* @param start the first character in the sequence* @param length the number of characters in the sequence- * @see InputStream#readChars(char[],int,int)+ * @see IndexInput#readChars(char[],int,int)*/public void writeChars(String s, int start, int length)throws IOException {diff --git a/src/java/org/apache/lucene/store/RAMDirectory.java b/src/java/org/apache/lucene/store/RAMDirectory.javaindex deff040e3e3..4d0728573bd 100644--- a/src/java/org/apache/lucene/store/RAMDirectory.java+++ b/src/java/org/apache/lucene/store/RAMDirectory.java@@ -22,8 +22,8 @@ import java.util.Hashtable;import java.util.Enumeration;import org.apache.lucene.store.Directory;-import org.apache.lucene.store.InputStream;-import org.apache.lucene.store.OutputStream;+import org.apache.lucene.store.IndexInput;+import org.apache.lucene.store.IndexOutput;/*** A memory-resident {@link Directory} implementation.@@ -55,9 +55,9 @@ public final class RAMDirectory extends Directory {final String[] files = dir.list();for (int i = 0; i < files.length; i++) {// make place on ram disk- OutputStream os = createFile(files[i]);+ IndexOutput os = createOutput(files[i]);// read current file- InputStream is = dir.openFile(files[i]);+ IndexInput is = dir.openInput(files[i]);// and copy to ram diskint len = (int) is.length();byte[] buf = new byte[len];@@ -153,14 +153,14 @@ public final class RAMDirectory extends Directory {/** Creates a new, empty file in the directory with the given name.Returns a stream writing this file. */- public final OutputStream createFile(String name) {+ public final IndexOutput createOutput(String name) {RAMFile file = new RAMFile();files.put(name, file);return new RAMOutputStream(file);}/** Returns a stream reading an existing file. */- public final InputStream openFile(String name) {+ public final IndexInput openInput(String name) {RAMFile file = (RAMFile)files.get(name);return new RAMInputStream(file);}@@ -173,7 +173,7 @@ public final class RAMDirectory extends Directory {public boolean obtain() throws IOException {synchronized (files) {if (!fileExists(name)) {- createFile(name).close();+ createOutput(name).close();return true;}return false;diff --git a/src/java/org/apache/lucene/store/RAMInputStream.java b/src/java/org/apache/lucene/store/RAMInputStream.javaindex 504ff015945..7e6f2467e99 100644--- a/src/java/org/apache/lucene/store/RAMInputStream.java+++ b/src/java/org/apache/lucene/store/RAMInputStream.java@@ -17,14 +17,15 @@ package org.apache.lucene.store;*//**- * A memory-resident {@link InputStream} implementation.+ * A memory-resident {@link IndexInput} implementation.** @version $Id$*/-class RAMInputStream extends InputStream implements Cloneable {+class RAMInputStream extends BufferedIndexInput implements Cloneable {private RAMFile file;private int pointer = 0;+ private long length;public RAMInputStream(RAMFile f) {file = f;@@ -54,4 +55,9 @@ class RAMInputStream extends InputStream implements Cloneable {public void seekInternal(long pos) {pointer = (int)pos;}++ public long length() {+ return length;+ }+}diff --git a/src/java/org/apache/lucene/store/RAMOutputStream.java b/src/java/org/apache/lucene/store/RAMOutputStream.javaindex 36216922422..94eabcbf75e 100644--- a/src/java/org/apache/lucene/store/RAMOutputStream.java+++ b/src/java/org/apache/lucene/store/RAMOutputStream.java@@ -24,7 +24,7 @@ import java.io.IOException;* @version $Id$*/-public class RAMOutputStream extends OutputStream {+public class RAMOutputStream extends BufferedIndexOutput {private RAMFile file;private int pointer = 0;diff --git a/src/test/org/apache/lucene/index/TestCompoundFile.java b/src/test/org/apache/lucene/index/TestCompoundFile.javaindex 0453b98d03c..b08b34c6848 100644--- a/src/test/org/apache/lucene/index/TestCompoundFile.java+++ b/src/test/org/apache/lucene/index/TestCompoundFile.java@@ -306,10 +306,10 @@ public class TestCompoundFile extends TestCasepublic void testReadAfterClose() throws IOException {- demo_FSInputStreamBug((FSDirectory) dir, "test");+ demo_FSIndexInputBug((FSDirectory) dir, "test");}- private void demo_FSInputStreamBug(FSDirectory fsdir, String file)+ private void demo_FSIndexInputBug(FSDirectory fsdir, String file)throws IOException{// Setup the test file - we need more than 1024 bytes@@ -352,7 +352,7 @@ public class TestCompoundFile extends TestCaseCompoundFileReader.CSIndexInput cis =(CompoundFileReader.CSIndexInput) is;- return _TestHelper.isFSInputStreamOpen(cis.base);+ return _TestHelper.isFSIndexInputOpen(cis.base);} else {return false;}@@ -365,7 +365,7 @@ public class TestCompoundFile extends TestCase// basic cloneIndexInput expected = dir.openInput("f11");- assertTrue(_TestHelper.isFSInputStreamOpen(expected));+ assertTrue(_TestHelper.isFSIndexInputOpen(expected));IndexInput one = cr.openInput("f11");assertTrue(isCSIndexInputOpen(one));diff --git a/src/test/org/apache/lucene/store/_TestHelper.java b/src/test/org/apache/lucene/store/_TestHelper.javaindex ba3b6c38d14..0f83f4dc315 100644--- a/src/test/org/apache/lucene/store/_TestHelper.java+++ b/src/test/org/apache/lucene/store/_TestHelper.java@@ -8,35 +8,35 @@ import java.io.IOException;public class _TestHelper {/** Returns true if the instance of the provided input stream is actually- * an FSInputStream.+ * an FSIndexInput.*/- public static boolean isFSInputStream(IndexInput is) {- return is instanceof FSInputStream;+ public static boolean isFSIndexInput(IndexInput is) {+ return is instanceof FSIndexInput;}- /** Returns true if the provided input stream is an FSInputStream and+ /** Returns true if the provided input stream is an FSIndexInput and* is a clone, that is it does not own its underlying file descriptor.*/- public static boolean isFSInputStreamClone(IndexInput is) {- if (isFSInputStream(is)) {- return ((FSInputStream) is).isClone;+ public static boolean isFSIndexInputClone(IndexInput is) {+ if (isFSIndexInput(is)) {+ return ((FSIndexInput) is).isClone;} else {return false;}}- /** Given an instance of FSDirectory.FSInputStream, this method returns+ /** Given an instance of FSDirectory.FSIndexInput, this method returns* true if the underlying file descriptor is valid, and false otherwise.* This can be used to determine if the OS file has been closed.* The descriptor becomes invalid when the non-clone instance of the- * FSInputStream that owns this descriptor is closed. However, the+ * FSIndexInput that owns this descriptor is closed. However, the* descriptor may possibly become invalid in other ways as well.*/- public static boolean isFSInputStreamOpen(IndexInput is)+ public static boolean isFSIndexInputOpen(IndexInput is)throws IOException{- if (isFSInputStream(is)) {- FSInputStream fis = (FSInputStream) is;+ if (isFSIndexInput(is)) {+ FSIndexInput fis = (FSIndexInput) is;return fis.isFDValid();} else {return false;