]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
Switch from RandomAccessFile to DataInput/DataOutput.
authorReimar Döffinger <Reimar.Doeffinger@gmx.de>
Sun, 6 Dec 2015 18:22:59 +0000 (19:22 +0100)
committerReimar Döffinger <Reimar.Doeffinger@gmx.de>
Sun, 6 Dec 2015 18:22:59 +0000 (19:22 +0100)
Should make it easier to add transparent compression support.

src/com/hughes/android/dictionary/engine/AbstractEntry.java
src/com/hughes/android/dictionary/engine/Dictionary.java
src/com/hughes/android/dictionary/engine/EntrySource.java
src/com/hughes/android/dictionary/engine/HtmlEntry.java
src/com/hughes/android/dictionary/engine/Index.java
src/com/hughes/android/dictionary/engine/PairEntry.java
src/com/hughes/android/dictionary/engine/RowBase.java
src/com/hughes/android/dictionary/engine/TextEntry.java
src/com/hughes/android/dictionary/engine/TokenRow.java

index 931f9a1bf3e4a53ae38384e773caf9072daa1cb5..2db0a3e4a83c3c4c8c24c5163e7b950ef374ada0 100644 (file)
@@ -16,8 +16,9 @@ package com.hughes.android.dictionary.engine;
 
 import com.hughes.util.IndexedObject;
 
+import java.io.DataInput;
+import java.io.DataOutput;
 import java.io.IOException;
