Source Code Cross Referenced for GenericInputStream.java in  » Web-Framework » anvil » anvil » java » 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 » Web Framework » anvil » anvil.java.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: GenericInputStream.java,v 1.5 2002/09/16 08:05:03 jkl Exp $
003:         *
004:         * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005:         *
006:         * Use is subject to license terms, as defined in
007:         * Anvil Sofware License, Version 1.1. See LICENSE 
008:         * file, or http://njet.org/license-1.1.txt
009:         */
010:        package anvil.java.io;
011:
012:        import java.io.InputStream;
013:        import java.io.IOException;
014:
015:        /**
016:         * GenericInputStream
017:         *
018:         * @author: Jani Lehtimäki
019:         */
020:        public final class GenericInputStream extends InputStream {
021:
022:            private static int BUFFERSIZE = 512;
023:
024:            private InputStream _input;
025:            private byte[] _array = null;
026:            private int _pos = 0;
027:            private int _end = 0;
028:            private int _line = 1;
029:            private int _column = 1;
030:
031:            public GenericInputStream(byte[] array) {
032:                _input = null;
033:                _array = array;
034:                _end = array.length;
035:            }
036:
037:            public GenericInputStream(InputStream input) {
038:                _input = input;
039:                _array = new byte[BUFFERSIZE];
040:            }
041:
042:            public GenericInputStream(InputStream input, int bufferSize) {
043:                _input = input;
044:                _array = new byte[bufferSize];
045:            }
046:
047:            private final void fillBuffer() throws IOException {
048:                if (_input == null) {
049:                    _end = -1;
050:                    return;
051:                }
052:                _pos = 0;
053:                try {
054:                    _end = _input.read(_array);
055:                } catch (IOException e) {
056:                    _end = -1;
057:                    e.fillInStackTrace();
058:                    throw e;
059:                }
060:            }
061:
062:            public final synchronized int available() throws IOException {
063:                if (_input == null) {
064:                    return _end - _pos;
065:                } else {
066:                    if (_end == -1) {
067:                        return 0;
068:                    }
069:                    return (_end - _pos) + _input.available();
070:                }
071:            }
072:
073:            public final synchronized int read(byte[] target, int offset,
074:                    int length) throws IOException {
075:                if (_end == -1) {
076:                    return 0;
077:                }
078:                int read = 0;
079:                int size = (_end - _pos);
080:                if (size > 0) {
081:                    if (length <= size) {
082:                        System.arraycopy(_array, _pos, target, offset, length);
083:                        read += length;
084:                        _pos += length;
085:                        length = 0;
086:                    } else {
087:                        System.arraycopy(_array, _pos, target, offset, size);
088:                        offset += size;
089:                        length -= size;
090:                        read += size;
091:                        _pos = _end;
092:                    }
093:                }
094:                if (length > 0) {
095:                    if (_input != null) {
096:                        read += _input.read(target, offset, length);
097:                    }
098:                }
099:
100:                int column = _column;
101:                int line = _line;
102:                for (int i = 0; i < read; i++) {
103:                    if (target[i] == '\n') {
104:                        line++;
105:                        column = 1;
106:                    } else {
107:                        column++;
108:                    }
109:                }
110:                _line = line;
111:                _column = column;
112:
113:                return read;
114:            }
115:
116:            public final synchronized int read() throws IOException {
117:                if (_end == -1) {
118:                    return -1;
119:                }
120:                if (_pos >= _end) {
121:                    fillBuffer();
122:                }
123:                if (_pos < _end) {
124:                    int ch = _array[_pos++];
125:                    ch = (ch + 0x100) & 0xff;
126:                    if (ch == '\n') {
127:                        _line++;
128:                        _column = 1;
129:                    } else {
130:                        _column++;
131:                    }
132:                    return ch;
133:                } else {
134:                    return -1;
135:                }
136:            }
137:
138:            public final synchronized String readLine() throws IOException {
139:                byte[] array = _array;
140:                StringBuffer buffer = new StringBuffer(64);
141:                char ch = 0;
142:
143:                int line = _line;
144:                int column = _column;
145:                while (_end != -1) {
146:                    if (_pos >= _end) {
147:                        fillBuffer();
148:                        continue;
149:                    }
150:                    ch = (char) array[_pos++];
151:                    if (ch == '\n') {
152:                        line++;
153:                        column = 1;
154:                        break;
155:                    } else {
156:                        column++;
157:                    }
158:                    buffer.append(ch);
159:                }
160:                _line = line;
161:                _column = column;
162:
163:                int n = buffer.length();
164:                if (n == 0) {
165:                    if (ch == 0) {
166:                        return null;
167:                    } else {
168:                        return "";
169:                    }
170:                } else {
171:                    if (buffer.charAt(n - 1) == '\r') {
172:                        buffer.setLength(n - 1);
173:                    }
174:                    return buffer.toString();
175:                }
176:            }
177:
178:            public final synchronized String read(int amount)
179:                    throws IOException {
180:                if (amount <= 0) {
181:                    return "";
182:                }
183:                byte[] array = _array;
184:                StringBuffer buffer = new StringBuffer(amount);
185:                char ch;
186:
187:                int line = _line;
188:                int column = _column;
189:                while ((_end != -1) && (amount > 0)) {
190:                    if (_pos >= _end) {
191:                        fillBuffer();
192:                        continue;
193:                    }
194:                    ch = (char) array[_pos++];
195:                    if (ch == '\n') {
196:                        line++;
197:                        column = 1;
198:                    } else {
199:                        column++;
200:                    }
201:                    buffer.append(ch);
202:                    amount--;
203:                }
204:                _line = line;
205:                _column = column;
206:
207:                if (buffer.length() == 0) {
208:                    return null;
209:                } else {
210:                    return buffer.toString();
211:                }
212:            }
213:
214:            public final synchronized long skip(long amount) throws IOException {
215:                byte[] array = _array;
216:                long skipped = 0;
217:                int line = _line;
218:                int column = _column;
219:                while ((_end != -1) && (skipped < amount)) {
220:                    if (_pos >= _end) {
221:                        fillBuffer();
222:                        continue;
223:                    }
224:                    if (array[_pos++] == '\n') {
225:                        line++;
226:                        column = 1;
227:                    } else {
228:                        column++;
229:                    }
230:                    skipped++;
231:                }
232:                _line = line;
233:                _column = column;
234:                if (_end == -1 && skipped == 0) {
235:                    return -1;
236:                } else {
237:                    return skipped;
238:                }
239:            }
240:
241:            public final int getColumnNumber() {
242:                return _column;
243:            }
244:
245:            public final int getLineNumber() {
246:                return _line;
247:            }
248:
249:            public final synchronized void close() throws IOException {
250:                if (_input != null) {
251:                    _input.close();
252:                }
253:            }
254:
255:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.