BBDecoder.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.transport.codec;
022 
023 import java.nio.ByteBuffer;
024 import java.nio.ByteOrder;
025 
026 import org.apache.qpid.transport.Binary;
027 
028 /**
029  * Byte Buffer Decoder.
030  * Decoder concrete implementor using a backing byte buffer for decoding data.
031  *
032  @author Rafael H. Schloming
033  */
034 public final class BBDecoder extends AbstractDecoder
035 {
036     private ByteBuffer in;
037 
038     public void init(ByteBuffer in)
039     {
040         this.in = in;
041         this.in.order(ByteOrder.BIG_ENDIAN);
042     }
043 
044     protected byte doGet()
045     {
046         return in.get();
047     }
048 
049     protected void doGet(byte[] bytes)
050     {
051         in.get(bytes);
052     }
053 
054     protected Binary get(int size)
055     {
056         if (in.hasArray())
057         {
058             byte[] bytes = in.array();
059             Binary bin = new Binary(bytes, in.arrayOffset() + in.position(), size);
060             in.position(in.position() + size);
061             return bin;
062         }
063         else
064         {
065             return super.get(size);
066         }
067     }
068 
069     public boolean hasRemaining()
070     {
071         return in.hasRemaining();
072     }
073 
074     public short readUint8()
075     {
076         return (short) (0xFF & in.get());
077     }
078 
079     public int readUint16()
080     {
081         return 0xFFFF & in.getShort();
082     }
083 
084     public long readUint32()
085     {
086         return 0xFFFFFFFFL & in.getInt();
087     }
088 
089     public long readUint64()
090     {
091         return in.getLong();
092     }
093 
094   public byte[] readBin128()
095   {
096     byte[] result = new byte[16];
097     get(result);
098     return result;
099   }
100   
101   public byte[] readBytes(int howManyBytes)
102   {
103     byte[] result = new byte[howManyBytes];
104     get(result);
105     return result;
106   }
107   
108   public double readDouble()
109   {
110     return in.getDouble();
111   }
112 
113   public float readFloat()
114   {
115     return in.getFloat();
116   }
117 
118   public short readInt16()
119   {
120     return in.getShort();
121   }
122 
123   public int readInt32()
124   {
125     return in.getInt();
126   }
127 
128   public byte readInt8()
129   {
130     return in.get();
131   }
132 
133   public byte[] readReaminingBytes()
134   {
135       byte[] result = new byte[in.limit() - in.position()];
136       get(result);
137       return result;    
138   }
139 
140   public long readInt64()
141   {
142     return in.getLong();
143   }
144 }