Source Code Cross Referenced for ReadByteStream.java in  » Test-Coverage » GroboUtils » net » sourceforge » groboutils » util » io » v1 » 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 » Test Coverage » GroboUtils » net.sourceforge.groboutils.util.io.v1 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)ReadByteStream.java    1.0.0 04/13/2001 - 11:44:03
003:         *
004:         * Copyright (C) 2001,,2003 2002 Matt Albrecht
005:         * groboclown@users.sourceforge.net
006:         * http://groboutils.sourceforge.net
007:         *
008:         *  Permission is hereby granted, free of charge, to any person obtaining a
009:         *  copy of this software and associated documentation files (the "Software"),
010:         *  to deal in the Software without restriction, including without limitation
011:         *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
012:         *  and/or sell copies of the Software, and to permit persons to whom the
013:         *  Software is furnished to do so, subject to the following conditions:
014:         *
015:         *  The above copyright notice and this permission notice shall be included in
016:         *  all copies or substantial portions of the Software.
017:         *
018:         *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019:         *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020:         *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
021:         *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022:         *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023:         *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024:         *  DEALINGS IN THE SOFTWARE.
025:         */
026:
027:        package net.sourceforge.groboutils.util.io.v1;
028:
029:        import java.io.File;
030:        import java.io.IOException;
031:        import java.io.ByteArrayOutputStream;
032:        import java.io.InputStream;
033:
034:        /**
035:         * Reads a byte array from a stream until the stream is finished.
036:         * You can specify a maximum size to read, and the block read size.
037:         *
038:         * @author   Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
039:         * @since    April 13, 2001
040:         * @version  $Date: 2003/05/19 20:31:47 $
041:         */
042:        public class ReadByteStream {
043:            //----------------------------
044:            // Public data
045:
046:            /**
047:             * Read in an unlimited number of bytes.  This can be very
048:             * dangerous.
049:             */
050:            public static final int READ_TO_END_OF_STREAM = Integer.MAX_VALUE;
051:
052:            /**
053:             * Default block read size.
054:             */
055:            public static final int DEFAULT_BLOCK_READ_SIZE = 4096;
056:
057:            //----------------------------
058:            // Private data
059:
060:            private InputStream m_is;
061:            private int m_maxSize;
062:            private int m_bufferSize;
063:
064:            //----------------------------
065:            // constructors
066:
067:            /**
068:             * Default constructor
069:             */
070:            public ReadByteStream(InputStream is) {
071:                this (is, READ_TO_END_OF_STREAM, DEFAULT_BLOCK_READ_SIZE);
072:            }
073:
074:            /**
075:             * 
076:             */
077:            public ReadByteStream(InputStream is, int maxReadSize,
078:                    int blockReadSize) {
079:                setInputStream(is);
080:                setSizes(maxReadSize, blockReadSize);
081:            }
082:
083:            //----------------------------
084:            // Public methods
085:
086:            /**
087:             * Sets the internal input stream.
088:             */
089:            public void setInputStream(InputStream is) {
090:                if (is == null) {
091:                    throw new IllegalArgumentException("input stream is null");
092:                }
093:                this .m_is = is;
094:            }
095:
096:            /**
097:             * Sets the internal sizes.
098:             */
099:            public void setSizes(int maxReadSize, int blockReadSize) {
100:                if (blockReadSize <= 0) {
101:                    blockReadSize = DEFAULT_BLOCK_READ_SIZE;
102:                }
103:                if (maxReadSize <= 0 || maxReadSize > READ_TO_END_OF_STREAM) {
104:                    maxReadSize = READ_TO_END_OF_STREAM;
105:                }
106:                if (maxReadSize < blockReadSize) {
107:                    blockReadSize = maxReadSize;
108:                }
109:                this .m_maxSize = maxReadSize;
110:                this .m_bufferSize = blockReadSize;
111:            }
112:
113:            /**
114:             * Read in the byte stream, using the current settings.
115:             */
116:            public byte[] readByteStream() throws IOException {
117:                return readByteStream(this .m_is, this .m_maxSize,
118:                        this .m_bufferSize);
119:            }
120:
121:            /**
122:             * Read in the byte stream.  Does not close the stream after it has
123:             * finished reading.  Uses the default sizes.
124:             *
125:             * @see #readByteStream( InputStream, int, int )
126:             */
127:            public static byte[] readByteStream(InputStream is)
128:                    throws IOException {
129:                return readByteStream(is, READ_TO_END_OF_STREAM,
130:                        DEFAULT_BLOCK_READ_SIZE);
131:            }
132:
133:            /**
134:             * Read in the byte stream.  Does not close the stream after it has
135:             * finished reading.
136:             * <P>
137:             * Note that there is no variable checking, for performance reasons.
138:             * The user needs to verify that:
139:             * @param is the input stream, which cannot be <tt>null</tt>.
140:             * @param maxReadSize the maximum number of bytes to read, which
141:             *      must be positive, and must be modulo 0 of <tt>blockReadSize</tt>.
142:             *      This is an "estimation", and may actually read in more than this
143:             *      many bytes if it is not modulo 0 of <tt>blockReadSize</tt>, but
144:             *      will always return all the bytes read.
145:             * @param blockReadSize the number of bytes to read in per read command,
146:             *      which cannot be more than <tt>maxReadSize</tt>, and cannot be
147:             *      less than or equal to zero.
148:             */
149:            public static byte[] readByteStream(InputStream is,
150:                    int maxReadSize, int blockReadSize) throws IOException {
151:                ByteArrayOutputStream baos = new ByteArrayOutputStream();
152:                byte buffer[] = new byte[blockReadSize];
153:                int size = is.read(buffer, 0, blockReadSize);
154:                int totSize = size;
155:                while (size > 0 && totSize < maxReadSize) {
156:                    baos.write(buffer, 0, size);
157:                    size = is.read(buffer, 0, blockReadSize);
158:                    totSize += size;
159:                }
160:                baos.close();
161:                return baos.toByteArray();
162:            }
163:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.