OSDN Git Service

modifications for Java1.5
[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         public void concatenate(OCTET_STRING data) {
94                 if (data == null) {
95                         return;
96                 }
97                 if (!getClass().equals(data.getClass())) {
98                         ASN1IllegalArgument ex = new ASN1IllegalArgument();
99                         ex.setMessage(
100                                         "The type '"
101                                                         + data.specification().fullIdentifier()
102                                                         + "' of the data to be concatenated is not the same type of this instance.",
103                                         null, getClass(), null, null);
104                         throw ex;
105                 }
106                 if (data.hasValue()) {
107                         if (!hasValue()) {
108                                 set(data.value());
109                         } else {
110                                 byte[] newValue = new byte[value().length + data.value().length];
111                                 System.arraycopy(value(), 0, newValue, 0, value().length);
112                                 System.arraycopy(data.value(), 0, newValue, value().length,
113                                                 data.value().length);
114                                 set(newValue);
115                         }
116                 }
117         }
118
119         /*
120          * (non-Javadoc)
121          * 
122          * @see jp.bitmeister.asn1.type.SizeCountable#size()
123          */
124         public int size() {
125                 return value().length;
126         }
127
128         /*
129          * (non-Javadoc)
130          * 
131          * @see jp.bitmeister.asn1.type.PrimitiveType#cloneValue()
132          */
133         @Override
134         protected byte[] cloneValue() {
135                 return value().clone();
136         }
137
138         /*
139          * (non-Javadoc)
140          * 
141          * @see jp.bitmeister.asn1.type.PrimitiveType#valueEquals(java.lang.Object)
142          */
143         @Override
144         public boolean valueEquals(Object other) {
145                 if (other instanceof OCTET_STRING) {
146                         byte[] otherValue = ((OCTET_STRING) other).value();
147                         if (value() != null) {
148                                 return Arrays.equals(value(), otherValue);
149                         }
150                         return otherValue == null;
151                 }
152                 return false;
153         }
154
155         /* 
156          * (non-Javadoc)
157          * 
158          * @see jp.bitmeister.asn1.type.PrimitiveType#hashCode()
159          */
160         @Override
161         public int hashCode() {
162                 return Arrays.hashCode(value());
163         }
164
165         /*
166          * (non-Javadoc)
167          * 
168          * @see
169          * jp.bitmeister.asn1.type.ASN1Type#accept(jp.bitmeister.asn1.processor.
170          * ASN1Visitor)
171          */
172         @Override
173         public <R, E extends Throwable> R accept(ASN1Visitor<R, E> visitor) throws E {
174                 return visitor.visit(this);
175         }
176
177 }