Source Code Cross Referenced for ReadConnection.java in  » Portal » Open-Portal » communications » 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 » Portal » Open Portal » communications 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *	CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF
003:         *	NETSCAPE COMMUNICATIONS CORPORATION
004:         *
005:         *	Copyright (c) 1996 Netscape Communications Corporation.
006:         *	All Rights Reserved.
007:         *	Use of this Source Code is subject to the terms of the applicable
008:         *	license agreement from Netscape Communications Corporation.
009:         */
010:
011:        package communications;
012:
013:        import util.ReportError;
014:
015:        import java.io.DataInputStream;
016:        import java.io.InputStream;
017:        import java.net.URL;
018:        import java.net.URLConnection;
019:
020:        import java.io.IOException;
021:        import java.lang.NullPointerException;
022:        import java.net.MalformedURLException;
023:
024:        /**
025:         * Manage "read" style server connections.
026:         * <p>
027:         * Note that currently the internal URLConnection
028:         * subverts defaults by doing <i>setAllowUserInteraction(true)</i>
029:         * and <i>setUseCaches(false)</i>.
030:         * Eventually it will be possible to set these differently,
031:         * currently they are hard coded this way.
032:         */
033:        public class ReadConnection {
034:            /**
035:             * The URL string.
036:             */
037:            protected String url_string;
038:
039:            /**
040:             * The URL.
041:             */
042:            protected URL url;
043:
044:            /**
045:             * The URL connection.
046:             */
047:            protected URLConnection urlc;
048:
049:            /**
050:             * The input stream.
051:             */
052:            protected InputStream is;
053:
054:            /**
055:             * The data input stream.
056:             */
057:            protected DataInputStream dis;
058:
059:            /**
060:             * Indicates if there is an established data input stream.
061:             */
062:            protected boolean validIn;
063:
064:            /**
065:             * Indicates if there is an established connection.
066:             */
067:            protected boolean validCon;
068:
069:            /**
070:             * Constructs a ReadConnection instance.
071:             * @param s the URL string
072:             */
073:            public ReadConnection(String s) {
074:                url_string = s;
075:                url = null;
076:                urlc = null;
077:                is = null;
078:                dis = null;
079:                validIn = false;
080:                validCon = false;
081:            }
082:
083:            /**
084:             * Set the URL.
085:             * @param s the URL string
086:             */
087:            public void setURL(String s) {
088:                url_string = new String(s);
089:                validIn = false;
090:            }
091:
092:            /**
093:             * Return whether the connection is valid.
094:             */
095:            public boolean isValidCon() {
096:                return validCon;
097:            }
098:
099:            /**
100:             * Open the connection.
101:             */
102:            protected boolean openCon() {
103:                if (validCon)
104:                    return true;
105:
106:                try {
107:                    url = new URL(url_string);
108:                } catch (MalformedURLException e) {
109:                    ReportError.reportError(util.ReportError.INTERNAL,
110:                            "ReadConnection", "openCon", Messages.MALFORMEDURL);
111:                    return false;
112:                }
113:
114:                try {
115:                    urlc = url.openConnection();
116:                    urlc.setAllowUserInteraction(true);
117:                    urlc.setUseCaches(false);
118:                } catch (IOException e) {
119:                    ReportError.reportError(util.ReportError.INTERNAL,
120:                            "ReadConnection", "openCon",
121:                            Messages.URLCONNECTIONOPENFAIL);
122:                    return false;
123:                }
124:
125:                validCon = true;
126:                return true;
127:            }
128:
129:            /**
130:             * Return whether the input stream is valid.
131:             */
132:            public boolean isValidIn() {
133:                return validIn;
134:            }
135:
136:            /**
137:             * Open the input stream.
138:             */
139:            public boolean openIn() {
140:                if (validIn)
141:                    return true;
142:
143:                openCon();
144:
145:                try {
146:                    is = urlc.getInputStream();
147:                } catch (IOException e) {
148:                    ReportError.reportError(util.ReportError.INTERNAL,
149:                            "ReadConnection", "openIn",
150:                            Messages.DATAINPUTSTREAMOPENFAIL);
151:                    return false;
152:                }
153:
154:                dis = new DataInputStream(is);
155:
156:                validIn = true;
157:                return true;
158:            }
159:
160:            /**
161:             * Close the input stream.
162:             */
163:            public boolean closeIn() {
164:                if (!validIn)
165:                    return true;
166:
167:                validIn = false;
168:                validCon = false;
169:
170:                try {
171:                    dis.close();
172:                } catch (IOException e) {
173:                    ReportError.reportError(util.ReportError.INTERNAL,
174:                            "ReadConnection", "closeIn",
175:                            Messages.DATAINPUTSTREAMCLOSEFAIL);
176:                    return false;
177:                }
178:
179:                return true;
180:            }
181:
182:            /**
183:             * Read a line from the input stream.
184:             * @deprecated 3.01C
185:             */
186:            public String readLine() {
187:                String s;
188:
189:                if (!validIn)
190:                    return null;
191:
192:                try {
193:                    s = dis.readLine();
194:                } catch (IOException e) {
195:                    ReportError.reportError(util.ReportError.INTERNAL,
196:                            "ReadConnection", "readLine",
197:                            Messages.DATAINPUTSTREAMREADFAIL);
198:                    return null;
199:                }
200:
201:                return s;
202:            }
203:
204:            /**
205:             * Read the entire input stream. Use with discretion!
206:             */
207:            public String doRead() {
208:
209:                try {
210:
211:                    if (openIn() == false)
212:                        return null;
213:
214:                    int r = 0, n = 0;
215:                    byte b[] = new byte[10000];
216:                    String s = "";
217:
218:                    while (r != -1) {
219:                        if ((r = is.read(b, n, b.length - n)) != -1)
220:                            n += r;
221:                        if (r == -1 || n == b.length) {
222:                            s = s.concat(new String(b, 0, 0, n));
223:                            n = 0;
224:                            // Buffer strategy is rapid growth with plateau.
225:                            // StringBuffer is too wasteful at large sizes
226:                            // - esp with 4M applet array limits lurking...
227:                            if (b.length == 10000)
228:                                b = new byte[100000];
229:                            else if (b.length == 100000)
230:                                b = new byte[1000000];
231:                        }
232:                        //System.out.println(""+r+" "+n+" "+b.length+" "+s.length());
233:                    }
234:
235:                    closeIn();
236:                    return s;
237:
238:                } catch (Exception e) {
239:                    ReportError.reportError(util.ReportError.INTERNAL,
240:                            "ReadConnection", "doRead",
241:                            Messages.DATAINPUTSTREAMREADFAIL);
242:                }
243:
244:                return null;
245:            }
246:
247:            /**
248:             * Returns the input stream
249:             * @deprecated 3.01C
250:             */
251:            public DataInputStream getInputStream() {
252:                return dis;
253:            }
254:
255:            /**
256:             * Returns the input stream
257:             */
258:            public InputStream getIS() {
259:                return is;
260:            }
261:
262:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.