1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2001,2008 Oracle. All rights reserved. 5 * 6 * $Id: DbUtil.java,v 12.6 2008/01/08 20:58:38 bostic Exp $ 7 */ 8package com.sleepycat.db.internal; 9 10/** 11 * DbUtil is a simple class that holds a few static utility functions other 12 * parts of the package share and that don't have a good home elsewhere. (For 13 * now, that's limited to byte-array-to-int conversion and back.) 14 */ 15 16public class DbUtil { 17 /** 18 * Get the u_int32_t stored beginning at offset "offset" into 19 * array "arr". We have to do the conversion manually since it's 20 * a C-native int, and we're not really supposed to make this 21 * kind of cast in Java. 22 * 23 * @return Description of the Return Value 24 */ 25 public static int array2int(byte[] arr, int offset) { 26 int b1; 27 int b2; 28 int b3; 29 int b4; 30 int pos = offset; 31 32 // Get the component bytes; b4 is most significant, b1 least. 33 if (big_endian) { 34 b4 = arr[pos++]; 35 b3 = arr[pos++]; 36 b2 = arr[pos++]; 37 b1 = arr[pos]; 38 } else { 39 b1 = arr[pos++]; 40 b2 = arr[pos++]; 41 b3 = arr[pos++]; 42 b4 = arr[pos]; 43 } 44 45 // Bytes are signed. Convert [-128, -1] to [128, 255]. 46 if (b1 < 0) { 47 b1 += 256; 48 } 49 if (b2 < 0) { 50 b2 += 256; 51 } 52 if (b3 < 0) { 53 b3 += 256; 54 } 55 if (b4 < 0) { 56 b4 += 256; 57 } 58 59 // Put the bytes in their proper places in an int. 60 b2 <<= 8; 61 b3 <<= 16; 62 b4 <<= 24; 63 64 // Return their sum. 65 return (b1 + b2 + b3 + b4); 66 } 67 68 69 /** 70 * Store the specified u_int32_t, with endianness appropriate to 71 * the platform we're running on, into four consecutive bytes of 72 * the specified byte array, starting from the specified offset. 73 */ 74 public static void int2array(int n, byte[] arr, int offset) { 75 int b1; 76 int b2; 77 int b3; 78 int b4; 79 int pos = offset; 80 81 b1 = n & 0xff; 82 b2 = (n >> 8) & 0xff; 83 b3 = (n >> 16) & 0xff; 84 b4 = (n >> 24) & 0xff; 85 86 // Bytes are signed. Convert [128, 255] to [-128, -1]. 87 if (b1 >= 128) { 88 b1 -= 256; 89 } 90 if (b2 >= 128) { 91 b2 -= 256; 92 } 93 if (b3 >= 128) { 94 b3 -= 256; 95 } 96 if (b4 >= 128) { 97 b4 -= 256; 98 } 99 100 // Put the bytes in the appropriate place in the array. 101 if (big_endian) { 102 arr[pos++] = (byte) b4; 103 arr[pos++] = (byte) b3; 104 arr[pos++] = (byte) b2; 105 arr[pos] = (byte) b1; 106 } else { 107 arr[pos++] = (byte) b1; 108 arr[pos++] = (byte) b2; 109 arr[pos++] = (byte) b3; 110 arr[pos] = (byte) b4; 111 } 112 } 113 114 115 /** 116 * Convert a byte array to a concise, readable string suitable 117 * for use in toString methods of the *Stat classes. 118 * 119 * @return Description of the Return Value 120 */ 121 public static String byteArrayToString(byte[] barr) { 122 if (barr == null) { 123 return "null"; 124 } 125 126 StringBuffer sb = new StringBuffer(); 127 int len = barr.length; 128 for (int i = 0; i < len; i++) { 129 sb.append('x'); 130 int val = (barr[i] >> 4) & 0xf; 131 if (val < 10) { 132 sb.append((char) ('0' + val)); 133 } else { 134 sb.append((char) ('a' + val - 10)); 135 } 136 val = barr[i] & 0xf; 137 if (val < 10) { 138 sb.append((char) ('0' + val)); 139 } else { 140 sb.append((char) ('a' + val - 10)); 141 } 142 } 143 return sb.toString(); 144 } 145 146 147 /** 148 * Convert an object array to a string, suitable for use in 149 * toString methods of the *Stat classes. 150 * 151 * @return Description of the Return Value 152 */ 153 public static String objectArrayToString(Object[] arr, String name) { 154 if (arr == null) { 155 return "null"; 156 } 157 158 StringBuffer sb = new StringBuffer(); 159 int len = arr.length; 160 for (int i = 0; i < len; i++) { 161 sb.append("\n " + name + "[" + i + "]:\n"); 162 sb.append(" " + arr[i].toString()); 163 } 164 return sb.toString(); 165 } 166 167 public static int default_lorder() { 168 return big_endian ? 4321 : 1234; 169 } 170 171 private final static boolean big_endian = is_big_endian(); 172 173 /** 174 * @return Description of the Return Value 175 */ 176 private native static boolean is_big_endian(); 177} 178