1 /**********************************************************************
2 * ConnectionHeader.java
3 * created on 04.03.2005 by netseeker
4 * $Source: /cvsroot/ejoe/EJOE/src/de/netseeker/ejoe/ConnectionHeader.java,v $
5 * $Date: 2006/02/04 14:16:35 $
6 * $Revision: 1.17 $
7 *********************************************************************/
8 package de.netseeker.ejoe;
9
10 import java.io.Serializable;
11 import java.io.UnsupportedEncodingException;
12 import java.nio.ByteBuffer;
13 import java.nio.channels.SelectableChannel;
14 import java.util.zip.Deflater;
15
16 /***
17 * A simple connection header contining informations about compression and
18 * blocking/non-blocking io features.
19 *
20 * @author netseeker
21 */
22 public final class ConnectionHeader implements Serializable
23 {
24 private static final long serialVersionUID = 3258131375298458928L;
25
26 private boolean[] _header;
27
28 private transient ByteBuffer _waitingBuffer;
29
30 private transient SelectableChannel _channel;
31
32 private Object _attachment;
33
34 private int _compressionLevel = Deflater.DEFAULT_COMPRESSION;
35
36 private String _adapterClass;
37
38 /***
39 *
40 */
41 public ConnectionHeader()
42 {
43 _header = new boolean[] { false, false, false, false, false, false, false, false };
44 }
45
46 /***
47 * @param channel
48 */
49 public ConnectionHeader(SelectableChannel channel)
50 {
51 this();
52 this._channel = channel;
53 }
54
55 /***
56 * @param channel
57 * @param header
58 */
59 public ConnectionHeader(SelectableChannel channel, byte header)
60 {
61 this._channel = channel;
62 fromBytes(header);
63 }
64
65 public boolean hasCompression()
66 {
67 return _header[0];
68 }
69
70 public void setCompression(boolean enable)
71 {
72 _header[0] = enable;
73 }
74
75 public void setCompressionLevel(int level)
76 {
77 this._compressionLevel = level;
78 }
79
80 public int getCompressionLevel()
81 {
82 return this._compressionLevel;
83 }
84
85 public boolean hasNonBlockingReadWrite()
86 {
87 return _header[1];
88 }
89
90 public void setNonBlockingReadWrite(boolean enable)
91 {
92 _header[1] = enable;
93 }
94
95 public boolean isConnected()
96 {
97 return _header[2];
98 }
99
100 public void setConnected(boolean enable)
101 {
102 _header[2] = enable;
103 }
104
105 public boolean isPersistent()
106 {
107 return _header[3];
108 }
109
110 public void setPersistent(boolean enable)
111 {
112 _header[3] = enable;
113 }
114
115 public boolean hasWaitingBuffer()
116 {
117 return this._waitingBuffer != null;
118 }
119
120 public ByteBuffer getWaitingBuffer()
121 {
122 return this._waitingBuffer;
123 }
124
125 public void setWaitingBuffer(ByteBuffer buf)
126 {
127 this._waitingBuffer = buf;
128 }
129
130 public void releaseWaitingBuffer()
131 {
132 if (this._waitingBuffer != null)
133 {
134 this._waitingBuffer = null;
135 }
136 }
137
138 public boolean hasAttachment()
139 {
140 return this._attachment != null;
141 }
142
143 public void setAttachment(Object attachment)
144 {
145 this._attachment = attachment;
146 }
147
148 public Object getAttachment()
149 {
150 return this._attachment;
151 }
152
153 public void releaseAttachment()
154 {
155 if (this._attachment != null)
156 {
157 this._attachment = null;
158 }
159 }
160
161 public void setChannel(SelectableChannel channel)
162 {
163 this._channel = channel;
164 }
165
166 public SelectableChannel getChannel()
167 {
168 return this._channel;
169 }
170
171 public void setAdapterName(String name)
172 {
173 if (name == null)
174 {
175 throw new NullPointerException("Name of the adapter class must not be null!");
176 }
177
178 this._adapterClass = name;
179 }
180
181 public String getAdapterName()
182 {
183 return this._adapterClass;
184 }
185
186 public byte toByte()
187 {
188 return bitsToByte(_header);
189 }
190
191 public void fromBytes(byte header)
192 {
193 _header = byteToBits(header);
194 }
195
196 public String toString()
197 {
198 StringBuffer buf = new StringBuffer();
199 for (int i = 0; i < _header.length; i++)
200 {
201 buf.append(_header[i] ? 1 : 0);
202 }
203
204 return buf.toString();
205 }
206
207 public ByteBuffer toByteBuffer()
208 {
209 byte[] adapterArr = null;
210 try
211 {
212 adapterArr = _adapterClass.getBytes("UTF8");
213 }
214 catch (UnsupportedEncodingException e)
215 {
216
217 }
218 int length = adapterArr.length;
219 ByteBuffer buf = ByteBuffer.allocateDirect(5 + length);
220 buf.put(toByte());
221 buf.putInt(length);
222 buf.put(adapterArr);
223 buf.flip();
224
225 return buf;
226 }
227
228 private final static byte bitsToByte(boolean[] bits)
229 {
230 int value = 0;
231 for (int i = 0; i < bits.length; i++)
232 {
233 if (bits[i] == true)
234 {
235 value = value | (1 << i);
236 }
237 }
238 return (byte) value;
239 }
240
241 private final static boolean[] byteToBits(byte b)
242 {
243 boolean[] bits = new boolean[8];
244 for (int i = 0; i < bits.length; i++)
245 {
246 bits[i] = ((b & (1 << i)) != 0);
247 }
248 return bits;
249 }
250 }