OSDN Git Service

006391d118309cdb19b173a534037f8e9525b49b
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / value / HexString.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.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 \n]*")) {
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                 StringBuilder builder = new StringBuilder();
52                 for (char c: string.toCharArray()) {
53                         if (Character.digit(c, 16) >= 0) {
54                                 builder.append(c);
55                         }
56                 }
57                 this.string = builder.toString().trim().toUpperCase();
58         }
59
60         /**
61          * Instantiates a {@code HexString} and initialize it with an array of
62          * {@code byte}.
63          * 
64          * @param array
65          *            The array of {@code byte}.
66          */
67         public HexString(byte... array) {
68                 StringBuilder builder = new StringBuilder();
69                 for (Byte b : array) {
70                         builder.append(String.format("%02X", b));
71                 }
72                 string = builder.toString();
73         }
74
75         /**
76          * Returns the string value of this hString.
77          * 
78          * @return The hexadecimal string.
79          */
80         public String string() {
81                 return string;
82         }
83
84         /**
85          * Converts the value of this hString to an array of {@code boolean}.
86          * 
87          * @return An array of {@code boolean}.
88          */
89         public boolean[] toBinArray() {
90                 boolean[] array = new boolean[string.length() * 4];
91                 int index = 0, mask = 0;
92                 byte octet = 0;
93                 for (int i = 0; i < array.length; i++) {
94                         if (mask == 0) {
95                                 octet = Byte.parseByte(string.substring(index++, index), 16);
96                                 mask = 8;
97                         }
98                         array[i] = (octet & mask) > 0;
99                         mask >>>= 1;
100                 }
101                 return array;
102         }
103
104         /**
105          * Converts the value of this hString to an array of {@code byte}.
106          * 
107          * @return An array of {@code byte}.
108          */
109         public byte[] toByteArray() {
110                 byte[] array = new byte[string.length() / 2 + string.length() % 2];
111                 byte octet = 0;
112                 for (int i = 0; i < string.length(); i++) {
113                         octet = Integer.valueOf(string.substring(i, i + 1), 16).byteValue();
114                         if (i % 2 == 0) {
115                                 array[i / 2] = (byte) (octet << 4);
116                         } else {
117                                 array[i / 2] = (byte) (array[i / 2] + octet);
118                         }
119                 }
120                 return array;
121         }
122
123         /**
124          * Returns a string representation of this item.
125          * 
126          * @return A string representation of this item.
127          */
128         @Override
129         public String toString() {
130                 return '\'' + string + "\'H";
131         }
132
133 }