2 * Copyright 2011 BitMeister Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package jp.bitmeister.asn1.type.builtin;
18 import java.util.Arrays;
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;
34 * Represents ASN.1 'OCTET STRING' type.
37 * An instance of this class represents an 'OCTET STRING' type data, and has an
38 * array of {@code byte} value.
41 * @author WATANABE, Jun. <jwat at bitmeister.jp>
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 {
51 * Instantiates an empty {@code OCTET_STRING}.
53 public OCTET_STRING() {
57 * Instantiates an {@code OCTET_STRING} and initialize it with the array of
61 * The value assigned to the instance.
63 public OCTET_STRING(byte... value) {
68 * Instantiates an {@code OCTET_STRING} and initialize it with the
72 * The value assigned to the instance.
74 public OCTET_STRING(StringItem item) {
79 * Sets the {@code StringItem} value to the instance.
82 * The value assigned to the instance.
84 public void set(StringItem item) {
85 set(item.toByteArray());
91 * @see jp.bitmeister.asn1.type.Concatenatable#concatenate(null)
93 public void concatenate(OCTET_STRING data) {
97 if (!getClass().equals(data.getClass())) {
98 ASN1IllegalArgument ex = new ASN1IllegalArgument();
101 + data.specification().fullIdentifier()
102 + "' of the data to be concatenated is not the same type of this instance.",
103 null, getClass(), null, null);
106 if (data.hasValue()) {
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);
122 * @see jp.bitmeister.asn1.type.SizeCountable#size()
125 return value().length;
131 * @see jp.bitmeister.asn1.type.PrimitiveType#cloneValue()
134 protected byte[] cloneValue() {
135 return value().clone();
141 * @see jp.bitmeister.asn1.type.PrimitiveType#valueEquals(java.lang.Object)
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);
150 return otherValue == null;
158 * @see jp.bitmeister.asn1.type.PrimitiveType#hashCode()
161 public int hashCode() {
162 return Arrays.hashCode(value());
169 * jp.bitmeister.asn1.type.ASN1Type#accept(jp.bitmeister.asn1.processor.
173 public <R, E extends Throwable> R accept(ASN1Visitor<R, E> visitor) throws E {
174 return visitor.visit(this);