Source Code Cross Referenced for ObjectConnection.java in  » Groupware » lucane » org » lucane » common » 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 » Groupware » lucane » org.lucane.common.net 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Lucane - a collaborative platform
003:         * Copyright (C) 2002-2004  Vincent Fiack <vfiack@mail15.com>
004:         *
005:         * This library is free software; you can redistribute it and/or
006:         * modify it under the terms of the GNU Lesser General Public
007:         * License as published by the Free Software Foundation; either
008:         * version 2.1 of the License, or (at your option) any later version.
009:         *
010:         * This library is distributed in the hope that it will be useful,
011:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013:         * Lesser General Public License for more details.
014:         *
015:         * You should have received a copy of the GNU Lesser General Public
016:         * License along with this library; if not, write to the Free Software
017:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018:         */
019:        package org.lucane.common.net;
020:
021:        import java.io.*;
022:        import java.net.*;
023:
024:        /**
025:         * An object connection
026:         */
027:        public class ObjectConnection {
028:            //-- attributes
029:            private Socket socket;
030:            private OutputStream output;
031:            private InputStream input;
032:            private ObjectListeningThread listener;
033:
034:            /**
035:             * Constructor
036:             * 
037:             * @param socket the underlying socket
038:             */
039:            public ObjectConnection(Socket socket) {
040:                this .listener = new ObjectListeningThread(this );
041:                this .socket = socket;
042:                try {
043:                    this .output = socket.getOutputStream();
044:                    this .input = socket.getInputStream();
045:                } catch (Exception e) {
046:                }
047:            }
048:
049:            /**
050:             * Add an ObjectListener to this connection
051:             * 
052:             * @param ol the listener
053:             */
054:            public void addObjectListener(ObjectListener ol) {
055:                this .listener.addObjectListener(ol);
056:            }
057:
058:            /**
059:             * Start the listening thread
060:             */
061:            public void listen() {
062:                this .listener.start();
063:            }
064:
065:            /**
066:             * Write an object
067:             *
068:             * @param o the object to write
069:             */
070:            public void write(Serializable o) throws IOException {
071:                byte[] data = ObjectZipper.objectToBytes(o);
072:                byte[] size = intToBytes(data.length);
073:
074:                this .output.write(size);
075:                this .output.write(data);
076:                this .output.flush();
077:            }
078:
079:            /**
080:             * Check if we have something to read
081:             * 
082:             * @return true if we can read
083:             */
084:            public boolean readyToRead() {
085:                try {
086:                    return this .socket.getInputStream().available() > 0;
087:                } catch (Exception e) {
088:                    e.printStackTrace();
089:                }
090:
091:                return false;
092:            }
093:
094:            /**
095:             * Read an object
096:             * 
097:             * @return the object read
098:             */
099:            public Serializable read() throws IOException,
100:                    ClassNotFoundException {
101:                byte[] size = new byte[4];
102:                this .input.read(size);
103:
104:                byte[] data = new byte[bytesToInt(size)];
105:
106:                int length = 0;
107:                while (length < data.length)
108:                    length += this .input.read(data, length, data.length
109:                            - length);
110:
111:                return ObjectZipper.bytesToObject(data);
112:            }
113:
114:            /**
115:             * Read a string
116:             * 
117:             * @return the string read
118:             */
119:            public String readString() throws IOException,
120:                    ClassNotFoundException {
121:                return (String) this .read();
122:            }
123:
124:            /**
125:             * Close this connection
126:             */
127:            public void close() {
128:                this .listener.close();
129:
130:                try {
131:                    output.close();
132:                    input.close();
133:                    socket.close();
134:                } catch (Exception e) {
135:                }
136:            }
137:
138:            //-- utilities
139:
140:            /**
141:             * Convert 1 int to 4 bytes
142:             */
143:            protected static byte[] intToBytes(int value) {
144:                byte[] bytes = new byte[4];
145:
146:                for (int i = 0; i < bytes.length; i++) {
147:                    int offset = (bytes.length - i - 1) * 8;
148:                    int mask = 0xff << offset;
149:                    bytes[i] = (byte) (value >>> offset);
150:                }
151:
152:                return bytes;
153:            }
154:
155:            /**
156:             * Convert 4 bytes to 1 int
157:             */
158:            protected static int bytesToInt(byte[] bytes) {
159:                int value = 0;
160:
161:                for (int i = 0; i < bytes.length; i++) {
162:                    value = value << 8;
163:                    value += bytes[i] & 0xff;
164:                }
165:
166:                return value;
167:            }
168:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.