summaryrefslogtreecommitdiff
path: root/org.fox.ttcomics/src/main/java/com/github/junrar/io/Raw.java
blob: 87affda443a598dd9cbaf4ac37a4b1a7688cac7e (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 * Copyright (c) 2007 innoSysTec (R) GmbH, Germany. All rights reserved.
 * Original author: Edmund Wagner
 * Creation date: 18.06.2007
 *
 * Source: $HeadURL$
 * Last changed: $LastChangedDate$
 * 
 * the unrar licence applies to all junrar source and binary distributions 
 * you are not allowed to use this source to re-create the RAR compression algorithm
 * 
 * Here some html entities which can be used for escaping javadoc tags:
 * "&":  "&" or "&"
 * "<":  "&#060;" or "&lt;"
 * ">":  "&#062;" or "&gt;"
 * "@":  "&#064;" 
 */
package com.github.junrar.io;

/**
 * Read / write numbers to a byte[] regarding the endianness of the array
 * 
 * @author $LastChangedBy$
 * @version $LastChangedRevision$
 */
public class Raw {
    /**
     * Read a short value from the byte array at the given position (Big Endian)
     * 
     * @param array
     *            the array to read from
     * @param pos
     *            the position
     * @return the value
     */
    public static final short readShortBigEndian(byte[] array, int pos) {
	short temp = 0;
	temp |= array[pos] & 0xff;
	temp <<= 8;
	temp |= array[pos + 1] & 0xff;
	return temp;
    }

    /**
     * Read a int value from the byte array at the given position (Big Endian)
     * 
     * @param array
     *            the array to read from
     * @param pos
     *            the offset
     * @return the value
     */
    public static final int readIntBigEndian(byte[] array, int pos) {
	int temp = 0;
	temp |= array[pos] & 0xff;
	temp <<= 8;
	temp |= array[pos + 1] & 0xff;
	temp <<= 8;
	temp |= array[pos + 2] & 0xff;
	temp <<= 8;
	temp |= array[pos + 3] & 0xff;
	return temp;
    }

    /**
     * Read a long value from the byte array at the given position (Big Endian)
     * 
     * @param array
     *            the array to read from
     * @param pos
     *            the offset
     * @return the value
     */
    public static final long readLongBigEndian(byte[] array, int pos) {
	int temp = 0;
	temp |= array[pos] & 0xff;
	temp <<= 8;
	temp |= array[pos + 1] & 0xff;
	temp <<= 8;
	temp |= array[pos + 2] & 0xff;
	temp <<= 8;
	temp |= array[pos + 3] & 0xff;
	temp <<= 8;
	temp |= array[pos + 4] & 0xff;
	temp <<= 8;
	temp |= array[pos + 5] & 0xff;
	temp <<= 8;
	temp |= array[pos + 6] & 0xff;
	temp <<= 8;
	temp |= array[pos + 7] & 0xff;
	return temp;
    }

    /**
     * Read a short value from the byte array at the given position (little
     * Endian)
     * 
     * @param array
     *            the array to read from
     * @param pos
     *            the offset
     * @return the value
     */
    public static final short readShortLittleEndian(byte[] array, int pos) {
	short result = 0;
	result += array[pos + 1] & 0xff;
	result <<= 8;
	result += array[pos] & 0xff;
	return result;
    }

    /**
     * Read an int value from the byte array at the given position (little
     * Endian)
     * 
     * @param array
     *            the array to read from
     * @param pos
     *            the offset
     * @return the value
     */
    public static final int readIntLittleEndian(byte[] array, int pos) {
	return ((array[pos + 3] & 0xff) << 24)
		| ((array[pos + 2] & 0xff) << 16)
		| ((array[pos + 1] & 0xff) << 8) | ((array[pos] & 0xff));
    }

    /**
     * Read an long value(unsigned int) from the byte array at the given
     * position (little Endian)
     * 
     * @param array
     * @param pos
     * @return
     */
    public static final long readIntLittleEndianAsLong(byte[] array, int pos) {
	return (((long) array[pos + 3] & 0xff) << 24)
		| (((long) array[pos + 2] & 0xff) << 16)
		| (((long) array[pos + 1] & 0xff) << 8)
		| (((long) array[pos] & 0xff));
    }

    /**
     * Read a long value from the byte array at the given position (little
     * Endian)
     * 
     * @param array
     *            the array to read from
     * @param pos
     *            the offset
     * @return the value
     */
    public static final long readLongLittleEndian(byte[] array, int pos) {
	int temp = 0;
	temp |= array[pos + 7] & 0xff;
	temp <<= 8;
	temp |= array[pos + 6] & 0xff;
	temp <<= 8;
	temp |= array[pos + 5] & 0xff;
	temp <<= 8;
	temp |= array[pos + 4] & 0xff;
	temp <<= 8;
	temp |= array[pos + 3] & 0xff;
	temp <<= 8;
	temp |= array[pos + 2] & 0xff;
	temp <<= 8;
	temp |= array[pos + 1] & 0xff;
	temp <<= 8;
	temp |= array[pos];
	return temp;
    }

    /**
     * Write a short value into the byte array at the given position (Big
     * endian)
     * 
     * @param array
     *            the array
     * @param pos
     *            the offset
     * @param value
     *            the value to write
     */
    public static final void writeShortBigEndian(byte[] array, int pos,
	    short value) {
	array[pos] = (byte) (value >>> 8);
	array[pos + 1] = (byte) (value & 0xFF);

    }

    /**
     * Write an int value into the byte array at the given position (Big endian)
     * 
     * @param array
     *            the array
     * @param pos
     *            the offset
     * @param value
     *            the value to write
     */
    public static final void writeIntBigEndian(byte[] array, int pos, int value) {
	array[pos] = (byte) ((value >>> 24) & 0xff);
	array[pos + 1] = (byte) ((value >>> 16) & 0xff);
	array[pos + 2] = (byte) ((value >>> 8) & 0xff);
	array[pos + 3] = (byte) ((value) & 0xff);

    }

    /**
     * Write a long value into the byte array at the given position (Big endian)
     * 
     * @param array
     *            the array
     * @param pos
     *            the offset
     * @param value
     *            the value to write
     */
    public static final void writeLongBigEndian(byte[] array, int pos,
	    long value) {
	array[pos] = (byte) (value >>> 56);
	array[pos + 1] = (byte) (value >>> 48);
	array[pos + 2] = (byte) (value >>> 40);
	array[pos + 3] = (byte) (value >>> 32);
	array[pos + 4] = (byte) (value >>> 24);
	array[pos + 5] = (byte) (value >>> 16);
	array[pos + 6] = (byte) (value >>> 8);
	array[pos + 7] = (byte) (value & 0xFF);

    }

    /**
     * Write a short value into the byte array at the given position (little
     * endian)
     * 
     * @param array
     *            the array
     * @param pos
     *            the offset
     * @param value
     *            the value to write
     */
    public static final void writeShortLittleEndian(byte[] array, int pos,
	    short value) {
	array[pos + 1] = (byte) (value >>> 8);
	array[pos] = (byte) (value & 0xFF);

    }

    /**
     * Increment a short value at the specified position by the specified amount
     * (little endian).
     */
    public static final void incShortLittleEndian(byte[] array, int pos, int dv) {
	int c = ((array[pos] & 0xff) + (dv & 0xff)) >>> 8;
	array[pos] += dv & 0xff;
	if ((c > 0) || ((dv & 0xff00) != 0)) {
	    array[pos + 1] += ((dv >>> 8) & 0xff) + c;
	}
    }

    /**
     * Write an int value into the byte array at the given position (little
     * endian)
     * 
     * @param array
     *            the array
     * @param pos
     *            the offset
     * @param value
     *            the value to write
     */
    public static final void writeIntLittleEndian(byte[] array, int pos,
	    int value) {
	array[pos + 3] = (byte) (value >>> 24);
	array[pos + 2] = (byte) (value >>> 16);
	array[pos + 1] = (byte) (value >>> 8);
	array[pos] = (byte) (value & 0xFF);

    }

    /**
     * Write a long value into the byte array at the given position (little
     * endian)
     * 
     * @param array
     *            the array
     * @param pos
     *            the offset
     * @param value
     *            the value to write
     */
    public static final void writeLongLittleEndian(byte[] array, int pos,
	    long value) {
	array[pos + 7] = (byte) (value >>> 56);
	array[pos + 6] = (byte) (value >>> 48);
	array[pos + 5] = (byte) (value >>> 40);
	array[pos + 4] = (byte) (value >>> 32);
	array[pos + 3] = (byte) (value >>> 24);
	array[pos + 2] = (byte) (value >>> 16);
	array[pos + 1] = (byte) (value >>> 8);
	array[pos] = (byte) (value & 0xFF);

    }
}