View Javadoc

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      private Bytes() {
32      }
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          byte[] z = new byte[a.length + b.length];
43          System.arraycopy(a, 0, z, 0, a.length);
44          System.arraycopy(b, 0, z, a.length, b.length);
45          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          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          b[7] = (byte) (n);
68          n >>>= 8;
69          b[6] = (byte) (n);
70          n >>>= 8;
71          b[5] = (byte) (n);
72          n >>>= 8;
73          b[4] = (byte) (n);
74          n >>>= 8;
75          b[3] = (byte) (n);
76          n >>>= 8;
77          b[2] = (byte) (n);
78          n >>>= 8;
79          b[1] = (byte) (n);
80          n >>>= 8;
81          b[0] = (byte) (n);
82  
83          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          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         int aLength = a.length;
112         if (aLength != b.length) {
113             return false;
114         }
115 
116         for (int i = 0; i < aLength; i++) {
117             if (a[i] != b[i]) {
118                 return false;
119             }
120         }
121         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         if (lhs == rhs) {
134             return 0;
135         }
136         if (lhs == null) {
137             return -1;
138         }
139         if (rhs == null) {
140             return +1;
141         }
142         if (lhs.length != rhs.length) {
143             return ((lhs.length < rhs.length) ? -1 : +1);
144         }
145         for (int i = 0; i < lhs.length; i++) {
146             if (lhs[i] < rhs[i]) {
147                 return -1;
148             } else if (lhs[i] > rhs[i]) {
149                 return 1;
150             }
151         }
152         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         return  (short) ((b[1] & 0xFF) + ((b[0] & 0xFF) << 8));
163     }
164 }