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.codec.ber;
18 import java.io.IOException;
19 import java.io.OutputStream;
22 * Abstract base class for classes represent BER encoded octets.
24 * @author WATANABE, Jun. <jwat at bitmeister.jp>
26 abstract class BerOctets {
28 private byte[] identifier = new byte[0];
30 private byte[] length = new byte[0];
33 * Returns BER encoded octets length includes prefix and contents.
35 * @return Total length of BER encoded octets.
38 return identifier.length + length.length + octetsLength();
42 * Sets identifier and length octets.
45 * BER encoded identifier octets.
47 * BER encoded contents length.
49 void setPrefix(byte[] identifier, byte[] length) {
50 this.identifier = identifier;
55 * Writes all BER encoded octets to the {@code OutputStream}
58 * The stream to be written.
60 * when {@code IOException} thrown by {@code OutputStream}.
62 void write(OutputStream out) throws IOException {
63 out.write(identifier);
69 * Returns this instance is constructed or not.
71 * @return {@code true} if this instance is constructed.
73 abstract boolean isConstructed();
76 * Returns length of contents octets.
78 * @return contents octets length.
80 abstract int octetsLength();
83 * Writes contents octets to the {@code OutputStream}.
86 * The stream to be written.
88 * when {@code OutputStream} throws {@code IOException}.
90 abstract void writeContents(OutputStream out) throws IOException;