ByteArray.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13: package xeij;
14:
15: import java.io.*;
16: import java.util.zip.*;
17:
18: public class ByteArray {
19:
20:
21:
22:
23: public static byte byaRbs (byte[] bb, int a) {
24: return bb[a];
25: }
26:
27:
28:
29:
30: public static int byaRbz (byte[] bb, int a) {
31: return bb[a] & 255;
32: }
33:
34:
35:
36: public static int byaRws (byte[] bb, int a) {
37: return bb[a] << 8 | bb[a + 1] & 255;
38: }
39:
40:
41:
42: public static int byaRiws (byte[] bb, int a) {
43: return bb[a + 1] << 8 | bb[a] & 255;
44: }
45:
46:
47:
48: public static int byaRwz (byte[] bb, int a) {
49: return (char) (bb[a] << 8 | bb[a + 1] & 255);
50: }
51:
52:
53:
54: public static int byaRiwz (byte[] bb, int a) {
55: return (char) (bb[a + 1] << 8 | bb[a] & 255);
56: }
57:
58:
59:
60: public static int byaRls (byte[] bb, int a) {
61: return bb[a] << 24 | (bb[a + 1] & 255) << 16 | (char) (bb[a + 2] << 8 | bb[a + 3] & 255);
62: }
63:
64:
65:
66: public static int byaRils (byte[] bb, int a) {
67: return bb[a + 3] << 24 | (bb[a + 2] & 255) << 16 | (char) (bb[a + 1] << 8 | bb[a] & 255);
68: }
69:
70:
71:
72: public static long byaRqs (byte[] bb, int a) {
73: return (long) byaRls (bb, a) << 32 | byaRls (bb, a + 4) & 0xffffffffL;
74: }
75:
76:
77:
78:
79:
80:
81: public static StringBuilder byaRstr (StringBuilder sb, byte[] bb, int a, int l) {
82: for (int i = 0; i < l; i++) {
83: int s = bb[a + i] & 255;
84: char c;
85: if (0x81 <= s && s <= 0x9f || 0xe0 <= s && s <= 0xef) {
86: int t = a + 1 < l ? bb[a + 1] & 255 : 0;
87: if (0x40 <= t && t != 0x7f && t <= 0xfc) {
88: c = CharacterCode.chrSJISToChar[s << 8 | t];
89: if (c == 0) {
90: c = '\ufffd';
91: }
92: a++;
93: } else {
94: c = '.';
95: }
96: } else {
97: c = CharacterCode.chrSJISToChar[s];
98: if (c == 0) {
99: c = '\ufffd';
100: }
101: }
102: sb.append (c);
103: }
104: return sb;
105: }
106:
107:
108:
109: public static void byaWb (byte[] bb, int a, int d) {
110: bb[a ] = (byte) d;
111: }
112:
113:
114:
115: public static void byaWw (byte[] bb, int a, int d) {
116: bb[a ] = (byte) (d >> 8);
117: bb[a + 1] = (byte) d;
118: }
119:
120:
121:
122: public static void byaWwArray (byte[] bb, int a, int[] da) {
123: for (int i = 0, l = da.length; i < l; i++) {
124: int d = da[i];
125: bb[a ] = (byte) (d >> 8);
126: bb[a + 1] = (byte) d;
127: a += 2;
128: }
129: }
130:
131:
132:
133: public static void byaWiw (byte[] bb, int a, int d) {
134: bb[a + 1] = (byte) (d >> 8);
135: bb[a ] = (byte) d;
136: }
137:
138:
139:
140: public static void byaWl (byte[] bb, int a, int d) {
141: bb[a ] = (byte) (d >> 24);
142: bb[a + 1] = (byte) (d >> 16);
143: bb[a + 2] = (byte) (d >> 8);
144: bb[a + 3] = (byte) d;
145: }
146:
147:
148:
149: public static void byaWil (byte[] bb, int a, int d) {
150: bb[a + 3] = (byte) (d >> 24);
151: bb[a + 2] = (byte) (d >> 16);
152: bb[a + 1] = (byte) (d >> 8);
153: bb[a ] = (byte) d;
154: }
155:
156:
157:
158:
159:
160:
161: public static int byaWstr (byte[] bb, int a, String s) {
162: int l = s.length ();
163: for (int i = 0; i < l; i++) {
164: int c = CharacterCode.chrCharToSJIS[s.charAt (i)];
165: if (c == 0 && s.charAt (i) != '\0') {
166: c = 0x81a6;
167: }
168: if (c >> 8 != 0) {
169: bb[a++] = (byte) (c >> 8);
170: }
171: bb[a++] = (byte) c;
172: }
173: return a;
174: }
175:
176:
177:
178: public static int byaCmp (byte[] bbx, int ax, int sx, byte[] bby, int ay, int sy) {
179: int i;
180: for (i = 0; i < sx && i < sy; i++) {
181: int x = 0xff & bbx[ax + i];
182: int y = 0xff & bby[ay + i];
183: if (x != y) {
184: return x - y;
185: }
186: }
187: return sx - sy;
188: }
189:
190:
191:
192: public static void byaDump (byte[] bb, int p, int q) {
193: for (int p0 = p & -16, q0 = q + 15 & -16; p0 < q0; p0 += 1024) {
194: StringBuilder sb = new StringBuilder ();
195: for (int p1 = p0, q1 = Math.min (p0 + 1024, q0); p1 < q1; p1 += 16) {
196: XEiJ.fmtHex8 (sb, p1).append (' ');
197: for (int p2 = p1, q2 = p1 + 16; p2 < q2; p2++) {
198: if (p <= p2 && p2 < q) {
199: XEiJ.fmtHex2 (sb.append (' '), bb[p2]);
200: } else {
201: sb.append (' ').append (' ').append (' ');
202: }
203: }
204: sb.append (' ').append (' ');
205: int h = 0;
206: for (int p2 = p1, q2 = p1 + 16; p2 < q2 || p2 == q2 && h != 0; p2++) {
207: int l = p <= p2 && p2 < q ? bb[p2] & 255 : ' ';
208: if ((0x81 <= h && h <= 0x9f || 0xe0 <= h && h <= 0xef) &&
209: (0x40 <= l && l != 0x7f && l <= 0xfc)) {
210: l |= h << 8;
211: int c = CharacterCode.chrSJISToChar[l];
212: if (c != 0) {
213: sb.append ((char) c);
214: } else {
215: sb.append ('※');
216: }
217: h = 0;
218: } else {
219: if (h != 0) {
220: sb.append ('.');
221: h = 0;
222: }
223:
224: if (0x81 <= l && l <= 0x9f || 0xe0 <= l && l <= 0xef) {
225: h = l;
226: } else {
227: int c = CharacterCode.chrSJISToChar[l];
228: if (0x20 <= c && c != 0x7f) {
229: sb.append ((char) c);
230: } else {
231: sb.append ('.');
232: }
233: }
234: }
235: }
236: sb.append ('\n');
237: }
238: System.out.print (sb.toString ());
239: }
240: }
241:
242:
243:
244:
245:
246:
247:
248:
249:
250: public static final char[] BYA_BASE64_ENCODE_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray ();
251:
252:
253:
254:
255:
256: public static String byaEncodeBase64 (byte[] bb) {
257: return byaEncodeBase64 (bb, 0, bb.length);
258: }
259: public static String byaEncodeBase64 (byte[] bb, int o, int l) {
260: char[] w = new char[(l + 2) / 3 << 2];
261: l += o - 2;
262: int i, j;
263: for (i = o, j = 0; i < l; i += 3, j += 4) {
264: int c = bb[i] & 255;
265: int d = bb[i + 1] & 255;
266: int e = bb[i + 2] & 255;
267:
268:
269: w[j ] = BYA_BASE64_ENCODE_TABLE[c >> 2];
270: w[j + 1] = BYA_BASE64_ENCODE_TABLE[(c & 3) << 4 | d >> 4];
271: w[j + 2] = BYA_BASE64_ENCODE_TABLE[(d & 15) << 2 | e >> 6];
272: w[j + 3] = BYA_BASE64_ENCODE_TABLE[e & 63];
273: }
274: l += 2;
275: if (i < l) {
276: int c = bb[i] & 255;
277: int d = i + 1 < l ? bb[i + 1] & 255 : 0;
278: int e = i + 2 < l ? bb[i + 2] & 255 : 0;
279: w[j ] = BYA_BASE64_ENCODE_TABLE[c >> 2];
280: w[j + 1] = BYA_BASE64_ENCODE_TABLE[(c & 3) << 4 | d >> 4];
281: w[j + 2] = i + 1 < l ? BYA_BASE64_ENCODE_TABLE[(d & 15) << 2 | e >> 6] : '=';
282: w[j + 3] = i + 2 < l ? BYA_BASE64_ENCODE_TABLE[e & 63] : '=';
283: }
284: return new String (w);
285: }
286:
287: static {
288: if (false) {
289: System.out.println (byaEncodeBase64 ("".getBytes (XEiJ.ISO_8859_1)));
290: System.out.println (byaEncodeBase64 ("f".getBytes (XEiJ.ISO_8859_1)));
291: System.out.println (byaEncodeBase64 ("fo".getBytes (XEiJ.ISO_8859_1)));
292: System.out.println (byaEncodeBase64 ("foo".getBytes (XEiJ.ISO_8859_1)));
293: System.out.println (byaEncodeBase64 ("foob".getBytes (XEiJ.ISO_8859_1)));
294: System.out.println (byaEncodeBase64 ("fooba".getBytes (XEiJ.ISO_8859_1)));
295: System.out.println (byaEncodeBase64 ("foobar".getBytes (XEiJ.ISO_8859_1)));
296: }
297: }
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312: public static final char[] BYA_BASE64_DECODE_TABLE = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@>@@@?456789:;<=@@@@@@@\0\1\2\3\4\5\6\7\b\t\n\13\f\r\16\17\20\21\22\23\24\25\26\27\30\31@@@@@@\32\33\34\35\36\37 !\"#$%&\'()*+,-./0123@@@@@".toCharArray ();
313:
314:
315:
316:
317: public static byte[] byaDecodeBase64 (String s) {
318: char[] w = s.toCharArray ();
319: int ll = s.length ();
320: int l = 0;
321: for (int i = 0; i < ll; i++) {
322: char c = w[i];
323: if (c <= 127 && (c = BYA_BASE64_DECODE_TABLE[c]) <= 63) {
324: w[l++] = c;
325: }
326: }
327: int l3 = l & 3;
328: l -= l3;
329: byte[] bb = new byte[(l >> 2) * 3 + (l3 <= 1 ? 0 : l3 - 1)];
330: int i, j;
331: for (i = 0, j = 0; i < l; i += 4, j += 3) {
332: char c = w[i];
333: char d = w[i + 1];
334: char e = w[i + 2];
335: char f = w[i + 3];
336:
337:
338: bb[j] = (byte) (c << 2 | d >> 4);
339: bb[j + 1] = (byte) (d << 4 | e >> 2);
340: bb[j + 2] = (byte) (e << 6 | f);
341: }
342: if (l3 >= 2) {
343: char c = w[i];
344: char d = w[i + 1];
345: bb[j] = (byte) (c << 2 | d >> 4);
346: if (l3 >= 3) {
347: char e = w[i + 2];
348: bb[j + 1] = (byte) (d << 4 | e >> 2);
349: }
350: }
351: return bb;
352: }
353:
354: static {
355: if (false) {
356: try {
357: System.out.println (new String (byaDecodeBase64 (""), "ISO_8859_1"));
358: System.out.println (new String (byaDecodeBase64 ("Zg=="), "ISO_8859_1"));
359: System.out.println (new String (byaDecodeBase64 ("Zm8="), "ISO_8859_1"));
360: System.out.println (new String (byaDecodeBase64 ("Zm9v"), "ISO_8859_1"));
361: System.out.println (new String (byaDecodeBase64 ("Zm9vYg=="), "ISO_8859_1"));
362: System.out.println (new String (byaDecodeBase64 ("Zm9vYmE="), "ISO_8859_1"));
363: System.out.println (new String (byaDecodeBase64 ("Zm9vYmFy"), "ISO_8859_1"));
364: } catch (UnsupportedEncodingException uee) {
365: }
366: }
367: }
368:
369:
370:
371:
372: public static byte[] byaEncodeGzip (byte[] bb) {
373: return byaEncodeGzip (bb, 0, bb.length);
374: }
375: public static byte[] byaEncodeGzip (byte[] bb, int o, int l) {
376: ByteArrayOutputStream baos = new ByteArrayOutputStream ();
377: try (GZIPOutputStream gos = new GZIPOutputStream (baos) {
378: {
379:
380: def.setLevel (Deflater.DEFAULT_COMPRESSION);
381:
382: }
383: }) {
384: gos.write (bb, o, l);
385: gos.flush ();
386: } catch (IOException ioe) {
387: }
388: return baos.toByteArray ();
389: }
390:
391:
392:
393:
394: public static byte[] byaDecodeGzip (byte[] bb) {
395: return byaDecodeGzip (bb, 0, bb.length);
396: }
397: public static byte[] byaDecodeGzip (byte[] bb, int o, int l) {
398: ByteArrayOutputStream baos = new ByteArrayOutputStream ();
399: try (GZIPInputStream gis = new GZIPInputStream (new ByteArrayInputStream (bb, o, l))) {
400: int tl = 4096;
401: byte[] tbb = new byte[tl];
402: for (int k = gis.read (tbb, 0, tl); k >= 0; k = gis.read (tbb, 0, tl)) {
403: baos.write (tbb, 0, k);
404: }
405: } catch (IOException ioe) {
406: }
407: return baos.toByteArray ();
408: }
409:
410: public static int crc32 (byte[] b) {
411: return crc32 (b, 0, b.length);
412: }
413: public static int crc32 (byte[] b, int off, int len) {
414: CRC32 crc32 = new CRC32 ();
415: crc32.reset ();
416: crc32.update (b, off, len);
417: return (int) crc32.getValue ();
418: }
419:
420: }
421:
422:
423: