-
Notifications
You must be signed in to change notification settings - Fork 1
/
ZipIndexInput.java
97 lines (84 loc) · 1.8 KB
/
ZipIndexInput.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package org.apache.lucene.store;
import org.apache.lucene.store.IndexInput;
import java.io.InputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZipIndexInput extends IndexInput {
private ZipEntry entry;
private ZipFile file;
private InputStream stream;
long position;
public ZipIndexInput(String name, ZipFile f)
throws IOException
{
ZipEntry e = f.getEntry(name);
if(e == null) {
throw new IOException("No entry with name " + name);
}
init(e, f);
}
public ZipIndexInput(ZipEntry e, ZipFile f)
throws IOException
{
init(e, f);
}
public synchronized byte readByte()
throws IOException
{
byte b[] = new byte[1];
if(stream.read(b) != 1) {
throw new IOException();
}
position++;
return b[0];
}
public synchronized void readBytes(byte[] b, int offset, int len)
throws IOException
{
position += stream.read(b, offset, len);
}
public void close()
throws IOException
{
position = -1;
stream.close();
}
public long getFilePointer()
{
return position;
}
public void seek(long pos)
throws IOException
{
if(pos < position) {
// we need to start over because our inputstream doesn't have a seek
// we should probably use mark and reset...
resetStream();
stream.skip(pos);
position = pos;
}
else {
long togo = pos - position;
stream.skip(togo);
position = pos;
}
}
public long length()
{
return entry.getSize();
}
private void resetStream()
throws IOException
{
stream = file.getInputStream(entry);
position = 0;
}
private void init(ZipEntry e, ZipFile f)
throws IOException
{
entry = e;
file = f;
resetStream();
}
}