001: /*
002: * Copyright 2004,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.bsf.util.event.generator;
018:
019: /**
020: * Byte handling utilities
021: *
022: * 5 April 1999 - functions to append standard types to byte arrays
023: * functions to produce standard types from byte arrays
024: *
025: * @author Richard F. Boehme
026: *
027: */
028: public class ByteUtility {
029: public static byte[] addBytes(byte[] array, byte[] value) {
030: if (null != array) {
031: byte newarray[] = new byte[array.length + value.length];
032: System.arraycopy(array, 0, newarray, 0, array.length);
033: System.arraycopy(value, 0, newarray, array.length,
034: value.length);
035: array = newarray;
036: } else {
037: array = value;
038: }
039: return array;
040: }
041:
042: public static byte[] addBytes(byte[] array, byte value) {
043: if (null != array) {
044: byte newarray[] = new byte[array.length + 1];
045: System.arraycopy(array, 0, newarray, 0, array.length);
046: newarray[newarray.length - 1] = value;
047: array = newarray;
048: } else {
049: array = new byte[1];
050: array[0] = value;
051: }
052: return array;
053: }
054:
055: public static byte[] addBytes(byte[] array, int value) {
056: if (null != array) {
057: byte newarray[] = new byte[array.length + 3];
058: System.arraycopy(array, 0, newarray, 0, array.length);
059: newarray[newarray.length - 3] = (byte) ((value >> 16) & 0xFF);
060: newarray[newarray.length - 2] = (byte) ((value >> 8) & 0xFF);
061: newarray[newarray.length - 1] = (byte) (value & 0xFF);
062: array = newarray;
063: } else {
064: array = new byte[3];
065: array[0] = (byte) ((value >> 16) & 0xFF);
066: array[1] = (byte) ((value >> 8) & 0xFF);
067: array[2] = (byte) (value & 0xFF);
068: }
069: return array;
070: }
071:
072: public static byte[] addBytes(byte[] array, long value) {
073: if (null != array) {
074: byte newarray[] = new byte[array.length + 4];
075: System.arraycopy(array, 0, newarray, 0, array.length);
076: newarray[newarray.length - 4] = (byte) ((value >> 24) & 0xFF);
077: newarray[newarray.length - 3] = (byte) ((value >> 16) & 0xFF);
078: newarray[newarray.length - 2] = (byte) ((value >> 8) & 0xFF);
079: newarray[newarray.length - 1] = (byte) (value & 0xFF);
080: array = newarray;
081: } else {
082: array = new byte[4];
083: array[0] = (byte) ((value >> 24) & 0xFF);
084: array[1] = (byte) ((value >> 16) & 0xFF);
085: array[2] = (byte) ((value >> 8) & 0xFF);
086: array[3] = (byte) (value & 0xFF);
087: }
088: return array;
089: }
090:
091: public static byte[] addBytes(byte[] array, String value) {
092: if (null != value) {
093: if (null != array) {
094: byte newarray[] = new byte[array.length
095: + value.length()];
096: System.arraycopy(array, 0, newarray, 0, array.length);
097: System.arraycopy(value.getBytes(), 0, newarray,
098: array.length, value.length());
099: array = newarray;
100: } else {
101: array = value.getBytes();
102: }
103: }
104: return array;
105: }
106:
107: public static byte[] addBytes(byte[] array, short value) {
108: if (null != array) {
109: byte newarray[] = new byte[array.length + 2];
110: System.arraycopy(array, 0, newarray, 0, array.length);
111: newarray[newarray.length - 2] = (byte) ((value >> 8) & 0xFF);
112: newarray[newarray.length - 1] = (byte) (value & 0xFF);
113: array = newarray;
114: } else {
115: array = new byte[2];
116: array[0] = (byte) ((value >> 8) & 0xFF);
117: array[1] = (byte) (value & 0xFF);
118: }
119: return array;
120: }
121:
122: public static double byteArrayToDouble(byte high[], byte low[]) {
123: double temp = 0;
124: // high bytes
125: temp += (((long) high[0]) & 0xFF) << 56;
126: temp += (((long) high[1]) & 0xFF) << 48;
127: temp += (((long) high[2]) & 0xFF) << 40;
128: temp += (((long) high[3]) & 0xFF) << 32;
129: // low bytes
130: temp += (((long) low[0]) & 0xFF) << 24;
131: temp += (((long) low[1]) & 0xFF) << 16;
132: temp += (((long) low[2]) & 0xFF) << 8;
133: temp += (((long) low[3]) & 0xFF);
134: return temp;
135: }
136:
137: public static double byteArrayToDouble(byte value[]) {
138: byte high[] = new byte[4];
139: byte low[] = new byte[4];
140: high[0] = value[0];
141: high[1] = value[1];
142: high[2] = value[2];
143: high[3] = value[3];
144: low[0] = value[4];
145: low[1] = value[5];
146: low[2] = value[6];
147: low[3] = value[7];
148: return byteArrayToDouble(high, low);
149: }
150:
151: public static float byteArrayToFloat(byte value[]) {
152: float temp = 0;
153: temp += (((int) value[0]) & 0xFF) << 24;
154: temp += (((int) value[1]) & 0xFF) << 16;
155: temp += (((int) value[2]) & 0xFF) << 8;
156: temp += (((int) value[3]) & 0xFF);
157: return temp;
158: }
159:
160: public static int byteArrayToInt(byte value[]) {
161: int temp = 0;
162: temp += (((int) value[0]) & 0xFF) << 24;
163: temp += (((int) value[1]) & 0xFF) << 16;
164: temp += (((int) value[2]) & 0xFF) << 8;
165: temp += (((int) value[3]) & 0xFF);
166: return temp;
167: }
168:
169: public static long byteArrayToLong(byte value[]) {
170: byte high[] = new byte[4];
171: byte low[] = new byte[4];
172: high[0] = value[0];
173: high[1] = value[1];
174: high[2] = value[2];
175: high[3] = value[3];
176: low[0] = value[4];
177: low[1] = value[5];
178: low[2] = value[6];
179: low[3] = value[7];
180: return byteArrayToLong(high, low);
181: }
182:
183: public static long byteArrayToLong(byte high[], byte low[]) {
184: long temp = 0;
185: // high bytes
186: temp += (((long) high[0]) & 0xFF) << 56;
187: temp += (((long) high[1]) & 0xFF) << 48;
188: temp += (((long) high[2]) & 0xFF) << 40;
189: temp += (((long) high[3]) & 0xFF) << 32;
190: // low bytes
191: temp += (((long) low[0]) & 0xFF) << 24;
192: temp += (((long) low[1]) & 0xFF) << 16;
193: temp += (((long) low[2]) & 0xFF) << 8;
194: temp += (((long) low[3]) & 0xFF);
195: return temp;
196: }
197:
198: // make the following loops with check on array length *****************
199: public static short byteArrayToShort(byte value[]) {
200: short temp = 0;
201: temp += (((int) value[0]) & 0xFF) << 8;
202: temp += (((int) value[1]) & 0xFF);
203: return temp;
204: }
205:
206: public static String byteToHexString(byte value) {
207: String temp = null;
208:
209: switch ((value & 0xF0) >> 4) {
210: case 0:
211: temp = "0";
212: break;
213: case 1:
214: temp = "1";
215: break;
216: case 2:
217: temp = "2";
218: break;
219: case 3:
220: temp = "3";
221: break;
222: case 4:
223: temp = "4";
224: break;
225: case 5:
226: temp = "5";
227: break;
228: case 6:
229: temp = "6";
230: break;
231: case 7:
232: temp = "7";
233: break;
234: case 8:
235: temp = "8";
236: break;
237: case 9:
238: temp = "9";
239: break;
240: case 10:
241: temp = "A";
242: break;
243: case 11:
244: temp = "B";
245: break;
246: case 12:
247: temp = "C";
248: break;
249: case 13:
250: temp = "D";
251: break;
252: case 14:
253: temp = "E";
254: break;
255: case 15:
256: temp = "F";
257: break;
258: }
259: switch ((value & 0x0F)) {
260: case 0:
261: temp += "0";
262: break;
263: case 1:
264: temp += "1";
265: break;
266: case 2:
267: temp += "2";
268: break;
269: case 3:
270: temp += "3";
271: break;
272: case 4:
273: temp += "4";
274: break;
275: case 5:
276: temp += "5";
277: break;
278: case 6:
279: temp += "6";
280: break;
281: case 7:
282: temp += "7";
283: break;
284: case 8:
285: temp += "8";
286: break;
287: case 9:
288: temp += "9";
289: break;
290: case 10:
291: temp += "A";
292: break;
293: case 11:
294: temp += "B";
295: break;
296: case 12:
297: temp += "C";
298: break;
299: case 13:
300: temp += "D";
301: break;
302: case 14:
303: temp += "E";
304: break;
305: case 15:
306: temp += "F";
307: break;
308: }
309: return temp;
310: }
311: }
|