1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.asn1.ber.tlv;
21
22
23 import java.nio.BufferOverflowException;
24 import java.nio.ByteBuffer;
25
26 import org.apache.directory.api.asn1.EncoderException;
27 import org.apache.directory.api.asn1.util.Asn1Buffer;
28 import org.apache.directory.api.asn1.util.Asn1StringUtils;
29 import org.apache.directory.api.asn1.util.BitString;
30 import org.apache.directory.api.asn1.util.Oid;
31 import org.apache.directory.api.i18n.I18n;
32 import org.apache.directory.api.util.Strings;
33
34
35
36
37
38
39
40 public class BerValue
41 {
42
43 private byte[] data;
44
45
46 private int currentPos;
47
48
49 public static final byte TRUE_VALUE = ( byte ) 0xFF;
50
51
52 public static final byte FALSE_VALUE = ( byte ) 0x00;
53
54
55 private static final byte[] ENCODED_TRUE = new byte[]
56 { 0x01, 0x01, TRUE_VALUE };
57
58
59 private static final byte[] ENCODED_FALSE = new byte[]
60 { 0x01, 0x01, FALSE_VALUE };
61
62
63 private static final int ONE_BYTE_MAX = ( 1 << 7 ) - 1;
64
65
66 private static final int ONE_BYTE_MIN = -( 1 << 7 );
67
68
69 private static final int TWO_BYTE_MAX = ( 1 << 15 ) - 1;
70
71
72 private static final int TWO_BYTE_MIN = -( 1 << 15 );
73
74
75 private static final int THREE_BYTE_MAX = ( 1 << 23 ) - 1;
76
77
78 private static final int THREE_BYTE_MIN = -( 1 << 23 );
79
80
81 private static final long FOUR_BYTE_MAX = ( 1L << 31 ) - 1L;
82
83
84 private static final long FOUR_BYTE_MIN = -( 1L << 31 );
85
86
87 private static final long FIVE_BYTE_MAX = ( 1L << 39 ) - 1L;
88
89
90 private static final long FIVE_BYTE_MIN = -( 1L << 39 );
91
92
93 private static final long SIX_BYTE_MAX = ( 1L << 47 ) - 1L;
94
95
96 private static final long SIX_BYTE_MIN = -( 1L << 47 );
97
98
99 private static final long SEVEN_BYTE_MAX = ( 1L << 55 ) - 1L;
100
101
102 private static final long SEVEN_BYTE_MIN = -( 1L << 55 );
103
104
105
106
107
108
109
110 public BerValue( byte[] value )
111 {
112
113 data = new byte[value.length];
114 System.arraycopy( value, 0, data, 0, value.length );
115 currentPos = 0;
116 }
117
118
119
120
121
122 public BerValue()
123 {
124 data = null;
125 currentPos = 0;
126 }
127
128
129
130
131
132
133
134 public void init( int size )
135 {
136 data = new byte[size];
137 currentPos = 0;
138 }
139
140
141
142
143
144 public void reset()
145 {
146 data = null;
147 currentPos = 0;
148 }
149
150
151
152
153
154
155
156 public byte[] getData()
157 {
158 return data;
159 }
160
161
162
163
164
165
166
167 public void setData( ByteBuffer data )
168 {
169 int length = data.remaining();
170 data.get( this.data, 0, length );
171 currentPos = length;
172 }
173
174
175
176
177
178
179
180 public void addData( ByteBuffer buffer )
181 {
182 int length = buffer.remaining();
183 buffer.get( data, currentPos, length );
184 currentPos += length;
185 }
186
187
188
189
190
191
192
193 public void setData( byte[] data )
194 {
195 System.arraycopy( data, 0, this.data, 0, data.length );
196 currentPos = data.length;
197 }
198
199
200
201
202
203
204
205 public void addData( byte[] array )
206 {
207 System.arraycopy( array, 0, this.data, currentPos, array.length );
208 currentPos = array.length;
209 }
210
211
212
213
214
215 public int getCurrentLength()
216 {
217 return currentPos;
218 }
219
220
221
222
223
224
225
226
227
228
229 public static int getNbBytes( int value )
230 {
231 if ( ( value >= ONE_BYTE_MIN ) && ( value <= ONE_BYTE_MAX ) )
232 {
233 return 1;
234 }
235 else if ( ( value >= TWO_BYTE_MIN ) && ( value <= TWO_BYTE_MAX ) )
236 {
237 return 2;
238 }
239 else if ( ( value >= THREE_BYTE_MIN ) && ( value <= THREE_BYTE_MAX ) )
240 {
241 return 3;
242 }
243 else
244 {
245 return 4;
246 }
247 }
248
249
250
251
252
253
254
255
256
257
258 public static int getNbBytes( long value )
259 {
260 if ( ( value >= ONE_BYTE_MIN ) && ( value <= ONE_BYTE_MAX ) )
261 {
262 return 1;
263 }
264 else if ( ( value >= TWO_BYTE_MIN ) && ( value <= TWO_BYTE_MAX ) )
265 {
266 return 2;
267 }
268 else if ( ( value >= THREE_BYTE_MIN ) && ( value <= THREE_BYTE_MAX ) )
269 {
270 return 3;
271 }
272 else if ( ( value >= FOUR_BYTE_MIN ) && ( value <= FOUR_BYTE_MAX ) )
273 {
274 return 4;
275 }
276 else if ( ( value >= FIVE_BYTE_MIN ) && ( value <= FIVE_BYTE_MAX ) )
277 {
278 return 5;
279 }
280 else if ( ( value >= SIX_BYTE_MIN ) && ( value <= SIX_BYTE_MAX ) )
281 {
282 return 6;
283 }
284 else if ( ( value >= SEVEN_BYTE_MIN ) && ( value <= SEVEN_BYTE_MAX ) )
285 {
286 return 7;
287 }
288 else
289 {
290 return 8;
291 }
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313 public static byte[] getBytes( int value )
314 {
315 byte[] bytes;
316
317 if ( value >= 0 )
318 {
319 if ( ( value >= 0 ) && ( value <= ONE_BYTE_MAX ) )
320 {
321 bytes = new byte[1];
322 bytes[0] = ( byte ) value;
323 }
324 else if ( ( value > ONE_BYTE_MAX ) && ( value <= TWO_BYTE_MAX ) )
325 {
326 bytes = new byte[2];
327 bytes[1] = ( byte ) value;
328 bytes[0] = ( byte ) ( value >> 8 );
329 }
330 else if ( ( value > TWO_BYTE_MAX ) && ( value <= THREE_BYTE_MAX ) )
331 {
332 bytes = new byte[3];
333 bytes[2] = ( byte ) value;
334 bytes[1] = ( byte ) ( value >> 8 );
335 bytes[0] = ( byte ) ( value >> 16 );
336 }
337 else
338 {
339 bytes = new byte[4];
340 bytes[3] = ( byte ) value;
341 bytes[2] = ( byte ) ( value >> 8 );
342 bytes[1] = ( byte ) ( value >> 16 );
343 bytes[0] = ( byte ) ( value >> 24 );
344 }
345 }
346 else
347 {
348
349 if ( value == 0x80000000 )
350 {
351 bytes = new byte[4];
352 bytes[3] = ( byte ) value;
353 bytes[2] = ( byte ) ( value >> 8 );
354 bytes[1] = ( byte ) ( value >> 16 );
355 bytes[0] = ( byte ) ( value >> 24 );
356 }
357 else
358 {
359
360 if ( value >= 0xFFFFFF80 )
361 {
362 bytes = new byte[1];
363 bytes[0] = ( byte ) value;
364 }
365 else if ( value >= 0xFFFF8000 )
366 {
367 bytes = new byte[2];
368 bytes[1] = ( byte ) ( value );
369 bytes[0] = ( byte ) ( value >> 8 );
370 }
371 else if ( value >= 0xFF800000 )
372 {
373 bytes = new byte[3];
374 bytes[2] = ( byte ) value;
375 bytes[1] = ( byte ) ( value >> 8 );
376 bytes[0] = ( byte ) ( value >> 16 );
377 }
378 else
379 {
380 bytes = new byte[4];
381 bytes[3] = ( byte ) value;
382 bytes[2] = ( byte ) ( value >> 8 );
383 bytes[1] = ( byte ) ( value >> 16 );
384 bytes[0] = ( byte ) ( value >> 24 );
385 }
386 }
387 }
388
389 return bytes;
390 }
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459 public static byte[] getBytes( long value )
460 {
461 byte[] bytes;
462
463 if ( value >= 0 )
464 {
465 if ( ( value >= 0 ) && ( value <= ONE_BYTE_MAX ) )
466 {
467 bytes = new byte[1];
468 bytes[0] = ( byte ) value;
469 }
470 else if ( ( value > ONE_BYTE_MAX ) && ( value <= TWO_BYTE_MAX ) )
471 {
472 bytes = new byte[2];
473 bytes[1] = ( byte ) value;
474 bytes[0] = ( byte ) ( value >> 8 );
475 }
476 else if ( ( value > TWO_BYTE_MAX ) && ( value <= THREE_BYTE_MAX ) )
477 {
478 bytes = new byte[3];
479 bytes[2] = ( byte ) value;
480 bytes[1] = ( byte ) ( value >> 8 );
481 bytes[0] = ( byte ) ( value >> 16 );
482 }
483 else if ( ( value > THREE_BYTE_MAX ) && ( value <= FOUR_BYTE_MAX ) )
484 {
485 bytes = new byte[4];
486 bytes[3] = ( byte ) value;
487 bytes[2] = ( byte ) ( value >> 8 );
488 bytes[1] = ( byte ) ( value >> 16 );
489 bytes[0] = ( byte ) ( value >> 24 );
490 }
491 else if ( ( value > FOUR_BYTE_MAX ) && ( value <= FIVE_BYTE_MAX ) )
492 {
493 bytes = new byte[5];
494 bytes[4] = ( byte ) value;
495 bytes[3] = ( byte ) ( value >> 8 );
496 bytes[2] = ( byte ) ( value >> 16 );
497 bytes[1] = ( byte ) ( value >> 24 );
498 bytes[0] = ( byte ) ( value >> 32 );
499 }
500 else if ( ( value > FIVE_BYTE_MAX ) && ( value <= SIX_BYTE_MAX ) )
501 {
502 bytes = new byte[6];
503 bytes[5] = ( byte ) value;
504 bytes[4] = ( byte ) ( value >> 8 );
505 bytes[3] = ( byte ) ( value >> 16 );
506 bytes[2] = ( byte ) ( value >> 24 );
507 bytes[1] = ( byte ) ( value >> 32 );
508 bytes[0] = ( byte ) ( value >> 40 );
509 }
510 else if ( ( value > SIX_BYTE_MAX ) && ( value <= SEVEN_BYTE_MAX ) )
511 {
512 bytes = new byte[7];
513 bytes[6] = ( byte ) value;
514 bytes[5] = ( byte ) ( value >> 8 );
515 bytes[4] = ( byte ) ( value >> 16 );
516 bytes[3] = ( byte ) ( value >> 24 );
517 bytes[2] = ( byte ) ( value >> 32 );
518 bytes[1] = ( byte ) ( value >> 40 );
519 bytes[0] = ( byte ) ( value >> 48 );
520 }
521 else
522 {
523 bytes = new byte[8];
524 bytes[7] = ( byte ) value;
525 bytes[6] = ( byte ) ( value >> 8 );
526 bytes[5] = ( byte ) ( value >> 16 );
527 bytes[4] = ( byte ) ( value >> 24 );
528 bytes[3] = ( byte ) ( value >> 32 );
529 bytes[2] = ( byte ) ( value >> 40 );
530 bytes[1] = ( byte ) ( value >> 48 );
531 bytes[0] = ( byte ) ( value >> 56 );
532 }
533 }
534 else
535 {
536
537 if ( value == 0x8000000000000000L )
538 {
539 bytes = new byte[8];
540 bytes[7] = ( byte ) 0x00;
541 bytes[6] = ( byte ) 0x00;
542 bytes[5] = ( byte ) 0x00;
543 bytes[4] = ( byte ) 0x00;
544 bytes[3] = ( byte ) 0x00;
545 bytes[2] = ( byte ) 0x00;
546 bytes[1] = ( byte ) 0x00;
547 bytes[0] = ( byte ) 0x80;
548 }
549 else
550 {
551
552 if ( value >= 0xFFFFFFFFFFFFFF80L )
553 {
554 bytes = new byte[1];
555 bytes[0] = ( byte ) value;
556 }
557 else if ( value >= 0xFFFFFFFFFFFF8000L )
558 {
559 bytes = new byte[2];
560 bytes[1] = ( byte ) ( value );
561 bytes[0] = ( byte ) ( value >> 8 );
562 }
563 else if ( value >= 0xFFFFFFFFFF800000L )
564 {
565 bytes = new byte[3];
566 bytes[2] = ( byte ) value;
567 bytes[1] = ( byte ) ( value >> 8 );
568 bytes[0] = ( byte ) ( value >> 16 );
569 }
570 else if ( value >= 0xFFFFFFFF80000000L )
571 {
572 bytes = new byte[4];
573 bytes[3] = ( byte ) value;
574 bytes[2] = ( byte ) ( value >> 8 );
575 bytes[1] = ( byte ) ( value >> 16 );
576 bytes[0] = ( byte ) ( value >> 24 );
577 }
578 else if ( value >= 0xFFFFFF8000000000L )
579 {
580 bytes = new byte[5];
581 bytes[4] = ( byte ) value;
582 bytes[3] = ( byte ) ( value >> 8 );
583 bytes[2] = ( byte ) ( value >> 16 );
584 bytes[1] = ( byte ) ( value >> 24 );
585 bytes[0] = ( byte ) ( value >> 32 );
586 }
587 else if ( value >= 0xFFFF800000000000L )
588 {
589 bytes = new byte[6];
590 bytes[5] = ( byte ) value;
591 bytes[4] = ( byte ) ( value >> 8 );
592 bytes[3] = ( byte ) ( value >> 16 );
593 bytes[2] = ( byte ) ( value >> 24 );
594 bytes[1] = ( byte ) ( value >> 32 );
595 bytes[0] = ( byte ) ( value >> 40 );
596 }
597 else if ( value >= 0xFF80000000000000L )
598 {
599 bytes = new byte[7];
600 bytes[6] = ( byte ) value;
601 bytes[5] = ( byte ) ( value >> 8 );
602 bytes[4] = ( byte ) ( value >> 16 );
603 bytes[3] = ( byte ) ( value >> 24 );
604 bytes[2] = ( byte ) ( value >> 32 );
605 bytes[1] = ( byte ) ( value >> 40 );
606 bytes[0] = ( byte ) ( value >> 48 );
607 }
608 else
609 {
610 bytes = new byte[8];
611 bytes[7] = ( byte ) value;
612 bytes[6] = ( byte ) ( value >> 8 );
613 bytes[5] = ( byte ) ( value >> 16 );
614 bytes[4] = ( byte ) ( value >> 24 );
615 bytes[3] = ( byte ) ( value >> 32 );
616 bytes[2] = ( byte ) ( value >> 40 );
617 bytes[1] = ( byte ) ( value >> 48 );
618 bytes[0] = ( byte ) ( value >> 56 );
619 }
620 }
621 }
622
623 return bytes;
624 }
625
626
627
628
629
630
631
632
633 public static void encodeBoolean( Asn1Buffer buffer, boolean bool )
634 {
635 if ( bool )
636 {
637 buffer.put( ENCODED_TRUE );
638 }
639 else
640 {
641 buffer.put( ENCODED_FALSE );
642 }
643 }
644
645
646
647
648
649
650
651
652
653 public static void encodeBoolean( Asn1Buffer buffer, byte tag, boolean bool )
654 {
655 if ( bool )
656 {
657 buffer.put( TRUE_VALUE );
658 buffer.put( ( byte ) 0x01 );
659 buffer.put( tag );
660 }
661 else
662 {
663 buffer.put( FALSE_VALUE );
664 buffer.put( ( byte ) 0x01 );
665 buffer.put( tag );
666 }
667 }
668
669
670
671
672
673
674
675
676 public static void encodeInteger( Asn1Buffer buffer, int value )
677 {
678 buffer.put( getBytes( value ) );
679 buffer.put( ( byte ) getNbBytes( value ) );
680 buffer.put( UniversalTag.INTEGER.getValue() );
681 }
682
683
684
685
686
687
688
689
690
691 public static void encodeInteger( Asn1Buffer buffer, byte tag, int value )
692 {
693 buffer.put( getBytes( value ) );
694 buffer.put( ( byte ) getNbBytes( value ) );
695 buffer.put( tag );
696 }
697
698
699
700
701
702
703
704
705 public static void encodeInteger( Asn1Buffer buffer, long value )
706 {
707 buffer.put( getBytes( value ) );
708 buffer.put( ( byte ) getNbBytes( value ) );
709 buffer.put( UniversalTag.INTEGER.getValue() );
710 }
711
712
713
714
715
716
717
718
719
720 public static void encodeInteger( Asn1Buffer buffer, byte tag, long value )
721 {
722 buffer.put( getBytes( value ) );
723 buffer.put( ( byte ) getNbBytes( value ) );
724 buffer.put( tag );
725 }
726
727
728
729
730
731
732
733
734 public static void encodeOctetString( Asn1Buffer buffer, byte[] data )
735 {
736 if ( Strings.isEmpty( data ) )
737 {
738 buffer.put( ( byte ) 0 );
739 }
740 else
741 {
742 buffer.put( data );
743 buffer.put( TLV.getBytes( data.length ) );
744 }
745
746 buffer.put( UniversalTag.OCTET_STRING.getValue() );
747 }
748
749
750
751
752
753
754
755
756 public static void encodeOctetString( Asn1Buffer buffer, String value )
757 {
758 if ( Strings.isEmpty( value ) )
759 {
760 buffer.put( ( byte ) 0 );
761 }
762 else
763 {
764 byte[] bytes = Strings.getBytesUtf8Ascii( value );
765 buffer.put( bytes );
766 buffer.put( TLV.getBytes( bytes.length ) );
767 }
768
769 buffer.put( UniversalTag.OCTET_STRING.getValue() );
770 }
771
772
773
774
775
776
777
778
779
780 public static void encodeOctetString( Asn1Buffer buffer, byte tag, byte[] data )
781 {
782 if ( Strings.isEmpty( data ) )
783 {
784 buffer.put( ( byte ) 0 );
785 }
786 else
787 {
788 buffer.put( data );
789 buffer.put( TLV.getBytes( data.length ) );
790 }
791
792 buffer.put( tag );
793 }
794
795
796
797
798
799
800
801
802
803 public static void encodeOctetString( Asn1Buffer buffer, byte tag, String value )
804 {
805 if ( Strings.isEmpty( value ) )
806 {
807 buffer.put( ( byte ) 0 );
808 }
809 else
810 {
811 byte[] bytes = Strings.getBytesUtf8Ascii( value );
812 buffer.put( bytes );
813 buffer.put( TLV.getBytes( bytes.length ) );
814 }
815
816 buffer.put( tag );
817 }
818
819
820
821
822
823
824
825 public static void encodeSequence( Asn1Buffer buffer )
826 {
827 buffer.put( TLV.getBytes( buffer.getPos() ) );
828 buffer.put( UniversalTag.SEQUENCE.getValue() );
829 }
830
831
832
833
834
835
836
837
838 public static void encodeSequence( Asn1Buffer buffer, int start )
839 {
840 buffer.put( TLV.getBytes( buffer.getPos() - start ) );
841 buffer.put( UniversalTag.SEQUENCE.getValue() );
842 }
843
844
845
846
847
848
849
850
851 public static void encodeSequence( Asn1Buffer buffer, byte tag )
852 {
853 buffer.put( TLV.getBytes( buffer.getPos() ) );
854 buffer.put( tag );
855 }
856
857
858
859
860
861
862
863
864
865 public static void encodeSequence( Asn1Buffer buffer, byte tag, int start )
866 {
867 buffer.put( TLV.getBytes( buffer.getPos() - start ) );
868 buffer.put( tag );
869 }
870
871
872
873
874
875
876
877 public static void encodeSet( Asn1Buffer buffer )
878 {
879 buffer.put( TLV.getBytes( buffer.getPos() ) );
880 buffer.put( UniversalTag.SET.getValue() );
881 }
882
883
884
885
886
887
888
889
890 public static void encodeSet( Asn1Buffer buffer, int start )
891 {
892 buffer.put( TLV.getBytes( buffer.getPos() - start ) );
893 buffer.put( UniversalTag.SET.getValue() );
894 }
895
896
897
898
899
900
901
902
903 public static void encodeSet( Asn1Buffer buffer, byte tag )
904 {
905 buffer.put( TLV.getBytes( buffer.getPos() ) );
906 buffer.put( tag );
907 }
908
909
910
911
912
913
914
915
916
917 public static void encodeSet( Asn1Buffer buffer, byte tag, int start )
918 {
919 buffer.put( TLV.getBytes( buffer.getPos() - start ) );
920 buffer.put( tag );
921 }
922
923
924
925
926
927
928
929
930
931
932 public static void encode( ByteBuffer buffer, String string ) throws EncoderException
933 {
934 if ( buffer == null )
935 {
936 throw new EncoderException( I18n.err( I18n.ERR_01300_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
937 }
938
939 try
940 {
941 buffer.put( UniversalTag.OCTET_STRING.getValue() );
942
943 byte[] value = Asn1StringUtils.getBytesUtf8( string );
944
945 buffer.put( TLV.getBytes( value.length ) );
946
947 if ( value.length != 0 )
948 {
949 buffer.put( value );
950 }
951 }
952 catch ( BufferOverflowException boe )
953 {
954 throw new EncoderException( I18n.err( I18n.ERR_01301_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
955 }
956 }
957
958
959
960
961
962
963
964
965
966
967 public static void encode( ByteBuffer buffer, BitString bitString ) throws EncoderException
968 {
969 if ( buffer == null )
970 {
971 throw new EncoderException( I18n.err( I18n.ERR_01300_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
972 }
973
974 try
975 {
976 buffer.put( UniversalTag.BIT_STRING.getValue() );
977
978
979
980 byte[] bytes = bitString.getData();
981 int length = bytes.length;
982
983 buffer.put( TLV.getBytes( length ) );
984 buffer.put( bytes );
985 }
986 catch ( BufferOverflowException boe )
987 {
988 throw new EncoderException( I18n.err( I18n.ERR_01301_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
989 }
990 }
991
992
993
994
995
996
997
998
999
1000
1001 public static void encode( ByteBuffer buffer, byte[] bytes ) throws EncoderException
1002 {
1003 if ( buffer == null )
1004 {
1005 throw new EncoderException( I18n.err( I18n.ERR_01300_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
1006 }
1007
1008 try
1009 {
1010 buffer.put( UniversalTag.OCTET_STRING.getValue() );
1011
1012 if ( ( bytes == null ) || ( bytes.length == 0 ) )
1013 {
1014 buffer.put( ( byte ) 0 );
1015 }
1016 else
1017 {
1018 buffer.put( TLV.getBytes( bytes.length ) );
1019 buffer.put( bytes );
1020 }
1021 }
1022 catch ( BufferOverflowException boe )
1023 {
1024 throw new EncoderException( I18n.err( I18n.ERR_01301_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
1025 }
1026 }
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037 public static void encode( ByteBuffer buffer, Oid oid ) throws EncoderException
1038 {
1039 if ( buffer == null )
1040 {
1041 throw new EncoderException( I18n.err( I18n.ERR_01300_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
1042 }
1043
1044 try
1045 {
1046 buffer.put( UniversalTag.OCTET_STRING.getValue() );
1047 buffer.put( TLV.getBytes( oid.getEncodedLength() ) );
1048
1049 if ( oid.getEncodedLength() != 0 )
1050 {
1051 oid.writeBytesTo( buffer );
1052 }
1053 }
1054 catch ( BufferOverflowException boe )
1055 {
1056 throw new EncoderException( I18n.err( I18n.ERR_01301_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
1057 }
1058 }
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069 public static void encode( ByteBuffer buffer, int value ) throws EncoderException
1070 {
1071 if ( buffer == null )
1072 {
1073 throw new EncoderException( I18n.err( I18n.ERR_01300_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
1074 }
1075
1076 try
1077 {
1078 buffer.put( UniversalTag.INTEGER.getValue() );
1079 buffer.put( ( byte ) getNbBytes( value ) );
1080 buffer.put( getBytes( value ) );
1081 }
1082 catch ( BufferOverflowException boe )
1083 {
1084 throw new EncoderException( I18n.err( I18n.ERR_01301_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
1085 }
1086 }
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097 public static void encode( ByteBuffer buffer, long value ) throws EncoderException
1098 {
1099 if ( buffer == null )
1100 {
1101 throw new EncoderException( I18n.err( I18n.ERR_01300_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
1102 }
1103
1104 try
1105 {
1106 buffer.put( UniversalTag.INTEGER.getValue() );
1107 buffer.put( ( byte ) getNbBytes( value ) );
1108 buffer.put( getBytes( value ) );
1109 }
1110 catch ( BufferOverflowException boe )
1111 {
1112 throw new EncoderException( I18n.err( I18n.ERR_01301_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
1113 }
1114 }
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126 public static void encode( ByteBuffer buffer, byte tag, int value ) throws EncoderException
1127 {
1128 if ( buffer == null )
1129 {
1130 throw new EncoderException( I18n.err( I18n.ERR_01300_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
1131 }
1132
1133 try
1134 {
1135 buffer.put( tag );
1136 buffer.put( ( byte ) getNbBytes( value ) );
1137 buffer.put( getBytes( value ) );
1138 }
1139 catch ( BufferOverflowException boe )
1140 {
1141 throw new EncoderException( I18n.err( I18n.ERR_01301_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
1142 }
1143 }
1144
1145
1146
1147
1148
1149
1150
1151
1152 public static void encodeEnumerated( Asn1Buffer buffer, int value )
1153 {
1154 buffer.put( getBytes( value ) );
1155 buffer.put( TLV.getBytes( getNbBytes( value ) ) );
1156 buffer.put( UniversalTag.ENUMERATED.getValue() );
1157 }
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167 public static void encodeEnumerated( Asn1Buffer buffer, byte tag, int value )
1168 {
1169 buffer.put( getBytes( value ) );
1170 buffer.put( TLV.getBytes( getNbBytes( value ) ) );
1171 buffer.put( tag );
1172 }
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183 public static void encodeEnumerated( ByteBuffer buffer, int value ) throws EncoderException
1184 {
1185 if ( buffer == null )
1186 {
1187 throw new EncoderException( I18n.err( I18n.ERR_01300_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
1188 }
1189
1190 try
1191 {
1192 buffer.put( UniversalTag.ENUMERATED.getValue() );
1193 buffer.put( TLV.getBytes( getNbBytes( value ) ) );
1194 buffer.put( getBytes( value ) );
1195 }
1196 catch ( BufferOverflowException boe )
1197 {
1198 throw new EncoderException( I18n.err( I18n.ERR_01301_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
1199 }
1200 }
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211 public static void encode( ByteBuffer buffer, boolean bool ) throws EncoderException
1212 {
1213 if ( buffer == null )
1214 {
1215 throw new EncoderException( I18n.err( I18n.ERR_01300_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
1216 }
1217
1218 try
1219 {
1220 if ( bool )
1221 {
1222 buffer.put( ENCODED_TRUE );
1223 }
1224 else
1225 {
1226 buffer.put( ENCODED_FALSE );
1227 }
1228 }
1229 catch ( BufferOverflowException boe )
1230 {
1231 throw new EncoderException( I18n.err( I18n.ERR_01301_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
1232 }
1233 }
1234
1235
1236
1237
1238
1239
1240
1241 @Override
1242 public String toString()
1243 {
1244 StringBuilder sb = new StringBuilder();
1245 sb.append( "DATA" );
1246
1247 if ( data != null )
1248 {
1249 sb.append( '[' );
1250 sb.append( Asn1StringUtils.dumpBytes( data ) );
1251 sb.append( ']' );
1252 }
1253 else
1254 {
1255 return "[]";
1256 }
1257
1258 return sb.toString();
1259 }
1260 }