KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > ByteSequenceData


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.util;
19
20 import java.io.IOException JavaDoc;
21
22
23 /**
24  * Used to write and read primitives to and from a ByteSequence.
25  */

26 final public class ByteSequenceData {
27     
28     
29     public static byte[] toByteArray(ByteSequence packet) {
30         if( packet.offset==0 && packet.length == packet.data.length )
31             return packet.data;
32         
33         byte rc[] = new byte[packet.length];
34         System.arraycopy(packet.data, packet.offset, rc, 0, packet.length);
35         return rc;
36     }
37
38     private static void spaceNeeded(ByteSequence packet, int i) {
39         assert packet.offset+i <= packet.length;
40     }
41     
42     public static int remaining(ByteSequence packet) {
43         return packet.length - packet.offset;
44     }
45     
46     public static int read(ByteSequence packet) {
47         return packet.data[packet.offset++] & 0xff;
48     }
49
50
51     public static void readFully(ByteSequence packet, byte[] b) throws IOException JavaDoc {
52         readFully(packet, b, 0, b.length);
53     }
54
55     public static void readFully(ByteSequence packet, byte[] b, int off, int len) throws IOException JavaDoc {
56         spaceNeeded(packet, len);
57         System.arraycopy(packet.data, packet.offset, b, off, len);
58         packet.offset += len;
59     }
60
61     public static int skipBytes(ByteSequence packet, int n) throws IOException JavaDoc {
62         int rc = Math.min(n, remaining(packet));
63         packet.offset += rc;
64         return rc;
65     }
66
67     public static boolean readBoolean(ByteSequence packet) throws IOException JavaDoc {
68         spaceNeeded(packet, 1);
69         return read(packet) != 0;
70     }
71
72     public static byte readByte(ByteSequence packet) throws IOException JavaDoc {
73         spaceNeeded(packet, 1);
74         return (byte) read(packet);
75     }
76
77     public static int readUnsignedByte(ByteSequence packet) throws IOException JavaDoc {
78         spaceNeeded(packet, 1);
79         return read(packet);
80     }
81
82     public static short readShortBig(ByteSequence packet) throws IOException JavaDoc {
83         spaceNeeded(packet, 2);
84         return (short) ((read(packet) << 8) + (read(packet) << 0));
85     }
86     public static short readShortLittle(ByteSequence packet) throws IOException JavaDoc {
87         spaceNeeded(packet, 2);
88         return (short) ((read(packet) << 0) + (read(packet) << 8) );
89     }
90
91     public static int readUnsignedShortBig(ByteSequence packet) throws IOException JavaDoc {
92         spaceNeeded(packet, 2);
93         return ((read(packet) << 8) + (read(packet) << 0));
94     }
95     public static int readUnsignedShortLittle(ByteSequence packet) throws IOException JavaDoc {
96         spaceNeeded(packet, 2);
97         return ((read(packet) << 0) + (read(packet) << 8) );
98     }
99
100     public static char readCharBig(ByteSequence packet) throws IOException JavaDoc {
101         spaceNeeded(packet, 2);
102         return (char) ((read(packet) << 8) + (read(packet) << 0));
103     }
104     public static char readCharLittle(ByteSequence packet) throws IOException JavaDoc {
105         spaceNeeded(packet, 2);
106         return (char) ((read(packet) << 0) + (read(packet) << 8) );
107     }
108
109     public static int readIntBig(ByteSequence packet) throws IOException JavaDoc {
110         spaceNeeded(packet, 4);
111         return ((read(packet) << 24) +
112                 (read(packet) << 16) +
113                 (read(packet) << 8) +
114                 (read(packet) << 0));
115     }
116     public static int readIntLittle(ByteSequence packet) throws IOException JavaDoc {
117         spaceNeeded(packet, 4);
118         return ((read(packet) << 0) +
119                 (read(packet) << 8) +
120                 (read(packet) << 16) +
121                 (read(packet) << 24));
122     }
123     
124     public static long readLongBig(ByteSequence packet) throws IOException JavaDoc {
125         spaceNeeded(packet, 8);
126         return (((long) read(packet) << 56) +
127                 ((long) read(packet) << 48) +
128                 ((long) read(packet) << 40) +
129                 ((long) read(packet) << 32) +
130                 ((long) read(packet) << 24) +
131                 ((read(packet)) << 16) +
132                 ((read(packet)) << 8) +
133                 ((read(packet)) << 0));
134     }
135     public static long readLongLittle(ByteSequence packet) throws IOException JavaDoc {
136         spaceNeeded(packet, 8);
137         return ((read(packet) << 0) +
138                 (read(packet) << 8) +
139                 (read(packet) << 16) +
140                 ((long) read(packet) << 24) +
141                 ((long) read(packet) << 32) +
142                 ((long) read(packet) << 40) +
143                 ((long) read(packet) << 48) +
144                 ((long) read(packet) << 56));
145     }
146     
147     public static double readDoubleBig(ByteSequence packet) throws IOException JavaDoc {
148         return Double.longBitsToDouble(readLongBig(packet));
149     }
150     public static double readDoubleLittle(ByteSequence packet) throws IOException JavaDoc {
151         return Double.longBitsToDouble(readLongLittle(packet));
152     }
153
154     public static float readFloatBig(ByteSequence packet) throws IOException JavaDoc {
155         return Float.intBitsToFloat(readIntBig(packet));
156     }
157     public static float readFloatLittle(ByteSequence packet) throws IOException JavaDoc {
158         return Float.intBitsToFloat(readIntLittle(packet));
159     }
160
161     public static void write(ByteSequence packet, int b) throws IOException JavaDoc {
162         spaceNeeded(packet, 1);
163         packet.data[packet.offset++] = (byte) b;
164     }
165     
166     public static void write(ByteSequence packet, byte[] b) throws IOException JavaDoc {
167         write(packet, b, 0, b.length);
168     }
169     public static void write(ByteSequence packet, byte[] b, int off, int len) throws IOException JavaDoc {
170         spaceNeeded(packet, len);
171         System.arraycopy(b, off, packet.data, packet.offset, len);
172         packet.offset += len;
173     }
174     public static void writeBoolean(ByteSequence packet, boolean v) throws IOException JavaDoc {
175         spaceNeeded(packet, 1);
176         write(packet,v ? 1 : 0);
177     }
178     public static void writeByte(ByteSequence packet, int v) throws IOException JavaDoc {
179         spaceNeeded(packet, 1);
180         write(packet,v);
181     }
182     public static void writeShortBig(ByteSequence packet, int v) throws IOException JavaDoc {
183         spaceNeeded(packet, 2);
184         write(packet,(v >>> 8) & 0xFF);
185         write(packet,(v >>> 0) & 0xFF);
186     }
187     public static void writeShortLittle(ByteSequence packet, int v) throws IOException JavaDoc {
188         spaceNeeded(packet, 2);
189         write(packet,(v >>> 0) & 0xFF);
190         write(packet,(v >>> 8) & 0xFF);
191     }
192     public static void writeCharBig(ByteSequence packet, int v) throws IOException JavaDoc {
193         spaceNeeded(packet, 2);
194         write(packet,(v >>> 8) & 0xFF);
195         write(packet,(v >>> 0) & 0xFF);
196     }
197     public static void writeCharLittle(ByteSequence packet, int v) throws IOException JavaDoc {
198         spaceNeeded(packet, 2);
199         write(packet,(v >>> 0) & 0xFF);
200         write(packet,(v >>> 8) & 0xFF);
201     }
202     public static void writeIntBig(ByteSequence packet, int v) throws IOException JavaDoc {
203         spaceNeeded(packet, 4);
204         write(packet,(v >>> 24) & 0xFF);
205         write(packet,(v >>> 16) & 0xFF);
206         write(packet,(v >>> 8) & 0xFF);
207         write(packet,(v >>> 0) & 0xFF);
208     }
209     public static void writeIntLittle(ByteSequence packet, int v) throws IOException JavaDoc {
210         spaceNeeded(packet, 4);
211         write(packet,(v >>> 0) & 0xFF);
212         write(packet,(v >>> 8) & 0xFF);
213         write(packet,(v >>> 16) & 0xFF);
214         write(packet,(v >>> 24) & 0xFF);
215     }
216     public static void writeLongBig(ByteSequence packet, long v) throws IOException JavaDoc {
217         spaceNeeded(packet, 8);
218         write(packet,(int) (v >>> 56) & 0xFF);
219         write(packet,(int) (v >>> 48) & 0xFF);
220         write(packet,(int) (v >>> 40) & 0xFF);
221         write(packet,(int) (v >>> 32) & 0xFF);
222         write(packet,(int) (v >>> 24) & 0xFF);
223         write(packet,(int) (v >>> 16) & 0xFF);
224         write(packet,(int) (v >>> 8) & 0xFF);
225         write(packet,(int) (v >>> 0) & 0xFF);
226     }
227     public static void writeLongLittle(ByteSequence packet, long v) throws IOException JavaDoc {
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 JavaDoc {
240         writeLongBig(packet, Double.doubleToLongBits(v));
241     }
242     public static void writeDoubleLittle(ByteSequence packet, double v) throws IOException JavaDoc {
243         writeLongLittle(packet, Double.doubleToLongBits(v));
244     }
245
246     public static void writeFloatBig(ByteSequence packet, float v) throws IOException JavaDoc {
247         writeIntBig(packet, Float.floatToIntBits(v));
248     }
249     public static void writeFloatLittle(ByteSequence packet, float v) throws IOException JavaDoc {
250         writeIntLittle(packet, Float.floatToIntBits(v));
251     }
252     
253     public static void writeRawDoubleBig(ByteSequence packet, double v) throws IOException JavaDoc {
254         writeLongBig(packet, Double.doubleToRawLongBits(v));
255     }
256     public static void writeRawDoubleLittle(ByteSequence packet, double v) throws IOException JavaDoc {
257         writeLongLittle(packet, Double.doubleToRawLongBits(v));
258     }
259
260     public static void writeRawFloatBig(ByteSequence packet, float v) throws IOException JavaDoc {
261         writeIntBig(packet, Float.floatToRawIntBits(v));
262     }
263     public static void writeRawFloatLittle(ByteSequence packet, float v) throws IOException JavaDoc {
264         writeIntLittle(packet, Float.floatToRawIntBits(v));
265     }
266
267 }
268
Popular Tags