KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > util > FastByteArrayOutputStream


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.util;
6
7 import java.io.IOException JavaDoc;
8 import java.io.OutputStream JavaDoc;
9 import java.io.RandomAccessFile JavaDoc;
10 import java.io.Writer JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.LinkedList JavaDoc;
13
14
15 /**
16  * A speedy implementation of ByteArrayOutputStream. It's not synchronized, and it
17  * does not copy buffers when it's expanded. There's also no copying of the internal buffer
18  * if it's contents is extracted with the writeTo(stream) method.
19  *
20  * @author Rickard Öberg
21  * @version $Revision: 1.10 $
22  */

23 public class FastByteArrayOutputStream extends OutputStream JavaDoc {
24     //~ Static fields/initializers /////////////////////////////////////////////
25

26     // Static --------------------------------------------------------
27
private static final int DEFAULT_BLOCK_SIZE = 8192;
28
29     //~ Instance fields ////////////////////////////////////////////////////////
30

31     private LinkedList JavaDoc buffers;
32
33     // Attributes ----------------------------------------------------
34
// internal buffer
35
private byte[] buffer;
36
37     // is the stream closed?
38
private boolean closed;
39     private int blockSize;
40     private int index;
41     private int size;
42
43     //~ Constructors ///////////////////////////////////////////////////////////
44

45     // Constructors --------------------------------------------------
46
public FastByteArrayOutputStream() {
47         this(DEFAULT_BLOCK_SIZE);
48     }
49
50     public FastByteArrayOutputStream(int aSize) {
51         blockSize = aSize;
52         buffer = new byte[blockSize];
53     }
54
55     //~ Methods ////////////////////////////////////////////////////////////////
56

57     public int getSize() {
58         return size + index;
59     }
60
61     public void close() {
62         closed = true;
63     }
64
65     public byte[] toByteArray() {
66         byte[] data = new byte[getSize()];
67
68         // Check if we have a list of buffers
69
int pos = 0;
70
71         if (buffers != null) {
72             Iterator JavaDoc iter = buffers.iterator();
73
74             while (iter.hasNext()) {
75                 byte[] bytes = (byte[]) iter.next();
76                 System.arraycopy(bytes, 0, data, pos, blockSize);
77                 pos += blockSize;
78             }
79         }
80
81         // write the internal buffer directly
82
System.arraycopy(buffer, 0, data, pos, index);
83
84         return data;
85     }
86
87     public String JavaDoc toString() {
88         return new String JavaDoc(toByteArray());
89     }
90
91     // OutputStream overrides ----------------------------------------
92
public void write(int datum) throws IOException JavaDoc {
93         if (closed) {
94             throw new IOException JavaDoc("Stream closed");
95         } else {
96             if (index == blockSize) {
97                 addBuffer();
98             }
99
100             // store the byte
101
buffer[index++] = (byte) datum;
102         }
103     }
104
105     public void write(byte[] data, int offset, int length) throws IOException JavaDoc {
106         if (data == null) {
107             throw new NullPointerException JavaDoc();
108         } else if ((offset < 0) || ((offset + length) > data.length) || (length < 0)) {
109             throw new IndexOutOfBoundsException JavaDoc();
110         } else if (closed) {
111             throw new IOException JavaDoc("Stream closed");
112         } else {
113             if ((index + length) > blockSize) {
114                 int copyLength;
115
116                 do {
117                     if (index == blockSize) {
118                         addBuffer();
119                     }
120
121                     copyLength = blockSize - index;
122
123                     if (length < copyLength) {
124                         copyLength = length;
125                     }
126
127                     System.arraycopy(data, offset, buffer, index, copyLength);
128                     offset += copyLength;
129                     index += copyLength;
130                     length -= copyLength;
131                 } while (length > 0);
132             } else {
133                 // Copy in the subarray
134
System.arraycopy(data, offset, buffer, index, length);
135                 index += length;
136             }
137         }
138     }
139
140     // Public
141
public void writeTo(OutputStream JavaDoc out) throws IOException JavaDoc {
142         // Check if we have a list of buffers
143
if (buffers != null) {
144             Iterator JavaDoc iter = buffers.iterator();
145
146             while (iter.hasNext()) {
147                 byte[] bytes = (byte[]) iter.next();
148                 out.write(bytes, 0, blockSize);
149             }
150         }
151
152         // write the internal buffer directly
153
out.write(buffer, 0, index);
154     }
155
156     public void writeTo(RandomAccessFile JavaDoc out) throws IOException JavaDoc {
157         // Check if we have a list of buffers
158
if (buffers != null) {
159             Iterator JavaDoc iter = buffers.iterator();
160
161             while (iter.hasNext()) {
162                 byte[] bytes = (byte[]) iter.next();
163                 out.write(bytes, 0, blockSize);
164             }
165         }
166
167         // write the internal buffer directly
168
out.write(buffer, 0, index);
169     }
170
171     public void writeTo(Writer JavaDoc out, String JavaDoc encoding) throws IOException JavaDoc {
172         // Check if we have a list of buffers
173
if (buffers != null) {
174             Iterator JavaDoc iter = buffers.iterator();
175
176             while (iter.hasNext()) {
177                 byte[] bytes = (byte[]) iter.next();
178
179                 if (encoding != null) {
180                     out.write(new String JavaDoc(bytes, encoding));
181                 } else {
182                     out.write(new String JavaDoc(bytes));
183                 }
184             }
185         }
186
187         // write the internal buffer directly
188
if (encoding != null) {
189             out.write(new String JavaDoc(buffer, 0, index, encoding));
190         } else {
191             out.write(new String JavaDoc(buffer, 0, index));
192         }
193     }
194
195     /**
196      * Create a new buffer and store the
197      * current one in linked list
198      */

199     protected void addBuffer() {
200         if (buffers == null) {
201             buffers = new LinkedList JavaDoc();
202         }
203
204         buffers.addLast(buffer);
205
206         buffer = new byte[blockSize];
207         size += index;
208         index = 0;
209     }
210 }
211
Popular Tags