1
2
3
4
5
6
7
8
9
10
11
12
13
14 package com.addthis.codec.binary;
15
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
18 import java.io.OutputStream;
19
20 import java.util.Stack;
21
22 import com.addthis.basis.util.Bytes;
23
24 final class BufferOut {
25
26 ByteArrayOutputStream out;
27 Stack<ByteArrayOutputStream> stack;
28
29 BufferOut() {
30 stack = new Stack<>();
31 push();
32 }
33
34 public OutputStream out() {
35 return out;
36 }
37
38 public void push() {
39 stack.push(new ByteArrayOutputStream());
40 out = stack.peek();
41 }
42
43 public void pop() throws IOException {
44 ByteArrayOutputStream last = stack.pop();
45 out = stack.peek();
46 Bytes.writeLength(last.size(), out());
47 last.writeTo(out());
48 }
49
50 @Override
51 public String toString() {
52 return "BufferOut:" + (out != null ? out.size() : -1);
53 }
54 }