2
* See the file LICENSE for redistribution information.
5
* Sleepycat Software. All rights reserved.
7
* $Id: DbUtil.java,v 11.3 2001/07/02 01:03:23 bostic Exp $
10
package com.sleepycat.db;
14
* @author David M. Krinsky
17
// DbUtil is a simple, package-private wrapper class that holds a few
18
// static utility functions other parts of the package share and that don't
19
// have a good home elsewhere. (For now, that's limited to byte-array-to-int
20
// conversion and back.)
24
// Get the u_int32_t stored beginning at offset "offset" into
25
// array "arr". We have to do the conversion manually since it's
26
// a C-native int, and we're not really supposed to make this kind of
28
static int array2int(byte[] arr, int offset)
33
// Get the component bytes; b4 is most significant, b1 least.
46
// Bytes are signed. Convert [-128, -1] to [128, 255].
47
if (b1 < 0) { b1 += 256; }
48
if (b2 < 0) { b2 += 256; }
49
if (b3 < 0) { b3 += 256; }
50
if (b4 < 0) { b4 += 256; }
52
// Put the bytes in their proper places in an int.
58
return (b1 + b2 + b3 + b4);
61
// Store the specified u_int32_t, with endianness appropriate
62
// to the platform we're running on, into four consecutive bytes of
63
// the specified byte array, starting from the specified offset.
64
static void int2array(int n, byte[] arr, int offset)
71
b3 = (n >> 16) & 0xff;
72
b4 = (n >> 24) & 0xff;
74
// Bytes are signed. Convert [128, 255] to [-128, -1].
75
if (b1 >= 128) { b1 -= 256; }
76
if (b2 >= 128) { b2 -= 256; }
77
if (b3 >= 128) { b3 -= 256; }
78
if (b4 >= 128) { b4 -= 256; }
80
// Put the bytes in the appropriate place in the array.
82
arr[pos++] = (byte)b4;
83
arr[pos++] = (byte)b3;
84
arr[pos++] = (byte)b2;
87
arr[pos++] = (byte)b1;
88
arr[pos++] = (byte)b2;
89
arr[pos++] = (byte)b3;
94
private static final boolean big_endian = am_big_endian();
95
private static native boolean am_big_endian();