View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package com.addthis.codec.binary;
15  
16  import java.io.ByteArrayInputStream;
17  import java.io.IOException;
18  
19  import java.util.Stack;
20  
21  import com.addthis.basis.util.Bytes;
22  
23  final class BufferIn {
24  
25      ByteArrayInputStream        in;
26      Stack<ByteArrayInputStream> stack;
27  
28      BufferIn(final byte[] data) throws IOException {
29          stack = new Stack<>();
30          in = new ByteArrayInputStream(data);
31      }
32  
33      public void push() throws IOException {
34          int len = (int) Bytes.readLength(in);
35          byte[] ndat = Bytes.readBytes(in, len);
36          stack.push(in);
37          in = new ByteArrayInputStream(ndat);
38      }
39  
40      public void pop() {
41          in = stack.pop();
42      }
43  
44      @Override
45      public String toString() {
46          return "BufferIn:" + (in != null ? in.available() : -1);
47      }
48  }