Skip to content

Apache Lucene open-source search software

LUCENE-3011: FST saving/reading from a plain DataInput/DataOutput stream. Modified CodecUtil as well to use DataOutput instead of IndexOutput. Patch by Robert Muir.

Dawid Weiss2fe7952ce98d
diff --git a/lucene/src/java/org/apache/lucene/util/CodecUtil.java b/lucene/src/java/org/apache/lucene/util/CodecUtil.java
index 93aa5dc7aa8..f4caa2715b1 100644
--- a/lucene/src/java/org/apache/lucene/util/CodecUtil.java
+++ b/lucene/src/java/org/apache/lucene/util/CodecUtil.java
@@ -18,8 +18,8 @@ package org.apache.lucene.util;
*/
-import org.apache.lucene.store.IndexOutput;
-import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.store.DataInput;
+import org.apache.lucene.store.DataOutput;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexFormatTooNewException;
import org.apache.lucene.index.IndexFormatTooOldException;
@@ -35,18 +35,16 @@ public final class CodecUtil {
private final static int CODEC_MAGIC = 0x3fd76c17;
- public static IndexOutput writeHeader(IndexOutput out, String codec, int version)
+ public static DataOutput writeHeader(DataOutput out, String codec, int version)
throws IOException {
- final long start = out.getFilePointer();
+ BytesRef bytes = new BytesRef(codec);
+ if (bytes.length != codec.length() || bytes.length >= 128) {
+ throw new IllegalArgumentException("codec must be simple ASCII, less than 128 characters in length [got " + codec + "]");
+ }
out.writeInt(CODEC_MAGIC);
out.writeString(codec);
out.writeInt(version);
- // We require this so we can easily pre-compute header length
- if (out.getFilePointer()-start != codec.length()+9) {
- throw new IllegalArgumentException("codec must be simple ASCII, less than 128 characters in length [got " + codec + "]");
- }
-
return out;
}
@@ -54,7 +52,7 @@ public final class CodecUtil {
return 9+codec.length();
}
- public static int checkHeader(IndexInput in, String codec, int minVersion, int maxVersion)
+ public static int checkHeader(DataInput in, String codec, int minVersion, int maxVersion)
throws IOException {
// Safety to guard against reading a bogus string:
diff --git a/lucene/src/java/org/apache/lucene/util/automaton/fst/FST.java b/lucene/src/java/org/apache/lucene/util/automaton/fst/FST.java
index 98a654cbed2..b1eabb44c9d 100644
--- a/lucene/src/java/org/apache/lucene/util/automaton/fst/FST.java
+++ b/lucene/src/java/org/apache/lucene/util/automaton/fst/FST.java
@@ -21,8 +21,6 @@ import java.io.IOException;
import org.apache.lucene.store.DataInput;
import org.apache.lucene.store.DataOutput;
-import org.apache.lucene.store.IndexInput;
-import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.CodecUtil;
import org.apache.lucene.util.automaton.fst.Builder.UnCompiledNode;
@@ -168,7 +166,7 @@ public class FST<T> {
}
// create an existing FST
- public FST(IndexInput in, Outputs<T> outputs) throws IOException {
+ public FST(DataInput in, Outputs<T> outputs) throws IOException {
this.outputs = outputs;
writer = null;
CodecUtil.checkHeader(in, FILE_FORMAT_NAME, VERSION_START, VERSION_START);
@@ -250,7 +248,7 @@ public class FST<T> {
writer.posWrite = posSave;
}
- public void save(IndexOutput out) throws IOException {
+ public void save(DataOutput out) throws IOException {
if (startNode == -1) {
throw new IllegalStateException("call finish first");
}