Source Code Cross Referenced for ReaderInputStream.java in  » Database-ORM » iBATIS » com » ibatis » common » io » 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 » Database ORM » iBATIS » com.ibatis.common.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Java Network Programming, Second Edition
003:         * Merlin Hughes, Michael Shoffner, Derek Hamner
004:         * Manning Publications Company; ISBN 188477749X
005:         *
006:         * http://nitric.com/jnp/
007:         *
008:         * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner;
009:         * all rights reserved.
010:         *
011:         * Redistribution and use in source and binary forms, with or without
012:         * modification, are permitted provided that the following conditions
013:         * are met:
014:         *
015:         * 1. Redistributions of source code must retain the above copyright
016:         *    notice, this list of conditions and the following disclaimer.
017:         *
018:         * 2. Redistributions in binary form must reproduce the above copyright
019:         *    notice, this list of conditions and the following disclaimer in
020:         *    the documentation and/or other materials provided with the
021:         *    distribution.
022:         *
023:         * THIS SOFTWARE IS PROVIDED BY THE ABOVE NAMED AUTHORS "AS IS" AND ANY
024:         * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
025:         * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026:         * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS, THEIR
027:         * PUBLISHER OR THEIR EMPLOYERS BE LIABLE FOR ANY DIRECT, INDIRECT,
028:         * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
029:         * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
030:         * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
031:         * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
032:         * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
033:         * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
034:         * OF THE POSSIBILITY OF SUCH DAMAGE.
035:         */
036:        package com.ibatis.common.io;
037:
038:        import java.io.*;
039:
040:        /**
041:         * An InputStream backed by a Reader
042:         */
043:        public class ReaderInputStream extends InputStream {
044:            protected Reader reader;
045:            protected ByteArrayOutputStream byteArrayOut;
046:            protected Writer writer;
047:            protected char[] chars;
048:            protected byte[] buffer;
049:            protected int index, length;
050:
051:            /**
052:             * Constructor to supply a Reader
053:             *
054:             * @param reader - the Reader used by the InputStream
055:             */
056:            public ReaderInputStream(Reader reader) {
057:                this .reader = reader;
058:                byteArrayOut = new ByteArrayOutputStream();
059:                writer = new OutputStreamWriter(byteArrayOut);
060:                chars = new char[1024];
061:            }
062:
063:            /**
064:             * Constructor to supply a Reader and an encoding
065:             *
066:             * @param reader   - the Reader used by the InputStream
067:             * @param encoding - the encoding to use for the InputStream
068:             * @throws UnsupportedEncodingException if the encoding is not supported
069:             */
070:            public ReaderInputStream(Reader reader, String encoding)
071:                    throws UnsupportedEncodingException {
072:                this .reader = reader;
073:                byteArrayOut = new ByteArrayOutputStream();
074:                writer = new OutputStreamWriter(byteArrayOut, encoding);
075:                chars = new char[1024];
076:            }
077:
078:            /**
079:             * @see java.io.InputStream#read()
080:             */
081:            public int read() throws IOException {
082:                if (index >= length)
083:                    fillBuffer();
084:                if (index >= length)
085:                    return -1;
086:                return 0xff & buffer[index++];
087:            }
088:
089:            protected void fillBuffer() throws IOException {
090:                if (length < 0)
091:                    return;
092:                int numChars = reader.read(chars);
093:                if (numChars < 0) {
094:                    length = -1;
095:                } else {
096:                    byteArrayOut.reset();
097:                    writer.write(chars, 0, numChars);
098:                    writer.flush();
099:                    buffer = byteArrayOut.toByteArray();
100:                    length = buffer.length;
101:                    index = 0;
102:                }
103:            }
104:
105:            /**
106:             * @see java.io.InputStream#read(byte[], int, int)
107:             */
108:            public int read(byte[] data, int off, int len) throws IOException {
109:                if (index >= length)
110:                    fillBuffer();
111:                if (index >= length)
112:                    return -1;
113:                int amount = Math.min(len, length - index);
114:                System.arraycopy(buffer, index, data, off, amount);
115:                index += amount;
116:                return amount;
117:            }
118:
119:            /**
120:             * @see java.io.InputStream#available()
121:             */
122:            public int available() throws IOException {
123:                return (index < length) ? length - index
124:                        : ((length >= 0) && reader.ready()) ? 1 : 0;
125:            }
126:
127:            /**
128:             * @see java.io.InputStream#close()
129:             */
130:            public void close() throws IOException {
131:                reader.close();
132:            }
133:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.