Source Code Cross Referenced for Stream.java in  » Net » hyperpool-0.4.0 » vicazh » hyperpool » stream » net » http » 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 » Net » hyperpool 0.4.0 » vicazh.hyperpool.stream.net.http 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package vicazh.hyperpool.stream.net.http;
002:
003:        import java.io.*;
004:        import java.util.*;
005:
006:        /**
007:         * This class is the superclass of all http stream data
008:         * 
009:         * @author Victor Zhigunov
010:         * @version 0.4.0
011:         */
012:        abstract public class Stream extends vicazh.hyperpool.stream.net.Stream {
013:            public Stream() {
014:            }
015:
016:            public Session session;
017:
018:            protected ByteArrayOutputStream stream;
019:
020:            private Map<String, String> fields;
021:
022:            /**
023:             * @param session
024:             *            parent session
025:             * @param outputstream
026:             *            linked output stream
027:             */
028:            public Stream(Session session, OutputStream outputstream) {
029:                super (session.connection, outputstream);
030:                this .session = session;
031:                stream = new ByteArrayOutputStream();
032:                fields = new HashMap<String, String>();
033:            }
034:
035:            protected int mode;
036:
037:            protected final static int HEAD = 0;
038:
039:            protected final static int FIELD = 1;
040:
041:            protected final static int CONTENT = 2;
042:
043:            protected final static int CHUNK = 3;
044:
045:            protected final static int POSTCHUNK = 4;
046:
047:            protected boolean isChunked;
048:
049:            protected long length = -1;
050:
051:            public void write(int b) throws IOException {
052:                if (mode == CONTENT)
053:                    content(b);
054:                else {
055:                    if (b == 10) {
056:                        byte[] a = stream.toByteArray();
057:                        String s = new String(a, 0, a.length > 0
058:                                && a[a.length - 1] == 13 ? a.length - 1
059:                                : a.length);
060:                        stream.reset();
061:                        if (mode == HEAD) {
062:                            if (s.length() == 0)
063:                                line(s);
064:                            else {
065:                                if (s.equals("0"))
066:                                    line(s);
067:                                else
068:                                    head(s);
069:                            }
070:                        } else if (mode == FIELD) {
071:                            if (s.length() == 0)
072:                                header();
073:                            else {
074:                                int j = s.indexOf(':');
075:                                field(s.substring(0, j),
076:                                        s.charAt(j + 1) == ' ' ? s
077:                                                .substring(j + 2) : s
078:                                                .substring(j + 1));
079:                            }
080:                        } else if (mode == CHUNK) {
081:                            line(s);
082:                            if (s.length() != 0) {
083:                                length = Long.parseLong(new StringTokenizer(s)
084:                                        .nextToken(), 16);
085:                                if (length == 0)
086:                                    mode = POSTCHUNK;
087:                                else
088:                                    mode = CONTENT;
089:                            }
090:                        } else {
091:                            line(s);
092:                            end();
093:                            set();
094:                            outputstream = null;
095:                        }
096:                    } else
097:                        stream.write(b);
098:                }
099:            }
100:
101:            /**
102:             * Writes the content to this stream
103:             * 
104:             * @param b
105:             *            the <code>byte</code>
106:             */
107:            public void content(int b) throws IOException {
108:                super .write(b);
109:                if (length > 0)
110:                    if (--length == 0)
111:                        if (isChunked)
112:                            mode = CHUNK;
113:                        else {
114:                            end();
115:                            set();
116:                            outputstream = null;
117:                        }
118:            }
119:
120:            /**
121:             * Writes the first line to this stream
122:             * 
123:             * @param s
124:             *            the <code>String</code> to be written
125:             */
126:            public void head(String s) throws IOException {
127:                line(s);
128:                mode = FIELD;
129:            }
130:
131:            /**
132:             * Writes the field to this stream
133:             * 
134:             * @param s1
135:             *            the name
136:             * @param s2
137:             *            the value
138:             */
139:            public void field(String s1, String s2) throws IOException {
140:                if (s1.equalsIgnoreCase("Content-Length"))
141:                    length = Long.parseLong(s2.trim());
142:                else if (s1.equalsIgnoreCase("Transfer-Encoding")
143:                        && s2.equalsIgnoreCase("chunked"))
144:                    isChunked = true;
145:                fields.put(s1.toLowerCase(), s2);
146:                line(s1 + ": " + s2);
147:            }
148:
149:            /**
150:             * Writes the line to this stream
151:             * 
152:             * @param s
153:             *            the <code>String</code> to be written
154:             */
155:            protected void line(String s) throws IOException {
156:                for (byte b : (s + "\r\n").getBytes())
157:                    super .write(b);
158:            }
159:
160:            /**
161:             * Writes the empty line after all fields
162:             */
163:            public void header() throws IOException {
164:                line("");
165:                if (isContent())
166:                    mode = isChunked ? CHUNK : CONTENT;
167:                else {
168:                    end();
169:                    set();
170:                    outputstream = null;
171:                }
172:            }
173:
174:            abstract protected boolean isContent();
175:
176:            public void setFields(Map<String, String> fields) {
177:                this .fields = fields;
178:            }
179:
180:            public Map<String, String> getFields() {
181:                return fields;
182:            }
183:
184:            /**
185:             * The ended state of the stream
186:             */
187:            public boolean isEnd;
188:
189:            /**
190:             * End this stream
191:             */
192:            synchronized public void end() {
193:                isEnd = true;
194:                notifyAll();
195:            }
196:
197:            public void close() throws IOException {
198:                if (!isEnd)
199:                    end();
200:                super .close();
201:            }
202:
203:            abstract protected void set() throws IOException;
204:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.