57 package com.boschrexroth.mlpi;
59 import java.io.UnsupportedEncodingException;
60 import java.lang.reflect.Array;
61 import java.math.BigInteger;
62 import java.nio.ByteBuffer;
63 import java.nio.ByteOrder;
64 import java.nio.CharBuffer;
65 import java.nio.ShortBuffer;
66 import java.util.Arrays;
67 import java.util.HashMap;
68 import java.util.LinkedList;
69 import java.util.List;
70 import java.util.StringTokenizer;
85 private final static BigInteger ULLONG_MAX_VALUE =
new BigInteger(
"18446744073709551615");
86 private final static long ULONG_MAX_VALUE = 4294967295L;
87 private final static int USHORT_MAX_VALUE = 65535;
88 private final static short UCHAR_MAX_VALUE = 255;
90 private final static BigInteger ULLONG_MIN_VALUE = BigInteger.ZERO;
91 private final static long ULONG_MIN_VALUE = 0L;
92 private final static int USHORT_MIN_VALUE = 0;
93 private final static short UCHAR_MIN_VALUE = 0;
106 private final String _name;
107 private final ContainerAccess _accessFlag;
108 private List<String> _tagList =
new LinkedList<String>();
111 private boolean _isDirty =
true;
113 private byte[] _data =
new byte[1];
114 private ByteBuffer _buffer;
121 _container = container;
122 _accessFlag = accessFlag;
125 public void finalize() {
129 public String getName() {
133 public void clear() {
135 _itemInfosMap.clear();
139 public void add(String tags) {
140 StringTokenizer tokenizer =
new StringTokenizer(tags,
";");
142 while (tokenizer.hasMoreTokens()) {
143 _tagList.add(tokenizer.nextToken());
148 public void add(String[] tags) {
149 for (String tag : tags) {
154 public void remove(String tags) {
155 StringTokenizer tokenizer =
new StringTokenizer(tags,
";");
157 while (tokenizer.hasMoreTokens()) {
158 _tagList.remove(tokenizer.nextToken());
163 public void remove(String[] tags) {
164 for (String tag : tags) {
169 public String[]
get() {
170 return _tagList.toArray(
new String[_tagList.size()]);
173 public void create(){
176 if (_containerHandle.containerID != -1) {
177 _container.
destroy(_containerHandle);
178 _containerHandle.connectionID = -1;
179 _containerHandle.containerID = -1;
183 StringBuilder tagList =
new StringBuilder();
184 for (String tag : _tagList) {
189 int size = _container.
create(tagList.toString(), _accessFlag, _containerHandle);
190 _data =
new byte[size];
191 _buffer = ByteBuffer.wrap(_data);
192 _buffer.order(ByteOrder.LITTLE_ENDIAN);
199 _itemInfosMap.clear();
202 _itemInfosMap.put(_tagList.get(i), item);
218 if (_containerHandle.containerID != -1) {
219 _container.
destroy(_containerHandle);
220 _containerHandle.connectionID = -1;
221 _containerHandle.containerID = -1;
237 _container.
update(_containerHandle, _data);
240 static private boolean isArray(
final Object obj) {
241 return obj instanceof Object[] || obj instanceof
boolean[] ||
242 obj instanceof byte[] || obj instanceof
short[] ||
243 obj instanceof
char[] || obj instanceof
int[] ||
244 obj instanceof
long[] || obj instanceof
float[] ||
245 obj instanceof
double[] || obj instanceof BigInteger[];
249 if (_buffer == null) {
250 throw new RuntimeException(
"Container not initialized! You need to successfully call create()!");
253 if (!isArray(data)) {
254 setDataByItemInfo(itemInfo, (Number) data);
258 final int len = Array.getLength(data);
260 _buffer.position(itemInfo.offset);
261 switch(itemInfo.type) {
262 case TYPE_CHAR_ARRAY:
264 if (len > itemInfo.dataSize / 1) {
265 throw new RuntimeException(
"Array is too long for container element in container " + _name);
268 for (
int i = 0; i < len; i++) {
269 final byte value = Array.getByte(data, i);
274 case TYPE_UCHAR_ARRAY:
276 if (len > itemInfo.dataSize / 1) {
277 throw new RuntimeException(
"Array is too long for container element in container " + _name);
280 for (
int i = 0; i < len; i++) {
281 final byte value = (byte) (Array.getShort(data, i) & UCHAR_MAX_VALUE);
286 case TYPE_SHORT_ARRAY:
288 if (len > itemInfo.dataSize / 2) {
289 throw new RuntimeException(
"Array is too long for container element in container " + _name);
292 for (
int i = 0; i < len; i++) {
293 final short value = Array.getShort(data, i);
294 _buffer.putShort(value);
298 case TYPE_USHORT_ARRAY:
300 if (len > itemInfo.dataSize / 2) {
301 throw new RuntimeException(
"Array is too long for container element in container " + _name);
304 for (
int i = 0; i < len; i++) {
305 final short value = (short) (Array.getInt(data, i) & USHORT_MAX_VALUE);
306 _buffer.putShort(value);
310 case TYPE_LONG_ARRAY:
312 if (len > itemInfo.dataSize / 4) {
313 throw new RuntimeException(
"Array is too long for container element in container " + _name);
316 for (
int i = 0; i < len; i++) {
317 final int value = Array.getInt(data, i);
318 _buffer.putInt(value);
322 case TYPE_ULONG_ARRAY:
324 if (len > itemInfo.dataSize / 4) {
325 throw new RuntimeException(
"Array is too long for container element in container " + _name);
328 for (
int i = 0; i < len; i++) {
329 final int value = (int) (Array.getLong(data, i) & ULONG_MAX_VALUE);
330 _buffer.putInt(value);
334 case TYPE_LLONG_ARRAY:
336 if (len > itemInfo.dataSize / 8) {
337 throw new RuntimeException(
"Array is too long for container element in container " + _name);
340 for (
int i = 0; i < len; i++) {
341 final long value = Array.getLong(data, i);
342 _buffer.putLong(value);
346 case TYPE_ULLONG_ARRAY:
348 if (len > itemInfo.dataSize / 8) {
349 throw new RuntimeException(
"Array is too long for container element in container " + _name);
352 BigInteger[] bigIntegerArray = (BigInteger[]) data;
353 for (
int i = 0; i < len; i++) {
354 _buffer.putLong(bigIntegerArray[i].longValue());
358 case TYPE_BOOL8_ARRAY:
360 if (len > itemInfo.dataSize / 1) {
361 throw new RuntimeException(
"Array is too long for container element in container " + _name);
364 for (
int i = 0; i < len; i++) {
365 final byte value = (Array.getBoolean(data, i)) ? (byte)1 : (byte)0;
370 case TYPE_FLOAT_ARRAY:
372 if (len > itemInfo.dataSize / 4) {
373 throw new RuntimeException(
"Array is too long for container element in container " + _name);
376 for (
int i = 0; i < len; i++) {
377 final float value = Array.getFloat(data, i);
378 _buffer.putFloat(value);
382 case TYPE_DOUBLE_ARRAY:
384 if (len > itemInfo.dataSize / 8) {
385 throw new RuntimeException(
"Array is too long for container element in container " + _name);
388 for (
int i = 0; i < len; i++) {
389 final double value = Array.getDouble(data, i);
390 _buffer.putDouble(value);
394 case TYPE_CHAR_UTF16:
396 case TYPE_CHAR_UTF8_ARRAY:
397 case TYPE_CHAR_UTF16_ARRAY:
400 throw new RuntimeException(
"Unsupported type in container. Expected: " + itemInfo.type.toString());
405 if (_buffer == null) {
406 throw new RuntimeException(
"Container not initialized! You need to successfully call create()!");
409 _buffer.position(itemInfo.offset);
410 switch(itemInfo.type) {
412 case TYPE_CHAR_ARRAY:
414 if (data.longValue() < Byte.MIN_VALUE || data.longValue() > Byte.MAX_VALUE) {
415 throw new ArithmeticException(
"Data " + data.toString() +
" can not be converted to SHORT type!");
417 _buffer.put(data.byteValue());
421 case TYPE_UCHAR_ARRAY:
423 if (data.longValue() < UCHAR_MIN_VALUE || data.longValue() > UCHAR_MAX_VALUE) {
424 throw new ArithmeticException(
"Data " + data.toString() +
" can not be converted to UCHAR type!");
426 _buffer.put((byte)data.byteValue());
430 case TYPE_SHORT_ARRAY:
432 if (data.longValue() < Short.MIN_VALUE || data.longValue() > Short.MAX_VALUE) {
433 throw new ArithmeticException(
"Data " + data.toString() +
" can not be converted to SHORT type!");
435 _buffer.putShort(data.shortValue());
439 case TYPE_USHORT_ARRAY:
441 if (data.longValue() < USHORT_MIN_VALUE || data.longValue() > USHORT_MAX_VALUE) {
442 throw new ArithmeticException(
"Data " + data.toString() +
" can not be converted to USHORT type!");
444 _buffer.putShort(data.shortValue());
448 case TYPE_LONG_ARRAY:
450 if (data.longValue() < Integer.MIN_VALUE || data.longValue() > Integer.MAX_VALUE) {
451 throw new ArithmeticException(
"Data " + data.toString() +
" can not be converted to USHORT type!");
453 _buffer.putInt(data.intValue());
457 case TYPE_ULONG_ARRAY:
459 if (data.longValue() < ULONG_MIN_VALUE || data.longValue() > ULONG_MAX_VALUE) {
460 throw new ArithmeticException(
"Data " + data.toString() +
" can not be converted to ULONG type!");
462 _buffer.putInt(data.intValue());
466 case TYPE_LLONG_ARRAY:
468 if (data.longValue() < Long.MIN_VALUE || data.longValue() > Long.MAX_VALUE) {
469 throw new ArithmeticException(
"Data " + data.toString() +
" can not be converted to LLONG type!");
471 _buffer.putLong(data.longValue());
475 case TYPE_ULLONG_ARRAY:
477 final BigInteger value =
new BigInteger(data.toString());
478 if ( (value.compareTo(ULLONG_MIN_VALUE) < 0) || (value.compareTo(ULLONG_MAX_VALUE) > 0)) {
479 throw new ArithmeticException(
"Data " + data.toString() +
" can not be converted to ULLONG type!");
481 _buffer.putLong(value.longValue());
485 case TYPE_FLOAT_ARRAY:
487 if (data.doubleValue() < -Float.MAX_VALUE || data.doubleValue() > Float.MAX_VALUE) {
488 throw new ArithmeticException(
"Data " + data.toString() +
" can not be converted to LLONG type!");
490 _buffer.putFloat(data.floatValue());
494 case TYPE_DOUBLE_ARRAY:
496 _buffer.putDouble(data.doubleValue());
500 case TYPE_BOOL8_ARRAY:
502 final byte value = (data.intValue() != 0) ? (byte)1 : (byte)0;
508 final String value = data.toString();
509 if (value.length() >= itemInfo.dataSize / 1) {
510 throw new RuntimeException(
"String too long for container element. Data: " + value);
514 byte[] value2 = value.getBytes(
"ISO-8859-1");
516 }
catch (UnsupportedEncodingException e) {
517 throw new RuntimeException(
"String cannot be converted to ansi string. Data: " + data);
522 case TYPE_CHAR_UTF16:
523 case TYPE_CHAR_UTF8_ARRAY:
524 case TYPE_CHAR_UTF16_ARRAY:
527 throw new RuntimeException(
"Unsupported type in container. Type: " + itemInfo.type.toString());
532 if (_buffer == null) {
533 throw new RuntimeException(
"Container not initialized! You need to successfully call create()!");
536 _buffer.position(itemInfo.offset);
537 switch(itemInfo.type) {
540 final byte value = Byte.parseByte(data);
546 short value = Short.parseShort(data);
547 if (value < UCHAR_MIN_VALUE || value > UCHAR_MAX_VALUE) {
548 throw new ArithmeticException(
"String " + data +
" can not be converted to UCHAR type!");
550 _buffer.put((byte)value);
555 final short value = Short.parseShort(data);
556 _buffer.putShort(value);
561 final int value = Integer.parseInt(data);
562 if (value < USHORT_MIN_VALUE || value > USHORT_MAX_VALUE) {
563 throw new ArithmeticException(
"String " + data +
" can not be converted to USHORT type!");
565 _buffer.putShort((
short)value);
570 final int value = Integer.parseInt(data);
571 _buffer.putInt(value);
576 final long value = Long.parseLong(data);
577 if (value < ULONG_MIN_VALUE || value > ULONG_MAX_VALUE) {
578 throw new ArithmeticException(
"String " + data +
" can not be converted to ULONG type!");
580 _buffer.putInt((
int)value);
585 final long value = Long.parseLong(data);
586 _buffer.putLong(value);
591 final BigInteger value =
new BigInteger(data);
592 if ( (value.compareTo(ULLONG_MIN_VALUE) < 0) || (value.compareTo(ULLONG_MAX_VALUE) > 0)) {
593 throw new ArithmeticException(
"String " + data +
" can not be converted to ULLONG type!");
595 _buffer.putLong(value.longValue());
600 final float value = Float.parseFloat(data);
601 _buffer.putFloat(value);
606 final double value = Double.parseDouble(data);
607 _buffer.putDouble(value);
612 final byte value = Boolean.parseBoolean(data) ? (byte)1 : (byte)0;
618 if (data.length() >= itemInfo.dataSize / 1) {
619 throw new RuntimeException(
"String too long for container element. Data: " + data);
623 byte[] value = data.getBytes(
"ISO-8859-1");
625 }
catch (UnsupportedEncodingException e) {
626 throw new RuntimeException(
"String cannot be converted to ansi string. Data: " + data);
630 case TYPE_CHAR_ARRAY:
632 String[] values = data.split(
" ");
633 if (values.length > itemInfo.dataSize / 1) {
634 throw new RuntimeException(
"Array is too long for container element in container " + _name);
637 for(
int i = 0; i < values.length; i++) {
638 final byte value = Byte.parseByte(values[i]);
643 case TYPE_UCHAR_ARRAY:
645 String[] values = data.split(
" ");
646 if (values.length > itemInfo.dataSize / 1) {
647 throw new RuntimeException(
"Array is too long for container element in container " + _name);
650 for(
int i = 0; i < values.length; i++) {
651 final short value = Short.parseShort(values[i]);
652 if (value < UCHAR_MIN_VALUE || value > UCHAR_MAX_VALUE) {
653 throw new ArithmeticException(
"String '" + data +
"' @ index [" + i +
"] can not be converted to UCHAR_ARRAY type!");
655 _buffer.put((byte)value);
659 case TYPE_SHORT_ARRAY:
661 String[] values = data.split(
" ");
662 if (values.length > itemInfo.dataSize / 2) {
663 throw new RuntimeException(
"Array is too long for container element in container " + _name);
666 for(
int i = 0; i < values.length; i++) {
667 final short value = Short.parseShort(values[i]);
668 _buffer.putShort(value);
672 case TYPE_USHORT_ARRAY:
674 String[] values = data.split(
" ");
675 if (values.length > itemInfo.dataSize / 2) {
676 throw new RuntimeException(
"Array is too long for container element in container " + _name);
679 for(
int i = 0; i < values.length; i++) {
680 final int value = Integer.parseInt(values[i]);
681 if (value < USHORT_MIN_VALUE || value > USHORT_MAX_VALUE) {
682 throw new ArithmeticException(
"String '" + data +
"' @ index [" + i +
"] can not be converted to USHORT_ARRAY type!");
684 _buffer.putShort((
short)value);
688 case TYPE_LONG_ARRAY:
690 String[] values = data.split(
" ");
691 if (values.length > itemInfo.dataSize / 4) {
692 throw new RuntimeException(
"Array is too long for container element in container " + _name);
695 for(
int i = 0; i < values.length; i++) {
696 final int value = Integer.parseInt(values[i]);
697 _buffer.putInt(value);
701 case TYPE_ULONG_ARRAY:
703 String[] values = data.split(
" ");
704 if (values.length > itemInfo.dataSize / 4) {
705 throw new RuntimeException(
"Array is too long for container element in container " + _name);
708 for(
int i = 0; i < values.length; i++) {
709 final long value = Long.parseLong(values[i]);
710 if (value < ULONG_MIN_VALUE || value > ULONG_MAX_VALUE) {
711 throw new ArithmeticException(
"String '" + data +
"' @ index [" + i +
"] can not be converted to ULONG_ARRAY type!");
713 _buffer.putInt((
int)value);
717 case TYPE_LLONG_ARRAY:
719 String[] values = data.split(
" ");
720 if (values.length > itemInfo.dataSize / 8) {
721 throw new RuntimeException(
"Array is too long for container element in container " + _name);
724 for(
int i = 0; i < values.length; i++) {
725 final long value = Long.parseLong(values[i]);
726 _buffer.putLong(value);
730 case TYPE_ULLONG_ARRAY:
732 String[] values = data.split(
" ");
733 if (values.length > itemInfo.dataSize / 8) {
734 throw new RuntimeException(
"Array is too long for container element in container " + _name);
737 for(
int i = 0; i < values.length; i++) {
738 final BigInteger value =
new BigInteger(values[i]);
739 if ( (value.compareTo(ULLONG_MIN_VALUE) < 0) || (value.compareTo(ULLONG_MAX_VALUE) > 0)) {
740 throw new ArithmeticException(
"String '" + data +
"' @ index [" + i +
"] can not be converted to ULLONG_ARRAY type!");
742 _buffer.putLong(value.longValue());
746 case TYPE_FLOAT_ARRAY:
748 String[] values = data.split(
" ");
749 if (values.length > itemInfo.dataSize / 4) {
750 throw new RuntimeException(
"Array is too long for container element in container " + _name);
753 for(
int i = 0; i < values.length; i++) {
754 final float value = Float.parseFloat(values[i]);
755 _buffer.putFloat(value);
759 case TYPE_DOUBLE_ARRAY:
761 String[] values = data.split(
" ");
762 if (values.length > itemInfo.dataSize / 8) {
763 throw new RuntimeException(
"Array is too long for container element in container " + _name);
766 for(
int i = 0; i < values.length; i++) {
767 final double value = Double.parseDouble(values[i]);
768 _buffer.putDouble(value);
772 case TYPE_BOOL8_ARRAY:
774 String[] values = data.split(
" ");
775 if (values.length > itemInfo.dataSize / 1) {
776 throw new RuntimeException(
"Array is too long for container element in container " + _name);
779 for(
int i = 0; i < values.length; i++) {
780 final byte value = Boolean.parseBoolean(values[i]) ? (byte)1 : (byte)0;
786 case TYPE_CHAR_UTF16:
787 case TYPE_CHAR_UTF8_ARRAY:
788 case TYPE_CHAR_UTF16_ARRAY:
791 throw new RuntimeException(
"Unsupported type in container. Type: " + itemInfo.type.toString());
796 if (_buffer == null) {
797 throw new RuntimeException(
"Container not initialized! You need to successfully call create()!");
800 _buffer.position(itemInfo.offset);
801 switch(itemInfo.type) {
804 final byte value = (byte) _buffer.get();
809 final short value = (short) (((
short) _buffer.get()) & UCHAR_MAX_VALUE);
814 final short value = (short) _buffer.getShort();
819 final int value = ((int) _buffer.getShort()) & USHORT_MAX_VALUE;
824 final int value = (int) _buffer.getInt();
829 final long value = ((long) _buffer.getInt()) & ULONG_MAX_VALUE;
834 final long value = (long) _buffer.getLong();
839 final long value = (long) _buffer.getLong();
840 final BigInteger bigValue =
new BigInteger(Long.toUnsignedString(value));
845 final float value = (float) _buffer.getFloat();
850 final double value = (double) _buffer.getDouble();
853 case TYPE_CHAR_ARRAY:
855 final int len = itemInfo.dataSize / 1;
856 byte[] values =
new byte[len];
857 for (
int i = 0; i < len; i++) {
858 values[i] = (byte) _buffer.get();
862 case TYPE_UCHAR_ARRAY:
864 final int len = itemInfo.dataSize / 1;
865 short[] values =
new short[len];
866 for (
int i = 0; i < len; i++) {
867 _buffer.position(itemInfo.offset+(i*1));
868 values[i] = (short) (((
short) _buffer.get()) & UCHAR_MAX_VALUE);
872 case TYPE_SHORT_ARRAY:
874 final int len = itemInfo.dataSize / 2;
875 short[] values =
new short[len];
876 for (
int i = 0; i < len; i++) {
877 values[i] = (short) _buffer.getShort();
881 case TYPE_USHORT_ARRAY:
883 final int len = itemInfo.dataSize / 2;
884 int[] values =
new int[len];
885 for (
int i = 0; i < len; i++) {
886 _buffer.position(itemInfo.offset+(i*2));
887 values[i] = ((int) _buffer.getShort()) & USHORT_MAX_VALUE;
891 case TYPE_LONG_ARRAY:
893 final int len = itemInfo.dataSize / 4;
894 int[] values =
new int[len];
895 for (
int i = 0; i < len; i++) {
896 values[i] = (int) _buffer.getInt();
900 case TYPE_ULONG_ARRAY:
902 final int len = itemInfo.dataSize / 4;
903 long[] values =
new long[len];
904 for (
int i = 0; i < len; i++) {
905 _buffer.position(itemInfo.offset+(i*4));
906 values[i] = ((long) _buffer.getInt()) & ULONG_MAX_VALUE;
910 case TYPE_LLONG_ARRAY:
912 final int len = itemInfo.dataSize / 8;
913 long[] values =
new long[len];
914 for (
int i = 0; i < len; i++) {
915 values[i] = (long) _buffer.getLong();
919 case TYPE_ULLONG_ARRAY:
921 final int len = itemInfo.dataSize / 8;
922 BigInteger[] bigValues =
new BigInteger[len];
923 for (
int i = 0; i < len; i++) {
924 _buffer.position(itemInfo.offset+(i*8));
925 long value = (long) _buffer.getLong();
926 bigValues[i] =
new BigInteger(Long.toUnsignedString(value));
930 case TYPE_FLOAT_ARRAY:
932 final int len = itemInfo.dataSize / 4;
933 float[] values =
new float[len];
934 for (
int i = 0; i < len; i++) {
935 values[i] = (float) _buffer.getFloat();
939 case TYPE_DOUBLE_ARRAY:
941 final int len = itemInfo.dataSize / 8;
942 double[] values =
new double[len];
943 for (
int i = 0; i < len; i++) {
944 values[i] = (double) _buffer.getDouble();
950 final boolean value = (_buffer.get() != 0) ? Boolean.TRUE : Boolean.FALSE;
953 case TYPE_BOOL8_ARRAY:
955 final int len = itemInfo.dataSize / 1;
956 boolean[] values =
new boolean[len];
957 for (
int i = 0; i < len; i++) {
958 values[i] = (_buffer.get() != 0) ? Boolean.TRUE : Boolean.FALSE;
964 byte[] value =
new byte[itemInfo.dataSize];
969 for (len = 0; len < value.length && value[len] != 0; len++) { }
970 return new String(value, 0, len,
"ISO-8859-1");
971 }
catch (UnsupportedEncodingException e) {
972 throw new RuntimeException(
"String in container is not a valid encoded ansi string. Type: " + itemInfo.type.toString());
975 case TYPE_CHAR_UTF16:
977 case TYPE_CHAR_UTF8_ARRAY:
978 case TYPE_CHAR_UTF16_ARRAY:
981 throw new RuntimeException(
"Unsupported type in container. Type: " + itemInfo.type.toString());
986 if (_buffer == null) {
987 throw new RuntimeException(
"Container not initialized! You need to successfully call create()!");
990 _buffer.position(itemInfo.offset);
991 switch(itemInfo.type) {
994 final byte value = (byte) _buffer.get();
995 return String.valueOf(value);
999 final short value = (short) (((
short) _buffer.get()) & UCHAR_MAX_VALUE);
1000 return String.valueOf(value);
1004 final short value = (short) _buffer.getShort();
1005 return String.valueOf(value);
1009 final int value = ((int) _buffer.getShort()) & USHORT_MAX_VALUE;
1010 return String.valueOf(value);
1014 final int value = (int) _buffer.getInt();
1015 return String.valueOf(value);
1019 final long value = ((long) _buffer.getInt()) & ULONG_MAX_VALUE;
1020 return String.valueOf(value);
1024 final long value = (long) _buffer.getLong();
1025 return String.valueOf(value);
1029 final long value = (long) _buffer.getLong();
1030 return Long.toUnsignedString(value);
1034 final float value = (float) _buffer.getFloat();
1035 return String.valueOf(value);
1039 final double value = (double) _buffer.getDouble();
1040 return String.valueOf(value);
1042 case TYPE_CHAR_ARRAY:
1045 for (
int i = 0; i < itemInfo.dataSize / 1; i++) {
1046 final byte value = (byte) _buffer.get();
1047 values += String.valueOf(value) +
" ";
1049 return values.trim();
1051 case TYPE_UCHAR_ARRAY:
1054 for (
int i = 0; i < itemInfo.dataSize / 1; i++) {
1055 _buffer.position(itemInfo.offset+(i*1));
1056 final short value = (short) (((
short) _buffer.get()) & UCHAR_MAX_VALUE);
1057 values += String.valueOf(value) +
" ";
1059 return values.trim();
1061 case TYPE_SHORT_ARRAY:
1064 for (
int i = 0; i < itemInfo.dataSize / 2; i++) {
1065 final short value = (short) _buffer.getShort();
1066 values += String.valueOf(value) +
" ";
1068 return values.trim();
1070 case TYPE_USHORT_ARRAY:
1073 for (
int i = 0; i < itemInfo.dataSize / 2; i++) {
1074 _buffer.position(itemInfo.offset+(i*2));
1075 final int value = ((int) _buffer.getInt()) & USHORT_MAX_VALUE;
1076 values += String.valueOf(value) +
" ";
1078 return values.trim();
1080 case TYPE_LONG_ARRAY:
1083 for (
int i = 0; i < itemInfo.dataSize / 4; i++) {
1084 final int value = (int) _buffer.getInt();
1085 values += String.valueOf(value) +
" ";
1087 return values.trim();
1089 case TYPE_ULONG_ARRAY:
1092 for (
int i = 0; i < itemInfo.dataSize / 4; i++) {
1093 _buffer.position(itemInfo.offset+(i*4));
1094 final long value = ((long) _buffer.getLong()) & ULONG_MAX_VALUE;
1095 values += String.valueOf(value) +
" ";
1097 return values.trim();
1099 case TYPE_LLONG_ARRAY:
1102 for (
int i = 0; i < itemInfo.dataSize / 8; i++) {
1103 final long value = (long) _buffer.getLong();
1104 values += String.valueOf(value) +
" ";
1106 return values.trim();
1108 case TYPE_ULLONG_ARRAY:
1111 for (
int i = 0; i < itemInfo.dataSize / 8; i++) {
1112 _buffer.position(itemInfo.offset+(i*8));
1113 final long value = (long) _buffer.getLong();
1114 values += Long.toUnsignedString(value) +
" ";
1116 return values.trim();
1118 case TYPE_FLOAT_ARRAY:
1121 for (
int i = 0; i < itemInfo.dataSize / 4; i++) {
1122 final float value = (float) _buffer.getFloat();
1123 values += String.valueOf(value) +
" ";
1125 return values.trim();
1127 case TYPE_DOUBLE_ARRAY:
1130 for (
int i = 0; i < itemInfo.dataSize / 8; i++) {
1131 final double value = (double) _buffer.getDouble();
1132 values += String.valueOf(value) +
" ";
1134 return values.trim();
1138 final byte value = _buffer.get();
1139 return (value != 0) ?
"TRUE" :
"FALSE";
1141 case TYPE_BOOL8_ARRAY:
1144 for (
int i = 0; i < itemInfo.dataSize / 1; i++) {
1145 final byte value = _buffer.get();
1146 values += (value != 0) ?
"TRUE" :
"FALSE" +
" ";
1148 return values.trim();
1150 case TYPE_CHAR_UTF8:
1152 byte[] value =
new byte[itemInfo.dataSize];
1157 for (len = 0; len < value.length && value[len] != 0; len++) { }
1158 return new String(value, 0, len,
"ISO-8859-1");
1159 }
catch (UnsupportedEncodingException e) {
1160 throw new RuntimeException(
"String in container is not a valid encoded ansi string. Type: " + itemInfo.type.toString());
1163 case TYPE_CHAR_UTF16:
1164 case TYPE_CHAR_UTF8_ARRAY:
1165 case TYPE_CHAR_UTF16_ARRAY:
1169 throw new RuntimeException(
"Unsupported type in container. Type: " + itemInfo.type.toString());
1179 public Object getDataByTagAsObject(
final int index) {
1180 return getDataByItemInfoAsObject(_itemInfosArray[index]);
1183 public String getDataByTagAsString(
final String tag) {
1185 if (itemInfo == null) {
1186 throw new RuntimeException(
"Could not find tag " + tag +
" in container structure " + _name);
1188 return getDataByItemInfoAsString(itemInfo);
1191 public Object getDataByTagAsObject(
final String tag) {
1193 if (itemInfo == null) {
1194 throw new RuntimeException(
"Could not find tag " + tag +
" in container structure " + _name);
1196 return getDataByItemInfoAsObject(itemInfo);
1199 public void setDataByTag(
final String tag,
final String data) {
1201 if (itemInfo == null) {
1202 throw new RuntimeException(
"Could not find tag " + tag +
" in container structure " + _name);
1204 setDataByItemInfo(itemInfo, data);
1207 public void setDataByTag(
final String tag,
final Number data) {
1209 if (itemInfo == null) {
1210 throw new RuntimeException(
"Could not find tag " + tag +
" in container structure " + _name);
1212 setDataByItemInfo(itemInfo, data);
1215 public void setDataByTag(
final String tag,
final Object data) {
1217 if (itemInfo == null) {
1218 throw new RuntimeException(
"Could not find tag " + tag +
" in container structure " + _name);
1220 setDataByItemInfo(itemInfo, data);
1223 public final byte[] getDataAsByteStream() {
1228 return _itemInfosMap.get(tag);
1231 public boolean isDirty() {
native byte[] update(ContainerHandle containerHandle, byte[] buffer)
native int create(String tagList, ContainerAccess accessFlag, ContainerHandle containerHandle)
native ContainerItemInformation[] getItemInformation(ContainerHandle containerHandle)
This class defines the handle to a container.
native void destroy(ContainerHandle containerHandle)