1 package com.tapina.robe.runtime;
2
3 import java.io.UnsupportedEncodingException;
4 import java.awt.*;
5
6 /***
7 * Created by IntelliJ IDEA.
8 * User: gareth
9 * Date: Aug 21, 2003
10 * Time: 8:22:42 AM
11 */
12 public final class ByteArrayUtils {
13 public final static int getInt(byte[] bytes, int offset) {
14 int result = 0;
15 for (int shift = 0; shift < 32; shift += 8) {
16 result |= (((int) bytes[offset++]) & 0xff) << shift;
17 }
18 return result;
19 }
20
21 public final static void putInt(byte[] bytes, int offset, int value) {
22 for (int shift = 0; shift < 32; shift += 8) {
23 bytes[offset++] = (byte) ((value >> shift) & 0xff);
24 }
25 }
26
27 public final static long get5ByteValue(byte[] bytes, int offset) {
28 long result = 0;
29 for (int shift = 0; shift < 40; shift += 8) {
30 result |= (((long) bytes[offset++]) & 0xffL) << shift;
31 }
32 return result;
33 }
34
35 public final static void put5ByteValue(byte[] bytes, int offset, long value) {
36 for (int shift = 0; shift < 40; shift += 8) {
37 bytes[offset++] = (byte) ((value >> shift) & 0xff);
38 }
39 }
40
41 public final static String getString0(byte[] bytes, int offset) {
42 int i;
43
44 for (i = offset; i < bytes.length; i++) {
45 if (bytes[i] == 0) break;
46 }
47 return getString(bytes, offset, i);
48 }
49
50 private static String getString(byte[] bytes, int startOffset, int terminatorOffset) {
51 try {
52 return new String(bytes, startOffset, terminatorOffset - startOffset, "ISO-8859-1");
53 } catch (UnsupportedEncodingException e) {
54 throw new RuntimeException(e);
55 }
56 }
57
58 public static String getStringControlTerminated(byte[] bytes, int offset, int maxLength) {
59 int i;
60
61 for (i = offset; i < bytes.length && (i - offset) < maxLength; i++) {
62 if (bytes[i] < 32) {
63 break;
64 }
65 }
66 return getString(bytes, offset, i);
67 }
68
69 public final static String getStringN(byte[] bytes, int offset, int maxLength) {
70 int i;
71
72 for (i = offset; i < bytes.length && (i - offset) < maxLength; i++) {
73 if (bytes[i] == 0) break;
74 }
75 return getString(bytes, offset, i);
76 }
77
78 public static String getTerminatedString(byte[] bytes, int offset, StringTerminator stringTerminator, int maxLength) {
79 int i;
80
81 for (i = offset; i < bytes.length && (i - offset) < maxLength; i++) {
82 if (stringTerminator.isTerminalByte(bytes[i])) {
83 break;
84 }
85 }
86 return getString(bytes, offset, i);
87 }
88
89 public final static void putString0(byte[] bytes, int offset, String string) {
90 putTerminatedString(bytes, offset, string, (byte) 0, Integer.MAX_VALUE);
91 }
92
93 public static void putString0(byte[] bytes, int offset, String string, int length) {
94 putTerminatedString(bytes, offset, string, (byte) 0, length);
95 }
96
97 static void putTerminatedString(byte[] bytes, int offset, String string, final byte terminator,
98 int maxLength) {
99 try {
100 byte[] strBytes = string.getBytes("ISO-8859-1");
101 if (maxLength <= strBytes.length) {
102 System.arraycopy(strBytes, 0, bytes, offset, maxLength);
103 } else {
104 System.arraycopy(strBytes, 0, bytes, offset, strBytes.length);
105 bytes[offset + strBytes.length] = terminator;
106 }
107 } catch (UnsupportedEncodingException e) {
108 throw new RuntimeException(e);
109 }
110 }
111
112 public static void putStringCR(byte[] bytes, int offset, String string) {
113 putTerminatedString(bytes, offset, string, (byte) '\r', Integer.MAX_VALUE);
114 }
115
116 public static Point getPoint(final byte[] data, final int offset) {
117 final int x = getInt(data, offset);
118 final int y = getInt(data, offset + 4);
119 return new Point(x, y);
120 }
121
122 public static Rectangle getRectangle(final byte[] data, final int offset) {
123 final int minX = getInt(data, offset);
124 final int minY = getInt(data, offset + 4);
125 final int width = getInt(data, offset + 8) - minX;
126 final int height = getInt(data, offset + 12) - minY;
127 return new Rectangle(minX, minY, width, height);
128 }
129
130 public static void putRectangle(byte[] data, int offset, Rectangle r) {
131 putInt(data, offset, r.x);
132 putInt(data, offset + 4, r.y);
133 putInt(data, offset + 8, r.x + r.width);
134 putInt(data, offset + 12, r.y + r.height);
135 }
136
137 public static void putPoint(byte[] data, int offset, Point p) {
138 putInt(data, offset, p.x);
139 putInt(data, offset + 4, p.y);
140 }
141 }