KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > common > net > ObjectConnection


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2002-2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.common.net;
20
21 import java.io.*;
22 import java.net.*;
23
24
25 /**
26  * An object connection
27  */

28 public class ObjectConnection
29 {
30     //-- attributes
31
private Socket socket;
32     private OutputStream output;
33     private InputStream input;
34     private ObjectListeningThread listener;
35     
36     /**
37      * Constructor
38      *
39      * @param socket the underlying socket
40      */

41     public ObjectConnection(Socket socket)
42     {
43         this.listener = new ObjectListeningThread(this);
44         this.socket = socket;
45         try {
46             this.output = socket.getOutputStream();
47             this.input = socket.getInputStream();
48         } catch(Exception JavaDoc e) {}
49     }
50     
51     /**
52      * Add an ObjectListener to this connection
53      *
54      * @param ol the listener
55      */

56     public void addObjectListener(ObjectListener ol)
57     {
58       this.listener.addObjectListener(ol);
59     }
60
61     /**
62      * Start the listening thread
63      */

64     public void listen()
65     {
66       this.listener.start();
67     }
68        
69     /**
70      * Write an object
71      *
72      * @param o the object to write
73      */

74     public void write(Serializable o)
75     throws IOException
76     {
77         byte[] data = ObjectZipper.objectToBytes(o);
78         byte[] size = intToBytes(data.length);
79         
80         this.output.write(size);
81         this.output.write(data);
82         this.output.flush();
83     }
84         
85     /**
86      * Check if we have something to read
87      *
88      * @return true if we can read
89      */

90     public boolean readyToRead()
91     {
92         try {
93             return this.socket.getInputStream().available() > 0;
94         } catch(Exception JavaDoc e) {e.printStackTrace();}
95         
96         return false;
97     }
98     
99     /**
100      * Read an object
101      *
102      * @return the object read
103      */

104     public Serializable read()
105     throws IOException, ClassNotFoundException JavaDoc
106     {
107         byte[] size = new byte[4];
108         this.input.read(size);
109                 
110         byte[] data = new byte[bytesToInt(size)];
111         
112         int length = 0;
113         while(length < data.length)
114             length += this.input.read(data, length, data.length-length);
115
116         return ObjectZipper.bytesToObject(data);
117     }
118     
119     /**
120      * Read a string
121      *
122      * @return the string read
123      */

124     public String JavaDoc readString()
125     throws IOException, ClassNotFoundException JavaDoc
126     {
127         return (String JavaDoc)this.read();
128     }
129     
130     /**
131      * Close this connection
132      */

133     public void close()
134     {
135         this.listener.close();
136
137         try {
138             output.close();
139             input.close();
140             socket.close();
141         } catch(Exception JavaDoc e) {}
142     }
143     
144     //-- utilities
145

146     /**
147      * Convert 1 int to 4 bytes
148      */

149     protected static byte[] intToBytes(int value)
150     {
151         byte[] bytes = new byte[4];
152         
153         for(int i=0; i<bytes.length; i++)
154         {
155             int offset = (bytes.length-i-1) * 8;
156             int mask = 0xff << offset;
157             bytes[i] = (byte)(value >>> offset);
158         }
159         
160         return bytes;
161     }
162     
163     /**
164      * Convert 4 bytes to 1 int
165      */

166     protected static int bytesToInt(byte[] bytes)
167     {
168         int value = 0;
169
170         for(int i=0; i<bytes.length; i++)
171         {
172             value = value << 8;
173             value += bytes[i] & 0xff;
174         }
175
176         return value;
177     }
178 }
179
Popular Tags