Debian Codegraph @

src/main/java/org/freehep/util/io/ByteCountOutputStream.java

raw
1 // Copyright 2001, FreeHEP.
2 package org.freehep.util.io;
3
4 import java.io.IOException;
5 import java.io.OutputStream;
6 import java.util.LinkedList;
7 import java.util.List;
8
9 /**
10 * Allows to write to some internal buffer and keep count of any set of stacked
11 * buffers written. When a pushBuffer is called, a new buffer is created. When a
12 * popBuffer is called, the number of bytes written to the popped buffer is
13 * returned. From this moment on one is writing again to the underlying
14 * buffer/outputstream. A header can be written and the header can be written.
15 * By calling append(), the previous buffer will be appended to the underlying
16 * one. This way one can write into a buffer, retrieve its length, create some
17 * header bytes and insert those in front of the just written buffer.
18 *
19 * @author Mark Donszelmann
20 * @author Ian Graham - added popBufferBytes() for use by CGMOutputStream
21 * @version $Id: ByteCountOutputStream.java 8584 2006-08-10 23:06:37Z duns $
22 */
23 public class ByteCountOutputStream extends ByteOrderOutputStream {
24
25 private int currentBuffer;
26
27 private List bufferList;
28
29 /**
30 * Create a Byte Count output stream from given stream
31 *
32 * @param out stream to write to
33 * @param littleEndian true if stream should be little endian
34 */
35 public ByteCountOutputStream(OutputStream out, boolean littleEndian) {
36 super(out, littleEndian);
37 currentBuffer = -1;
38 bufferList = new LinkedList();
39 }
40
41 public void write(int b) throws IOException {
42 // System.out.println(Integer.toHexString(b)+" "+index);
43 // original stream
44 if (currentBuffer == -1) {
45 super.write(b);
46 return;
47 }
48
49 Buffer buffer = (Buffer) bufferList.get(currentBuffer);
50 buffer.add((byte) b);
51 }
52
53 /**
54 * Pushes the buffer and strat writing to a new one.
55 *
56 * @throws IOException if the write fails
57 */
58 public void pushBuffer() throws IOException {
59 // call append just to make sure we did not leave anything in the
60 // buffer...
61 append();
62 bufferList.add(new Buffer());
63 currentBuffer++;
64 }
65
66 /**
67 * returns the number of bytes written since the last pushBuffer call. It
68 * also puts the write pointer at the start of the buffer, to be able to
69 * "insert" a header. If no buffer was ever pushed, or the last one has been
70 * popped -1 is returned.
71 *
72 * @return number of bytes written or -1
73 * @throws IOException if the write fails
74 */
75 public int popBuffer() throws IOException {
76 if (currentBuffer >= 0) {
77 append();
78 int len = getBufferLength();
79 currentBuffer--;
80 return len;
81 }
82 return -1;
83 }
84
85 /**
86 * Similar to pop buffer, but returns the actual byte[] buffer and then
87 * removes it from the bufferList so that subsequent appends will have no
88 * action. When using this method, the caller is responsible for writing all
89 * buffered data as desired. The byte[] array will usually be larger than
90 * the actual content, so to determine the length of the actual data, you
91 * must call getBufferLength() <i>before</i> invoking this method.
92 *
93 * @return byte array of bytes that need to be written
94 * @throws IOException if write fails
95 */
96 public byte[] popBufferBytes() throws IOException {
97 int len = popBuffer();
98 if (len >= 0) {
99 Buffer buffer = (Buffer) bufferList.remove(currentBuffer + 1);
100 return buffer.getBytes();
101 } else {
102 return new byte[0];
103 }
104 }
105
106 /**
107 * @return valid number of bytes in the buffer
108 */
109 public int getBufferLength() {
110 return (currentBuffer >= 0) ? ((Buffer) bufferList.get(currentBuffer))
111 .getLength() : -1;
112 }
113
114 /**
115 * @return total number of bytes written
116 */
117 public int getLength() {
118 int length = 0;
119 for (int i = 0; i < bufferList.size(); i++) {
120 length += ((Buffer) bufferList.get(i)).getLength();
121 }
122 return (currentBuffer >= 0) ? length : -1;
123 }
124
125 /**
126 * Inserts the bytes written as header and puts the write pointer at the end
127 * of the stream.
128 *
129 * @throws IOException if write fails
130 */
131 public void append() throws IOException {
132 // append the top-level buffer
133 super.byteAlign();
134
135 if (currentBuffer + 1 >= bufferList.size()) {
136 // there is no buffer to append
137 return;
138 }
139
140 Buffer append = (Buffer) bufferList.get(currentBuffer + 1);
141 if (append.getLength() > 0) {
142 if (currentBuffer >= 0) {
143 ((Buffer) bufferList.get(currentBuffer)).add(append);
144 } else {
145 super.write(append.getBytes(), 0, append.getLength());
146 }
147 }
148 bufferList.remove(currentBuffer + 1);
149 }
150
151 /**
152 * closes the stream, inserting any non-written header.
153 */
154 public void close() throws IOException {
155 append();
156 super.close();
157 }
158
159 static class Buffer {
160
161 byte[] buffer;
162
163 int len;
164
165 Buffer() {
166 len = 0;
167 buffer = new byte[256]; // 8192
168 }
169
170 void add(byte b) {
171 if (len + 1 > buffer.length) {
172 byte newBuffer[] = new byte[buffer.length << 1];
173 System.arraycopy(buffer, 0, newBuffer, 0, len);
174 buffer = newBuffer;
175 }
176 buffer[len] = b;
177 len++;
178 }
179
180 void add(Buffer append) {
181 int appendLength = append.getLength();
182 int needed = len + appendLength;
183 if (needed > buffer.length) {
184 byte[] newBuffer = new byte[needed];
185 System.arraycopy(buffer, 0, newBuffer, 0, len);
186 buffer = newBuffer;
187 }
188 System.arraycopy(append.getBytes(), 0, buffer, len, appendLength);
189 len += appendLength;
190 }
191
192 int getLength() {
193 return len;
194 }
195
196 byte[] getBytes() {
197 return buffer;
198 }
199 }
200 }