Source Code Cross Referenced for StreamUtil.java in  » Testing » mockrunner-0.4 » com » mockrunner » util » common » 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 » Testing » mockrunner 0.4 » com.mockrunner.util.common 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.mockrunner.util.common;
002:
003:        import java.io.BufferedReader;
004:        import java.io.ByteArrayInputStream;
005:        import java.io.ByteArrayOutputStream;
006:        import java.io.IOException;
007:        import java.io.InputStream;
008:        import java.io.Reader;
009:        import java.io.StringReader;
010:        import java.util.ArrayList;
011:        import java.util.Arrays;
012:        import java.util.List;
013:
014:        import org.apache.commons.logging.Log;
015:        import org.apache.commons.logging.LogFactory;
016:
017:        import com.mockrunner.base.NestedApplicationException;
018:
019:        /**
020:         * Simple util class for manipulating streams
021:         */
022:        public class StreamUtil {
023:            private final static Log log = LogFactory.getLog(StreamUtil.class);
024:
025:            /**
026:             * Returns the contents of the input stream as byte array.
027:             * @param stream the <code>InputStream</code>
028:             * @return the stream content as byte array
029:             */
030:            public static byte[] getStreamAsByteArray(InputStream stream) {
031:                return getStreamAsByteArray(stream, -1);
032:            }
033:
034:            /**
035:             * Returns the contents of the input stream as byte array.
036:             * @param stream the <code>InputStream</code>
037:             * @param length the number of bytes to copy, if length < 0,
038:             *        the number is unlimited
039:             * @return the stream content as byte array
040:             */
041:            public static byte[] getStreamAsByteArray(InputStream stream,
042:                    int length) {
043:                if (length == 0)
044:                    return new byte[0];
045:                boolean checkLength = true;
046:                if (length < 0) {
047:                    length = Integer.MAX_VALUE;
048:                    checkLength = false;
049:                }
050:                ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
051:                try {
052:                    int nextValue = stream.read();
053:                    if (checkLength)
054:                        length--;
055:                    while (-1 != nextValue && length >= 0) {
056:                        byteStream.write(nextValue);
057:                        nextValue = stream.read();
058:                        if (checkLength)
059:                            length--;
060:                    }
061:                    byteStream.flush();
062:                } catch (IOException exc) {
063:                    log.error("Exception while reading from stream", exc);
064:                }
065:                return byteStream.toByteArray();
066:            }
067:
068:            /**
069:             * Returns the contents of the reader as a string.
070:             * @param reader the <code>Reader</code>
071:             * @return the reader content as <code>String</code>
072:             */
073:            public static String getReaderAsString(Reader reader) {
074:                return getReaderAsString(reader, -1);
075:            }
076:
077:            /**
078:             * Returns the contents of the reader as a string.
079:             * @param reader the <code>Reader</code>
080:             * @param length the number of chars to copy, if length < 0,
081:             *        the number is unlimited
082:             * @return the reader content as <code>String</code>
083:             */
084:            public static String getReaderAsString(Reader reader, int length) {
085:                if (length == 0)
086:                    return "";
087:                boolean checkLength = true;
088:                if (length < 0) {
089:                    length = Integer.MAX_VALUE;
090:                    checkLength = false;
091:                }
092:                StringBuffer buffer = new StringBuffer();
093:                try {
094:                    int nextValue = reader.read();
095:                    if (checkLength)
096:                        length--;
097:                    while (-1 != nextValue && length >= 0) {
098:                        buffer.append((char) nextValue);
099:                        nextValue = reader.read();
100:                        if (checkLength)
101:                            length--;
102:                    }
103:                } catch (IOException exc) {
104:                    log.error("Exception while reading from reader", exc);
105:                }
106:                return buffer.toString();
107:            }
108:
109:            /**
110:             * Returns a copy of the specified stream. If the specified stream supports
111:             * marking, it will be reset after the copy.
112:             * @param sourceStream the stream to copy
113:             * @return a copy of the stream
114:             */
115:            public static InputStream copyStream(InputStream sourceStream) {
116:                try {
117:                    if (sourceStream.markSupported())
118:                        sourceStream.mark(sourceStream.available());
119:                    byte[] sourceData = getStreamAsByteArray(sourceStream);
120:                    if (sourceStream.markSupported())
121:                        sourceStream.reset();
122:                    return new ByteArrayInputStream(sourceData);
123:                } catch (IOException exc) {
124:                    log.error("Exception while copying stream", exc);
125:                    return null;
126:                }
127:            }
128:
129:            /**
130:             * Returns a copy of the specified reader. If the specified reader supports
131:             * marking, it will be reset after the copy.
132:             * @param sourceReader the stream to reader
133:             * @return a copy of the reader
134:             */
135:            public static Reader copyReader(Reader sourceReader) {
136:                try {
137:                    if (sourceReader.markSupported())
138:                        sourceReader.mark(Integer.MAX_VALUE);
139:                    String sourceData = getReaderAsString(sourceReader);
140:                    if (sourceReader.markSupported())
141:                        sourceReader.reset();
142:                    return new StringReader(sourceData);
143:                } catch (IOException exc) {
144:                    log.error("Exception while copying reader", exc);
145:                    return null;
146:                }
147:            }
148:
149:            /**
150:             * Compares the content of the streams. If the streams support
151:             * marking, they will be reset after the comparison.
152:             * @param sourceStream the source stream
153:             * @param targetStream the target stream
154:             * @return <code>true</code>, if the streams are identical, <code>false</code> otherwise
155:             */
156:            public static boolean compareStreams(InputStream sourceStream,
157:                    InputStream targetStream) {
158:                try {
159:                    if (sourceStream.markSupported())
160:                        sourceStream.mark(sourceStream.available());
161:                    if (targetStream.markSupported())
162:                        targetStream.mark(targetStream.available());
163:                    byte[] sourceArray = getStreamAsByteArray(sourceStream);
164:                    byte[] targetArray = getStreamAsByteArray(targetStream);
165:                    if (sourceStream.markSupported())
166:                        sourceStream.reset();
167:                    if (targetStream.markSupported())
168:                        targetStream.reset();
169:                    return Arrays.equals(sourceArray, targetArray);
170:                } catch (IOException exc) {
171:                    log.error("Exception while comparing streams", exc);
172:                    return false;
173:                }
174:            }
175:
176:            /**
177:             * Compares the content of the readers. If the readers support
178:             * marking, they will be reset after the comparison.
179:             * @param sourceReader the source stream
180:             * @param targetReader the target stream
181:             * @return <code>true</code>, if the streams are identical, <code>false</code> otherwise
182:             */
183:            public static boolean compareReaders(Reader sourceReader,
184:                    Reader targetReader) {
185:                try {
186:                    if (sourceReader.markSupported())
187:                        sourceReader.mark(Integer.MAX_VALUE);
188:                    if (targetReader.markSupported())
189:                        targetReader.mark(Integer.MAX_VALUE);
190:                    String sourceString = getReaderAsString(sourceReader);
191:                    String targetString = getReaderAsString(targetReader);
192:                    if (sourceReader.markSupported())
193:                        sourceReader.reset();
194:                    if (targetReader.markSupported())
195:                        targetReader.reset();
196:                    return sourceString.equals(targetString);
197:                } catch (IOException exc) {
198:                    log.error("Exception while comparing readers", exc);
199:                    return false;
200:                }
201:            }
202:
203:            /**
204:             * Reads the lines from the specified reader and adds them to a <code>List</code>.
205:             * @param reader the reader
206:             * @return the <code>List</code> with the lines
207:             */
208:            public static List getLinesFromReader(Reader reader) {
209:                List resultList = new ArrayList();
210:                try {
211:                    BufferedReader bufferedReader = new BufferedReader(reader);
212:                    String line = bufferedReader.readLine();
213:                    while (line != null) {
214:                        resultList.add(line);
215:                        line = bufferedReader.readLine();
216:                    }
217:                } catch (IOException exc) {
218:                    throw new NestedApplicationException(exc);
219:                }
220:                return resultList;
221:            }
222:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.