OSDN Git Service

The first commit.
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / codec / ber / BerOctets.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.codec.ber;
17
18 import java.io.IOException;
19 import java.io.OutputStream;
20
21 /**
22  * Abstract base class for classes represent BER encoded octets.
23  * 
24  * @author WATANABE, Jun. <jwat at bitmeister.jp>
25  */
26 abstract class BerOctets {
27
28         private byte[] identifier = new byte[0];
29
30         private byte[] length = new byte[0];
31
32         /**
33          * Returns BER encoded octets length includes prefix and contents.
34          * 
35          * @return Total length of BER encoded octets.
36          */
37         int totalLength() {
38                 return identifier.length + length.length + octetsLength();
39         }
40
41         /**
42          * Sets identifier and length octets.
43          * 
44          * @param identifier
45          *            BER encoded identifier octets.
46          * @param length
47          *            BER encoded contents length.
48          */
49         void setPrefix(byte[] identifier, byte[] length) {
50                 this.identifier = identifier;
51                 this.length = length;
52         }
53
54         /**
55          * Writes all BER encoded octets to the {@code OutputStream}
56          * 
57          * @param out
58          *            The stream to be written.
59          * @throws IOException
60          *             when {@code IOException} thrown by {@code OutputStream}.
61          */
62         void write(OutputStream out) throws IOException {
63                 out.write(identifier);
64                 out.write(length);
65                 writeContents(out);
66         }
67
68         /**
69          * Returns this instance is constructed or not.
70          * 
71          * @return {@code true} if this instance is constructed.
72          */
73         abstract boolean isConstructed();
74
75         /**
76          * Returns length of contents octets.
77          * 
78          * @return contents octets length.
79          */
80         abstract int octetsLength();
81
82         /**
83          * Writes contents octets to the {@code OutputStream}.
84          * 
85          * @param out
86          *            The stream to be written.
87          * @throws IOException
88          *             when {@code OutputStream} throws {@code IOException}.
89          */
90         abstract void writeContents(OutputStream out) throws IOException;
91
92 }