Updated FSDirectory and RAMDirectory to no longer use deprecated methods.

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150537 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Doug Cutting 2004-09-28 20:45:26 +00:00
parent 79892c1f40
commit b32c045d35
8 changed files with 60 additions and 73 deletions

View File

@ -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++)

View File

@ -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");
}

View File

@ -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 {

View File

@ -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 disk
int 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;

View File

@ -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;
}
}

View File

@ -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;

View File

@ -306,10 +306,10 @@ public class TestCompoundFile extends TestCase
public 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 TestCase
CompoundFileReader.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 clone
IndexInput expected = dir.openInput("f11");
assertTrue(_TestHelper.isFSInputStreamOpen(expected));
assertTrue(_TestHelper.isFSIndexInputOpen(expected));
IndexInput one = cr.openInput("f11");
assertTrue(isCSIndexInputOpen(one));

View File

@ -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;