1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18
19 package org.codehaus.activemq.message.util;
20 import java.io.ByteArrayOutputStream;
21
22 /***
23 * Optimized ByteArrayOutputStream
24 *
25 * @version $Revision: 1.2 $
26 */
27 public class WireByteArrayOutputStream extends ByteArrayOutputStream {
28 /***
29 * Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if
30 * necessary.
31 */
32 public WireByteArrayOutputStream() {
33 super(32);
34 }
35
36 /***
37 * Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
38 *
39 * @param size the initial size.
40 * @exception IllegalArgumentException if size is negative.
41 */
42 public WireByteArrayOutputStream(int size) {
43 super(size);
44 }
45
46 /***
47 * start using a fresh byte array
48 *
49 * @param size
50 */
51 public void restart(int size) {
52 buf = new byte[size];
53 count = 0;
54 }
55
56 /***
57 * start using a fresh byte array
58 */
59 public void restart() {
60 restart(32);
61 }
62
63 /***
64 * Writes the specified byte to this byte array output stream.
65 *
66 * @param b the byte to be written.
67 */
68 public void write(int b) {
69 int newcount = count + 1;
70 if (newcount > buf.length) {
71 byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
72 System.arraycopy(buf, 0, newbuf, 0, count);
73 buf = newbuf;
74 }
75 buf[count] = (byte) b;
76 count = newcount;
77 }
78
79 /***
80 * Writes <code>len</code> bytes from the specified byte array starting at offset <code>off</code> to this byte
81 * array output stream.
82 *
83 * @param b the data.
84 * @param off the start offset in the data.
85 * @param len the number of bytes to write.
86 */
87 public void write(byte b[], int off, int len) {
88 if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) {
89 throw new IndexOutOfBoundsException();
90 }
91 else if (len == 0) {
92 return;
93 }
94 int newcount = count + len;
95 if (newcount > buf.length) {
96 byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
97 System.arraycopy(buf, 0, newbuf, 0, count);
98 buf = newbuf;
99 }
100 System.arraycopy(b, off, buf, count, len);
101 count = newcount;
102 }
103
104 /***
105 * @return the underlying byte[] buffer
106 */
107 public byte[] getData() {
108 return buf;
109 }
110
111 /***
112 * reset the output stream
113 */
114 public void reset(){
115 count = 0;
116 }
117 }