1 // Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 
6 package org.rocksdb;
7 
8 /**
9  * Simple type conversion methods
10  * for use in tests
11  */
12 public class Types {
13 
14   /**
15    * Convert first 4 bytes of a byte array to an int
16    *
17    * @param data The byte array
18    *
19    * @return An integer
20    */
byteToInt(final byte data[])21   public static int byteToInt(final byte data[]) {
22     return (data[0] & 0xff) |
23         ((data[1] & 0xff) << 8) |
24         ((data[2] & 0xff) << 16) |
25         ((data[3] & 0xff) << 24);
26   }
27 
28   /**
29    * Convert an int to 4 bytes
30    *
31    * @param v The int
32    *
33    * @return A byte array containing 4 bytes
34    */
intToByte(final int v)35   public static byte[] intToByte(final int v) {
36     return new byte[] {
37         (byte)((v >>> 0) & 0xff),
38         (byte)((v >>> 8) & 0xff),
39         (byte)((v >>> 16) & 0xff),
40         (byte)((v >>> 24) & 0xff)
41     };
42   }
43 }
44