Source Code Cross Referenced for NetworkClient.java in  » 6.0-JDK-Modules » j2me » sun » net » 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 » 6.0 JDK Modules » j2me » sun.net 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)NetworkClient.java	1.31 06/10/10
003:         *
004:         * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.  
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER  
006:         *   
007:         * This program is free software; you can redistribute it and/or  
008:         * modify it under the terms of the GNU General Public License version  
009:         * 2 only, as published by the Free Software Foundation.   
010:         *   
011:         * This program is distributed in the hope that it will be useful, but  
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of  
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  
014:         * General Public License version 2 for more details (a copy is  
015:         * included at /legal/license.txt).   
016:         *   
017:         * You should have received a copy of the GNU General Public License  
018:         * version 2 along with this work; if not, write to the Free Software  
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  
020:         * 02110-1301 USA   
021:         *   
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa  
023:         * Clara, CA 95054 or visit www.sun.com if you need additional  
024:         * information or have any questions. 
025:         *
026:         */
027:        package sun.net;
028:
029:        import java.io.*;
030:        import java.net.Socket;
031:        import java.net.InetAddress;
032:        import java.net.InetSocketAddress;
033:        import java.net.UnknownHostException;
034:        import java.net.URL;
035:        import sun.io.CharToByteConverter;
036:        import java.util.Arrays;
037:
038:        /**
039:         * This is the base class for network clients.
040:         *
041:         * @version	1.31 10/10/06
042:         * @author	Jonathan Payne
043:         */
044:        public class NetworkClient {
045:            /** Socket for communicating with server. */
046:            protected Socket serverSocket = null;
047:
048:            /** Stream for printing to the server. */
049:            public PrintStream serverOutput;
050:
051:            /** Buffered stream for reading replies from server. */
052:            public InputStream serverInput;
053:
054:            protected static int defaultSoTimeout;
055:            protected static int defaultConnectTimeout;
056:
057:            /* Name of encoding to use for output */
058:            protected static String encoding;
059:
060:            static {
061:                Integer tm = (Integer) java.security.AccessController
062:                        .doPrivileged(new java.security.PrivilegedAction() {
063:                            public Object run() {
064:                                return (Integer
065:                                        .getInteger("sun.net.client.defaultReadTimeout"));
066:                            }
067:                        });
068:                if (tm == null)
069:                    defaultSoTimeout = -1;
070:                else
071:                    defaultSoTimeout = tm.intValue();
072:
073:                tm = (Integer) java.security.AccessController
074:                        .doPrivileged(new java.security.PrivilegedAction() {
075:                            public Object run() {
076:                                return (Integer
077:                                        .getInteger("sun.net.client.defaultConnectTimeout"));
078:                            }
079:                        });
080:                if (tm == null)
081:                    defaultConnectTimeout = -1;
082:                else
083:                    defaultConnectTimeout = tm.intValue();
084:
085:                encoding = (String) java.security.AccessController
086:                        .doPrivileged(new sun.security.action.GetPropertyAction(
087:                                "file.encoding", "ISO8859_1"));
088:                try {
089:                    if (!isASCIISuperset(encoding)) {
090:                        encoding = "ISO8859_1";
091:                    }
092:                } catch (Exception e) {
093:                    encoding = "ISO8859_1";
094:                }
095:            }
096:
097:            /**
098:             * Test the named character encoding to verify that it converts ASCII 
099:             * characters correctly. We have to use an ASCII based encoding, or else 
100:             * the NetworkClients will not work correctly in EBCDIC based systems.
101:             * However, we cannot just use ASCII or ISO8859_1 universally, because in 
102:             * Asian locales, non-ASCII characters may be embedded in otherwise 
103:             * ASCII based protocols (eg. HTTP). The specifications (RFC2616, 2398) 
104:             * are a little ambiguous in this matter. For instance, RFC2398 [part 2.1] 
105:             * says that the HTTP request URI should be escaped using a defined 
106:             * mechanism, but there is no way to specify in the escaped string what 
107:             * the original character set is. It is not correct to assume that 
108:             * UTF-8 is always used (as in URLs in HTML 4.0).  For this reason, 
109:             * until the specifications are updated to deal with this issue more
110:             * comprehensively, and more importantly, HTTP servers are known to 
111:             * support these mechanisms, we will maintain the current behavior 
112:             * where it is possible to send non-ASCII characters in their original 
113:             * unescaped form. 
114:             */
115:            private static boolean isASCIISuperset(String encoding)
116:                    throws Exception {
117:                String chkS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
118:                        + "abcdefghijklmnopqrstuvwxyz-_.!~*'();/?:@&=+$,";
119:
120:                // Expected byte sequence for string above
121:                byte[] chkB = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66,
122:                        67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
123:                        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99,
124:                        100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
125:                        111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
126:                        122, 45, 95, 46, 33, 126, 42, 39, 40, 41, 59, 47, 63,
127:                        58, 64, 38, 61, 43, 36, 44 };
128:
129:                CharToByteConverter ctob = CharToByteConverter
130:                        .getConverter(encoding);
131:                byte[] b = ctob.convertAll(chkS.toCharArray());
132:                return Arrays.equals(b, chkB);
133:            }
134:
135:            /** Open a connection to the server. */
136:            public void openServer(String server, int port) throws IOException,
137:                    UnknownHostException {
138:                if (serverSocket != null)
139:                    closeServer();
140:                serverSocket = doConnect(server, port);
141:                try {
142:                    serverOutput = new PrintStream(new BufferedOutputStream(
143:                            serverSocket.getOutputStream()), true, encoding);
144:                } catch (UnsupportedEncodingException e) {
145:                    throw new InternalError(encoding + "encoding not found");
146:                }
147:                serverInput = new BufferedInputStream(serverSocket
148:                        .getInputStream());
149:            }
150:
151:            /**
152:             * Return a socket connected to the server, with any
153:             * appropriate options pre-established
154:             */
155:            protected Socket doConnect(String server, int port)
156:                    throws IOException, UnknownHostException {
157:                Socket s = new Socket();
158:                if (defaultConnectTimeout > 0) {
159:                    s.connect(new InetSocketAddress(server, port),
160:                            defaultConnectTimeout);
161:                } else {
162:                    s.connect(new InetSocketAddress(server, port));
163:                }
164:                if (defaultSoTimeout > 0) {
165:                    s.setSoTimeout(defaultSoTimeout);
166:                }
167:                return s;
168:            }
169:
170:            protected InetAddress getLocalAddress() throws IOException {
171:                if (serverSocket == null)
172:                    throw new IOException("not connected");
173:                return serverSocket.getLocalAddress();
174:            }
175:
176:            /** Close an open connection to the server. */
177:            public void closeServer() throws IOException {
178:                if (!serverIsOpen()) {
179:                    return;
180:                }
181:                serverSocket.close();
182:                serverSocket = null;
183:                serverInput = null;
184:                serverOutput = null;
185:            }
186:
187:            /** Return server connection status */
188:            public boolean serverIsOpen() {
189:                return serverSocket != null;
190:            }
191:
192:            /** Create connection with host <i>host</i> on port <i>port</i> */
193:            public NetworkClient(String host, int port) throws IOException {
194:                openServer(host, port);
195:            }
196:
197:            public NetworkClient() {
198:            }
199:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.