KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > module > sitemesh > util > FastByteArrayOutputStream


1 /*
2  * Title: FastByteArrayOutputStream
3  * Description:
4  *
5  * This software is published under the terms of the OpenSymphony Software
6  * License version 1.1, of which a copy has been included with this
7  * distribution in the LICENSE.txt file.
8  */

9
10 package com.opensymphony.module.sitemesh.util;
11
12 import java.io.ByteArrayOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.OutputStream JavaDoc;
15 import java.io.UnsupportedEncodingException JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.LinkedList JavaDoc;
18
19 /**
20  * A speedy implementation of ByteArrayOutputStream. It's not synchronized, and it
21  * does not copy buffers when it's expanded. There's also no copying of the internal buffer
22  * if it's contents is extracted with the writeTo(stream) method.
23  *
24  * @author Rickard �berg
25  * @author <a HREF="mailto:scott@atlassian.com">Scott Farquhar</a>
26  * @version $Revision: 1.2 $
27  */

28 public class FastByteArrayOutputStream extends ByteArrayOutputStream JavaDoc {
29     private static final int DEFAULT_BLOCK_SIZE = 8192;
30
31     /** Internal buffer. */
32     private byte[] buffer;
33
34     private LinkedList JavaDoc buffers;
35
36     private int index;
37     private int size;
38     private int blockSize;
39
40     public FastByteArrayOutputStream() {
41         this(DEFAULT_BLOCK_SIZE);
42     }
43
44     public FastByteArrayOutputStream(int aSize) {
45         blockSize = aSize;
46         buffer = new byte[blockSize];
47     }
48
49     public void writeTo(OutputStream JavaDoc out) throws IOException JavaDoc {
50         // check if we have a list of buffers
51
if (buffers != null) {
52             Iterator JavaDoc iterator = buffers.iterator();
53             while (iterator.hasNext()) {
54                 byte[] bytes = (byte[]) iterator.next();
55                 out.write(bytes, 0, blockSize);
56             }
57         }
58
59         // write the internal buffer directly
60
out.write(buffer, 0, index);
61     }
62
63
64     public int size() {
65         return size + index;
66     }
67
68     public byte[] toByteArray() {
69         byte[] data = new byte[size()];
70
71         // check if we have a list of buffers
72
int pos = 0;
73         if (buffers != null) {
74             Iterator JavaDoc iterator = buffers.iterator();
75             while (iterator.hasNext()) {
76                 byte[] bytes = (byte[]) iterator.next();
77                 System.arraycopy(bytes, 0, data, pos, blockSize);
78                 pos += blockSize;
79             }
80         }
81
82         // write the internal buffer directly
83
System.arraycopy(buffer, 0, data, pos, index);
84
85         return data;
86     }
87
88     public void write(int datum) {
89         if (index == blockSize) {
90             // Create new buffer and store current in linked list
91
if (buffers == null)
92                 buffers = new LinkedList JavaDoc();
93
94             buffers.addLast(buffer);
95
96             buffer = new byte[blockSize];
97             size += index;
98             index = 0;
99         }
100
101         // store the byte
102
buffer[index++] = (byte) datum;
103     }
104
105     public void write(byte[] data, int offset, int length) {
106         if (data == null) {
107             throw new NullPointerException JavaDoc();
108         }
109         else if ((offset < 0) || (offset + length > data.length)
110                 || (length < 0)) {
111             throw new IndexOutOfBoundsException JavaDoc();
112         }
113         else {
114             if (index + length >= blockSize) {
115                 // Write byte by byte
116
// FIXME optimize this to use arraycopy's instead
117
for (int i = 0; i < length; i++)
118                     write(data[offset + i]);
119             }
120             else {
121                 // copy in the subarray
122
System.arraycopy(data, offset, buffer, index, length);
123                 index += length;
124             }
125         }
126     }
127
128     public synchronized void reset() {
129         buffer = new byte[blockSize];
130         buffers = null;
131     }
132
133     public String JavaDoc toString(String JavaDoc enc) throws UnsupportedEncodingException JavaDoc {
134         return new String JavaDoc(toByteArray(), enc);
135     }
136
137     public String JavaDoc toString() {
138         return new String JavaDoc(toByteArray());
139     }
140
141     public void flush() throws IOException JavaDoc {
142         // does nothing
143
}
144
145     public void close() throws IOException JavaDoc {
146         // does nothing
147
}
148 }
Popular Tags