OSDN Git Service

5dc740f944d0c87a87dc104edf0ef77b9b0dabb6
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / type / builtin / OCTET_STRING.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.type.builtin;
17
18 import java.util.Arrays;
19
20 import jp.bitmeister.asn1.annotation.ASN1BuiltIn;
21 import jp.bitmeister.asn1.annotation.ASN1Identifier;
22 import jp.bitmeister.asn1.annotation.ASN1Tag;
23 import jp.bitmeister.asn1.annotation.ASN1XmlTypeName;
24 import jp.bitmeister.asn1.exception.ASN1IllegalArgument;
25 import jp.bitmeister.asn1.processor.ASN1Visitor;
26 import jp.bitmeister.asn1.type.ASN1TagClass;
27 import jp.bitmeister.asn1.type.ASN1TagMode;
28 import jp.bitmeister.asn1.type.Concatenatable;
29 import jp.bitmeister.asn1.type.PrimitiveType;
30 import jp.bitmeister.asn1.type.SizeCountable;
31 import jp.bitmeister.asn1.value.StringItem;
32
33 /**
34  * Represents ASN.1 'OCTET STRING' type.
35  * 
36  * <p>
37  * An instance of this class represents an 'OCTET STRING' type data, and has an
38  * array of {@code byte} value.
39  * </p>
40  * 
41  * @author WATANABE, Jun. <jwat at bitmeister.jp>
42  */
43 @ASN1BuiltIn
44 @ASN1Identifier("OCTET STRING")
45 @ASN1XmlTypeName("OCTET_STRING")
46 @ASN1Tag(tagClass = ASN1TagClass.UNIVERSAL, value = 4, tagMode = ASN1TagMode.IMPLICIT)
47 public class OCTET_STRING extends PrimitiveType<byte[]> implements
48                 Concatenatable<OCTET_STRING>, SizeCountable {
49
50         /**
51          * Instantiates an empty {@code OCTET_STRING}.
52          */
53         public OCTET_STRING() {
54         }
55
56         /**
57          * Instantiates an {@code OCTET_STRING} and initialize it with the array of
58          * {@code byte} value.
59          * 
60          * @param value
61          *            The value assigned to the instance.
62          */
63         public OCTET_STRING(byte... value) {
64                 set(value);
65         }
66
67         /**
68          * Instantiates an {@code OCTET_STRING} and initialize it with the
69          * {@code StringItem}.
70          * 
71          * @param item
72          *            The value assigned to the instance.
73          */
74         public OCTET_STRING(StringItem item) {
75                 set(item);
76         }
77
78         /**
79          * Sets the {@code StringItem} value to the instance.
80          * 
81          * @param item
82          *            The value assigned to the instance.
83          */
84         public void set(StringItem item) {
85                 set(item.toByteArray());
86         }
87
88         /*
89          * (non-Javadoc)
90          * 
91          * @see jp.bitmeister.asn1.type.Concatenatable#concatenate(null)
92          */
93         @Override
94         public void concatenate(OCTET_STRING data) {
95                 if (data == null) {
96                         return;
97                 }
98                 if (!getClass().equals(data.getClass())) {
99                         ASN1IllegalArgument ex = new ASN1IllegalArgument();
100                         ex.setMessage(
101                                         "The type '"
102                                                         + data.specification().fullIdentifier()
103                                                         + "' of the data to be concatenated is not the same type of this instance.",
104                                         null, getClass(), null, null);
105                         throw ex;
106                 }
107                 if (data.hasValue()) {
108                         if (!hasValue()) {
109                                 set(data.value());
110                         } else {
111                                 byte[] newValue = new byte[value().length + data.value().length];
112                                 System.arraycopy(value(), 0, newValue, 0, value().length);
113                                 System.arraycopy(data.value(), 0, newValue, value().length,
114                                                 data.value().length);
115                                 set(newValue);
116                         }
117                 }
118         }
119
120         /*
121          * (non-Javadoc)
122          * 
123          * @see jp.bitmeister.asn1.type.SizeCountable#size()
124          */
125         @Override
126         public int size() {
127                 return value().length;
128         }
129
130         /*
131          * (non-Javadoc)
132          * 
133          * @see jp.bitmeister.asn1.type.PrimitiveType#cloneValue()
134          */
135         @Override
136         protected byte[] cloneValue() {
137                 return value().clone();
138         }
139
140         /*
141          * (non-Javadoc)
142          * 
143          * @see jp.bitmeister.asn1.type.PrimitiveType#valueEquals(java.lang.Object)
144          */
145         @Override
146         public boolean valueEquals(Object other) {
147                 if (other instanceof OCTET_STRING) {
148                         byte[] otherValue = ((OCTET_STRING) other).value();
149                         if (value() != null) {
150                                 return Arrays.equals(value(), otherValue);
151                         }
152                         return otherValue == null;
153                 }
154                 return false;
155         }
156
157         /*
158          * (non-Javadoc)
159          * 
160          * @see
161          * jp.bitmeister.asn1.type.ASN1Type#accept(jp.bitmeister.asn1.processor.
162          * ASN1Visitor)
163          */
164         @Override
165         public <R, E extends Throwable> R accept(ASN1Visitor<R, E> visitor) throws E {
166                 return visitor.visit(this);
167         }
168
169 }