OSDN Git Service

modifications for Java1.5
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / value / HexString.java
1 /*
2  * Copyright 2011 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.value;
17
18 import jp.bitmeister.asn1.exception.ASN1IllegalArgument;
19
20 /**
21  * Represents ASN.1 'Hex string(hString)' item.
22  * 
23  * <p>
24  * An instance of this class represents a 'hString' item.
25  * </p>
26  * 
27  * @author WATANABE, Jun. <jwat at bitmeister.jp>
28  */
29 public class HexString implements StringItem {
30
31         /**
32          * The hexadecimal string.
33          */
34         private String string;
35
36         /**
37          * Instantiates a {@code HexString} and initialize it with a {@code String}.
38          * 
39          * @param string
40          *            The {@code String} that consist of an arbitrary numbers of
41          *            hexadecimal characters.
42          */
43         public HexString(String string) {
44                 if (!string.matches("[0-9A-Fa-f]*")) {
45                         ASN1IllegalArgument ex = new ASN1IllegalArgument();
46                         ex.setMessage("Invalid string '" + string
47                                         + "'. hString must consist of hexadecimal string.", null,
48                                         null, null, null);
49                         throw ex;
50                 }
51                 this.string = string.trim().toUpperCase();
52         }
53
54         /**
55          * Instantiates a {@code HexString} and initialize it with an array of
56          * {@code byte}.
57          * 
58          * @param array
59          *            The array of {@code byte}.
60          */
61         public HexString(byte... array) {
62                 StringBuilder builder = new StringBuilder();
63                 for (Byte b : array) {
64                         builder.append(String.format("%02X", b));
65                 }
66                 string = builder.toString();
67         }
68
69         /**
70          * Returns the string value of this hString.
71          * 
72          * @return The hexadecimal string.
73          */
74         public String string() {
75                 return string;
76         }
77
78         /**
79          * Converts the value of this hString to an array of {@code boolean}.
80          * 
81          * @return An array of {@code boolean}.
82          */
83         public boolean[] toBinArray() {
84                 boolean[] array = new boolean[string.length() * 4];
85                 int index = 0, mask = 0;
86                 byte octet = 0;
87                 for (int i = 0; i < array.length; i++) {
88                         if (mask == 0) {
89                                 octet = Byte.parseByte(string.substring(index++, index), 16);
90                                 mask = 8;
91                         }
92                         array[i] = (octet & mask) > 0;
93                         mask >>>= 1;
94                 }
95                 return array;
96         }
97
98         /**
99          * Converts the value of this hString to an array of {@code byte}.
100          * 
101          * @return An array of {@code byte}.
102          */
103         public byte[] toByteArray() {
104                 byte[] array = new byte[string.length() / 2 + string.length() % 2];
105                 byte octet = 0;
106                 for (int i = 0; i < string.length(); i++) {
107                         octet = Integer.valueOf(string.substring(i, i + 1), 16).byteValue();
108                         if (i % 2 == 0) {
109                                 array[i / 2] = (byte) (octet << 4);
110                         } else {
111                                 array[i / 2] = (byte) (array[i / 2] + octet);
112                         }
113                 }
114                 return array;
115         }
116
117         /**
118          * Returns a string representation of this item.
119          * 
120          * @return A string representation of this item.
121          */
122         @Override
123         public String toString() {
124                 return '\'' + string + "\'H";
125         }
126
127 }