001: /*
002: * MCS Media Computer Software Copyright (c) 2006 by MCS
003: * -------------------------------------- Created on 06.01.2006 by w.klaas
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package de.mcs.utils;
018:
019: /**
020: * This class encapsulates methods to deal with arrays of numbers, converting
021: * from numbers to bytes and bytes to numbers.
022: * <p>
023: * Methods xxxToByte() convert a Java array of primitive numbers (int, short,
024: * ...) to a Java array of bytes. Methods byteToXxx() convert from a Java array
025: * of bytes into a Java array of primitive numbers (int, short, ...)
026: * <p>
027: * Variant interfaces convert a section of an array, and also can convert to
028: * sub-classes of Java <b>Number</b>.
029: * <P>
030: */
031: public final class Conversions {
032:
033: /** to prevent instancing. */
034: private Conversions() {
035:
036: }
037:
038: /**
039: * Convert an array of bytes into an array of ints.
040: *
041: * @param data
042: * The input array of bytes
043: * @throws ConversionException
044: * if something goes wrong.
045: * @return an array of int
046: */
047: public static int[] byteToInt(final byte[] data)
048: throws ConversionException {
049: if ((data.length % 4) > 0) {
050: throw new ConversionException("array length doesn't match.");
051: }
052: int[] values = new int[data.length >> 2];
053:
054: int i = 0;
055: for (byte b : data) {
056: int pos = i >> 2;
057: values[pos] <<= 8;
058: int aByte = b < 0 ? b + 256 : b;
059: values[pos] |= aByte;
060: i++;
061: }
062: return values;
063: }
064:
065: /**
066: * Convert an array of bytes into an array of floats.
067: *
068: * @param data
069: * The input array of bytes
070: * @throws ConversionException
071: * if something goes wrong.
072: * @return an array of float
073: */
074: public static float[] byteToFloat(final byte[] data)
075: throws ConversionException {
076: throw new ConversionException("not implemented yet.");
077: }
078:
079: /**
080: * Convert an array of bytes into an array of shorts.
081: *
082: * @param data
083: * The input array of bytes
084: * @throws ConversionException
085: * if something goes wrong.
086: * @return an array of short
087: */
088: public static short[] byteToShort(final byte[] data)
089: throws ConversionException {
090: if ((data.length % 2) > 0) {
091: throw new ConversionException("array length doesn't match.");
092: }
093: short[] values = new short[data.length >> 1];
094:
095: int i = 0;
096: for (byte b : data) {
097: int pos = i >> 1;
098: values[pos] <<= 8;
099: int aByte = b < 0 ? b + 256 : b;
100: values[pos] |= aByte;
101: i++;
102: }
103: return values;
104: }
105:
106: /**
107: * Convert an array of bytes into an array of long.
108: *
109: * @param data
110: * The input array of bytes
111: *
112: * @throws ConversionException
113: * if something goes wrong.
114: * @return an array of long
115: *
116: */
117: public static long[] byteToLong(final byte[] data)
118: throws ConversionException {
119: if ((data.length % 8) > 0) {
120: throw new ConversionException("array length doesn't match.");
121: }
122: long[] values = new long[data.length >> 3];
123:
124: int i = 0;
125: for (byte b : data) {
126: int pos = i >> 3;
127: values[pos] <<= 8;
128: int aByte = b < 0 ? b + 256 : b;
129: values[pos] |= aByte;
130: i++;
131: }
132: return values;
133: }
134:
135: /**
136: * Convert an array of bytes into an array of double.
137: *
138: * @param data
139: * The input array of bytes
140: * @throws ConversionException
141: * if something goes wrong.
142: * @return an array of double
143: */
144: public static double[] byteToDouble(final byte[] data)
145: throws ConversionException {
146: throw new ConversionException("not implemented yet.");
147: }
148:
149: /**
150: * Convert a range from an array of bytes into an array of int.
151: *
152: * @param start
153: * The position in the input array of bytes to start
154: * @param len
155: * The number of 'int' to convert
156: * @param data
157: * The input array of bytes
158: * @throws ConversionException
159: * if something goes wrong.
160: * @return an array of 'len' int
161: */
162: public static int[] byteToInt(final int start, final int len,
163: final byte[] data) throws ConversionException {
164: byte[] datas = new byte[len];
165: System.arraycopy(data, start, datas, 0, len);
166: return byteToInt(datas);
167: }
168:
169: /**
170: * Convert 4 bytes from an array of bytes into a single int.
171: *
172: * @param start
173: * The position in the input array of bytes to start
174: * @param data
175: * The input array of bytes
176: * @throws ConversionException
177: * if something goes wrong.
178: * @return The integer value of the bytes.
179: */
180: public static int byteToInt(final byte[] data, final int start)
181: throws ConversionException {
182: int[] ival = new int[1];
183: ival = byteToInt(start, 4, data);
184: return (ival[0]);
185: }
186:
187: /**
188: * Convert a range from an array of bytes into an array of short.
189: *
190: * @param start
191: * The position in the input array of bytes to start
192: * @param len
193: * The number of 'short' to convert
194: * @param data
195: * The input array of bytes
196: * @throws ConversionException
197: * if something goes wrong.
198: * @return an array of 'len' short
199: */
200: public static short[] byteToShort(final int start, final int len,
201: final byte[] data) throws ConversionException {
202: byte[] datas = new byte[len];
203: System.arraycopy(data, start, datas, 0, len);
204: return byteToShort(datas);
205: }
206:
207: /**
208: * Convert 2 bytes from an array of bytes into a single short.
209: *
210: * @param start
211: * The position in the input array of bytes to start
212: * @param data
213: * The input array of bytes
214: * @throws ConversionException
215: * if something goes wrong.
216: * @return The short value of the bytes.
217: */
218: public static short byteToShort(final byte[] data, final int start)
219: throws ConversionException {
220: short[] sval = new short[1];
221: sval = byteToShort(start, 2, data);
222: return (sval[0]);
223: }
224:
225: /**
226: * Convert a range from an array of bytes into an array of float.
227: *
228: * @param start
229: * The position in the input array of bytes to start
230: * @param len
231: * The number of 'float' to convert
232: * @param data
233: * The input array of bytes
234: * @throws ConversionException
235: * if something goes wrong.
236: * @return an array of 'len' float
237: */
238: public static float[] byteToFloat(final int start, final int len,
239: final byte[] data) throws ConversionException {
240: byte[] datas = new byte[len];
241: System.arraycopy(data, start, datas, 0, len);
242: return byteToFloat(datas);
243: }
244:
245: /**
246: * Convert 4 bytes from an array of bytes into a single float.
247: *
248: * @param start
249: * The position in the input array of bytes to start
250: * @param data
251: * The input array of bytes
252: * @throws ConversionException
253: * if something goes wrong.
254: * @return The float value of the bytes.
255: */
256: public static float byteToFloat(final byte[] data, final int start)
257: throws ConversionException {
258: float[] fval = new float[1];
259: fval = byteToFloat(start, 4, data);
260: return (fval[0]);
261: }
262:
263: /**
264: * Convert a range from an array of bytes into an array of long.
265: *
266: * @param start
267: * The position in the input array of bytes to start
268: * @param len
269: * The number of 'long' to convert
270: * @param data
271: * The input array of bytes
272: * @throws ConversionException
273: * if something goes wrong.
274: * @return an array of 'len' long
275: */
276: public static long[] byteToLong(final int start, final int len,
277: final byte[] data) throws ConversionException {
278: byte[] datas = new byte[len];
279: System.arraycopy(data, start, datas, 0, len);
280: return byteToLong(datas);
281: }
282:
283: /**
284: * Convert 8(?) bytes from an array of bytes into a single long.
285: *
286: * @param start
287: * The position in the input array of bytes to start
288: * @param data
289: * The input array of bytes
290: * @throws ConversionException
291: * if something goes wrong.
292: * @return The long value of the bytes.
293: */
294: public static long byteToLong(final byte[] data, final int start)
295: throws ConversionException {
296: long[] lval = new long[1];
297: lval = byteToLong(start, 8, data);
298: return (lval[0]);
299: }
300:
301: /**
302: * Convert a range from an array of bytes into an array of double.
303: *
304: * @param start
305: * The position in the input array of bytes to start
306: * @param len
307: * The number of 'double' to convert
308: * @param data
309: * The input array of bytes
310: * @throws ConversionException
311: * if something goes wrong.
312: * @return an array of 'len' double
313: */
314: public static double[] byteToDouble(final int start, final int len,
315: final byte[] data) throws ConversionException {
316: byte[] datas = new byte[len];
317: System.arraycopy(data, start, datas, 0, len);
318: return byteToDouble(datas);
319: }
320:
321: /**
322: * Convert 8 bytes from an array of bytes into a single double.
323: *
324: * @param start
325: * The position in the input array of bytes to start
326: * @param data
327: * The input array of bytes
328: * @throws ConversionException
329: * if something goes wrong.
330: * @return The double value of the bytes.
331: */
332: public static double byteToDouble(final byte[] data, final int start)
333: throws ConversionException {
334: double[] dval = new double[1];
335: dval = byteToDouble(start, 8, data);
336: return (dval[0]);
337: }
338:
339: /**
340: * Convert a range from an array of int into an array of bytes.
341: *
342: * @param start
343: * The position in the input array of int to start
344: * @param len
345: * The number of 'int' to convert
346: * @param data
347: * The input array of int
348: * @throws ConversionException
349: * if something goes wrong.
350: * @return an array of bytes
351: */
352: public static byte[] intToByte(final int start, final int len,
353: final int[] data) throws ConversionException {
354: int[] values = new int[len];
355: byte[] result = new byte[len * 4];
356: System.arraycopy(data, start, values, 0, len);
357: for (int i = 0; i < values.length; i++) {
358: byte[] news = intToByte(values[i]);
359: System.arraycopy(news, 0, result, i * 4, news.length);
360: }
361: return result;
362: }
363:
364: /**
365: * Convert a range from an array of short into an array of bytes.
366: *
367: * @param start
368: * The position in the input array of int to start
369: * @param len
370: * The number of 'short' to convert
371: * @param data
372: * The input array of short
373: * @throws ConversionException
374: * if something goes wrong.
375: * @return an array of bytes
376: */
377: public static byte[] shortToByte(final int start, final int len,
378: final short[] data) throws ConversionException {
379: short[] values = new short[len];
380: byte[] result = new byte[len * 2];
381: System.arraycopy(data, start, values, 0, len);
382: for (int i = 0; i < values.length; i++) {
383: byte[] news = shortToByte(values[i]);
384: System.arraycopy(news, 0, result, i * 2, news.length);
385: }
386: return result;
387: }
388:
389: /**
390: * Convert a range from an array of float into an array of bytes.
391: *
392: * @param start
393: * The position in the input array of int to start
394: * @param len
395: * The number of 'float' to convert
396: * @param data
397: * The input array of float
398: * @throws ConversionException
399: * if something goes wrong.
400: * @return an array of bytes
401: */
402: public static byte[] floatToByte(final int start, final int len,
403: final float[] data) throws ConversionException {
404: throw new ConversionException("not implemented yet.");
405: }
406:
407: /**
408: * Convert a range from an array of long into an array of bytes.
409: *
410: * @param start
411: * The position in the input array of int to start
412: * @param len
413: * The number of 'long' to convert
414: * @param data
415: * The input array of long
416: * @throws ConversionException
417: * if something goes wrong.
418: * @return an array of bytes
419: */
420: public static byte[] longToByte(final int start, final int len,
421: final long[] data) throws ConversionException {
422: long[] values = new long[len];
423: byte[] result = new byte[len * 8];
424: System.arraycopy(data, start, values, 0, len);
425: for (int i = 0; i < values.length; i++) {
426: byte[] news = longToByte(values[i]);
427: System.arraycopy(news, 0, result, i * 8, news.length);
428: }
429: return result;
430: }
431:
432: /**
433: * Convert a range from an array of double into an array of bytes.
434: *
435: * @param start
436: * The position in the input array of double to start
437: * @param len
438: * The number of 'double' to convert
439: * @param data
440: * The input array of double
441: * @throws ConversionException
442: * if something goes wrong.
443: * @return an array of bytes
444: */
445: public static byte[] doubleToByte(final int start, final int len,
446: final double[] data) throws ConversionException {
447: throw new ConversionException("not implemented yet.");
448: }
449:
450: /**
451: * Convert a single byte into an array of one byte.
452: * <p>
453: * (This is a trivial method.)
454: *
455: * @param data
456: * The input byte
457: * @return an array of bytes
458: */
459: public static byte[] byteToByte(final byte data) {
460: return new byte[] { data };
461: }
462:
463: /**
464: * Convert a single Byte object into an array of one byte.
465: * <p>
466: * (This is an almost trivial method.)
467: *
468: * @param data
469: * The input Byte
470: * @return an array of bytes
471: */
472: public static byte[] byteToByte(final Byte data) {
473: return byteToByte(data.byteValue());
474: }
475:
476: /**
477: * Convert a single int into an array of 4 bytes.
478: *
479: * @param data
480: * The input int
481: * @return an array of bytes
482: */
483: public static byte[] intToByte(final int data) {
484: byte[] value = new byte[4];
485: value[0] = (byte) (data >> 24);
486: value[1] = (byte) (data >> 16);
487: value[2] = (byte) (data >> 8);
488: value[3] = (byte) (data);
489: return value;
490: }
491:
492: /**
493: * Convert a single Integer object into an array of 4 bytes.
494: *
495: * @param data
496: * The input Integer
497: * @return an array of bytes
498: */
499: public static byte[] intToByte(final Integer data) {
500: return intToByte(data.intValue());
501: }
502:
503: /**
504: * Convert a single short into an array of 2 bytes.
505: *
506: * @param data
507: * The input short
508: * @return an array of bytes
509: */
510: public static byte[] shortToByte(final short data) {
511: byte[] value = new byte[2];
512: value[0] = (byte) (data >> 8);
513: value[1] = (byte) (data);
514: return value;
515: }
516:
517: /**
518: * Convert a single Short object into an array of 2 bytes.
519: *
520: * @param data
521: * The input Short
522: * @return an array of bytes
523: */
524: public static byte[] shortToByte(final Short data) {
525: return shortToByte(data.shortValue());
526: }
527:
528: /**
529: * Convert a single float into an array of 4 bytes.
530: *
531: * @param data
532: * The input float
533: * @return an array of bytes
534: */
535: public static byte[] floatToByte(final float data) {
536: return null;
537: }
538:
539: /**
540: * Convert a single Float object into an array of 4 bytes.
541: *
542: * @param data
543: * The input Float
544: * @return an array of bytes
545: */
546: public static byte[] floatToByte(final Float data) {
547: return floatToByte(data.floatValue());
548: };
549:
550: /**
551: * Convert a single long into an array of 8 bytes.
552: *
553: * @param data
554: * The input long
555: * @return an array of bytes
556: */
557: public static byte[] longToByte(final long data) {
558: byte[] value = new byte[8];
559: value[0] = (byte) (data >> 56);
560: value[1] = (byte) (data >> 48);
561: value[2] = (byte) (data >> 40);
562: value[3] = (byte) (data >> 32);
563: value[4] = (byte) (data >> 24);
564: value[5] = (byte) (data >> 16);
565: value[6] = (byte) (data >> 8);
566: value[7] = (byte) (data);
567: return value;
568: }
569:
570: /**
571: * Convert a single Long object into an array of 8(?) bytes.
572: *
573: * @param data
574: * The input Long
575: * @return an array of bytes
576: */
577: public static byte[] longToByte(final Long data) {
578: return longToByte(data.longValue());
579: }
580:
581: /**
582: * Convert a single double into an array of 8 bytes.
583: *
584: * @param data
585: * The input double
586: * @return an array of bytes
587: */
588: public static byte[] doubleToByte(final double data) {
589: return null;
590: }
591:
592: /**
593: * Convert a single Double object into an array of 8 bytes.
594: *
595: * @param data
596: * The input Double
597: * @return an array of bytes
598: */
599: public static byte[] doubleToByte(final Double data) {
600: return doubleToByte(data.doubleValue());
601: }
602:
603: /**
604: * Create a Number object from an array of bytes.
605: *
606: * @param barray
607: * The bytes to be converted
608: * @param obj
609: * Input object of the desired output class. Must be a sub-class
610: * of Number.
611: * @return A Object of the type of obj.
612: * @throws ConversionException
613: * if something goes wrong.
614: */
615: public static Object byteToNumber(final byte[] barray,
616: final Object obj) throws ConversionException {
617: Class theClass = obj.getClass();
618: String type = theClass.getName();
619: Object retobj = null;
620:
621: if (type.equals("java.lang.Integer")) {
622: int[] i = Conversions.byteToInt(0, 1, barray);
623: retobj = new Integer(i[0]);
624: } else if (type.equals("java.lang.Byte")) {
625: retobj = new Byte(barray[0]);
626: } else if (type.equals("java.lang.Short")) {
627: short[] f = Conversions.byteToShort(0, 1, barray);
628: retobj = new Short(f[0]);
629: } else if (type.equals("java.lang.Float")) {
630: float[] f = Conversions.byteToFloat(0, 1, barray);
631: retobj = new Float(f[0]);
632: } else if (type.equals("java.lang.Long")) {
633: long[] f = Conversions.byteToLong(0, 1, barray);
634: retobj = new Long(f[0]);
635: } else if (type.equals("java.lang.Double")) {
636: double[] f = Conversions.byteToDouble(0, 1, barray);
637: retobj = new Double(f[0]);
638: } else {
639: /* exception: unsupported type */
640: ConversionException ex = new ConversionException(
641: "byteToNumber: setfield bad type: " + obj + " "
642: + type);
643: throw (ex);
644: }
645: return (retobj);
646: }
647: }
|