OSDN Git Service

Optimize byte[]->String/String->byte[]
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / sample / FrightStatusMain.java
1 /*
2  * Copyright 2011-2012 BitMeister Inc.
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 package jp.bitmeister.asn1.sample;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20
21 import jp.bitmeister.asn1.codec.ber.BerDecoder;
22 import jp.bitmeister.asn1.codec.ber.BerEncoder;
23 import jp.bitmeister.asn1.codec.ber.DerEncoder;
24 import jp.bitmeister.asn1.codec.xer.XerDecoder;
25 import jp.bitmeister.asn1.codec.xer.XerEncoder;
26 import jp.bitmeister.asn1.exception.ASN1DecodingException;
27 import jp.bitmeister.asn1.exception.ASN1EncodingException;
28 import jp.bitmeister.asn1.sample.FrightStatusTypes.Airport;
29 import jp.bitmeister.asn1.sample.FrightStatusTypes.AllFrights;
30 import jp.bitmeister.asn1.sample.FrightStatusTypes.FrightNumber;
31 import jp.bitmeister.asn1.sample.FrightStatusTypes.Information;
32 import jp.bitmeister.asn1.sample.FrightStatusTypes.Status;
33 import jp.bitmeister.asn1.type.ASN1Type;
34 import jp.bitmeister.asn1.type.builtin.INTEGER;
35 import jp.bitmeister.asn1.type.useful.UTCTime;
36 import jp.bitmeister.asn1.value.HexString;
37
38 public class FrightStatusMain {
39
40         public static void main(String[] args) {
41                 AllFrights frights = new AllFrights(
42                                 new FrightStatus(
43                                                 new FrightNumber("JP041"),
44                                                 new Information(
45                                                                 new Airport(Airport.tokyo),
46                                                                 new UTCTime("110627073000"),
47                                                                 new UTCTime("110627073000")
48                                                 ),
49                                                 new Information(
50                                                                 new Airport(Airport.osaka),
51                                                                 new UTCTime("110627090000"),
52                                                                 null
53                                                 )
54                                 ),
55                                 new FrightStatus(
56                                                 new FrightNumber("NI022"),
57                                                 new Information(
58                                                                 new Airport(Airport.fukuoka),
59                                                                 new UTCTime("110627080000"),
60                                                                 new UTCTime("110627081000")
61                                                 ),
62                                                 new Information(
63                                                                 new Airport(Airport.nagoya),
64                                                                 new UTCTime("110627093000"),
65                                                                 null
66                                                 ),
67                                                 new Status(new INTEGER(10))
68                                                 )
69                                 );
70
71                 System.out.println("Source data:");
72                 System.out.println(frights);
73                 derEncAndDec(frights);
74                 xerEncAndDec(frights);
75         }
76
77         static void derEncAndDec(ASN1Type data) {
78                 System.out.println("BER encoding:");
79                 ByteArrayOutputStream bo = new ByteArrayOutputStream();
80                 BerEncoder enc = new DerEncoder(bo);
81                 try {
82                         enc.encode(data);
83                 } catch (ASN1EncodingException e) {
84                         e.printStackTrace();
85                 }
86
87                 System.out.println(new HexString(bo.toByteArray()));
88
89                 ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
90                 BerDecoder dec = new BerDecoder(FrightStatusTypes.class, bi);
91                 try {
92                         ASN1Type result = dec.decode();
93                         System.out.println("BER decoding:");
94                         System.out.println(result);
95                 } catch (ASN1DecodingException e) {
96                         e.printStackTrace();
97                 }
98
99         }
100
101         static void xerEncAndDec(ASN1Type data) {
102
103                 System.out.println("XER encoding:");
104                 ByteArrayOutputStream bo = new ByteArrayOutputStream();
105                 XerEncoder enc = new XerEncoder(bo);
106                 try {
107                         enc.encode(data);
108                 } catch (ASN1EncodingException e) {
109                         e.printStackTrace();
110                 }
111                 System.out.println(new String(bo.toByteArray()));
112
113                 ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
114                 XerDecoder dec = new XerDecoder(FrightStatusTypes.class, bi);
115                 try {
116                         ASN1Type result = dec.decode();
117                         System.out.println("XER decoding:");
118                         System.out.println(result);
119                 } catch (ASN1DecodingException e) {
120                         e.printStackTrace();
121                 }
122
123         }
124
125 }