Source Code Cross Referenced for FastByteArrayOutputStream.java in  » J2EE » webwork-2.2.6 » com » opensymphony » webwork » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » webwork 2.2.6 » com.opensymphony.webwork.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2003 by OpenSymphony
003:         * All rights reserved.
004:         */
005:        package com.opensymphony.webwork.util;
006:
007:        import java.io.IOException;
008:        import java.io.OutputStream;
009:        import java.io.RandomAccessFile;
010:        import java.io.Writer;
011:        import java.util.Iterator;
012:        import java.util.LinkedList;
013:
014:        /**
015:         * A speedy implementation of ByteArrayOutputStream. It's not synchronized, and it
016:         * does not copy buffers when it's expanded. There's also no copying of the internal buffer
017:         * if it's contents is extracted with the writeTo(stream) method.
018:         *
019:         * @author Rickard Öberg
020:         * @version $Revision: 1282 $
021:         */
022:        public class FastByteArrayOutputStream extends OutputStream {
023:
024:            // Static --------------------------------------------------------
025:            private static final int DEFAULT_BLOCK_SIZE = 8192;
026:
027:            private LinkedList buffers;
028:
029:            // Attributes ----------------------------------------------------
030:            // internal buffer
031:            private byte[] buffer;
032:
033:            // is the stream closed?
034:            private boolean closed;
035:            private int blockSize;
036:            private int index;
037:            private int size;
038:
039:            // Constructors --------------------------------------------------
040:            public FastByteArrayOutputStream() {
041:                this (DEFAULT_BLOCK_SIZE);
042:            }
043:
044:            public FastByteArrayOutputStream(int aSize) {
045:                blockSize = aSize;
046:                buffer = new byte[blockSize];
047:            }
048:
049:            public int getSize() {
050:                return size + index;
051:            }
052:
053:            public void close() {
054:                closed = true;
055:            }
056:
057:            public byte[] toByteArray() {
058:                byte[] data = new byte[getSize()];
059:
060:                // Check if we have a list of buffers
061:                int pos = 0;
062:
063:                if (buffers != null) {
064:                    Iterator iter = buffers.iterator();
065:
066:                    while (iter.hasNext()) {
067:                        byte[] bytes = (byte[]) iter.next();
068:                        System.arraycopy(bytes, 0, data, pos, blockSize);
069:                        pos += blockSize;
070:                    }
071:                }
072:
073:                // write the internal buffer directly
074:                System.arraycopy(buffer, 0, data, pos, index);
075:
076:                return data;
077:            }
078:
079:            public String toString() {
080:                return new String(toByteArray());
081:            }
082:
083:            // OutputStream overrides ----------------------------------------
084:            public void write(int datum) throws IOException {
085:                if (closed) {
086:                    throw new IOException("Stream closed");
087:                } else {
088:                    if (index == blockSize) {
089:                        addBuffer();
090:                    }
091:
092:                    // store the byte
093:                    buffer[index++] = (byte) datum;
094:                }
095:            }
096:
097:            public void write(byte[] data, int offset, int length)
098:                    throws IOException {
099:                if (data == null) {
100:                    throw new NullPointerException();
101:                } else if ((offset < 0) || ((offset + length) > data.length)
102:                        || (length < 0)) {
103:                    throw new IndexOutOfBoundsException();
104:                } else if (closed) {
105:                    throw new IOException("Stream closed");
106:                } else {
107:                    if ((index + length) > blockSize) {
108:                        int copyLength;
109:
110:                        do {
111:                            if (index == blockSize) {
112:                                addBuffer();
113:                            }
114:
115:                            copyLength = blockSize - index;
116:
117:                            if (length < copyLength) {
118:                                copyLength = length;
119:                            }
120:
121:                            System.arraycopy(data, offset, buffer, index,
122:                                    copyLength);
123:                            offset += copyLength;
124:                            index += copyLength;
125:                            length -= copyLength;
126:                        } while (length > 0);
127:                    } else {
128:                        // Copy in the subarray
129:                        System.arraycopy(data, offset, buffer, index, length);
130:                        index += length;
131:                    }
132:                }
133:            }
134:
135:            // Public
136:            public void writeTo(OutputStream out) throws IOException {
137:                // Check if we have a list of buffers
138:                if (buffers != null) {
139:                    Iterator iter = buffers.iterator();
140:
141:                    while (iter.hasNext()) {
142:                        byte[] bytes = (byte[]) iter.next();
143:                        out.write(bytes, 0, blockSize);
144:                    }
145:                }
146:
147:                // write the internal buffer directly
148:                out.write(buffer, 0, index);
149:            }
150:
151:            public void writeTo(RandomAccessFile out) throws IOException {
152:                // Check if we have a list of buffers
153:                if (buffers != null) {
154:                    Iterator iter = buffers.iterator();
155:
156:                    while (iter.hasNext()) {
157:                        byte[] bytes = (byte[]) iter.next();
158:                        out.write(bytes, 0, blockSize);
159:                    }
160:                }
161:
162:                // write the internal buffer directly
163:                out.write(buffer, 0, index);
164:            }
165:
166:            public void writeTo(Writer out, String encoding) throws IOException {
167:                // Check if we have a list of buffers
168:                if (buffers != null) {
169:                    Iterator iter = buffers.iterator();
170:
171:                    while (iter.hasNext()) {
172:                        byte[] bytes = (byte[]) iter.next();
173:
174:                        if (encoding != null) {
175:                            out.write(new String(bytes, encoding));
176:                        } else {
177:                            out.write(new String(bytes));
178:                        }
179:                    }
180:                }
181:
182:                // write the internal buffer directly
183:                if (encoding != null) {
184:                    out.write(new String(buffer, 0, index, encoding));
185:                } else {
186:                    out.write(new String(buffer, 0, index));
187:                }
188:            }
189:
190:            /**
191:             * Create a new buffer and store the
192:             * current one in linked list
193:             */
194:            protected void addBuffer() {
195:                if (buffers == null) {
196:                    buffers = new LinkedList();
197:                }
198:
199:                buffers.addLast(buffer);
200:
201:                buffer = new byte[blockSize];
202:                size += index;
203:                index = 0;
204:            }
205:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.