001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.openwire;
018    
019    import java.io.DataInput;
020    import java.io.DataOutput;
021    import java.io.IOException;
022    import java.nio.ByteBuffer;
023    
024    public final class BooleanStream {
025    
026        byte data[] = new byte[48];
027        short arrayLimit;
028        short arrayPos;
029        byte bytePos;
030    
031        public boolean readBoolean() throws IOException {
032            assert arrayPos <= arrayLimit;
033            byte b = data[arrayPos];
034            boolean rc = ((b >> bytePos) & 0x01) != 0;
035            bytePos++;
036            if (bytePos >= 8) {
037                bytePos = 0;
038                arrayPos++;
039            }
040            return rc;
041        }
042    
043        public void writeBoolean(boolean value) throws IOException {
044            if (bytePos == 0) {
045                arrayLimit++;
046                if (arrayLimit >= data.length) {
047                    // re-grow the array.
048                    byte d[] = new byte[data.length * 2];
049                    System.arraycopy(data, 0, d, 0, data.length);
050                    data = d;
051                }
052            }
053            if (value) {
054                data[arrayPos] |= 0x01 << bytePos;
055            }
056            bytePos++;
057            if (bytePos >= 8) {
058                bytePos = 0;
059                arrayPos++;
060            }
061        }
062    
063        public void marshal(DataOutput dataOut) throws IOException {
064            if (arrayLimit < 64) {
065                dataOut.writeByte(arrayLimit);
066            } else if (arrayLimit < 256) { // max value of unsigned byte
067                dataOut.writeByte(0xC0);
068                dataOut.writeByte(arrayLimit);
069            } else {
070                dataOut.writeByte(0x80);
071                dataOut.writeShort(arrayLimit);
072            }
073    
074            dataOut.write(data, 0, arrayLimit);
075            clear();
076        }
077    
078        public void marshal(ByteBuffer dataOut) {
079            if (arrayLimit < 64) {
080                dataOut.put((byte)arrayLimit);
081            } else if (arrayLimit < 256) { // max value of unsigned byte
082                dataOut.put((byte)0xC0);
083                dataOut.put((byte)arrayLimit);
084            } else {
085                dataOut.put((byte)0x80);
086                dataOut.putShort(arrayLimit);
087            }
088    
089            dataOut.put(data, 0, arrayLimit);
090        }
091    
092        public void unmarshal(DataInput dataIn) throws IOException {
093    
094            arrayLimit = (short)(dataIn.readByte() & 0xFF);
095            if (arrayLimit == 0xC0) {
096                arrayLimit = (short)(dataIn.readByte() & 0xFF);
097            } else if (arrayLimit == 0x80) {
098                arrayLimit = dataIn.readShort();
099            }
100            if (data.length < arrayLimit) {
101                data = new byte[arrayLimit];
102            }
103            dataIn.readFully(data, 0, arrayLimit);
104            clear();
105        }
106    
107        public void clear() {
108            arrayPos = 0;
109            bytePos = 0;
110        }
111    
112        public int marshalledSize() {
113            if (arrayLimit < 64) {
114                return 1 + arrayLimit;
115            } else if (arrayLimit < 256) {
116                return 2 + arrayLimit;
117            } else {
118                return 3 + arrayLimit;
119            }
120        }
121    
122    }