root/as3/OSCemote/branches/saqoosha/server/flosc-0.3.1/Bytes.java

リビジョン 1048, 9.6 kB (コミッタ: uranodai, コミット時期: 4 年 前)

--

Line 
1 /*
2  * Copyright (c) 1997-1999 The Java Apache Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  *    software must display the following acknowledgment:
18  *    "This product includes software developed by the Java Apache
19  *    Project for use in the Apache JServ servlet engine project
20  *    <http://java.apache.org/>."
21  *
22  * 4. The names "Apache JServ", "Apache JServ Servlet Engine" and
23  *    "Java Apache Project" must not be used to endorse or promote products
24  *    derived from this software without prior written permission.
25  *
26  * 5. Products derived from this software may not be called "Apache JServ"
27  *    nor may "Apache" nor "Apache JServ" appear in their names without
28  *    prior written permission of the Java Apache Project.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the Java Apache
33  *    Project for use in the Apache JServ servlet engine project
34  *    <http://java.apache.org/>."
35  *   
36  * THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE JAVA APACHE PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Java Apache Group. For more information
51  * on the Java Apache Project and the Apache JServ Servlet Engine project,
52  * please see <http://java.apache.org/>.
53  *
54  */
55
56
57 import java.io.*;
58
59 /**
60  * Static methods for managing byte arrays
61  * (all methods follow Big Endian order
62  * where most significant bits are in front).
63  *
64  * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
65  * @author Ben Chun
66  * @version 1.8 (added toFloat and toDouble methods)
67  */
68 public class Bytes {
69
70     /**
71      * Build an int from first 4 bytes of the array.
72      *
73      * @param b    the byte array to convert.
74      */
75     public static int toInt(byte[] b) {
76         return (((int) b[3]) & 0xFF) +
77                ((((int) b[2]) & 0xFF) << 8) +
78                ((((int) b[1]) & 0xFF) << 16) +
79                ((((int) b[0]) & 0xFF) << 24);
80     }
81
82     /**
83      * Build a long from first 8 bytes of the array.
84      *
85      * @param b    the byte array to convert.
86      */
87     public static long toLong(byte[] b) {
88         return (((long) b[7]) & 0xFF) +
89                ((((long) b[6]) & 0xFF) << 8) +
90                ((((long) b[5]) & 0xFF) << 16) +
91                ((((long) b[4]) & 0xFF) << 24) +
92                ((((long) b[3]) & 0xFF) << 32) +
93                ((((long) b[2]) & 0xFF) << 40) +
94                ((((long) b[1]) & 0xFF) << 48) +
95                ((((long) b[0]) & 0xFF) << 56);
96     }
97
98     /**
99      * Build a float from the first 4 bytes of the array.
100      *
101      * @param b    the byte array to convert.
102      */
103     public static float toFloat(byte[] b) {
104         int i = toInt( b ) ;
105         return Float.intBitsToFloat( i ) ;
106     }
107    
108     /**
109      * Build a double-precision float from the first 8 bytes of the array.
110      *
111      * @param b    the byte array to convert.
112      */
113     public static double toDouble(byte[] b) {
114         long l = toLong( b );
115         return Double.longBitsToDouble( l ) ;
116     }
117
118
119     /**
120      * Returns a 4-byte array built from an int.
121      *
122      * @param  n  the number to convert.
123      */
124     public static byte[] toBytes(int n) {
125         return toBytes(n, new byte[4]);
126     }
127
128
129     /**
130      * Build a 4-byte array from an int.
131      * No check is performed on the array length.
132      *
133      * @param  n  the number to convert.
134      * @param  b  the array to fill.
135      */
136     public static byte[] toBytes(int n, byte[] b) {
137         b[3] = (byte) (n);
138         n >>>= 8;
139         b[2] = (byte) (n);
140         n >>>= 8;
141         b[1] = (byte) (n);
142         n >>>= 8;
143         b[0] = (byte) (n);
144
145         return b;
146     }
147
148     /**
149      * Returns a 8-byte array built from a long.
150      *
151      * @param  n  the number to convert.
152      */
153     public static byte[] toBytes(long n) {
154         return toBytes(n, new byte[8]);
155     }
156
157     /**
158      * Build a 8-byte array from a long.
159      * No check is performed on the array length.
160      *
161      * @param  n  the number to convert.
162      * @param  b  the array to fill.
163      */
164     public static byte[] toBytes(long n, byte[] b) {
165         b[7] = (byte) (n);
166         n >>>= 8;
167         b[6] = (byte) (n);
168         n >>>= 8;
169         b[5] = (byte) (n);
170         n >>>= 8;
171         b[4] = (byte) (n);
172         n >>>= 8;
173         b[3] = (byte) (n);
174         n >>>= 8;
175         b[2] = (byte) (n);
176         n >>>= 8;
177         b[1] = (byte) (n);
178         n >>>= 8;
179         b[0] = (byte) (n);
180
181         return b;
182     }
183
184     /**
185      * Compares two byte arrays for equality.
186      *
187      * @return true if the arrays have identical contents
188      */
189     public static boolean areEqual(byte[] a, byte[] b) {
190         int aLength = a.length;
191         if (aLength != b.length) return false;
192
193         for (int i = 0; i < aLength; i++)
194             if (a[i] != b[i]) return false;
195
196         return true;
197     }
198
199     /**
200      * Appends two bytes array into one.
201      */
202     public static byte[] append(byte[] a, byte[] b) {
203         byte[] z = new byte[a.length + b.length];
204         System.arraycopy(a, 0, z, 0, a.length);
205         System.arraycopy(b, 0, z, a.length, b.length);
206         return z;
207     }
208
209     /**
210      * Appends three bytes array into one.
211      */
212     public static byte[] append(byte[] a, byte[] b, byte[] c) {
213         byte[] z = new byte[a.length + b.length + c.length];
214         System.arraycopy(a, 0, z, 0, a.length);
215         System.arraycopy(b, 0, z, a.length, b.length);
216         System.arraycopy(c, 0, z, a.length + b.length, c.length);
217         return z;
218     }
219
220     /**
221      * Gets the end of the byte array given.
222      * @param b byte array
223      * @param pos the position from which to start
224      * @return a byte array consisting of the portion of b between pos and
225      * the end of b.
226      */
227     public static byte[] copy(byte[] b, int pos) {
228         return copy(b, pos, b.length - pos);
229     }
230
231     /**
232      * Gets a sub-set of the byte array given.
233      * @param b byte array
234      * @param pos the position from which to start
235      * @param length the number of bytes to copy from the original byte array
236      * to the new one.
237      * @return a byte array consisting of the portion of b starting at pos
238      * and continuing for length bytes, or until the end of b is reached,
239      * which ever occurs first.
240      */
241     public static byte[] copy(byte[] b, int pos, int length) {
242         byte[] z = new byte[length];
243         System.arraycopy(b, pos, z, 0, length);
244         return z;
245     }
246
247     /**
248      * Merges a bytes array into another starting from the
249      * given positions.
250      */
251     public static void merge(byte[] src, byte[] dest, int srcpos, int destpos, int length) {
252         System.arraycopy(src, srcpos, dest, destpos, length);
253     }
254
255     /**
256      * Merges a bytes array into another starting from the
257      * given position.
258      */
259     public static void merge(byte[] src, byte[] dest, int pos) {
260         System.arraycopy(src, 0, dest, pos, src.length);
261     }
262
263     /**
264      * Merges a bytes array into another.
265      */
266     public static void merge(byte[] src, byte[] dest) {
267         System.arraycopy(src, 0, dest, 0, src.length);
268     }
269
270     /**
271      * Merges a bytes array into another starting from the
272      * given position.
273      */
274     public static void merge(byte[] src, byte[] dest, int pos, int length) {
275         System.arraycopy(src, 0, dest, pos, length);
276     }
277
278     private static final char[] hexDigits = {
279         '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
280     };
281
282     /**
283      * Returns a string of hexadecimal digits from a byte array,
284      * starting at offset and continuing for length bytes.
285      */
286     public static String toString(byte[] b, int offset, int length) {
287         char[] buf = new char[length * 2];
288
289         for (int i = offset, j = 0, k; i < offset + length; i++) {
290             k = b[i];
291             buf[j++] = hexDigits[(k >>> 4) & 0x0F];
292             buf[j++] = hexDigits[k & 0x0F];
293         }
294
295         return new String(buf);
296     }
297
298     /**
299      * Returns a string of hexadecimal digits from a byte array..
300      */
301     public static String toString(byte[] b) {
302         return toString(b, 0, b.length);
303     }
304 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。