1 18 package org.apache.activemq.openwire; 19 20 import java.io.DataInput ; 21 import java.io.DataInputStream ; 22 import java.io.DataOutput ; 23 import java.io.DataOutputStream ; 24 import java.io.IOException ; 25 import java.nio.ByteBuffer ; 26 27 final public class BooleanStream { 28 29 byte data[] = new byte[48]; 30 short arrayLimit; 31 short arrayPos; 32 byte bytePos; 33 34 public boolean readBoolean() throws IOException { 35 assert arrayPos <= arrayLimit; 36 byte b = data[arrayPos]; 37 boolean rc = ((b>>bytePos)&0x01)!=0; 38 bytePos++; 39 if( bytePos >= 8 ) { 40 bytePos=0; 41 arrayPos++; 42 } 43 return rc; 44 } 45 46 public void writeBoolean(boolean value) throws IOException { 47 if( bytePos == 0 ) { 48 arrayLimit++; 49 if( arrayLimit >= data.length ) { 50 byte d[] = new byte[data.length*2]; 52 System.arraycopy(data, 0, d, 0, data.length); 53 data = d; 54 } 55 } 56 if( value ) { 57 data[arrayPos] |= (0x01 << bytePos); 58 } 59 bytePos++; 60 if( bytePos >= 8 ) { 61 bytePos=0; 62 arrayPos++; 63 } 64 } 65 66 public void marshal(DataOutput dataOut) throws IOException { 67 if( arrayLimit < 64 ) { 68 dataOut.writeByte(arrayLimit); 69 } else if( arrayLimit < 256 ) { dataOut.writeByte(0xC0); 71 dataOut.writeByte(arrayLimit); 72 } else { 73 dataOut.writeByte(0x80); 74 dataOut.writeShort(arrayLimit); 75 } 76 77 dataOut.write(data, 0, arrayLimit); 78 clear(); 79 } 80 81 public void marshal(ByteBuffer dataOut) { 82 if( arrayLimit < 64 ) { 83 dataOut.put((byte) arrayLimit); 84 } else if( arrayLimit < 256 ) { dataOut.put((byte) 0xC0); 86 dataOut.put((byte) arrayLimit); 87 } else { 88 dataOut.put((byte) 0x80); 89 dataOut.putShort(arrayLimit); 90 } 91 92 dataOut.put(data, 0, arrayLimit); 93 } 94 95 96 public void unmarshal(DataInput dataIn) throws IOException { 97 98 arrayLimit = (short) (dataIn.readByte() & 0xFF); 99 if ( arrayLimit == 0xC0 ) { 100 arrayLimit = (short)(dataIn.readByte() & 0xFF); 101 } else if( arrayLimit == 0x80 ) { 102 arrayLimit = dataIn.readShort(); 103 } 104 if( data.length < arrayLimit ) { 105 data = new byte[arrayLimit]; 106 } 107 dataIn.readFully(data, 0, arrayLimit); 108 clear(); 109 } 110 111 public void clear() { 112 arrayPos=0; 113 bytePos=0; 114 } 115 116 public int marshalledSize() { 117 if( arrayLimit < 64 ) { 118 return 1+arrayLimit; 119 } else if (arrayLimit < 256) { 120 return 2+arrayLimit; 121 } else { 122 return 3+arrayLimit; 123 } 124 } 125 126 127 } 128 | Popular Tags |