1 28 29 package com.caucho.jms.message; 30 31 import javax.jms.JMSException ; 32 import javax.jms.MessageEOFException ; 33 import javax.jms.StreamMessage ; 34 import java.util.ArrayList ; 35 36 39 public class StreamMessageImpl extends MessageImpl implements StreamMessage { 40 private ArrayList <Object > _values = new ArrayList <Object >(); 41 private int _index; 42 private byte []_bytes; 43 private int _bytesOffset; 44 45 48 public void setReceive() 49 throws JMSException 50 { 51 super.setReceive(); 52 53 reset(); 54 } 55 56 59 public void reset() 60 throws JMSException 61 { 62 setBodyReadOnly(); 63 64 _index = 0; 65 _bytes = null; 66 _bytesOffset = 0; 67 } 68 69 72 public boolean readBoolean() 73 throws JMSException 74 { 75 boolean value = ObjectConverter.toBoolean(readObjectImpl()); 76 77 _index++; 78 79 return value; 80 } 81 82 85 public byte readByte() 86 throws JMSException 87 { 88 byte value = ObjectConverter.toByte(readObjectImpl()); 89 90 _index++; 91 92 return value; 93 } 94 95 98 public short readShort() 99 throws JMSException 100 { 101 short value = ObjectConverter.toShort(readObjectImpl()); 102 103 _index++; 104 105 return value; 106 } 107 108 111 public int readInt() 112 throws JMSException 113 { 114 int value = ObjectConverter.toInt(readObjectImpl()); 115 116 _index++; 117 118 return value; 119 } 120 121 124 public long readLong() 125 throws JMSException 126 { 127 long value = ObjectConverter.toLong(readObjectImpl()); 128 129 _index++; 130 131 return value; 132 } 133 134 137 public float readFloat() 138 throws JMSException 139 { 140 float value = ObjectConverter.toFloat(readObjectImpl()); 141 142 _index++; 143 144 return value; 145 } 146 147 150 public double readDouble() 151 throws JMSException 152 { 153 double value = ObjectConverter.toDouble(readObjectImpl()); 154 155 _index++; 156 157 return value; 158 } 159 160 163 public char readChar() 164 throws JMSException 165 { 166 char value = ObjectConverter.toChar(readObjectImpl()); 167 168 _index++; 169 170 return value; 171 } 172 173 176 public String readString() 177 throws JMSException 178 { 179 String value = ObjectConverter.toString(readObjectImpl()); 180 181 _index++; 182 183 return value; 184 } 185 186 189 public int readBytes(byte []value) 190 throws JMSException 191 { 192 byte []bytes; 193 194 if (_bytes != null) { 195 if (_bytesOffset == _bytes.length) { 196 _bytes = null; 197 _bytesOffset = 0; 198 return -1; 199 } 200 } 201 else { 202 _bytes = ObjectConverter.toBytes(readObjectImpl()); 203 _index++; 204 } 205 206 if (_bytes == null) 207 return -1; 208 209 int sublen = _bytes.length - _bytesOffset; 210 if (value.length < sublen) 211 sublen = value.length; 212 213 for (int i = 0; i < sublen; i++) 214 value[i] = _bytes[_bytesOffset++]; 215 216 return sublen; 217 } 218 219 222 public Object readObject() 223 throws JMSException 224 { 225 Object value = readObjectImpl(); 226 227 _index++; 228 229 return value; 230 } 231 232 235 private Object readObjectImpl() 236 throws JMSException 237 { 238 checkBodyReadable(); 239 240 if (_values.size() <= _index) 241 throw new MessageEOFException (L.l("end of message in stream")); 242 243 _bytes = null; 244 _bytesOffset = 0; 245 246 return _values.get(_index); 247 } 248 249 252 public void clearBody() 253 throws JMSException 254 { 255 super.clearBody(); 256 257 _values.clear(); 258 _index = 0; 259 _bytes = null; 260 _bytesOffset = 0; 261 } 262 263 266 public void writeBoolean(boolean b) 267 throws JMSException 268 { 269 writeObject(new Boolean (b)); 270 } 271 272 275 public void writeByte(byte b) 276 throws JMSException 277 { 278 writeObject(new Byte (b)); 279 } 280 281 284 public void writeShort(short s) 285 throws JMSException 286 { 287 writeObject(new Short (s)); 288 } 289 290 293 public void writeInt(int i) 294 throws JMSException 295 { 296 writeObject(new Integer (i)); 297 } 298 299 302 public void writeLong(long l) 303 throws JMSException 304 { 305 writeObject(new Long (l)); 306 } 307 308 311 public void writeFloat(float f) 312 throws JMSException 313 { 314 writeObject(new Float (f)); 315 } 316 317 320 public void writeDouble(double d) 321 throws JMSException 322 { 323 writeObject(new Double (d)); 324 } 325 326 329 public void writeString(String s) 330 throws JMSException 331 { 332 writeObject(s); 333 } 334 335 338 public void writeChar(char ch) 339 throws JMSException 340 { 341 writeObject(new Character (ch)); 342 } 343 344 347 public void writeBytes(byte []buf) 348 throws JMSException 349 { 350 writeBytes(buf, 0, buf.length); 351 } 352 353 356 public void writeBytes(byte []buf, int offset, int length) 357 throws JMSException 358 { 359 byte []newBuf = new byte[length]; 360 361 System.arraycopy(buf, offset, newBuf, 0, length); 362 363 writeObject(newBuf); 364 } 365 366 369 public void writeObject(Object obj) 370 throws JMSException 371 { 372 checkBodyWriteable(); 373 374 _values.add(obj); 375 } 376 377 public MessageImpl copy() 378 { 379 StreamMessageImpl msg = new StreamMessageImpl(); 380 381 copy(msg); 382 383 return msg; 384 } 385 386 protected void copy(StreamMessageImpl newMsg) 387 { 388 super.copy(newMsg); 389 390 newMsg._values = new ArrayList (_values); 391 newMsg._index = 0; 392 } 393 } 394 395 | Popular Tags |