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 */
017package org.apache.activemq.util;
018
019import java.io.IOException;
020
021/**
022 * Used to write and read primitives to and from a ByteSequence.
023 */
024public final class ByteSequenceData {
025
026    private ByteSequenceData() {    
027    }
028    
029    public static byte[] toByteArray(ByteSequence packet) {
030        if (packet.offset == 0 && packet.length == packet.data.length) {
031            return packet.data;
032        }
033
034        byte rc[] = new byte[packet.length];
035        System.arraycopy(packet.data, packet.offset, rc, 0, packet.length);
036        return rc;
037    }
038
039    private static void spaceNeeded(ByteSequence packet, int i) {
040        assert packet.offset + i <= packet.length;
041    }
042
043    public static int remaining(ByteSequence packet) {
044        return packet.length - packet.offset;
045    }
046
047    public static int read(ByteSequence packet) {
048        return packet.data[packet.offset++] & 0xff;
049    }
050
051    public static void readFully(ByteSequence packet, byte[] b) throws IOException {
052        readFully(packet, b, 0, b.length);
053    }
054
055    public static void readFully(ByteSequence packet, byte[] b, int off, int len) throws IOException {
056        spaceNeeded(packet, len);
057        System.arraycopy(packet.data, packet.offset, b, off, len);
058        packet.offset += len;
059    }
060
061    public static int skipBytes(ByteSequence packet, int n) throws IOException {
062        int rc = Math.min(n, remaining(packet));
063        packet.offset += rc;
064        return rc;
065    }
066
067    public static boolean readBoolean(ByteSequence packet) throws IOException {
068        spaceNeeded(packet, 1);
069        return read(packet) != 0;
070    }
071
072    public static byte readByte(ByteSequence packet) throws IOException {
073        spaceNeeded(packet, 1);
074        return (byte)read(packet);
075    }
076
077    public static int readUnsignedByte(ByteSequence packet) throws IOException {
078        spaceNeeded(packet, 1);
079        return read(packet);
080    }
081
082    public static short readShortBig(ByteSequence packet) throws IOException {
083        spaceNeeded(packet, 2);
084        return (short)((read(packet) << 8) + (read(packet) << 0));
085    }
086
087    public static short readShortLittle(ByteSequence packet) throws IOException {
088        spaceNeeded(packet, 2);
089        return (short)((read(packet) << 0) + (read(packet) << 8));
090    }
091
092    public static int readUnsignedShortBig(ByteSequence packet) throws IOException {
093        spaceNeeded(packet, 2);
094        return (read(packet) << 8) + (read(packet) << 0);
095    }
096
097    public static int readUnsignedShortLittle(ByteSequence packet) throws IOException {
098        spaceNeeded(packet, 2);
099        return (read(packet) << 0) + (read(packet) << 8);
100    }
101
102    public static char readCharBig(ByteSequence packet) throws IOException {
103        spaceNeeded(packet, 2);
104        return (char)((read(packet) << 8) + (read(packet) << 0));
105    }
106
107    public static char readCharLittle(ByteSequence packet) throws IOException {
108        spaceNeeded(packet, 2);
109        return (char)((read(packet) << 0) + (read(packet) << 8));
110    }
111
112    public static int readIntBig(ByteSequence packet) throws IOException {
113        spaceNeeded(packet, 4);
114        return (read(packet) << 24) + (read(packet) << 16) + (read(packet) << 8) + (read(packet) << 0);
115    }
116
117    public static int readIntLittle(ByteSequence packet) throws IOException {
118        spaceNeeded(packet, 4);
119        return (read(packet) << 0) + (read(packet) << 8) + (read(packet) << 16) + (read(packet) << 24);
120    }
121
122    public static long readLongBig(ByteSequence packet) throws IOException {
123        spaceNeeded(packet, 8);
124        return ((long)read(packet) << 56) + ((long)read(packet) << 48) + ((long)read(packet) << 40) + ((long)read(packet) << 32) + ((long)read(packet) << 24)
125                + ((read(packet)) << 16) + ((read(packet)) << 8) + ((read(packet)) << 0);
126    }
127
128    public static long readLongLittle(ByteSequence packet) throws IOException {
129        spaceNeeded(packet, 8);
130        return (read(packet) << 0) + (read(packet) << 8) + (read(packet) << 16) + ((long)read(packet) << 24) + ((long)read(packet) << 32) + ((long)read(packet) << 40)
131                + ((long)read(packet) << 48) + ((long)read(packet) << 56);
132    }
133
134    public static double readDoubleBig(ByteSequence packet) throws IOException {
135        return Double.longBitsToDouble(readLongBig(packet));
136    }
137
138    public static double readDoubleLittle(ByteSequence packet) throws IOException {
139        return Double.longBitsToDouble(readLongLittle(packet));
140    }
141
142    public static float readFloatBig(ByteSequence packet) throws IOException {
143        return Float.intBitsToFloat(readIntBig(packet));
144    }
145
146    public static float readFloatLittle(ByteSequence packet) throws IOException {
147        return Float.intBitsToFloat(readIntLittle(packet));
148    }
149
150    public static void write(ByteSequence packet, int b) throws IOException {
151        spaceNeeded(packet, 1);
152        packet.data[packet.offset++] = (byte)b;
153    }
154
155    public static void write(ByteSequence packet, byte[] b) throws IOException {
156        write(packet, b, 0, b.length);
157    }
158
159    public static void write(ByteSequence packet, byte[] b, int off, int len) throws IOException {
160        spaceNeeded(packet, len);
161        System.arraycopy(b, off, packet.data, packet.offset, len);
162        packet.offset += len;
163    }
164
165    public static void writeBoolean(ByteSequence packet, boolean v) throws IOException {
166        spaceNeeded(packet, 1);
167        write(packet, v ? 1 : 0);
168    }
169
170    public static void writeByte(ByteSequence packet, int v) throws IOException {
171        spaceNeeded(packet, 1);
172        write(packet, v);
173    }
174
175    public static void writeShortBig(ByteSequence packet, int v) throws IOException {
176        spaceNeeded(packet, 2);
177        write(packet, (v >>> 8) & 0xFF);
178        write(packet, (v >>> 0) & 0xFF);
179    }
180
181    public static void writeShortLittle(ByteSequence packet, int v) throws IOException {
182        spaceNeeded(packet, 2);
183        write(packet, (v >>> 0) & 0xFF);
184        write(packet, (v >>> 8) & 0xFF);
185    }
186
187    public static void writeCharBig(ByteSequence packet, int v) throws IOException {
188        spaceNeeded(packet, 2);
189        write(packet, (v >>> 8) & 0xFF);
190        write(packet, (v >>> 0) & 0xFF);
191    }
192
193    public static void writeCharLittle(ByteSequence packet, int v) throws IOException {
194        spaceNeeded(packet, 2);
195        write(packet, (v >>> 0) & 0xFF);
196        write(packet, (v >>> 8) & 0xFF);
197    }
198
199    public static void writeIntBig(ByteSequence packet, int v) throws IOException {
200        spaceNeeded(packet, 4);
201        write(packet, (v >>> 24) & 0xFF);
202        write(packet, (v >>> 16) & 0xFF);
203        write(packet, (v >>> 8) & 0xFF);
204        write(packet, (v >>> 0) & 0xFF);
205    }
206
207    public static void writeIntLittle(ByteSequence packet, int v) throws IOException {
208        spaceNeeded(packet, 4);
209        write(packet, (v >>> 0) & 0xFF);
210        write(packet, (v >>> 8) & 0xFF);
211        write(packet, (v >>> 16) & 0xFF);
212        write(packet, (v >>> 24) & 0xFF);
213    }
214
215    public static void writeLongBig(ByteSequence packet, long v) throws IOException {
216        spaceNeeded(packet, 8);
217        write(packet, (int)(v >>> 56) & 0xFF);
218        write(packet, (int)(v >>> 48) & 0xFF);
219        write(packet, (int)(v >>> 40) & 0xFF);
220        write(packet, (int)(v >>> 32) & 0xFF);
221        write(packet, (int)(v >>> 24) & 0xFF);
222        write(packet, (int)(v >>> 16) & 0xFF);
223        write(packet, (int)(v >>> 8) & 0xFF);
224        write(packet, (int)(v >>> 0) & 0xFF);
225    }
226
227    public static void writeLongLittle(ByteSequence packet, long v) throws IOException {
228        spaceNeeded(packet, 8);
229        write(packet, (int)(v >>> 0) & 0xFF);
230        write(packet, (int)(v >>> 8) & 0xFF);
231        write(packet, (int)(v >>> 16) & 0xFF);
232        write(packet, (int)(v >>> 24) & 0xFF);
233        write(packet, (int)(v >>> 32) & 0xFF);
234        write(packet, (int)(v >>> 40) & 0xFF);
235        write(packet, (int)(v >>> 48) & 0xFF);
236        write(packet, (int)(v >>> 56) & 0xFF);
237    }
238
239    public static void writeDoubleBig(ByteSequence packet, double v) throws IOException {
240        writeLongBig(packet, Double.doubleToLongBits(v));
241    }
242
243    public static void writeDoubleLittle(ByteSequence packet, double v) throws IOException {
244        writeLongLittle(packet, Double.doubleToLongBits(v));
245    }
246
247    public static void writeFloatBig(ByteSequence packet, float v) throws IOException {
248        writeIntBig(packet, Float.floatToIntBits(v));
249    }
250
251    public static void writeFloatLittle(ByteSequence packet, float v) throws IOException {
252        writeIntLittle(packet, Float.floatToIntBits(v));
253    }
254
255    public static void writeRawDoubleBig(ByteSequence packet, double v) throws IOException {
256        writeLongBig(packet, Double.doubleToRawLongBits(v));
257    }
258
259    public static void writeRawDoubleLittle(ByteSequence packet, double v) throws IOException {
260        writeLongLittle(packet, Double.doubleToRawLongBits(v));
261    }
262
263    public static void writeRawFloatBig(ByteSequence packet, float v) throws IOException {
264        writeIntBig(packet, Float.floatToRawIntBits(v));
265    }
266
267    public static void writeRawFloatLittle(ByteSequence packet, float v) throws IOException {
268        writeIntLittle(packet, Float.floatToRawIntBits(v));
269    }
270
271}