-import java.io.RandomAccessFile;
 
 public abstract class AbstractEntry extends IndexedObject {
 
@@ -28,7 +29,7 @@ public abstract class AbstractEntry extends IndexedObject {
         this.entrySource = entrySource;
     }
 
-    public AbstractEntry(Dictionary dictionary, RandomAccessFile raf, final int index)
+    public AbstractEntry(Dictionary dictionary, DataInput raf, final int index)
             throws IOException {
         super(index);
         if (dictionary.dictFileVersion >= 1) {
@@ -39,7 +40,7 @@ public abstract class AbstractEntry extends IndexedObject {
         }
     }
 
-    public void write(RandomAccessFile raf) throws IOException {
+    public void write(DataOutput raf) throws IOException {
         raf.writeShort(entrySource.index());
     }
 
index 309ee60fb61691c33aaf1184f83e80f9fa918653..2cc05b4f573213659b17636fbb2d2a52a91cd003 100644 (file)
@@ -20,6 +20,8 @@ import com.hughes.util.raf.RAFList;
 import com.hughes.util.raf.RAFListSerializer;
 import com.hughes.util.raf.RAFSerializable;
 
+import java.io.DataInput;
+import java.io.DataOutput;
 import java.io.File;
 import java.io.IOException;
 import java.io.PrintStream;
@@ -104,7 +106,8 @@ public class Dictionary implements RAFSerializable<Dictionary> {
     }
 
     @Override
-    public void write(RandomAccessFile raf) throws IOException {
+    public void write(DataOutput out) throws IOException {
+        RandomAccessFile raf = (RandomAccessFile)out;
         raf.writeInt(dictFileVersion);
         raf.writeLong(creationMillis);
         raf.writeUTF(dictInfo);
@@ -118,26 +121,26 @@ public class Dictionary implements RAFSerializable<Dictionary> {
 
     private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
         @Override
-        public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
+        public Index read(DataInput raf, final int readIndex) throws IOException {
             return new Index(Dictionary.this, raf);
         }
 
         @Override
-        public void write(RandomAccessFile raf, Index t) throws IOException {
+        public void write(DataOutput raf, Index t) throws IOException {
             t.write(raf);
         }
     };
 
     final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
         @Override
-        public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
+        public void write(DataOutput raf, HtmlEntry t) throws IOException {
             if (t.index() == -1)
                 throw new IndexOutOfBoundsException();
             raf.writeInt(t.index());
         }
 
         @Override
-        public HtmlEntry read(RandomAccessFile raf, int readIndex) throws IOException {
+        public HtmlEntry read(DataInput raf, int readIndex) throws IOException {
             return htmlEntries.get(raf.readInt());
         }
     };
index 9ed16a19960d00ce50569c59009f3efcacd434da..c04358761c67bb98e5d51d4e3421a21fe2fb2fd9 100644 (file)
@@ -17,8 +17,9 @@ package com.hughes.android.dictionary.engine;
 import com.hughes.util.IndexedObject;
 import com.hughes.util.raf.RAFListSerializer;
 
+import java.io.DataInput;
+import java.io.DataOutput;
 import java.io.IOException;
-import java.io.RandomAccessFile;
 import java.io.Serializable;
 
 public class EntrySource extends IndexedObject implements Serializable {
@@ -56,7 +57,7 @@ public class EntrySource extends IndexedObject implements Serializable {
         }
 
         @Override
-        public EntrySource read(RandomAccessFile raf, int readIndex)
+        public EntrySource read(DataInput raf, int readIndex)
                 throws IOException {
             final String name = raf.readUTF();
             final int numEntries = dictionary.dictFileVersion >= 3 ? raf.readInt() : 0;
@@ -64,7 +65,7 @@ public class EntrySource extends IndexedObject implements Serializable {
         }
 
         @Override
-        public void write(RandomAccessFile raf, EntrySource t) throws IOException {
+        public void write(DataOutput raf, EntrySource t) throws IOException {
             raf.writeUTF(t.name);
             raf.writeInt(t.numEntries);
         }
index ae1f9f8755864a693e9c52b0652e8aa6ba039c26..e37d19c0abc33bb7c0f95806c57fb86a9ad4dcd0 100644 (file)
@@ -6,6 +6,8 @@ import com.hughes.util.raf.RAFListSerializer;
 import com.hughes.util.raf.RAFSerializable;
 import com.ibm.icu.text.Transliterator;
 
+import java.io.DataInput;
+import java.io.DataOutput;
 import java.io.IOException;
 import java.io.PrintStream;
 import java.io.RandomAccessFile;
@@ -27,7 +29,7 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntr
         lazyHtmlLoader = null;
     }
 
-    public HtmlEntry(Dictionary dictionary, RandomAccessFile raf, final int index)
+    public HtmlEntry(Dictionary dictionary, DataInput raf, final int index)
             throws IOException {
         super(dictionary, raf, index);
         title = raf.readUTF();
@@ -36,7 +38,7 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntr
     }
 
     @Override
-    public void write(RandomAccessFile raf) throws IOException {
+    public void write(DataOutput raf) throws IOException {
         super.write(raf);
         raf.writeUTF(title);
 
@@ -72,12 +74,12 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntr
         }
 
         @Override
-        public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
+        public HtmlEntry read(DataInput raf, final int index) throws IOException {
             return new HtmlEntry(dictionary, raf, index);
         }
 
         @Override
-        public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
+        public void write(DataOutput raf, HtmlEntry t) throws IOException {
             t.write(raf);
         }
     }
@@ -105,7 +107,7 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntr
 
         boolean isExpanded = false;
 
-        Row(final RandomAccessFile raf, final int thisRowIndex,
+        Row(final DataInput raf, final int thisRowIndex,
                 final Index index) throws IOException {
             super(raf, thisRowIndex, index);
         }
@@ -186,8 +188,8 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntr
         // Not sure this volatile is right, but oh well.
         volatile SoftReference<String> htmlRef = new SoftReference<String>(null);
 
-        private LazyHtmlLoader(final RandomAccessFile raf) throws IOException {
-            this.raf = raf;
+        private LazyHtmlLoader(final DataInput inp) throws IOException {
+            raf = (RandomAccessFile)inp;
             numBytes = raf.readInt();
             numZipBytes = raf.readInt();
             offset = raf.getFilePointer();
index f4666ccf11bcd9a6a5da590206f5755e5dbe0907..ce6947768ee77a8fd345eedbb53fb65e6c7f1d96 100644 (file)
@@ -31,6 +31,8 @@ import com.hughes.util.raf.UniformRAFList;
 import com.ibm.icu.text.Collator;
 import com.ibm.icu.text.Transliterator;
 
+import java.io.DataInput;
+import java.io.DataOutput;
 import java.io.IOException;
 import java.io.PrintStream;
 import java.io.RandomAccessFile;
@@ -115,8 +117,9 @@ public final class Index implements RAFSerializable<Index> {
         return new NormalizeComparator(normalizer(), sortLanguage.getCollator());
     }
 
-    public Index(final Dictionary dict, final RandomAccessFile raf) throws IOException {
+    public Index(final Dictionary dict, final DataInput inp) throws IOException {
         this.dict = dict;
+        RandomAccessFile raf = (RandomAccessFile)inp;
         shortName = raf.readUTF();
         longName = raf.readUTF();
         final String languageCode = raf.readUTF();
@@ -142,7 +145,8 @@ public final class Index implements RAFSerializable<Index> {
     }
 
     @Override
-    public void write(final RandomAccessFile raf) throws IOException {
+    public void write(final DataOutput out) throws IOException {
+        RandomAccessFile raf = (RandomAccessFile)out;
         raf.writeUTF(shortName);
         raf.writeUTF(longName);
         raf.writeUTF(sortLanguage.getIsoCode());
@@ -168,12 +172,12 @@ public final class Index implements RAFSerializable<Index> {
 
     private final RAFSerializer<IndexEntry> indexEntrySerializer = new RAFSerializer<IndexEntry>() {
         @Override
-        public IndexEntry read(RandomAccessFile raf) throws IOException {
+        public IndexEntry read(DataInput raf) throws IOException {
             return new IndexEntry(Index.this, raf);
         }
 
         @Override
-        public void write(RandomAccessFile raf, IndexEntry t) throws IOException {
+        public void write(DataOutput raf, IndexEntry t) throws IOException {
             t.write(raf);
         }
     };
@@ -198,8 +202,9 @@ public final class Index implements RAFSerializable<Index> {
             this.htmlEntries = new ArrayList<HtmlEntry>();
         }
 
-        public IndexEntry(final Index index, final RandomAccessFile raf) throws IOException {
+        public IndexEntry(final Index index, final DataInput inp) throws IOException {
             this.index = index;
+            RandomAccessFile raf = (RandomAccessFile)inp;
             token = raf.readUTF();
             startRow = raf.readInt();
             numRows = raf.readInt();
@@ -214,7 +219,8 @@ public final class Index implements RAFSerializable<Index> {
             }
         }
 
-        public void write(RandomAccessFile raf) throws IOException {
+        public void write(DataOutput out) throws IOException {
+            RandomAccessFile raf = (RandomAccessFile)out;
             raf.writeUTF(token);
             raf.writeInt(startRow);
             raf.writeInt(numRows);
index 6e95d32a203cadf2e5cf5e5585da988a436d8bde..00f690a3b0a7d4462b70844ed1048a4f51049434 100644 (file)
@@ -18,9 +18,10 @@ import com.hughes.util.raf.RAFListSerializer;
 import com.hughes.util.raf.RAFSerializable;
 import com.ibm.icu.text.Transliterator;
 
+import java.io.DataInput;
+import java.io.DataOutput;
 import java.io.IOException;
 import java.io.PrintStream;
-import java.io.RandomAccessFile;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.regex.Pattern;
@@ -40,7 +41,7 @@ public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntr
         this.pairs.add(new Pair(lang1, lang2));
     }
 
-    public PairEntry(final Dictionary dictionary, final RandomAccessFile raf, final int index)
+    public PairEntry(final Dictionary dictionary, final DataInput raf, final int index)
             throws IOException {
         super(dictionary, raf, index);
         final int size = raf.readInt();
@@ -51,7 +52,7 @@ public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntr
     }
 
     @Override
-    public void write(RandomAccessFile raf) throws IOException {
+    public void write(DataOutput raf) throws IOException {
         super.write(raf);
         // TODO: this could be a short.
         raf.writeInt(pairs.size());
@@ -71,12 +72,12 @@ public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntr
         }
 
         @Override
-        public PairEntry read(RandomAccessFile raf, int index) throws IOException {
+        public PairEntry read(DataInput raf, int index) throws IOException {
             return new PairEntry(dictionary, raf, index);
         }
 
         @Override
-        public void write(RandomAccessFile raf, PairEntry t) throws IOException {
+        public void write(DataOutput raf, PairEntry t) throws IOException {
             t.write(raf);
         }
     }
@@ -97,7 +98,7 @@ public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntr
 
     public static class Row extends RowBase {
 
-        Row(final RandomAccessFile raf, final int thisRowIndex,
+        Row(final DataInput raf, final int thisRowIndex,
                 final Index index) throws IOException {
             super(raf, thisRowIndex, index);
         }
index d76eb30bc6c6455a217280f62054a45c40a95cdb..1cbf047ab8aab1eafb627f0c2b772cde900bc9d7 100644 (file)
@@ -18,9 +18,10 @@ import com.hughes.util.IndexedObject;
 import com.hughes.util.raf.RAFListSerializer;
 import com.ibm.icu.text.Transliterator;
 
+import java.io.DataInput;
+import java.io.DataOutput;
 import java.io.IOException;
 import java.io.PrintStream;
-import java.io.RandomAccessFile;
 import java.util.Comparator;
 import java.util.List;
 import java.util.regex.Pattern;
@@ -41,7 +42,7 @@ public abstract class RowBase extends IndexedObject {
      */
     private TokenRow tokenRow = null;
 
-    RowBase(final RandomAccessFile raf, final int thisRowIndex, final Index index)
+    RowBase(final DataInput raf, final int thisRowIndex, final Index index)
             throws IOException {
         super(thisRowIndex);
         this.index = index;
@@ -147,7 +148,7 @@ public abstract class RowBase extends IndexedObject {
         }
 
         @Override
-        public RowBase read(RandomAccessFile raf, final int listIndex) throws IOException {
+        public RowBase read(DataInput raf, final int listIndex) throws IOException {
             final byte rowType = raf.readByte();
             if (rowType == 0) {
                 return new PairEntry.Row(raf, listIndex, index);
@@ -162,7 +163,7 @@ public abstract class RowBase extends IndexedObject {
         }
 
         @Override
-        public void write(RandomAccessFile raf, RowBase t) throws IOException {
+        public void write(DataOutput raf, RowBase t) throws IOException {
             if (t instanceof PairEntry.Row) {
                 raf.writeByte(0);
             } else if (t instanceof TokenRow) {
index fdd66b54b41937ca699b27f24cab64b372206acf..187216fe0eb1993ea28ba1a652a5919039268c6f 100644 (file)
@@ -18,9 +18,10 @@ import com.hughes.util.raf.RAFListSerializer;
 import com.hughes.util.raf.RAFSerializable;
 import com.ibm.icu.text.Transliterator;
 
+import java.io.DataInput;
+import java.io.DataOutput;
 import java.io.IOException;
 import java.io.PrintStream;
-import java.io.RandomAccessFile;
 import java.util.List;
 import java.util.regex.Pattern;
 
@@ -28,7 +29,7 @@ public class TextEntry extends AbstractEntry implements RAFSerializable<TextEntr
 
     final String text;
 
-    public TextEntry(final Dictionary dictionary, final RandomAccessFile raf, final int index)
+    public TextEntry(final Dictionary dictionary, final DataInput raf, final int index)
             throws IOException {
         super(dictionary, raf, index);
         text = raf.readUTF();
@@ -36,7 +37,7 @@ public class TextEntry extends AbstractEntry implements RAFSerializable<TextEntr
     }
 
     @Override
-    public void write(RandomAccessFile raf) throws IOException {
+    public void write(DataOutput raf) throws IOException {
         super.write(raf);
         raf.writeUTF(text);
     }
@@ -50,12 +51,12 @@ public class TextEntry extends AbstractEntry implements RAFSerializable<TextEntr
         }
 
         @Override
-        public TextEntry read(RandomAccessFile raf, final int index) throws IOException {
+        public TextEntry read(DataInput raf, final int index) throws IOException {
             return new TextEntry(dictionary, raf, index);
         }
 
         @Override
-        public void write(RandomAccessFile raf, TextEntry t) throws IOException {
+        public void write(DataOutput raf, TextEntry t) throws IOException {
             t.write(raf);
         }
     }
@@ -74,7 +75,7 @@ public class TextEntry extends AbstractEntry implements RAFSerializable<TextEntr
 
     public static class Row extends RowBase {
 
-        Row(final RandomAccessFile raf, final int thisRowIndex,
+        Row(final DataInput raf, final int thisRowIndex,
                 final Index index) throws IOException {
             super(raf, thisRowIndex, index);
         }
index 2f31fec746c4bd8d28a8115a3fce6c3fa04436ae..6ae67b9fb476fff0e52de4adbe9c5c843d8d36bc 100644 (file)
@@ -17,9 +17,9 @@ package com.hughes.android.dictionary.engine;
 import com.hughes.android.dictionary.engine.Index.IndexEntry;
 import com.ibm.icu.text.Transliterator;
 
+import java.io.DataInput;
 import java.io.IOException;
 import java.io.PrintStream;
-import java.io.RandomAccessFile;
 import java.util.List;
 import java.util.regex.Pattern;
 
@@ -27,7 +27,7 @@ public class TokenRow extends RowBase {
 
     public final boolean hasMainEntry;
 
-    TokenRow(final RandomAccessFile raf, final int thisRowIndex, final Index index,
+    TokenRow(final DataInput raf, final int thisRowIndex, final Index index,
             final boolean hasMainEntry) throws IOException {
         super(raf, thisRowIndex, index);
         this.hasMainEntry = hasMainEntry;