summaryrefslogtreecommitdiff
path: root/org.fox.ttcomics/src/main/java/org/fox/ttcomics2/junrar/io/RandomAccessStream.java
blob: 3f8085fec4075d4a0d897f859772b300df41e9c0 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * public domain as of http://rsbweb.nih.gov/ij/disclaimer.html
 */
package org.fox.ttcomics2.junrar.io;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Vector;

@SuppressWarnings("rawtypes")
public final class RandomAccessStream extends InputStream {

    private static final int BLOCK_SIZE = 512;
    private static final int BLOCK_MASK = 511;
    private static final int BLOCK_SHIFT = 9;

    private InputStream src;
    private RandomAccessFile ras;
    private long pointer;
    private Vector data;
    private int length;
    private boolean foundEOS;

    public RandomAccessStream(InputStream inputstream) {
        pointer = 0L;
        data = new Vector();
        length = 0;
        foundEOS = false;
        src = inputstream;
    }

    public RandomAccessStream(RandomAccessFile ras) {
        this.ras = ras;
    }

    public int getFilePointer() throws IOException {
        if (ras != null)
            return (int) ras.getFilePointer();
        else
            return (int) pointer;
    }

    public long getLongFilePointer() throws IOException {
        if (ras != null)
            return ras.getFilePointer();
        else
            return pointer;
    }

    public int read() throws IOException {
        if (ras != null)
            return ras.read();
        long l = pointer + 1L;
        long l1 = readUntil(l);
        if (l1 >= l) {
            byte abyte0[] = (byte[]) data
                    .elementAt((int) (pointer >> BLOCK_SHIFT));
            return abyte0[(int) (pointer++ & BLOCK_MASK)] & 0xff;
        } else
            return -1;
    }

    public int read(byte[] bytes, int off, int len) throws IOException {
        if (bytes == null)
            throw new NullPointerException();
        if (ras != null)
            return ras.read(bytes, off, len);
        if (off < 0 || len < 0 || off + len > bytes.length)
            throw new IndexOutOfBoundsException();
        if (len == 0)
            return 0;
        long l = readUntil(pointer + len);
        if (l <= pointer)
            return -1;
        else {
            byte abyte1[] = (byte[]) data
                    .elementAt((int) (pointer >> BLOCK_SHIFT));
            int k = Math.min(len, BLOCK_SIZE - (int) (pointer & BLOCK_MASK));
            System.arraycopy(abyte1, (int) (pointer & BLOCK_MASK), bytes, off,
                    k);
            pointer += k;
            return k;
        }
    }

    public final void readFully(byte[] bytes) throws IOException {
        readFully(bytes, bytes.length);
    }

    public final void readFully(byte[] bytes, int len) throws IOException {
        int read = 0;
        do {
            int l = read(bytes, read, len - read);
            if (l < 0)
                break;
            read += l;
        } while (read < len);
    }

    @SuppressWarnings("unchecked")
    private long readUntil(long l) throws IOException {
        if (l < length)
            return l;
        if (foundEOS)
            return length;
        int i = (int) (l >> BLOCK_SHIFT);
        int j = length >> BLOCK_SHIFT;
        for (int k = j; k <= i; k++) {
            byte abyte0[] = new byte[BLOCK_SIZE];
            data.addElement(abyte0);
            int i1 = BLOCK_SIZE;
            int j1 = 0;
            while (i1 > 0) {
                int k1 = src.read(abyte0, j1, i1);
                if (k1 == -1) {
                    foundEOS = true;
                    return length;
                }
                j1 += k1;
                i1 -= k1;
                length += k1;
            }

        }

        return length;
    }

    public void seek(long loc) throws IOException {
        if (ras != null) {
            ras.seek(loc);
            return;
        }
        if (loc < 0L)
            pointer = 0L;
        else
            pointer = loc;
    }

    public void seek(int loc) throws IOException {
        long lloc = ((long) loc) & 0xffffffffL;
        if (ras != null) {
            ras.seek(lloc);
            return;
        }
        if (lloc < 0L)
            pointer = 0L;
        else
            pointer = lloc;
    }

    public final int readInt() throws IOException {
        int i = read();
        int j = read();
        int k = read();
        int l = read();
        if ((i | j | k | l) < 0)
            throw new EOFException();
        else
            return (i << 24) + (j << 16) + (k << 8) + l;
    }

    public final long readLong() throws IOException {
        return ((long) readInt() << 32) + ((long) readInt() & 0xffffffffL);
    }

    public final double readDouble() throws IOException {
        return Double.longBitsToDouble(readLong());
    }

    public final short readShort() throws IOException {
        int i = read();
        int j = read();
        if ((i | j) < 0)
            throw new EOFException();
        else
            return (short) ((i << 8) + j);
    }

    public final float readFloat() throws IOException {
        return Float.intBitsToFloat(readInt());
    }

    public void close() throws IOException {
        if (ras != null)
            ras.close();
        else {
            data.removeAllElements();
            src.close();
        }
    }

}