]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/engine/HtmlEntry.java
More compact row list format.
[Dictionary.git] / src / com / hughes / android / dictionary / engine / HtmlEntry.java
index 24424aa21c8bc7d6161f77d5d3299e7e11975da9..573a96992d88e2f53e33c44206c9486e927d3f2a 100644 (file)
@@ -1,15 +1,13 @@
 
 package com.hughes.android.dictionary.engine;
 
-import android.content.Intent;
-import android.util.Log;
-
-import com.hughes.android.dictionary.C;
 import com.hughes.util.StringUtil;
 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;
@@ -31,23 +29,22 @@ 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();
-        lazyHtmlLoader = new LazyHtmlLoader(raf);
+        lazyHtmlLoader = new LazyHtmlLoader(raf, dictionary.dictFileVersion);
         html = null;
     }
 
     @Override
-    public void write(RandomAccessFile raf) throws IOException {
+    public void write(DataOutput raf) throws IOException {
         super.write(raf);
         raf.writeUTF(title);
 
         final byte[] bytes = getHtml().getBytes("UTF-8");
         final byte[] zipBytes = StringUtil.zipBytes(bytes);
-        raf.writeInt(bytes.length);
-        raf.writeInt(zipBytes.length);
+        StringUtil.writeVarInt(raf, zipBytes.length);
         raf.write(zipBytes);
     }
 
@@ -76,15 +73,15 @@ 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);
         }
-    };
+    }
 
     public String getRawText(final boolean compact) {
         return title + ":\n" + getHtml();
@@ -109,9 +106,9 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntr
 
         boolean isExpanded = false;
 
-        Row(final RandomAccessFile raf, final int thisRowIndex,
-                final Index index) throws IOException {
-            super(raf, thisRowIndex, index);
+        Row(final DataInput raf, final int thisRowIndex,
+                final Index index, int extra) throws IOException {
+            super(raf, thisRowIndex, index, extra);
         }
 
         Row(final int referenceIndex, final int thisRowIndex,
@@ -179,16 +176,6 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntr
         return url.startsWith("q://d?");
     }
 
-    public static void quickdicUrlToIntent(final String url, final Intent intent) {
-        int firstColon = url.indexOf("?");
-        if (firstColon == -1)
-            return;
-        int secondColon = url.indexOf("&", firstColon + 1);
-        if (secondColon == -1)
-            return;
-        intent.putExtra(C.SEARCH_TOKEN, StringUtil.decodeFromUrl(url.substring(secondColon + 1)));
-    }
-
     // --------------------------------------------------------------------
 
     public static final class LazyHtmlLoader {
@@ -200,10 +187,15 @@ 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;
-            numBytes = raf.readInt();
-            numZipBytes = raf.readInt();
+        private LazyHtmlLoader(final DataInput inp, int version) throws IOException {
+            raf = (RandomAccessFile)inp;
+            if (version >= 7) {
+                numBytes = -1;
+                numZipBytes = StringUtil.readVarInt(raf);
+            } else {
+                numBytes = raf.readInt();
+                numZipBytes = raf.readInt();
+            }
             offset = raf.getFilePointer();
             raf.skipBytes(numZipBytes);
         }
@@ -215,7 +207,6 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntr
             }
             System.out.println("Loading Html: numBytes=" + numBytes + ", numZipBytes="
                     + numZipBytes);
-            final byte[] bytes = new byte[numBytes];
             final byte[] zipBytes = new byte[numZipBytes];
             synchronized (raf) {
                 try {
@@ -226,7 +217,7 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntr
                 }
             }
             try {
-                StringUtil.unzipFully(zipBytes, bytes);
+                final byte[] bytes = StringUtil.unzipFully(zipBytes, numBytes);
                 html = new String(bytes, "UTF-8");
             } catch (IOException e) {
                 throw new RuntimeException(e);