Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Bytes |
|
| 3.25;3.25 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.apache.commons.id.uuid; | |
18 | ||
19 | /** | |
20 | * <p>Static methods for managing byte arrays (all methods follow Big | |
21 | * Endian order where most significant bits are in front).</p> | |
22 | * | |
23 | * @author Commons-Id Team | |
24 | * @version $Id: Bytes.java 480488 2006-11-29 08:57:26Z bayard $ | |
25 | */ | |
26 | public final class Bytes { | |
27 | ||
28 | /** | |
29 | * <p>Hide constructor in utility class.</p> | |
30 | */ | |
31 | 0 | private Bytes() { |
32 | 0 | } |
33 | ||
34 | /** | |
35 | * Appends two bytes array into one. | |
36 | * | |
37 | * @param a A byte[]. | |
38 | * @param b A byte[]. | |
39 | * @return A byte[]. | |
40 | */ | |
41 | public static byte[] append(byte[] a, byte[] b) { | |
42 | 1 | byte[] z = new byte[a.length + b.length]; |
43 | 1 | System.arraycopy(a, 0, z, 0, a.length); |
44 | 1 | System.arraycopy(b, 0, z, a.length, b.length); |
45 | 1 | return z; |
46 | } | |
47 | ||
48 | /** | |
49 | * Returns a 8-byte array built from a long. | |
50 | * | |
51 | * @param n The number to convert. | |
52 | * @return A byte[]. | |
53 | */ | |
54 | public static byte[] toBytes(long n) { | |
55 | 3 | return toBytes(n, new byte[8]); |
56 | } | |
57 | ||
58 | /** | |
59 | * Build a 8-byte array from a long. No check is performed on the | |
60 | * array length. | |
61 | * | |
62 | * @param n The number to convert. | |
63 | * @param b The array to fill. | |
64 | * @return A byte[]. | |
65 | */ | |
66 | public static byte[] toBytes(long n, byte[] b) { | |
67 | 3 | b[7] = (byte) (n); |
68 | 3 | n >>>= 8; |
69 | 3 | b[6] = (byte) (n); |
70 | 3 | n >>>= 8; |
71 | 3 | b[5] = (byte) (n); |
72 | 3 | n >>>= 8; |
73 | 3 | b[4] = (byte) (n); |
74 | 3 | n >>>= 8; |
75 | 3 | b[3] = (byte) (n); |
76 | 3 | n >>>= 8; |
77 | 3 | b[2] = (byte) (n); |
78 | 3 | n >>>= 8; |
79 | 3 | b[1] = (byte) (n); |
80 | 3 | n >>>= 8; |
81 | 3 | b[0] = (byte) (n); |
82 | ||
83 | 3 | return b; |
84 | } | |
85 | ||
86 | /** | |
87 | * Build a long from first 8 bytes of the array. | |
88 | * | |
89 | * @param b The byte[] to convert. | |
90 | * @return A long. | |
91 | */ | |
92 | public static long toLong(byte[] b) { | |
93 | 2 | return ((((long) b[7]) & 0xFF) |
94 | + ((((long) b[6]) & 0xFF) << 8) | |
95 | + ((((long) b[5]) & 0xFF) << 16) | |
96 | + ((((long) b[4]) & 0xFF) << 24) | |
97 | + ((((long) b[3]) & 0xFF) << 32) | |
98 | + ((((long) b[2]) & 0xFF) << 40) | |
99 | + ((((long) b[1]) & 0xFF) << 48) | |
100 | + ((((long) b[0]) & 0xFF) << 56)); | |
101 | } | |
102 | ||
103 | /** | |
104 | * Compares two byte arrays for equality. | |
105 | * | |
106 | * @param a A byte[]. | |
107 | * @param b A byte[]. | |
108 | * @return True if the arrays have identical contents. | |
109 | */ | |
110 | public static boolean areEqual(byte[] a, byte[] b) { | |
111 | 23 | int aLength = a.length; |
112 | 23 | if (aLength != b.length) { |
113 | 0 | return false; |
114 | } | |
115 | ||
116 | 327 | for (int i = 0; i < aLength; i++) { |
117 | 308 | if (a[i] != b[i]) { |
118 | 4 | return false; |
119 | } | |
120 | } | |
121 | 19 | return true; |
122 | } | |
123 | ||
124 | /** | |
125 | * <p>Compares two byte arrays as specified by <code>Comparable</code>. | |
126 | * | |
127 | * @param lhs - left hand value in the comparison operation. | |
128 | * @param rhs - right hand value in the comparison operation. | |
129 | * @return a negative integer, zero, or a positive integer as <code>lhs</code> | |
130 | * is less than, equal to, or greater than <code>rhs</code>. | |
131 | */ | |
132 | public static int compareTo(byte[] lhs, byte[] rhs) { | |
133 | 3 | if (lhs == rhs) { |
134 | 0 | return 0; |
135 | } | |
136 | 3 | if (lhs == null) { |
137 | 0 | return -1; |
138 | } | |
139 | 3 | if (rhs == null) { |
140 | 0 | return +1; |
141 | } | |
142 | 3 | if (lhs.length != rhs.length) { |
143 | 0 | return ((lhs.length < rhs.length) ? -1 : +1); |
144 | } | |
145 | 33 | for (int i = 0; i < lhs.length; i++) { |
146 | 32 | if (lhs[i] < rhs[i]) { |
147 | 1 | return -1; |
148 | 31 | } else if (lhs[i] > rhs[i]) { |
149 | 1 | return 1; |
150 | } | |
151 | } | |
152 | 1 | return 0; |
153 | } | |
154 | ||
155 | /** | |
156 | * Build a short from first 2 bytes of the array. | |
157 | * | |
158 | * @param b The byte[] to convert. | |
159 | * @return A short. | |
160 | */ | |
161 | public static short toShort(byte[] b) { | |
162 | 12 | return (short) ((b[1] & 0xFF) + ((b[0] & 0xFF) << 8)); |
163 | } | |
164 | } |