• R/O
  • HTTP
  • SSH
  • HTTPS

dictzip-java: Commit


Commit MetaInfo

Revision68fcb738dd28bae1fc49ec3e55030f4344f648f7 (tree)
Zeit2022-02-27 00:36:01
AutorHiroshi Miura <miurahr@linu...>
CommiterHiroshi Miura

Log Message

RAIS: support buffering

- new method int read(long pos)
- Buffering input for performance.

Signed-off-by: Hiroshi Miura <miurahr@linux.com>

Ändern Zusammenfassung

Diff

--- a/dictzip-lib/src/main/java/org/dict/zip/RandomAccessInputStream.java
+++ b/dictzip-lib/src/main/java/org/dict/zip/RandomAccessInputStream.java
@@ -26,23 +26,37 @@ import java.io.RandomAccessFile;
2626
2727 /**
2828 * RandomAccessInputStream.
29+ * Buffering RandomAccessFile and provide InputStream interface.
2930 *
3031 * @author Ho Ngoc Duc
3132 * @author Hiroshi Miura
3233 */
3334 public class RandomAccessInputStream extends InputStream {
34-
35+ private static final int DEFAULT_BUFSIZE = 4096;
3536 private RandomAccessFile in;
37+ private byte inbuf[];
38+
39+ private long currentpos = 0;
40+ private long startpos = -1;
41+ private long endpos = -1;
3642
3743 private long mark = 0;
3844
45+ private int bufsize;
46+
47+ public RandomAccessInputStream(final RandomAccessFile inFile, final int bufsize) {
48+ this.in = inFile;
49+ this.bufsize = bufsize;
50+ inbuf = new byte[bufsize];
51+ }
52+
3953 /**
4054 * Construct RandomAccessInputStream from file.
4155 *
4256 * @param inFile RandomAccessFile
4357 */
4458 public RandomAccessInputStream(final RandomAccessFile inFile) {
45- this.in = inFile;
59+ this(inFile, DEFAULT_BUFSIZE);
4660 }
4761
4862 /**
@@ -92,7 +106,7 @@ public class RandomAccessInputStream extends InputStream {
92106 * @exception IOException if an I/O error has occurred.
93107 */
94108 public final long position() throws IOException {
95- return in.getFilePointer();
109+ return currentpos;
96110 }
97111
98112 @Deprecated
@@ -105,11 +119,7 @@ public class RandomAccessInputStream extends InputStream {
105119 */
106120 @Override
107121 public final synchronized void mark(final int markpos) {
108- try {
109- mark = position();
110- } catch (IOException e) {
111- throw new RuntimeException(e.toString());
112- }
122+ mark = currentpos;
113123 }
114124
115125 /**
@@ -125,7 +135,26 @@ public class RandomAccessInputStream extends InputStream {
125135 */
126136 @Override
127137 public final synchronized int read() throws IOException {
128- return in.read();
138+ return read(currentpos++);
139+ }
140+
141+ public int read(long pos) {
142+ if (pos < startpos || pos > endpos) {
143+ long blockstart = (pos/ bufsize) * bufsize;
144+ int n;
145+ try {
146+ in.seek(blockstart);
147+ n = in.read(inbuf);
148+ } catch (IOException e) {
149+ return -1;
150+ }
151+ startpos = blockstart;
152+ endpos = blockstart + n - 1;
153+ if (pos < startpos || pos > endpos) {
154+ return -1;
155+ }
156+ }
157+ return inbuf[(int) (pos - startpos)] & 0xff;
129158 }
130159
131160 /**
@@ -133,7 +162,17 @@ public class RandomAccessInputStream extends InputStream {
133162 */
134163 @Override
135164 public final int read(final byte[] buf, final int off, final int len) throws IOException {
136- return in.read(buf, off, len);
165+ int idx = 0;
166+ while (idx < len) {
167+ int c = read(currentpos);
168+ if (c == -1) {
169+ return idx;
170+ } else {
171+ buf[off + idx++] = (byte) c;
172+ currentpos++;
173+ }
174+ }
175+ return idx;
137176 }
138177
139178 /**
@@ -143,7 +182,15 @@ public class RandomAccessInputStream extends InputStream {
143182 * @exception IOException if an I/O error has occurred.
144183 */
145184 public final void readFully(final byte[] buf) throws IOException {
146- in.readFully(buf);
185+ int idx = 0;
186+ while (idx < buf.length) {
187+ int c = read(currentpos);
188+ if (c == -1) {
189+ throw new IOException();
190+ }
191+ buf[idx++] = (byte) c;
192+ currentpos++;
193+ }
147194 }
148195
149196 /**
@@ -151,7 +198,7 @@ public class RandomAccessInputStream extends InputStream {
151198 */
152199 @Override
153200 public final synchronized void reset() throws IOException {
154- in.seek(mark);
201+ currentpos = mark;
155202 }
156203
157204 /**
@@ -161,7 +208,7 @@ public class RandomAccessInputStream extends InputStream {
161208 * @exception IOException if an I/O error has occurred.
162209 */
163210 public final void seek(final long pos) throws IOException {
164- in.seek(pos);
211+ currentpos = pos;
165212 }
166213
167214 /**
@@ -169,6 +216,11 @@ public class RandomAccessInputStream extends InputStream {
169216 */
170217 @Override
171218 public final long skip(final long size) throws IOException {
172- return in.skipBytes((int) size);
219+ if (currentpos + size > length()) {
220+ currentpos = length();
221+ } else {
222+ currentpos += size;
223+ }
224+ return currentpos;
173225 }
174226 }
Show on old repository browser