Source Code Cross Referenced for BlockingClient.java in  » Net » QuickServer » org » quickserver » net » client » 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 » QuickServer » org.quickserver.net.client 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * This file is part of the QuickServer library 
003:         * Copyright (C) 2003-2005 QuickServer.org
004:         *
005:         * Use, modification, copying and distribution of this software is subject to
006:         * the terms and conditions of the GNU Lesser General Public License. 
007:         * You should have received a copy of the GNU LGP License along with this 
008:         * library; if not, you can download a copy from <http://www.quickserver.org/>.
009:         *
010:         * For questions, suggestions, bug-reports, enhancement-requests etc.
011:         * visit http://www.quickserver.org
012:         *
013:         */
014:
015:        package org.quickserver.net.client;
016:
017:        import java.io.*;
018:        import java.net.*;
019:        import java.util.logging.*;
020:
021:        /**
022:         * Blocking Client socket.
023:         * @author Akshathkumar Shetty
024:         * @since 1.4.7
025:         */
026:        public class BlockingClient implements  ClientService {
027:            private static Logger logger = Logger
028:                    .getLogger(BlockingClient.class.getName());
029:            private static String charset = "ISO-8859-1";
030:
031:            private String host = "localhost";
032:            private int port = 0;
033:
034:            private Socket socket;
035:
036:            private OutputStream out;
037:            private BufferedOutputStream b_out;
038:            private ObjectOutputStream o_out;
039:
040:            private InputStream in;
041:            private BufferedInputStream b_in;
042:            private BufferedReader br;
043:            private ObjectInputStream o_in;
044:
045:            public int getMode() {
046:                return ClientService.BLOCKING;
047:            }
048:
049:            public void connect(String host, int port) throws IOException {
050:                this .host = host;
051:                this .port = port;
052:
053:                logger.fine("Connecting to " + host + ":" + port);
054:                socket = new Socket(host, port);
055:
056:                in = socket.getInputStream();
057:                out = socket.getOutputStream();
058:                logger.fine("Connected");
059:            }
060:
061:            public boolean isConnected() {
062:                if (socket == null)
063:                    return false;
064:                return socket.isConnected();
065:            }
066:
067:            public void close() throws IOException {
068:                logger.fine("Closing");
069:                try {
070:                    if (out != null) {
071:                        logger.finest("Closing output streams");
072:                        try {
073:                            out.flush();
074:                        } catch (IOException ioe) {
075:                            logger.finest("Flushing output streams failed: "
076:                                    + ioe);
077:                        }
078:
079:                        if (socket != null /*&& isSecure()==false*/) {
080:                            socket.shutdownOutput();
081:                        }
082:                        if (o_out != null) {
083:                            o_out.close();
084:                        }
085:                        if (b_out != null) {
086:                            b_out.close();
087:                        }
088:                        if (out != null)
089:                            out.close();
090:                    }
091:
092:                    if (in != null) {
093:                        logger.finest("Closing input streams");
094:                        /*
095:                        if(socket!=null) {
096:                        	socket.shutdownInput();
097:                        }*/
098:
099:                        if (o_in != null) {
100:                            o_in.close();
101:                        }
102:                        if (b_in != null) {
103:                            b_in.close();
104:                        }
105:                        if (br != null) {
106:                            br.close();
107:                        }
108:                        if (in != null)
109:                            in.close();
110:                    }
111:                } catch (IOException e) {
112:                    logger.warning("Error in closing streams: " + e);
113:                }
114:                socket.close();
115:                socket = null;
116:            }
117:
118:            public void sendBinary(byte[] data) throws IOException {
119:                logger.fine("Sending bytes: " + data.length);
120:                checkBufferedOutputStream();
121:                b_out.write(data);
122:                b_out.flush();
123:            }
124:
125:            public void sendBytes(String data) throws IOException {
126:                logger.fine("Sending: " + data);
127:                checkBufferedOutputStream();
128:                byte d[] = data.getBytes(charset);
129:                b_out.write(d, 0, d.length);
130:                b_out.flush();
131:            }
132:
133:            public void sendString(String data) throws IOException {
134:                logger.fine("Sending: " + data);
135:                checkBufferedOutputStream();
136:                byte d[] = data.getBytes(charset);
137:                b_out.write(d, 0, d.length);
138:                d = "\r\n".getBytes(charset);
139:                b_out.write(d, 0, d.length);
140:                b_out.flush();
141:            }
142:
143:            public void sendObject(Object data) throws IOException {
144:                checkObjectOutputStream();
145:                o_out.writeObject(data);
146:                o_out.flush();
147:            }
148:
149:            public byte[] readBinary() throws IOException {
150:                checkBufferedInputStream();
151:                return readInputStream(b_in);
152:            }
153:
154:            public String readBytes() throws IOException {
155:                byte data[] = readBinary();
156:                return new String(data, charset);
157:            }
158:
159:            public String readString() throws IOException {
160:                checkBufferedReader();
161:                return br.readLine();
162:            }
163:
164:            public Object readObject() throws IOException,
165:                    ClassNotFoundException {
166:                checkObjectInputStream();
167:                return o_in.readObject();
168:            }
169:
170:            public Socket getSocket() {
171:                return socket;
172:            }
173:
174:            private void checkObjectOutputStream() throws IOException {
175:                if (o_out == null) {
176:                    b_out = null;
177:                    o_out = new ObjectOutputStream(out);
178:                    o_out.flush();
179:                }
180:            }
181:
182:            private void checkBufferedOutputStream() throws IOException {
183:                if (b_out == null) {
184:                    o_out = null;
185:                    b_out = new BufferedOutputStream(out);
186:                }
187:            }
188:
189:            private void checkBufferedInputStream() throws IOException {
190:                if (b_in == null) {
191:                    br = null;
192:                    o_in = null;
193:                    b_in = new BufferedInputStream(in);
194:                }
195:            }
196:
197:            private void checkBufferedReader() throws IOException {
198:                if (br == null) {
199:                    b_in = null;
200:                    o_in = null;
201:                    br = new BufferedReader(new InputStreamReader(in));
202:                }
203:            }
204:
205:            private void checkObjectInputStream() throws IOException {
206:                if (o_in == null) {
207:                    b_in = null;
208:                    br = null;
209:                    o_in = new ObjectInputStream(in);
210:                }
211:            }
212:
213:            protected static byte[] readInputStream(InputStream _in)
214:                    throws IOException {
215:                byte data[] = null;
216:                if (_in == null)
217:                    throw new IOException("InputStream can't be null!");
218:
219:                int s = _in.read();
220:                if (s == -1) {
221:                    return null; //Connection lost
222:                }
223:                int alength = _in.available();
224:                if (alength > 0) {
225:                    data = new byte[alength + 1];
226:                    _in.read(data, 1, alength);
227:                } else {
228:                    data = new byte[1];
229:                }
230:                data[0] = (byte) s;
231:                return data;
232:            }
233:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.