Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

DbUtil.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2001-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: DbUtil.java,v 12.1 2005/06/16 20:23:04 bostic Exp $
00008  */
00009 package com.sleepycat.db.internal;
00010 
00017 public class DbUtil {
00026     public static int array2int(byte[] arr, int offset) {
00027         int b1;
00028         int b2;
00029         int b3;
00030         int b4;
00031         int pos = offset;
00032 
00033         // Get the component bytes;  b4 is most significant, b1 least.
00034         if (big_endian) {
00035             b4 = arr[pos++];
00036             b3 = arr[pos++];
00037             b2 = arr[pos++];
00038             b1 = arr[pos];
00039         } else {
00040             b1 = arr[pos++];
00041             b2 = arr[pos++];
00042             b3 = arr[pos++];
00043             b4 = arr[pos];
00044         }
00045 
00046         // Bytes are signed.  Convert [-128, -1] to [128, 255].
00047         if (b1 < 0) {
00048             b1 += 256;
00049         }
00050         if (b2 < 0) {
00051             b2 += 256;
00052         }
00053         if (b3 < 0) {
00054             b3 += 256;
00055         }
00056         if (b4 < 0) {
00057             b4 += 256;
00058         }
00059 
00060         // Put the bytes in their proper places in an int.
00061         b2 <<= 8;
00062         b3 <<= 16;
00063         b4 <<= 24;
00064 
00065         // Return their sum.
00066         return (b1 + b2 + b3 + b4);
00067     }
00068 
00069 
00075     public static void int2array(int n, byte[] arr, int offset) {
00076         int b1;
00077         int b2;
00078         int b3;
00079         int b4;
00080         int pos = offset;
00081 
00082         b1 = n & 0xff;
00083         b2 = (n >> 8) & 0xff;
00084         b3 = (n >> 16) & 0xff;
00085         b4 = (n >> 24) & 0xff;
00086 
00087         // Bytes are signed.  Convert [128, 255] to [-128, -1].
00088         if (b1 >= 128) {
00089             b1 -= 256;
00090         }
00091         if (b2 >= 128) {
00092             b2 -= 256;
00093         }
00094         if (b3 >= 128) {
00095             b3 -= 256;
00096         }
00097         if (b4 >= 128) {
00098             b4 -= 256;
00099         }
00100 
00101         // Put the bytes in the appropriate place in the array.
00102         if (big_endian) {
00103             arr[pos++] = (byte) b4;
00104             arr[pos++] = (byte) b3;
00105             arr[pos++] = (byte) b2;
00106             arr[pos] = (byte) b1;
00107         } else {
00108             arr[pos++] = (byte) b1;
00109             arr[pos++] = (byte) b2;
00110             arr[pos++] = (byte) b3;
00111             arr[pos] = (byte) b4;
00112         }
00113     }
00114 
00115 
00122     public static String byteArrayToString(byte[] barr) {
00123         if (barr == null) {
00124             return "null";
00125         }
00126 
00127         StringBuffer sb = new StringBuffer();
00128         int len = barr.length;
00129         for (int i = 0; i < len; i++) {
00130             sb.append('x');
00131             int val = (barr[i] >> 4) & 0xf;
00132             if (val < 10) {
00133                 sb.append((char) ('0' + val));
00134             } else {
00135                 sb.append((char) ('a' + val - 10));
00136             }
00137             val = barr[i] & 0xf;
00138             if (val < 10) {
00139                 sb.append((char) ('0' + val));
00140             } else {
00141                 sb.append((char) ('a' + val - 10));
00142             }
00143         }
00144         return sb.toString();
00145     }
00146 
00147 
00154     public static String objectArrayToString(Object[] arr, String name) {
00155         if (arr == null) {
00156             return "null";
00157         }
00158 
00159         StringBuffer sb = new StringBuffer();
00160         int len = arr.length;
00161         for (int i = 0; i < len; i++) {
00162             sb.append("\n    " + name + "[" + i + "]:\n");
00163             sb.append("    " + arr[i].toString());
00164         }
00165         return sb.toString();
00166     }
00167 
00168     public static int default_lorder() {
00169         return big_endian ? 4321 : 1234;
00170     }
00171 
00172     private final static boolean big_endian = is_big_endian();
00173 
00177     private native static boolean is_big_endian();
00178 }

Generated on Sun Dec 25 12:14:33 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2