KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > openwire > BooleanStream


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.openwire;
19
20 import java.io.DataInput JavaDoc;
21 import java.io.DataInputStream JavaDoc;
22 import java.io.DataOutput JavaDoc;
23 import java.io.DataOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.nio.ByteBuffer JavaDoc;
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 JavaDoc {
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 JavaDoc {
47         if( bytePos == 0 ) {
48             arrayLimit++;
49             if( arrayLimit >= data.length ) {
50                 // re-grow the array.
51
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 JavaDoc dataOut) throws IOException JavaDoc {
67         if( arrayLimit < 64 ) {
68             dataOut.writeByte(arrayLimit);
69         } else if( arrayLimit < 256 ) { // max value of unsigned byte
70
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 JavaDoc dataOut) {
82         if( arrayLimit < 64 ) {
83             dataOut.put((byte) arrayLimit);
84         } else if( arrayLimit < 256 ) { // max value of unsigned byte
85
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 JavaDoc dataIn) throws IOException JavaDoc {
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