Open Core Interface - MLPI
MLPI-MotionLogicProgrammingInterface(mlpi4Java)  1.26.2
ContainerStructure.java
Go to the documentation of this file.
1 // -----------------------------------------------------------------------
2 // MLPI - <System.java>
3 // -----------------------------------------------------------------------
4 // Copyright (c) 2012 Bosch Rexroth. All rights reserved.
5 // Redistribution and use in source and binary forms of this MLPI software
6 // (SW) provided to you, with or without modification, are permitted
7 // without prior approval provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code of SW must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form of SW must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // 3. User recognizes and acknowledges that it acquires no right,
17 // title or interest in or to any of the names or trademarks used in
18 // connection with the SW ("names") by virtue of this License and waives
19 // any right to or interest in the names. User recognizes and acknowledges
20 // that names of companies or names or products of companies displayed
21 // in the documentation of SW to indicate the interoperability of products
22 // with the SW are the names of their respective owners. The use of such
23 // names in the documentation of SW does not imply any sponsorship,
24 // approval, or endorsement by such companies of this product.
25 //
26 // 4. Modified code versions, i.e. any addition to or deletion from
27 // the substance or structure of the original code of the SW running
28 // the MLPI must be plainly marked as such and must not be misrepresented
29 // as being original SW.
30 //
31 // 5. The SW may only be used in connection with a Bosch Rexroth product.
32 //
33 // THIS INFORMATION IS PROVIDED BY BOSCH REXROTH CORPORATION "AS IS"
34 // AND WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING
35 // (BUT NOTLIMITED TO) ANY IMPLIED WARRANTIES OF MERCHANTABILITY,
36 // FITNESS FOR ANY PARTICULAR PURPOSE, OR NON-INFRINGEMENT. WHILE THE
37 // INFORMATION PROVIDED IS BELIEVED TO BE ACCURATE, IT MAY INCLUDE
38 // ERRORS OR INACCURACIES.
39 // SUBJECT TO COMPULSORY STATUTORY PROVISIONS OF THE GERMAN LAW AS
40 // THE APPLICABLE LAW FOR THIS LICENSE BOSCH REXROTH CORPORATION WILL
41 // NOT BE LIABLE FOR ANY DAMAGES OF ANY KIND ARISING FROM THE USE OF
42 // THE SOFTWARE DISTRIBUTED HEREUNDER, INCLUDING BUT NOT LIMITED TO
43 // DIRECT, INDIRECT, INCIDENTAL, PUNITIVE, AND CONSEQUENTIAL DAMAGES.
44 // -----------------------------------------------------------------------
45 //
55 //
56 // -----------------------------------------------------------------------
57 package com.boschrexroth.mlpi;
58 
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;
71 
75 
76 
81 public class ContainerStructure {
82  /*
83  * Constants
84  */
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;
89 
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;
94  /*
95  * Enums
96  */
97 
98  /*
99  * Structs
100  */
101 
102  /*
103  * Fields
104  */
105  private final Container _container;
106  private final String _name;
107  private final ContainerAccess _accessFlag;
108  private List<String> _tagList = new LinkedList<String>();
109  private ContainerItemInformation[] _itemInfosArray;
110  private HashMap<String, ContainerItemInformation> _itemInfosMap = new HashMap<String, Container.ContainerItemInformation>();
111  private boolean _isDirty = true;
112  private ContainerHandle _containerHandle = new ContainerHandle();
113  private byte[] _data = new byte[1];
114  private ByteBuffer _buffer;
115 
116  /*
117  * Methods
118  */
119  ContainerStructure(Container container, String name, ContainerAccess accessFlag) {
120  _name = name;
121  _container = container;
122  _accessFlag = accessFlag;
123  }
124 
125  public void finalize() {
126  destroy();
127  }
128 
129  public String getName() {
130  return _name;
131  }
132 
133  public void clear() {
134  _tagList.clear();
135  _itemInfosMap.clear();
136  _isDirty = true;
137  }
138 
139  public void add(String tags) {
140  StringTokenizer tokenizer = new StringTokenizer(tags, ";");
141 
142  while (tokenizer.hasMoreTokens()) {
143  _tagList.add(tokenizer.nextToken());
144  _isDirty = true;
145  }
146  }
147 
148  public void add(String[] tags) {
149  for (String tag : tags) {
150  add(tag);
151  }
152  }
153 
154  public void remove(String tags) {
155  StringTokenizer tokenizer = new StringTokenizer(tags, ";");
156 
157  while (tokenizer.hasMoreTokens()) {
158  _tagList.remove(tokenizer.nextToken());
159  _isDirty = true;
160  }
161  }
162 
163  public void remove(String[] tags) {
164  for (String tag : tags) {
165  remove(tag);
166  }
167  }
168 
169  public String[] get() {
170  return _tagList.toArray(new String[_tagList.size()]);
171  }
172 
173  public void create(){
174  if (_isDirty) {
175  // Remove old container
176  if (_containerHandle.containerID != -1) {
177  _container.destroy(_containerHandle);
178  _containerHandle.connectionID = -1;
179  _containerHandle.containerID = -1;
180  }
181 
182  // Create new container
183  StringBuilder tagList = new StringBuilder();
184  for (String tag : _tagList) {
185  tagList.append(tag);
186  tagList.append(";");
187  }
188 
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);
193 
194 
195  // ReadContainer information. We need these information later to interpret the
196  // content of the data buffer. The information tells us the offset, type and data size.
197  // For faster access, we also store the information in a hash map.
198  int i = 0;
199  _itemInfosMap.clear();
200  _itemInfosArray = _container.getItemInformation(_containerHandle);
201  for (ContainerItemInformation item : _itemInfosArray) {
202  _itemInfosMap.put(_tagList.get(i), item);
203  i++;
204  }
205 
206  _isDirty = false;
207  }
208  }
209 
217  public void destroy() {
218  if (_containerHandle.containerID != -1) {
219  _container.destroy(_containerHandle);
220  _containerHandle.connectionID = -1;
221  _containerHandle.containerID = -1;
222  }
223  _buffer = null;
224  }
225 
233  public void update() {
234  if (_isDirty) {
235  create();
236  }
237  _container.update(_containerHandle, _data);
238  }
239 
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[];
246  }
247 
248  public void setDataByItemInfo(final ContainerItemInformation itemInfo, final Object data) {
249  if (_buffer == null) {
250  throw new RuntimeException("Container not initialized! You need to successfully call create()!");
251  }
252 
253  if (!isArray(data)) {
254  setDataByItemInfo(itemInfo, (Number) data);
255  return;
256  }
257 
258  final int len = Array.getLength(data);
259 
260  _buffer.position(itemInfo.offset);
261  switch(itemInfo.type) {
262  case TYPE_CHAR_ARRAY:
263  {
264  if (len > itemInfo.dataSize / 1) {
265  throw new RuntimeException("Array is too long for container element in container " + _name);
266  }
267 
268  for (int i = 0; i < len; i++) {
269  final byte value = Array.getByte(data, i);
270  _buffer.put(value);
271  }
272  break;
273  }
274  case TYPE_UCHAR_ARRAY:
275  {
276  if (len > itemInfo.dataSize / 1) {
277  throw new RuntimeException("Array is too long for container element in container " + _name);
278  }
279 
280  for (int i = 0; i < len; i++) {
281  final byte value = (byte) (Array.getShort(data, i) & UCHAR_MAX_VALUE);
282  _buffer.put(value);
283  }
284  break;
285  }
286  case TYPE_SHORT_ARRAY:
287  {
288  if (len > itemInfo.dataSize / 2) {
289  throw new RuntimeException("Array is too long for container element in container " + _name);
290  }
291 
292  for (int i = 0; i < len; i++) {
293  final short value = Array.getShort(data, i);
294  _buffer.putShort(value);
295  }
296  break;
297  }
298  case TYPE_USHORT_ARRAY:
299  {
300  if (len > itemInfo.dataSize / 2) {
301  throw new RuntimeException("Array is too long for container element in container " + _name);
302  }
303 
304  for (int i = 0; i < len; i++) {
305  final short value = (short) (Array.getInt(data, i) & USHORT_MAX_VALUE);
306  _buffer.putShort(value);
307  }
308  break;
309  }
310  case TYPE_LONG_ARRAY:
311  {
312  if (len > itemInfo.dataSize / 4) {
313  throw new RuntimeException("Array is too long for container element in container " + _name);
314  }
315 
316  for (int i = 0; i < len; i++) {
317  final int value = Array.getInt(data, i);
318  _buffer.putInt(value);
319  }
320  break;
321  }
322  case TYPE_ULONG_ARRAY:
323  {
324  if (len > itemInfo.dataSize / 4) {
325  throw new RuntimeException("Array is too long for container element in container " + _name);
326  }
327 
328  for (int i = 0; i < len; i++) {
329  final int value = (int) (Array.getLong(data, i) & ULONG_MAX_VALUE);
330  _buffer.putInt(value);
331  }
332  break;
333  }
334  case TYPE_LLONG_ARRAY:
335  {
336  if (len > itemInfo.dataSize / 8) {
337  throw new RuntimeException("Array is too long for container element in container " + _name);
338  }
339 
340  for (int i = 0; i < len; i++) {
341  final long value = Array.getLong(data, i);
342  _buffer.putLong(value);
343  }
344  break;
345  }
346  case TYPE_ULLONG_ARRAY:
347  {
348  if (len > itemInfo.dataSize / 8) {
349  throw new RuntimeException("Array is too long for container element in container " + _name);
350  }
351 
352  BigInteger[] bigIntegerArray = (BigInteger[]) data;
353  for (int i = 0; i < len; i++) {
354  _buffer.putLong(bigIntegerArray[i].longValue());
355  }
356  break;
357  }
358  case TYPE_BOOL8_ARRAY:
359  {
360  if (len > itemInfo.dataSize / 1) {
361  throw new RuntimeException("Array is too long for container element in container " + _name);
362  }
363 
364  for (int i = 0; i < len; i++) {
365  final byte value = (Array.getBoolean(data, i)) ? (byte)1 : (byte)0;
366  _buffer.put(value);
367  }
368  break;
369  }
370  case TYPE_FLOAT_ARRAY:
371  {
372  if (len > itemInfo.dataSize / 4) {
373  throw new RuntimeException("Array is too long for container element in container " + _name);
374  }
375 
376  for (int i = 0; i < len; i++) {
377  final float value = Array.getFloat(data, i);
378  _buffer.putFloat(value);
379  }
380  break;
381  }
382  case TYPE_DOUBLE_ARRAY:
383  {
384  if (len > itemInfo.dataSize / 8) {
385  throw new RuntimeException("Array is too long for container element in container " + _name);
386  }
387 
388  for (int i = 0; i < len; i++) {
389  final double value = Array.getDouble(data, i);
390  _buffer.putDouble(value);
391  }
392  break;
393  }
394  case TYPE_CHAR_UTF16:
395  case TYPE_INVALID:
396  case TYPE_CHAR_UTF8_ARRAY:
397  case TYPE_CHAR_UTF16_ARRAY:
398  default:
399  // not supported
400  throw new RuntimeException("Unsupported type in container. Expected: " + itemInfo.type.toString());
401  }
402  }
403 
404  public void setDataByItemInfo(final ContainerItemInformation itemInfo, final Number data) {
405  if (_buffer == null) {
406  throw new RuntimeException("Container not initialized! You need to successfully call create()!");
407  }
408 
409  _buffer.position(itemInfo.offset);
410  switch(itemInfo.type) {
411  case TYPE_CHAR:
412  case TYPE_CHAR_ARRAY:
413  {
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!");
416  }
417  _buffer.put(data.byteValue());
418  break;
419  }
420  case TYPE_UCHAR:
421  case TYPE_UCHAR_ARRAY:
422  {
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!");
425  }
426  _buffer.put((byte)data.byteValue());
427  break;
428  }
429  case TYPE_SHORT:
430  case TYPE_SHORT_ARRAY:
431  {
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!");
434  }
435  _buffer.putShort(data.shortValue());
436  break;
437  }
438  case TYPE_USHORT:
439  case TYPE_USHORT_ARRAY:
440  {
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!");
443  }
444  _buffer.putShort(data.shortValue());
445  break;
446  }
447  case TYPE_LONG:
448  case TYPE_LONG_ARRAY:
449  {
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!");
452  }
453  _buffer.putInt(data.intValue());
454  break;
455  }
456  case TYPE_ULONG:
457  case TYPE_ULONG_ARRAY:
458  {
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!");
461  }
462  _buffer.putInt(data.intValue());
463  break;
464  }
465  case TYPE_LLONG:
466  case TYPE_LLONG_ARRAY:
467  {
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!");
470  }
471  _buffer.putLong(data.longValue());
472  break;
473  }
474  case TYPE_ULLONG:
475  case TYPE_ULLONG_ARRAY:
476  {
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!");
480  }
481  _buffer.putLong(value.longValue());
482  break;
483  }
484  case TYPE_FLOAT:
485  case TYPE_FLOAT_ARRAY:
486  {
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!");
489  }
490  _buffer.putFloat(data.floatValue());
491  break;
492  }
493  case TYPE_DOUBLE:
494  case TYPE_DOUBLE_ARRAY:
495  {
496  _buffer.putDouble(data.doubleValue());
497  break;
498  }
499  case TYPE_BOOL8:
500  case TYPE_BOOL8_ARRAY:
501  {
502  final byte value = (data.intValue() != 0) ? (byte)1 : (byte)0;
503  _buffer.put(value);
504  break;
505  }
506  case TYPE_CHAR_UTF8:
507  {
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);
511  }
512 
513  try {
514  byte[] value2 = value.getBytes("ISO-8859-1");
515  _buffer.put(value2);
516  } catch (UnsupportedEncodingException e) {
517  throw new RuntimeException("String cannot be converted to ansi string. Data: " + data);
518  }
519  break;
520  }
521  case TYPE_INVALID:
522  case TYPE_CHAR_UTF16:
523  case TYPE_CHAR_UTF8_ARRAY:
524  case TYPE_CHAR_UTF16_ARRAY:
525  default:
526  // not supported
527  throw new RuntimeException("Unsupported type in container. Type: " + itemInfo.type.toString());
528  }
529  }
530 
531  public void setDataByItemInfo(final ContainerItemInformation itemInfo, final String data) {
532  if (_buffer == null) {
533  throw new RuntimeException("Container not initialized! You need to successfully call create()!");
534  }
535 
536  _buffer.position(itemInfo.offset);
537  switch(itemInfo.type) {
538  case TYPE_CHAR:
539  {
540  final byte value = Byte.parseByte(data);
541  _buffer.put(value);
542  break;
543  }
544  case TYPE_UCHAR:
545  {
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!");
549  }
550  _buffer.put((byte)value);
551  break;
552  }
553  case TYPE_SHORT:
554  {
555  final short value = Short.parseShort(data);
556  _buffer.putShort(value);
557  break;
558  }
559  case TYPE_USHORT:
560  {
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!");
564  }
565  _buffer.putShort((short)value);
566  break;
567  }
568  case TYPE_LONG:
569  {
570  final int value = Integer.parseInt(data);
571  _buffer.putInt(value);
572  break;
573  }
574  case TYPE_ULONG:
575  {
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!");
579  }
580  _buffer.putInt((int)value);
581  break;
582  }
583  case TYPE_LLONG:
584  {
585  final long value = Long.parseLong(data);
586  _buffer.putLong(value);
587  break;
588  }
589  case TYPE_ULLONG:
590  {
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!");
594  }
595  _buffer.putLong(value.longValue());
596  break;
597  }
598  case TYPE_FLOAT:
599  {
600  final float value = Float.parseFloat(data);
601  _buffer.putFloat(value);
602  break;
603  }
604  case TYPE_DOUBLE:
605  {
606  final double value = Double.parseDouble(data);
607  _buffer.putDouble(value);
608  break;
609  }
610  case TYPE_BOOL8:
611  {
612  final byte value = Boolean.parseBoolean(data) ? (byte)1 : (byte)0;
613  _buffer.put(value);
614  break;
615  }
616  case TYPE_CHAR_UTF8:
617  {
618  if (data.length() >= itemInfo.dataSize / 1) {
619  throw new RuntimeException("String too long for container element. Data: " + data);
620  }
621 
622  try {
623  byte[] value = data.getBytes("ISO-8859-1");
624  _buffer.put(value);
625  } catch (UnsupportedEncodingException e) {
626  throw new RuntimeException("String cannot be converted to ansi string. Data: " + data);
627  }
628  break;
629  }
630  case TYPE_CHAR_ARRAY:
631  {
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);
635  }
636 
637  for(int i = 0; i < values.length; i++) {
638  final byte value = Byte.parseByte(values[i]);
639  _buffer.put(value);
640  }
641  break;
642  }
643  case TYPE_UCHAR_ARRAY:
644  {
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);
648  }
649 
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!");
654  }
655  _buffer.put((byte)value);
656  }
657  break;
658  }
659  case TYPE_SHORT_ARRAY:
660  {
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);
664  }
665 
666  for(int i = 0; i < values.length; i++) {
667  final short value = Short.parseShort(values[i]);
668  _buffer.putShort(value);
669  }
670  break;
671  }
672  case TYPE_USHORT_ARRAY:
673  {
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);
677  }
678 
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!");
683  }
684  _buffer.putShort((short)value);
685  }
686  break;
687  }
688  case TYPE_LONG_ARRAY:
689  {
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);
693  }
694 
695  for(int i = 0; i < values.length; i++) {
696  final int value = Integer.parseInt(values[i]);
697  _buffer.putInt(value);
698  }
699  break;
700  }
701  case TYPE_ULONG_ARRAY:
702  {
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);
706  }
707 
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!");
712  }
713  _buffer.putInt((int)value);
714  }
715  break;
716  }
717  case TYPE_LLONG_ARRAY:
718  {
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);
722  }
723 
724  for(int i = 0; i < values.length; i++) {
725  final long value = Long.parseLong(values[i]);
726  _buffer.putLong(value);
727  }
728  break;
729  }
730  case TYPE_ULLONG_ARRAY:
731  {
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);
735  }
736 
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!");
741  }
742  _buffer.putLong(value.longValue());
743  }
744  break;
745  }
746  case TYPE_FLOAT_ARRAY:
747  {
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);
751  }
752 
753  for(int i = 0; i < values.length; i++) {
754  final float value = Float.parseFloat(values[i]);
755  _buffer.putFloat(value);
756  }
757  break;
758  }
759  case TYPE_DOUBLE_ARRAY:
760  {
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);
764  }
765 
766  for(int i = 0; i < values.length; i++) {
767  final double value = Double.parseDouble(values[i]);
768  _buffer.putDouble(value);
769  }
770  break;
771  }
772  case TYPE_BOOL8_ARRAY:
773  {
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);
777  }
778 
779  for(int i = 0; i < values.length; i++) {
780  final byte value = Boolean.parseBoolean(values[i]) ? (byte)1 : (byte)0;
781  _buffer.put(value);
782  }
783  break;
784  }
785  case TYPE_INVALID:
786  case TYPE_CHAR_UTF16:
787  case TYPE_CHAR_UTF8_ARRAY:
788  case TYPE_CHAR_UTF16_ARRAY:
789  default:
790  // not supported
791  throw new RuntimeException("Unsupported type in container. Type: " + itemInfo.type.toString());
792  }
793  }
794 
795  public Object getDataByItemInfoAsObject(final ContainerItemInformation itemInfo) {
796  if (_buffer == null) {
797  throw new RuntimeException("Container not initialized! You need to successfully call create()!");
798  }
799 
800  _buffer.position(itemInfo.offset);
801  switch(itemInfo.type) {
802  case TYPE_CHAR:
803  {
804  final byte value = (byte) _buffer.get();
805  return value;
806  }
807  case TYPE_UCHAR:
808  {
809  final short value = (short) (((short) _buffer.get()) & UCHAR_MAX_VALUE);
810  return value;
811  }
812  case TYPE_SHORT:
813  {
814  final short value = (short) _buffer.getShort();
815  return value;
816  }
817  case TYPE_USHORT:
818  {
819  final int value = ((int) _buffer.getShort()) & USHORT_MAX_VALUE;
820  return value;
821  }
822  case TYPE_LONG:
823  {
824  final int value = (int) _buffer.getInt();
825  return value;
826  }
827  case TYPE_ULONG:
828  {
829  final long value = ((long) _buffer.getInt()) & ULONG_MAX_VALUE;
830  return value;
831  }
832  case TYPE_LLONG:
833  {
834  final long value = (long) _buffer.getLong();
835  return value;
836  }
837  case TYPE_ULLONG:
838  {
839  final long value = (long) _buffer.getLong();
840  final BigInteger bigValue = new BigInteger(Long.toUnsignedString(value));
841  return bigValue;
842  }
843  case TYPE_FLOAT:
844  {
845  final float value = (float) _buffer.getFloat();
846  return value;
847  }
848  case TYPE_DOUBLE:
849  {
850  final double value = (double) _buffer.getDouble();
851  return value;
852  }
853  case TYPE_CHAR_ARRAY:
854  {
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();
859  }
860  return values;
861  }
862  case TYPE_UCHAR_ARRAY:
863  {
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);
869  }
870  return values;
871  }
872  case TYPE_SHORT_ARRAY:
873  {
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();
878  }
879  return values;
880  }
881  case TYPE_USHORT_ARRAY:
882  {
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;
888  }
889  return values;
890  }
891  case TYPE_LONG_ARRAY:
892  {
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();
897  }
898  return values;
899  }
900  case TYPE_ULONG_ARRAY:
901  {
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;
907  }
908  return values;
909  }
910  case TYPE_LLONG_ARRAY:
911  {
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();
916  }
917  return values;
918  }
919  case TYPE_ULLONG_ARRAY:
920  {
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));
927  }
928  return bigValues;
929  }
930  case TYPE_FLOAT_ARRAY:
931  {
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();
936  }
937  return values;
938  }
939  case TYPE_DOUBLE_ARRAY:
940  {
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();
945  }
946  return values;
947  }
948  case TYPE_BOOL8:
949  {
950  final boolean value = (_buffer.get() != 0) ? Boolean.TRUE : Boolean.FALSE;
951  return value;
952  }
953  case TYPE_BOOL8_ARRAY:
954  {
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;
959  }
960  return values;
961  }
962  case TYPE_CHAR_UTF8:
963  {
964  byte[] value = new byte[itemInfo.dataSize];
965  _buffer.get(value);
966 
967  try {
968  int len;
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());
973  }
974  }
975  case TYPE_CHAR_UTF16:
976  case TYPE_INVALID:
977  case TYPE_CHAR_UTF8_ARRAY:
978  case TYPE_CHAR_UTF16_ARRAY:
979  default:
980  // not supported
981  throw new RuntimeException("Unsupported type in container. Type: " + itemInfo.type.toString());
982  }
983  }
984 
985  public String getDataByItemInfoAsString(final ContainerItemInformation itemInfo) {
986  if (_buffer == null) {
987  throw new RuntimeException("Container not initialized! You need to successfully call create()!");
988  }
989 
990  _buffer.position(itemInfo.offset);
991  switch(itemInfo.type) {
992  case TYPE_CHAR:
993  {
994  final byte value = (byte) _buffer.get();
995  return String.valueOf(value);
996  }
997  case TYPE_UCHAR:
998  {
999  final short value = (short) (((short) _buffer.get()) & UCHAR_MAX_VALUE);
1000  return String.valueOf(value);
1001  }
1002  case TYPE_SHORT:
1003  {
1004  final short value = (short) _buffer.getShort();
1005  return String.valueOf(value);
1006  }
1007  case TYPE_USHORT:
1008  {
1009  final int value = ((int) _buffer.getShort()) & USHORT_MAX_VALUE;
1010  return String.valueOf(value);
1011  }
1012  case TYPE_LONG:
1013  {
1014  final int value = (int) _buffer.getInt();
1015  return String.valueOf(value);
1016  }
1017  case TYPE_ULONG:
1018  {
1019  final long value = ((long) _buffer.getInt()) & ULONG_MAX_VALUE;
1020  return String.valueOf(value);
1021  }
1022  case TYPE_LLONG:
1023  {
1024  final long value = (long) _buffer.getLong();
1025  return String.valueOf(value);
1026  }
1027  case TYPE_ULLONG:
1028  {
1029  final long value = (long) _buffer.getLong();
1030  return Long.toUnsignedString(value);
1031  }
1032  case TYPE_FLOAT:
1033  {
1034  final float value = (float) _buffer.getFloat();
1035  return String.valueOf(value);
1036  }
1037  case TYPE_DOUBLE:
1038  {
1039  final double value = (double) _buffer.getDouble();
1040  return String.valueOf(value);
1041  }
1042  case TYPE_CHAR_ARRAY:
1043  {
1044  String values = "";
1045  for (int i = 0; i < itemInfo.dataSize / 1; i++) {
1046  final byte value = (byte) _buffer.get();
1047  values += String.valueOf(value) + " ";
1048  }
1049  return values.trim();
1050  }
1051  case TYPE_UCHAR_ARRAY:
1052  {
1053  String values = "";
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) + " ";
1058  }
1059  return values.trim();
1060  }
1061  case TYPE_SHORT_ARRAY:
1062  {
1063  String values = "";
1064  for (int i = 0; i < itemInfo.dataSize / 2; i++) {
1065  final short value = (short) _buffer.getShort();
1066  values += String.valueOf(value) + " ";
1067  }
1068  return values.trim();
1069  }
1070  case TYPE_USHORT_ARRAY:
1071  {
1072  String values = "";
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) + " ";
1077  }
1078  return values.trim();
1079  }
1080  case TYPE_LONG_ARRAY:
1081  {
1082  String values = "";
1083  for (int i = 0; i < itemInfo.dataSize / 4; i++) {
1084  final int value = (int) _buffer.getInt();
1085  values += String.valueOf(value) + " ";
1086  }
1087  return values.trim();
1088  }
1089  case TYPE_ULONG_ARRAY:
1090  {
1091  String values = "";
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) + " ";
1096  }
1097  return values.trim();
1098  }
1099  case TYPE_LLONG_ARRAY:
1100  {
1101  String values = "";
1102  for (int i = 0; i < itemInfo.dataSize / 8; i++) {
1103  final long value = (long) _buffer.getLong();
1104  values += String.valueOf(value) + " ";
1105  }
1106  return values.trim();
1107  }
1108  case TYPE_ULLONG_ARRAY:
1109  {
1110  String values = "";
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) + " ";
1115  }
1116  return values.trim();
1117  }
1118  case TYPE_FLOAT_ARRAY:
1119  {
1120  String values = "";
1121  for (int i = 0; i < itemInfo.dataSize / 4; i++) {
1122  final float value = (float) _buffer.getFloat();
1123  values += String.valueOf(value) + " ";
1124  }
1125  return values.trim();
1126  }
1127  case TYPE_DOUBLE_ARRAY:
1128  {
1129  String values = "";
1130  for (int i = 0; i < itemInfo.dataSize / 8; i++) {
1131  final double value = (double) _buffer.getDouble();
1132  values += String.valueOf(value) + " ";
1133  }
1134  return values.trim();
1135  }
1136  case TYPE_BOOL8:
1137  {
1138  final byte value = _buffer.get();
1139  return (value != 0) ? "TRUE" : "FALSE";
1140  }
1141  case TYPE_BOOL8_ARRAY:
1142  {
1143  String values = "";
1144  for (int i = 0; i < itemInfo.dataSize / 1; i++) {
1145  final byte value = _buffer.get();
1146  values += (value != 0) ? "TRUE" : "FALSE" + " ";
1147  }
1148  return values.trim();
1149  }
1150  case TYPE_CHAR_UTF8:
1151  {
1152  byte[] value = new byte[itemInfo.dataSize];
1153  _buffer.get(value);
1154 
1155  try {
1156  int len;
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());
1161  }
1162  }
1163  case TYPE_CHAR_UTF16:
1164  case TYPE_CHAR_UTF8_ARRAY:
1165  case TYPE_CHAR_UTF16_ARRAY:
1166  case TYPE_INVALID:
1167  default:
1168  // not supported
1169  throw new RuntimeException("Unsupported type in container. Type: " + itemInfo.type.toString());
1170  }
1171  }
1172 
1173  /*
1174  public String getDataByIndexAsString(final int index) {
1175  return getDataByItemInfoAsString(_itemInfosArray[index]);
1176  }
1177  */
1178 
1179  public Object getDataByTagAsObject(final int index) {
1180  return getDataByItemInfoAsObject(_itemInfosArray[index]);
1181  }
1182 
1183  public String getDataByTagAsString(final String tag) {
1184  final ContainerItemInformation itemInfo = _itemInfosMap.get(tag);
1185  if (itemInfo == null) {
1186  throw new RuntimeException("Could not find tag " + tag + " in container structure " + _name);
1187  }
1188  return getDataByItemInfoAsString(itemInfo);
1189  }
1190 
1191  public Object getDataByTagAsObject(final String tag) {
1192  final ContainerItemInformation itemInfo = _itemInfosMap.get(tag);
1193  if (itemInfo == null) {
1194  throw new RuntimeException("Could not find tag " + tag + " in container structure " + _name);
1195  }
1196  return getDataByItemInfoAsObject(itemInfo);
1197  }
1198 
1199  public void setDataByTag(final String tag, final String data) {
1200  final ContainerItemInformation itemInfo = _itemInfosMap.get(tag);
1201  if (itemInfo == null) {
1202  throw new RuntimeException("Could not find tag " + tag + " in container structure " + _name);
1203  }
1204  setDataByItemInfo(itemInfo, data);
1205  }
1206 
1207  public void setDataByTag(final String tag, final Number data) {
1208  final ContainerItemInformation itemInfo = _itemInfosMap.get(tag);
1209  if (itemInfo == null) {
1210  throw new RuntimeException("Could not find tag " + tag + " in container structure " + _name);
1211  }
1212  setDataByItemInfo(itemInfo, data);
1213  }
1214 
1215  public void setDataByTag(final String tag, final Object data) {
1216  final ContainerItemInformation itemInfo = _itemInfosMap.get(tag);
1217  if (itemInfo == null) {
1218  throw new RuntimeException("Could not find tag " + tag + " in container structure " + _name);
1219  }
1220  setDataByItemInfo(itemInfo, data);
1221  }
1222 
1223  public final byte[] getDataAsByteStream() {
1224  return _data;
1225  }
1226 
1227  public final ContainerItemInformation getItemInfoByTag(final String tag) {
1228  return _itemInfosMap.get(tag);
1229  }
1230 
1231  public boolean isDirty() {
1232  return _isDirty;
1233  }
1234 }
1235 
native byte[] update(ContainerHandle containerHandle, byte[] buffer)
native int create(String tagList, ContainerAccess accessFlag, ContainerHandle containerHandle)
This class defines the information of a container item.
Definition: Container.java:146
native ContainerItemInformation[] getItemInformation(ContainerHandle containerHandle)
This class defines the handle to a container.
Definition: Container.java:167
native void destroy(ContainerHandle containerHandle)