ffead.server.doc
AMEFDecoder.h
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #ifndef AMEFDECODER_H_
18 #define AMEFDECODER_H_
19 
20 #include "AMEFObject.h"
21 #include "iostream"
23 {
24 
25 
26 public:
27  ~AMEFDecoder();
28  AMEFDecoder();
29  static char* longTocharArray(long l,int ind)
30  {
31  char* result = new char[ind];
32  for (int i = 0; i<ind; i++)
33  {
34  int offset = (ind - 1 - i) * 8;
35  result[i] = (char) ((l >> offset) & 0xFF);
36  }
37  return result;
38  }
39 
40  static string longTocharArrayS(long l,int ind)
41  {
42  char* result = new char[ind];
43  for (int i = 0; i<ind; i++)
44  {
45  int offset = (ind - 1 - i) * 8;
46  result[i] = (char) ((l >> offset) & 0xFF);
47  }
48  string tem(result);
49  return tem;
50  }
51 
52  static char* intTocharArray(int l,int ind)
53  {
54  char* result = new char[ind];
55  for (int i = 0; i<ind; i++)
56  {
57  int offset = (ind - 1 - i) * 8;
58  result[i] = (char) ((l >> offset) & 0xFF);
59  }
60  return result;
61  }
62 
63  static char* intTocharArrayWI(int l)
64  {
65  int ind = 1;
66  if(l<256)
67  ind =1;
68  else if(l<65536)
69  ind = 2;
70  else if(l<16777216)
71  ind =3;
72  else
73  ind =4;
74  char* result = new char[ind];
75  for (int i = 0; i<ind; i++)
76  {
77  int offset = (ind - 1 - i) * 8;
78  result[i] = (char) ((l >> offset) & 0xFF);
79  }
80  return result;
81  }
82 
83  static int charArrayToInt(char l[])
84  {
85  int t = 0;
86  int ind = sizeof l;
87  for (int i = 0; i < ind; i++)
88  {
89  int offset = (ind -1 - i) * 8;
90  t += (l[i] & 0x000000FF) << offset;
91  }
92  return t;
93  }
94 
95  static int charArrayToInt(string l,int off,int ind)
96  {
97  int t = 0;
98  for (int i = 0; i < ind; i++)
99  {
100  int offset = (ind -1 - i) * 8;
101  t += (l[off+i] & 0x000000FF) << offset;
102  }
103  return t;
104  }
105 
106  static long charArrayToLong(char l[])
107  {
108  long t = 0;
109  int ind = sizeof l;
110  for (int i = 0; i < ind; i++)
111  {
112  int offset = (ind -1 - i) * 8;
113  t += (l[i] & 0x000000FF) << offset;
114  }
115  return t;
116  }
117  static long charArrayToLong(char* l,int off,int ind)
118  {
119  long t = 0;
120  for (int i = 0; i < ind; i++)
121  {
122  int offset = (ind -1 - i) * 8;
123  t += (l[off+i] & 0x000000FF) << offset;
124  }
125  return t;
126  }
127  static long charArrayToLong(char* l,int ind)
128  {
129  long t = 0;
130  for (int i = 0; i < ind; i++)
131  {
132  int offset = (ind -1 - i) * 8;
133  t += (l[i] & 0x000000FF) << offset;
134  }
135  return t;
136  }
137 
138  static string intTocharArrayS(int l, int ind)
139  {
140  char* result = new char[ind];
141  for (int i = 0; i<ind; i++)
142  {
143  int offset = (ind - 1 - i) * 8;
144  result[i] = (char) ((l >> offset) & 0xFF);
145  }
146  string tem(result);
147  return tem;
148  }
149 
150  AMEFObject* decodeB(string buffer,bool considerLength,bool ignoreName)
151  {
152  position = 0;
153  string strdata;
154  if(considerLength)
155  {
156  strdata = buffer.substr(4);
157  }
158  else
159  {
160  strdata = buffer;
161  }
162  AMEFObject* AMEFObject = decodeSinglePacketB(strdata,ignoreName);
163  return AMEFObject;
164  }
165 
166 
167 
168  /*AMEFObject decodeJDBString(char* data,bool considerLength)
169  {
170  int startWith = 0;
171  if(considerLength)
172  startWith = 4;
173  String strdata = new String(data);
174  strdata = strdata.substring(startWith);
175  AMEFObject AMEFObject = decodeSingleJDBStringPacket(strdata);
176  return AMEFObject;
177  }
178 
179  AMEFObject decodeSingleJDBStringPacket(String strdata)
180  {
181  AMEFObject jDBObject = null;
182  char type = strdata.charAt(0);
183  if(type==AMEFObject::STRING_TYPE)
184  {
185  jDBObject = new AMEFObject();
186  jDBObject->setType(type);
187  int lengthm = ((strdata.charAt(1) & 0xff) << 24) | ((strdata.charAt(2) & 0xff) << 16)
188  | ((strdata.charAt(3) & 0xff) << 8) | ((strdata.charAt(4) & 0xff));
189  jDBObject->setLength(lengthm);
190  String value = strdata.substring(5,5+lengthm);
191  jDBObject->setValue(value,lengthm);
192  tempVal = strdata.substring(5+lengthm);
193  }
194  else if(type==AMEFObject::STRING_65536_TYPE)
195  {
196  jDBObject = new AMEFObject();
197  jDBObject->setType(type);
198  int lengthm = ((strdata.charAt(1) & 0xff) << 8) | (strdata.charAt(2) & 0xff);
199  jDBObject->setLength(lengthm);
200  String value = strdata.substring(3,3+lengthm);
201  jDBObject->setValue(value,lengthm);
202  tempVal = strdata.substring(3+lengthm);
203  }
204  else if(type==AMEFObject::STRING_16777216_TYPE)
205  {
206  jDBObject = new AMEFObject();
207  jDBObject->setType(type);
208  int lengthm = ((strdata.charAt(1) & 0xff) << 16)
209  | ((strdata.charAt(2) & 0xff) << 8) | (strdata.charAt(3) & 0xff);
210  jDBObject->setLength(lengthm);
211  String value = strdata.substring(3,3+lengthm);
212  jDBObject->setValue(value,lengthm);
213  tempVal = strdata.substring(3+lengthm);
214  }
215  else if(type==AMEFObject::DATE_TYPE || type==AMEFObject::STRING_256_TYPE || type==AMEFObject::DOUBLE_FLOAT_TYPE)
216  {
217  jDBObject = new AMEFObject();
218  jDBObject->setType(type);
219  int lengthm = strdata.charAt(1) & 0xff;
220  jDBObject->setLength(lengthm);
221  String value = strdata.substring(2,2+lengthm);
222  jDBObject->setValue(value,lengthm);
223  tempVal = strdata.substring(2+lengthm);
224  }
225  else if(type==AMEFObject::VERY_SMALL_INT_TYPE)
226  {
227  jDBObject = new AMEFObject();
228  jDBObject->setType(type);
229  String value = (strdata.charAt(1) & 0xff) + "";
230  jDBObject->setValue(value,lengthm);
231  tempVal = strdata.substring(2);
232  }
233  else if(type==AMEFObject::SMALL_INT_TYPE)
234  {
235  jDBObject = new AMEFObject();
236  jDBObject->setType(type);
237  int lengthm = ((strdata.charAt(1) & 0xff) << 8) | ((strdata.charAt(2) & 0xff));
238  String value = lengthm + "";
239  jDBObject->setValue(value,lengthm);
240  tempVal = strdata.substring(3);
241  }
242  else if(type==AMEFObject::BIG_INT_TYPE)
243  {
244  jDBObject = new AMEFObject();
245  jDBObject->setType(type);
246  int lengthm = ((strdata.charAt(1) & 0xff) << 16) | ((strdata.charAt(2) & 0xff) << 8)
247  | ((strdata.charAt(3) & 0xff));
248  String value = lengthm + "";
249  jDBObject->setValue(value,lengthm);
250  tempVal = strdata.substring(4);
251  }
252  else if(type==AMEFObject::INT_TYPE)
253  {
254  jDBObject = new AMEFObject();
255  jDBObject->setType(type);
256  int lengthm = ((strdata.charAt(1) & 0xff) << 24) | ((strdata.charAt(2) & 0xff) << 16)
257  | ((strdata.charAt(3) & 0xff) << 8) | ((strdata.charAt(4) & 0xff));
258  String value = lengthm + "";
259  jDBObject->setValue(value,lengthm);
260  tempVal = strdata.substring(5);
261  }
262  else if(type==AMEFObject::VS_LONG_INT_TYPE)
263  {
264  jDBObject = new AMEFObject();
265  jDBObject->setType(type);
266  long lengthm = ((strdata.charAt(1) & 0xff) << 32) | ((strdata.charAt(2) & 0xff) << 24)
267  | ((strdata.charAt(3) & 0xff) << 16) | ((strdata.charAt(4) & 0xff) << 8)
268  | ((strdata.charAt(5) & 0xff));
269  String value = lengthm + "";
270  jDBObject->setValue(value,lengthm);
271  tempVal = strdata.substring(6);
272  }
273  else if(type==AMEFObject::S_LONG_INT_TYPE)
274  {
275  jDBObject = new AMEFObject();
276  jDBObject->setType(type);
277  long lengthm = ((strdata.charAt(1) & 0xff) << 40) | ((strdata.charAt(2) & 0xff) << 32)
278  | ((strdata.charAt(3) & 0xff) << 24)
279  | ((strdata.charAt(4) & 0xff) << 16) | ((strdata.charAt(5) & 0xff) << 8)
280  | ((strdata.charAt(6) & 0xff));
281  String value = lengthm + "";
282  jDBObject->setValue(value,lengthm);
283  tempVal = strdata.substring(7);
284  }
285  else if(type==AMEFObject::B_LONG_INT_TYPE)
286  {
287  jDBObject = new AMEFObject();
288  jDBObject->setType(type);
289  long lengthm = ((strdata.charAt(1) & 0xff) << 48) | ((strdata.charAt(2) & 0xff) << 40)
290  | ((strdata.charAt(3) & 0xff) << 32)
291  | ((strdata.charAt(4) & 0xff) << 24)
292  | ((strdata.charAt(5) & 0xff) << 16) | ((strdata.charAt(6) & 0xff) << 8)
293  | ((strdata.charAt(7) & 0xff));
294  String value = lengthm + "";
295  jDBObject->setValue(value,lengthm);
296  tempVal = strdata.substring(8);
297  }
298  else if(type==AMEFObject::LONG_INT_TYPE)
299  {
300  jDBObject = new AMEFObject();
301  jDBObject->setType(type);
302  long lengthm = ((strdata.charAt(1) & 0xff) << 56) | ((strdata.charAt(2) & 0xff) << 48)
303  | ((strdata.charAt(3) & 0xff) << 40)
304  | ((strdata.charAt(4) & 0xff) << 32)
305  | ((strdata.charAt(5) & 0xff) << 24)
306  | ((strdata.charAt(6) & 0xff) << 16) | ((strdata.charAt(7) & 0xff) << 8)
307  | ((strdata.charAt(8) & 0xff));
308  String value = lengthm + "";
309  jDBObject->setValue(value,lengthm);
310  tempVal = strdata.substring(9);
311  }
312  else if(type==AMEFObject::BOOLEAN_TYPE || type==AMEFObject::CHAR_TYPE)
313  {
314  jDBObject = new AMEFObject();
315  jDBObject->setType(type);
316  jDBObject->setLength(1);
317  String value = strdata.charAt(1)+"";
318  jDBObject->setValue(value,lengthm);
319  tempVal = strdata.substring(2);
320  }
321  else if(type==AMEFObject::VS_OBJECT_TYPE)
322  {
323  jDBObject = new AMEFObject();
324  jDBObject->setType(type);
325  int lengthm = charArrayToInt(strdata.substring(1,2).getchars());
326  jDBObject->setLength(lengthm);
327  tempVal = strdata.substring(2,2+lengthm);
328  while(!tempVal.equals(""))
329  {
330  AMEFObject obj = decodeSingleJDBStringPacket(tempVal);
331  jDBObject->addPacket(obj);
332  }
333  tempVal = strdata.substring(2+lengthm);
334  }
335  else if(type==AMEFObject::S_OBJECT_TYPE)
336  {
337  jDBObject = new AMEFObject();
338  jDBObject->setType(type);
339  int lengthm = ((strdata.charAt(1) & 0xff) << 8) | ((strdata.charAt(2) & 0xff));
340  jDBObject->setLength(lengthm);
341  tempVal = strdata.substring(3,3+lengthm);
342  while(!tempVal.equals(""))
343  {
344  AMEFObject obj = decodeSingleJDBStringPacket(tempVal);
345  jDBObject->addPacket(obj);
346  }
347  tempVal = strdata.substring(3+lengthm);
348  }
349  else if(type==AMEFObject::B_OBJECT_TYPE)
350  {
351  jDBObject = new AMEFObject();
352  jDBObject->setType(type);
353  int lengthm = ((strdata.charAt(1) & 0xff) << 16)
354  | ((strdata.charAt(2) & 0xff) << 8) | ((strdata.charAt(3) & 0xff));
355  jDBObject->setLength(lengthm);
356  tempVal = strdata.substring(4,4+lengthm);
357  while(!tempVal.equals(""))
358  {
359  AMEFObject obj = decodeSingleJDBStringPacket(tempVal);
360  jDBObject->addPacket(obj);
361  }
362  tempVal = strdata.substring(4+lengthm);
363  }
364  else if(type==AMEFObject::OBJECT_TYPE)
365  {
366  jDBObject = new AMEFObject();
367  jDBObject->setType(type);
368  int lengthm = ((strdata.charAt(1) & 0xff) << 24) | ((strdata.charAt(2) & 0xff) << 16)
369  | ((strdata.charAt(3) & 0xff) << 8) | ((strdata.charAt(4) & 0xff));
370  jDBObject->setLength(lengthm);
371  tempVal = strdata.substring(5,5+lengthm);
372  while(!tempVal.equals(""))
373  {
374  AMEFObject obj = decodeSingleJDBStringPacket(tempVal);
375  jDBObject->addPacket(obj);
376  }
377  tempVal = strdata.substring(5+lengthm);
378  }
379  return jDBObject;
380  }
381 
382  *//*
389  AMEFObject decodeSinglePacket(String strdata,bool ignoreName), UnsupportedEncodingException
390  {
391  AMEFObject jDBObject = null;
392  char type = strdata.charAt(0);
393  if(type==AMEFObject::STRING_TYPE)
394  {
395  if(strdata.charAt(1)==',')
396  {
397  jDBObject = new AMEFObject();
398  jDBObject->setType(type);
399  int pos = 2;
400  String name = "";
401  if(!ignoreName)
402  {
403  while(strdata.charAt(pos)!=',')
404  {
405  name += strdata.charAt(pos++);
406  }
407  if(name.equals("") && strdata.charAt(2)!=',')
408  {
409  throw new AMEFDecodeException("Invalid character after name specifier, expected ,");
410  }
411  }
412  if(pos>=strdata.length())
413  {
414  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
415  }
416  jDBObject->setName(buffer.substr(st,en-st));}
417  String length = "";
418  if(!ignoreName)pos++;
419  while(length.length()<4)
420  {
421  length += strdata.charAt(pos++);
422  }
423  if(length.equals("") && strdata.charAt(3)!=',')
424  {
425  throw new AMEFDecodeException("Invalid character after length specifier, expected ,");
426  }
427  else if(pos>=strdata.length())
428  {
429  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
430  }
431  int lengthm = ((length.getchars()[0] & 0xff) << 24) | ((length.getchars()[1] & 0xff) << 16)
432  | ((length.getchars()[2] & 0xff) << 8) | ((length.getchars()[3] & 0xff));
433  jDBObject->setLength(lengthm);
434  String value = strdata.substring(pos+1,pos+lengthm+1);
435  jDBObject->setValue(value,lengthm);
436  tempVal = strdata.substring(pos+lengthm+1);
437  }
438  else
439  {
440  throw new AMEFDecodeException("Invalid character after type specifier, expected ,");
441  }
442  }
443  else if(type==AMEFObject::STRING_65536_TYPE)
444  {
445  if(strdata.charAt(1)==',')
446  {
447  jDBObject = new AMEFObject();
448  jDBObject->setType(type);
449  int pos = 2;
450  String name = "";
451  if(!ignoreName)
452  {
453  while(strdata.charAt(pos)!=',')
454  {
455  name += strdata.charAt(pos++);
456  }
457  if(name.equals("") && strdata.charAt(2)!=',')
458  {
459  throw new AMEFDecodeException("Invalid character after name specifier, expected ,");
460  }
461  }
462  if(pos>=strdata.length())
463  {
464  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
465  }
466  jDBObject->setName(buffer.substr(st,en-st));}
467  String length = "";
468  if(!ignoreName)pos++;
469  while(length.length()<2)
470  {
471  length += strdata.charAt(pos++);
472  }
473  if(length.equals("") && strdata.charAt(3)!=',')
474  {
475  throw new AMEFDecodeException("Invalid character after length specifier, expected ,");
476  }
477  else if(pos>=strdata.length())
478  {
479  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
480  }
481  int lengthm = ((length.getchars()[0] & 0xff) << 8) | ((length.getchars()[1] & 0xff));
482  jDBObject->setLength(lengthm);
483  String value = strdata.substring(pos,pos+lengthm);
484  jDBObject->setValue(value,lengthm);
485  tempVal = strdata.substring(pos+lengthm);
486  }
487  else
488  {
489  throw new AMEFDecodeException("Invalid character after type specifier, expected ,");
490  }
491  }
492  else if(type==AMEFObject::STRING_16777216_TYPE)
493  {
494  if(strdata.charAt(1)==',')
495  {
496  jDBObject = new AMEFObject();
497  jDBObject->setType(type);
498  int pos = 2;
499  String name = "";
500  if(!ignoreName)
501  {
502  while(strdata.charAt(pos)!=',')
503  {
504  name += strdata.charAt(pos++);
505  }
506  if(name.equals("") && strdata.charAt(2)!=',')
507  {
508  throw new AMEFDecodeException("Invalid character after name specifier, expected ,");
509  }
510  }
511  if(pos>=strdata.length())
512  {
513  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
514  }
515  jDBObject->setName(buffer.substr(st,en-st));}
516  String length = "";
517  if(!ignoreName)pos++;
518  while(length.length()<3)
519  {
520  length += strdata.charAt(pos++);
521  }
522  if(length.equals("") && strdata.charAt(3)!=',')
523  {
524  throw new AMEFDecodeException("Invalid character after length specifier, expected ,");
525  }
526  else if(pos>=strdata.length())
527  {
528  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
529  }
530  int lengthm = ((length.getchars()[0] & 0xff) << 16) | ((length.getchars()[1] & 0xff) << 8)
531  | ((length.getchars()[2] & 0xff));
532  jDBObject->setLength(lengthm);
533  String value = strdata.substring(pos,pos+lengthm);
534  jDBObject->setValue(value,lengthm);
535  tempVal = strdata.substring(pos+lengthm);
536  }
537  else
538  {
539  throw new AMEFDecodeException("Invalid character after type specifier, expected ,");
540  }
541  }
542  else if(type==AMEFObject::BOOLEAN_TYPE || type==AMEFObject::CHAR_TYPE)
543  {
544  if(strdata.charAt(1)==',')
545  {
546  jDBObject = new AMEFObject();
547  jDBObject->setType(type);
548  int pos = 2;
549  String name = "";
550  if(!ignoreName)
551  {
552  while(strdata.charAt(pos)!=',')
553  {
554  name += strdata.charAt(pos++);
555  }
556  if(name.equals("") && strdata.charAt(2)!=',')
557  {
558  throw new AMEFDecodeException("Invalid character after name specifier, expected ,");
559  }
560  }
561  if(pos>=strdata.length())
562  {
563  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
564  }
565  jDBObject->setName(buffer.substr(st,en-st));}
566  if(pos>=strdata.length())
567  {
568  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
569  }
570  jDBObject->setLength(1);
571  String value = "";
572  if(!ignoreName)
573  {
574  value = strdata.substring(pos+1,pos+2);
575  tempVal = strdata.substring(pos+2);
576  }
577  else
578  {
579  value = strdata.substring(pos,pos+1);
580  tempVal = strdata.substring(pos+1);
581  }
582  jDBObject->setValue(value,lengthm);
583  }
584  else
585  {
586  throw new AMEFDecodeException("Invalid character after type specifier, expected ,");
587  }
588  }
589  else if(type==AMEFObject::STRING_256_TYPE || type==AMEFObject::DATE_TYPE || type==AMEFObject::DOUBLE_FLOAT_TYPE)
590  {
591  if(strdata.charAt(1)==',')
592  {
593  jDBObject = new AMEFObject();
594  jDBObject->setType(type);
595  int pos = 2;
596  String name = "";
597  if(!ignoreName)
598  {
599  while(strdata.charAt(pos)!=',')
600  {
601  name += strdata.charAt(pos++);
602  }
603  if(name.equals("") && strdata.charAt(2)!=',')
604  {
605  throw new AMEFDecodeException("Invalid character after name specifier, expected ,");
606  }
607  }
608  if(pos>=strdata.length())
609  {
610  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
611  }
612  jDBObject->setName(buffer.substr(st,en-st));}
613  String length = "";
614  if(!ignoreName)pos++;
615  while(length.length()<1)
616  {
617  length += strdata.charAt(pos++);
618  }
619  if(length.equals("") && strdata.charAt(3)!=',')
620  {
621  throw new AMEFDecodeException("Invalid character after length specifier, expected ,");
622  }
623  else if(pos>=strdata.length())
624  {
625  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
626  }
627  int lengthm = length.charAt(0) & 0xff;
628  jDBObject->setLength(lengthm);
629  String value = strdata.substring(pos,pos+lengthm);
630  jDBObject->setValue(value,lengthm);
631  tempVal = strdata.substring(pos+lengthm);
632  }
633  else
634  {
635  throw new AMEFDecodeException("Invalid character after type specifier, expected ,");
636  }
637  }
638  else if(type==AMEFObject::VERY_SMALL_INT_TYPE)
639  {
640  jDBObject = getObject(strdata, type, ignoreName, 1);
641  }
642  else if(type==AMEFObject::SMALL_INT_TYPE)
643  {
644  jDBObject = getObject(strdata, type, ignoreName, 2);
645  }
646  else if(type==AMEFObject::BIG_INT_TYPE)
647  {
648  jDBObject = getObject(strdata, type, ignoreName, 3);
649  }
650  else if(type==AMEFObject::INT_TYPE)
651  {
652  jDBObject = getObject(strdata, type, ignoreName, 4);
653  }
654  else if(type==AMEFObject::VS_LONG_INT_TYPE)
655  {
656  jDBObject = getObject(strdata, type, ignoreName, 5);
657  }
658  else if(type==AMEFObject::S_LONG_INT_TYPE)
659  {
660  jDBObject = getObject(strdata, type, ignoreName, 6);
661  }
662  else if(type==AMEFObject::B_LONG_INT_TYPE)
663  {
664  jDBObject = getObject(strdata, type, ignoreName, 7);
665  }
666  else if(type==AMEFObject::LONG_INT_TYPE)
667  {
668  jDBObject = getObject(strdata, type, ignoreName, 8);
669  }
670  else if(type==AMEFObject::VS_OBJECT_TYPE)
671  {
672  if(strdata.charAt(1)==',')
673  {
674  jDBObject = new AMEFObject();
675  jDBObject->setType(type);
676  int pos = 2;
677  String name = "";
678  if(!ignoreName)
679  {
680  while(strdata.charAt(pos)!=',')
681  {
682  name += strdata.charAt(pos++);
683  }
684  if(name.equals("") && strdata.charAt(2)!=',')
685  {
686  throw new AMEFDecodeException("Invalid character after name specifier, expected ,");
687  }
688  }
689  if(pos>=strdata.length())
690  {
691  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
692  }
693  jDBObject->setName(buffer.substr(st,en-st));}
694  String length = "";
695  if(!ignoreName)pos++;
696  while(length.length()<1)
697  {
698  length += strdata.charAt(pos++);
699  }
700  if(length.equals("") && strdata.charAt(3)!=',')
701  {
702  throw new AMEFDecodeException("Invalid character after length specifier, expected ,");
703  }
704  else if(pos>=strdata.length())
705  {
706  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
707  }
708  int lengthm = ((length.getchars()[0]) & 0xff);
709  jDBObject->setLength(lengthm);
710  //
711  //String value = strdata.substring(pos+1,pos+lengthm+1);
712  tempVal = strdata.substring(pos,pos+lengthm);
713  while(!tempVal.equals(""))
714  {
715  AMEFObject obj = decodeSinglePacket(tempVal,ignoreName);
716  jDBObject->addPacket(obj);
717  }
718  tempVal = strdata.substring(pos+lengthm);
719  }
720  else
721  {
722  throw new AMEFDecodeException("Invalid character after type specifier, expected ,");
723  }
724  }
725  else if(type==AMEFObject::S_OBJECT_TYPE)
726  {
727  if(strdata.charAt(1)==',')
728  {
729  jDBObject = new AMEFObject();
730  jDBObject->setType(type);
731  int pos = 2;
732  String name = "";
733  if(!ignoreName)
734  {
735  while(strdata.charAt(pos)!=',')
736  {
737  name += strdata.charAt(pos++);
738  }
739  if(name.equals("") && strdata.charAt(2)!=',')
740  {
741  throw new AMEFDecodeException("Invalid character after name specifier, expected ,");
742  }
743  }
744  if(pos>=strdata.length())
745  {
746  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
747  }
748  jDBObject->setName(buffer.substr(st,en-st));}
749  String length = "";
750  if(!ignoreName)pos++;
751  while(length.length()<2)
752  {
753  length += strdata.charAt(pos++);
754  }
755  if(length.equals("") && strdata.charAt(3)!=',')
756  {
757  throw new AMEFDecodeException("Invalid character after length specifier, expected ,");
758  }
759  else if(pos>=strdata.length())
760  {
761  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
762  }
763  int lengthm = ((length.getchars()[0] & 0xff) << 8) | ((length.getchars()[1]) & 0xff);
764  jDBObject->setLength(lengthm);
765  //String value = strdata.substring(pos+1,pos+lengthm+1);
766  //tempVal = value;
767  tempVal = strdata.substring(pos,pos+lengthm);
768  while(!tempVal.equals(""))
769  {
770  AMEFObject obj = decodeSinglePacket(tempVal,ignoreName);
771  jDBObject->addPacket(obj);
772  }
773  tempVal = strdata.substring(pos+lengthm);
774  }
775  else
776  {
777  throw new AMEFDecodeException("Invalid character after type specifier, expected ,");
778  }
779  }
780  else if(type==AMEFObject::B_OBJECT_TYPE)
781  {
782  if(strdata.charAt(1)==',')
783  {
784  jDBObject = new AMEFObject();
785  jDBObject->setType(type);
786  int pos = 2;
787  String name = "";
788  if(!ignoreName)
789  {
790  while(strdata.charAt(pos)!=',')
791  {
792  name += strdata.charAt(pos++);
793  }
794  if(name.equals("") && strdata.charAt(2)!=',')
795  {
796  throw new AMEFDecodeException("Invalid character after name specifier, expected ,");
797  }
798  }
799  if(pos>=strdata.length())
800  {
801  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
802  }
803  jDBObject->setName(buffer.substr(st,en-st));}
804  String length = "";
805  if(!ignoreName)pos++;
806  while(length.length()<3)
807  {
808  length += strdata.charAt(pos++);
809  }
810  if(length.equals("") && strdata.charAt(3)!=',')
811  {
812  throw new AMEFDecodeException("Invalid character after length specifier, expected ,");
813  }
814  else if(pos>=strdata.length())
815  {
816  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
817  }
818  int lengthm = ((length.getchars()[0] & 0xff) << 16)
819  | ((length.getchars()[1] & 0xff) << 8) | ((length.getchars()[2]) & 0xff);
820  jDBObject->setLength(lengthm);
821  //String value = strdata.substring(pos+1,pos+lengthm+1);
822  //tempVal = value;
823  tempVal = strdata.substring(pos,pos+lengthm);
824  while(!tempVal.equals(""))
825  {
826  AMEFObject obj = decodeSinglePacket(tempVal,ignoreName);
827  jDBObject->addPacket(obj);
828  }
829  tempVal = strdata.substring(pos+lengthm);
830  }
831  else
832  {
833  throw new AMEFDecodeException("Invalid character after type specifier, expected ,");
834  }
835  }
836  else if(type==AMEFObject::OBJECT_TYPE)
837  {
838  if(strdata.charAt(1)==',')
839  {
840  jDBObject = new AMEFObject();
841  jDBObject->setType(type);
842  int pos = 2;
843  String name = "";
844  if(!ignoreName)
845  {
846  while(strdata.charAt(pos)!=',')
847  {
848  name += strdata.charAt(pos++);
849  }
850  if(name.equals("") && strdata.charAt(2)!=',')
851  {
852  throw new AMEFDecodeException("Invalid character after name specifier, expected ,");
853  }
854  }
855  if(pos>=strdata.length())
856  {
857  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
858  }
859  jDBObject->setName(buffer.substr(st,en-st));}
860  String length = "";
861  if(!ignoreName)pos++;
862  while(length.length()<4)
863  {
864  length += strdata.charAt(pos++);
865  }
866  if(length.equals("") && strdata.charAt(3)!=',')
867  {
868  throw new AMEFDecodeException("Invalid character after length specifier, expected ,");
869  }
870  else if(pos>=strdata.length())
871  {
872  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
873  }
874  int lengthm = ((length.getchars()[0] & 0xff) << 24) | ((length.getchars()[1] & 0xff) << 16)
875  | ((length.getchars()[2] & 0xff) << 8) | ((length.getchars()[3]) & 0xff);
876  jDBObject->setLength(lengthm);
877  //String value = strdata.substring(pos+1,pos+lengthm+1);
878  //tempVal = value;
879  tempVal = strdata.substring(pos,pos+lengthm);
880  while(!tempVal.equals(""))
881  {
882  AMEFObject obj = decodeSinglePacket(tempVal,ignoreName);
883  jDBObject->addPacket(obj);
884  }
885  tempVal = strdata.substring(pos+lengthm);
886  }
887  else
888  {
889  throw new AMEFDecodeException("Invalid character after type specifier, expected ,");
890  }
891  }
892  return jDBObject;
893  }
894 
895  AMEFObject getObject(String strdata,char type,bool ignoreName,int typ)
896  {
897  AMEFObject jDBObject = null;
898  if(strdata.charAt(1)==',')
899  {
900  jDBObject = new AMEFObject();
901  jDBObject->setType(type);
902  int pos = 2;
903  String name = "";
904  if(!ignoreName)
905  {
906  while(strdata.charAt(pos)!=',')
907  {
908  name += strdata.charAt(pos++);
909  }
910  if(name.equals("") && strdata.charAt(2)!=',')
911  {
912  throw new AMEFDecodeException("Invalid character after name specifier, expected ,");
913  }
914  }
915  if(pos>=strdata.length())
916  {
917  throw new AMEFDecodeException("Reached end of AMEF string, not found ,");
918  }
919  jDBObject->setName(buffer.substr(st,en-st));}
920  if(!ignoreName)pos++;
921  String value = strdata.substring(pos,pos+typ);
922  jDBObject->setValue(value,lengthm);
923  tempVal = strdata.substring(pos+typ);
924  }
925  else
926  {
927  throw new AMEFDecodeException("Invalid character after type specifier, expected ,");
928  }
929  return jDBObject;
930  }*/
931 
932  int position;
933  AMEFObject* decodeSinglePacketB(string buffer,bool ignoreName)
934  {
935  char type = (char)buffer[position];
936  AMEFObject *jDBObject = NULL;
937  int st, en;
938  if(type==AMEFObject::NULL_STRING || type==AMEFObject::NULL_DATE || type==AMEFObject::NULL_NUMBER
939  || type==AMEFObject::NULL_BOOL || type==AMEFObject::NULL_CHAR)
940  {
941  jDBObject = new AMEFObject();
942  jDBObject->setType(type);
943  if(!ignoreName)
944  {
945  while(buffer[position++]!=44){}
946  st = position;
947  while(buffer[position++]!=44){}
948  en = position - 1;
949  if(en>st){
950 
951  jDBObject->setName(buffer.substr(st,en-st));}
952  }
953  else
954  position++;
955  }
956  else if(type==AMEFObject::STRING_TYPE)
957  {
958  jDBObject = new AMEFObject();
959  jDBObject->setType(type);
960  if(!ignoreName)
961  {
962  while(buffer[position++]!=44){}
963  st = position;
964  while(buffer[position++]!=44){}
965  en = position - 1;
966  if(en>st){
967 
968  jDBObject->setName(buffer.substr(st,en-st));}
969  }
970  else
971  position++;
972  int lengthm = charArrayToInt(buffer,position,4);
973  jDBObject->setLength(lengthm);
974  position += 4;
975  string value = buffer.substr(position,lengthm);
976  jDBObject->setValue(value);
977  position += 5+lengthm;
978  }
979  else if(type==AMEFObject::STRING_65536_TYPE)
980  {
981  jDBObject = new AMEFObject();
982  jDBObject->setType(type);
983  if(!ignoreName)
984  {
985  while(buffer[position++]!=44){}
986  st = position;
987  while(buffer[position++]!=44){}
988  en = position - 1;
989  if(en>st){
990 
991  jDBObject->setName(buffer.substr(st,en-st));}
992  }
993  else
994  position++;
995  int lengthm = charArrayToInt(buffer,position,2);
996  jDBObject->setLength(lengthm);
997  position += 2;
998  string value = buffer.substr(position,lengthm);
999  jDBObject->setValue(value);
1000  position += 3+lengthm;
1001  }
1002  else if(type==AMEFObject::STRING_16777216_TYPE)
1003  {
1004  jDBObject = new AMEFObject();
1005  jDBObject->setType(type);
1006  if(!ignoreName)
1007  {
1008  while(buffer[position++]!=44){}
1009  st = position;
1010  while(buffer[position++]!=44){}
1011  en = position - 1;
1012  if(en>st){
1013 
1014  jDBObject->setName(buffer.substr(st,en-st));}
1015  }
1016  else
1017  position++;
1018  int lengthm = charArrayToInt(buffer,position,3);
1019  jDBObject->setLength(lengthm);
1020  position += 3;
1021  string value = buffer.substr(position,lengthm);
1022  jDBObject->setValue(value);
1023  position += 4+lengthm;
1024  }
1025  else if(type==AMEFObject::DATE_TYPE || type==AMEFObject::STRING_256_TYPE || type==AMEFObject::DOUBLE_FLOAT_TYPE)
1026  {
1027  jDBObject = new AMEFObject();
1028  jDBObject->setType(type);
1029  if(!ignoreName)
1030  {
1031  while(buffer[position++]!=44){}
1032  st = position;
1033  while(buffer[position++]!=44){}
1034  en = position - 1;
1035  if(en>st){
1036 
1037  jDBObject->setName(buffer.substr(st,en-st));}
1038  }
1039  else
1040  position++;
1041  int lengthm = charArrayToInt(buffer,position,1);
1042  jDBObject->setLength(lengthm);
1043  position++;
1044  string value = buffer.substr(position,lengthm);
1045  jDBObject->setValue(value);
1046  position += lengthm;
1047  }
1048  else if(type==AMEFObject::VERY_SMALL_INT_TYPE)
1049  {
1050  jDBObject = new AMEFObject();
1051  jDBObject->setType(type);
1052  if(!ignoreName)
1053  {
1054  while(buffer[position++]!=44){}
1055  st = position;
1056  while(buffer[position++]!=44){}
1057  en = position - 1;
1058  if(en>st){
1059 
1060  jDBObject->setName(buffer.substr(st,en-st));}
1061  }
1062  else
1063  position++;
1064  jDBObject->setLength(1);
1065  jDBObject->pushChar(buffer[position]);
1066  position += 1;
1067  }
1068  else if(type==AMEFObject::SMALL_INT_TYPE)
1069  {
1070  jDBObject = new AMEFObject();
1071  jDBObject->setType(type);
1072  if(!ignoreName)
1073  {
1074  while(buffer[position++]!=44){}
1075  st = position;
1076  while(buffer[position++]!=44){}
1077  en = position - 1;
1078  if(en>st){
1079 
1080  jDBObject->setName(buffer.substr(st,en-st));}
1081  }
1082  else
1083  position++;
1084  jDBObject->setLength(2);
1085  jDBObject->pushChar(buffer[position]);
1086  jDBObject->pushChar(buffer[position+1]);
1087  position += 2;
1088  }
1089  else if(type==AMEFObject::BIG_INT_TYPE)
1090  {
1091  jDBObject = new AMEFObject();
1092  jDBObject->setType(type);
1093  if(!ignoreName)
1094  {
1095  while(buffer[position++]!=44){}
1096  st = position;
1097  while(buffer[position++]!=44){}
1098  en = position - 1;
1099  if(en>st){
1100 
1101  jDBObject->setName(buffer.substr(st,en-st));}
1102  }
1103  else
1104  position++;
1105  jDBObject->setLength(3);
1106  jDBObject->pushChar(buffer[position]);
1107  jDBObject->pushChar(buffer[position+1]);
1108  jDBObject->pushChar(buffer[position+2]);
1109  position += 3;
1110  }
1111  else if(type==AMEFObject::INT_TYPE)
1112  {
1113  jDBObject = new AMEFObject();
1114  jDBObject->setType(type);
1115  if(!ignoreName)
1116  {
1117  while(buffer[position++]!=44){}
1118  st = position;
1119  while(buffer[position++]!=44){}
1120  en = position - 1;
1121  if(en>st){
1122 
1123  jDBObject->setName(buffer.substr(st,en-st));}
1124  }
1125  else
1126  position++;
1127  jDBObject->setLength(4);
1128  jDBObject->pushChar(buffer[position]);
1129  jDBObject->pushChar(buffer[position+1]);
1130  jDBObject->pushChar(buffer[position+2]);
1131  jDBObject->pushChar(buffer[position+3]);
1132  position += 4;
1133  }
1134  else if(type==AMEFObject::VS_LONG_INT_TYPE)
1135  {
1136  jDBObject = new AMEFObject();
1137  jDBObject->setType(type);
1138  if(!ignoreName)
1139  {
1140  while(buffer[position++]!=44){}
1141  st = position;
1142  while(buffer[position++]!=44){}
1143  en = position - 1;
1144  if(en>st){
1145 
1146  jDBObject->setName(buffer.substr(st,en-st));}
1147  }
1148  else
1149  position++;
1150  jDBObject->setLength(5);
1151  jDBObject->pushChar(buffer[position]);
1152  jDBObject->pushChar(buffer[position+1]);
1153  jDBObject->pushChar(buffer[position+2]);
1154  jDBObject->pushChar(buffer[position+3]);
1155  jDBObject->pushChar(buffer[position+4]);
1156  position += 5;
1157  }
1158  else if(type==AMEFObject::S_LONG_INT_TYPE)
1159  {
1160  jDBObject = new AMEFObject();
1161  jDBObject->setType(type);
1162  if(!ignoreName)
1163  {
1164  while(buffer[position++]!=44){}
1165  st = position;
1166  while(buffer[position++]!=44){}
1167  en = position - 1;
1168  if(en>st){
1169 
1170  jDBObject->setName(buffer.substr(st,en-st));}
1171  }
1172  else
1173  position++;
1174  jDBObject->setLength(6);
1175  jDBObject->pushChar(buffer[position]);
1176  jDBObject->pushChar(buffer[position+1]);
1177  jDBObject->pushChar(buffer[position+2]);
1178  jDBObject->pushChar(buffer[position+3]);
1179  jDBObject->pushChar(buffer[position+4]);
1180  jDBObject->pushChar(buffer[position+5]);
1181  position += 6;
1182  }
1183  else if(type==AMEFObject::B_LONG_INT_TYPE)
1184  {
1185  jDBObject = new AMEFObject();
1186  jDBObject->setType(type);
1187  if(!ignoreName)
1188  {
1189  while(buffer[position++]!=44){}
1190  st = position;
1191  while(buffer[position++]!=44){}
1192  en = position - 1;
1193  if(en>st){
1194 
1195  jDBObject->setName(buffer.substr(st,en-st));}
1196  }
1197  else
1198  position++;
1199  jDBObject->setLength(7);
1200  jDBObject->pushChar(buffer[position]);
1201  jDBObject->pushChar(buffer[position+1]);
1202  jDBObject->pushChar(buffer[position+2]);
1203  jDBObject->pushChar(buffer[position+3]);
1204  jDBObject->pushChar(buffer[position+4]);
1205  jDBObject->pushChar(buffer[position+5]);
1206  jDBObject->pushChar(buffer[position+6]);
1207  position += 7;
1208  }
1209  else if(type==AMEFObject::LONG_INT_TYPE)
1210  {
1211  jDBObject = new AMEFObject();
1212  jDBObject->setType(type);
1213  if(!ignoreName)
1214  {
1215  while(buffer[position++]!=44){}
1216  st = position;
1217  while(buffer[position++]!=44){}
1218  en = position - 1;
1219  if(en>st){
1220 
1221  jDBObject->setName(buffer.substr(st,en-st));}
1222  }
1223  else
1224  position++;
1225  jDBObject->setLength(8);
1226  jDBObject->pushChar(buffer[position]);
1227  jDBObject->pushChar(buffer[position+1]);
1228  jDBObject->pushChar(buffer[position+2]);
1229  jDBObject->pushChar(buffer[position+3]);
1230  jDBObject->pushChar(buffer[position+4]);
1231  jDBObject->pushChar(buffer[position+5]);
1232  jDBObject->pushChar(buffer[position+6]);
1233  jDBObject->pushChar(buffer[position+7]);
1234  position += 8;
1235  }
1236  else if(type==AMEFObject::BOOLEAN_TYPE || type==AMEFObject::CHAR_TYPE)
1237  {
1238  jDBObject = new AMEFObject();
1239  jDBObject->setType(type);
1240  if(!ignoreName)
1241  {
1242  while(buffer[position++]!=44){}
1243  st = position;
1244  while(buffer[position++]!=44){}
1245  en = position - 1;
1246  if(en>st){
1247 
1248  jDBObject->setName(buffer.substr(st,en-st));}
1249  }
1250  else
1251  position++;
1252  jDBObject->setLength(1);
1253  jDBObject->pushChar(buffer[position]);
1254  position += 1;
1255  }
1256  else if(type==AMEFObject::VS_OBJECT_TYPE)
1257  {
1258  jDBObject = new AMEFObject();
1259  jDBObject->setType(type);
1260  if(!ignoreName)
1261  {
1262  while(buffer[position++]!=44){}
1263  st = position;
1264  while(buffer[position++]!=44){}
1265  en = position - 1;
1266  if(en>st){
1267 
1268  jDBObject->setName(buffer.substr(st,en-st));}
1269  }
1270  else
1271  position++;
1272  int lengthm = charArrayToInt(buffer,position,1);
1273  jDBObject->setLength(lengthm);
1274  position++;
1275  while(position<(int)buffer.length())
1276  {
1277  AMEFObject *obj = decodeSinglePacketB(buffer,ignoreName);
1278  jDBObject->addPacket(obj);
1279  }
1280  }
1281  else if(type==AMEFObject::S_OBJECT_TYPE)
1282  {
1283  jDBObject = new AMEFObject();
1284  jDBObject->setType(type);
1285  if(!ignoreName)
1286  {
1287  while(buffer[position++]!=44){}
1288  st = position;
1289  while(buffer[position++]!=44){}
1290  en = position - 1;
1291  if(en>st){
1292 
1293  jDBObject->setName(buffer.substr(st,en-st));}
1294  }
1295  else
1296  position++;
1297  int lengthm = charArrayToInt(buffer,position,2);
1298  jDBObject->setLength(lengthm);
1299  //char* value = new char[lengthm];
1300  //System.arraycopy(buffer, 3, value, 0, lengthm);
1301  position += 2;
1302  while(position<(int)buffer.length())
1303  {
1304  AMEFObject* obj = decodeSinglePacketB(buffer,ignoreName);
1305  jDBObject->addPacket(obj);
1306  }
1307  }
1308  else if(type==AMEFObject::B_OBJECT_TYPE)
1309  {
1310  jDBObject = new AMEFObject();
1311  jDBObject->setType(type);
1312  if(!ignoreName)
1313  {
1314  while(buffer[position++]!=44){}
1315  st = position;
1316  while(buffer[position++]!=44){}
1317  en = position - 1;
1318  if(en>st){
1319 
1320  jDBObject->setName(buffer.substr(st,en-st));}
1321  }
1322  else
1323  position++;
1324  int lengthm = charArrayToInt(buffer,position,3);
1325  jDBObject->setLength(lengthm);
1326  position += 3;
1327  while(position<(int)buffer.length())
1328  {
1329  AMEFObject* obj = decodeSinglePacketB(buffer,ignoreName);
1330  jDBObject->addPacket(obj);
1331  }
1332  }
1333  else if(type==AMEFObject::OBJECT_TYPE)
1334  {
1335  jDBObject = new AMEFObject();
1336  jDBObject->setType(type);
1337  if(!ignoreName)
1338  {
1339  while(buffer[position++]!=44){}
1340  st = position;
1341  while(buffer[position++]!=44){}
1342  en = position - 1;
1343  if(en>st){
1344 
1345  jDBObject->setName(buffer.substr(st,en-st));}
1346  }
1347  else
1348  position++;
1349  int lengthm = charArrayToInt(buffer,position,4);
1350  jDBObject->setLength(lengthm);
1351  position += 4;
1352  while(position<(int)buffer.length())
1353  {
1354  AMEFObject* obj = decodeSinglePacketB(buffer,ignoreName);
1355  jDBObject->addPacket(obj);
1356  }
1357  }
1358  return jDBObject;
1359  }
1360 
1361 };
1362 
1363 #endif /* AMEFDECODER_H_ */