~ubuntu-branches/ubuntu/edgy/rpm/edgy

« back to all changes in this revision

Viewing changes to db/java/src/com/sleepycat/db/DbUtil.java

  • Committer: Bazaar Package Importer
  • Author(s): Joey Hess
  • Date: 2002-01-22 20:56:57 UTC
  • Revision ID: james.westby@ubuntu.com-20020122205657-l74j50mr9z8ofcl5
Tags: upstream-4.0.3
ImportĀ upstreamĀ versionĀ 4.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-
 
2
 * See the file LICENSE for redistribution information.
 
3
 *
 
4
 * Copyright (c) 2001
 
5
 *      Sleepycat Software.  All rights reserved.
 
6
 *
 
7
 * $Id: DbUtil.java,v 11.3 2001/07/02 01:03:23 bostic Exp $
 
8
 */
 
9
 
 
10
package com.sleepycat.db;
 
11
 
 
12
/**
 
13
 *
 
14
 * @author David M. Krinsky
 
15
 */
 
16
 
 
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.)
 
21
 
 
22
class DbUtil
 
23
{
 
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
 
27
    // cast in Java.
 
28
    static int array2int(byte[] arr, int offset)
 
29
    {
 
30
        int b1, b2, b3, b4;
 
31
        int pos = offset;
 
32
 
 
33
        // Get the component bytes;  b4 is most significant, b1 least.
 
34
        if (big_endian) {
 
35
            b4 = arr[pos++];
 
36
            b3 = arr[pos++];
 
37
            b2 = arr[pos++];
 
38
            b1 = arr[pos];
 
39
        } else {
 
40
            b1 = arr[pos++];
 
41
            b2 = arr[pos++];
 
42
            b3 = arr[pos++];
 
43
            b4 = arr[pos];
 
44
        }
 
45
 
 
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; }
 
51
 
 
52
        // Put the bytes in their proper places in an int.
 
53
        b2 <<= 8;
 
54
        b3 <<= 16;
 
55
        b4 <<= 24;
 
56
 
 
57
        // Return their sum.
 
58
        return (b1 + b2 + b3 + b4);
 
59
    }
 
60
 
 
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)
 
65
    {
 
66
        int b1, b2, b3, b4;
 
67
        int pos = offset;
 
68
 
 
69
        b1 = n & 0xff;
 
70
        b2 = (n >> 8) & 0xff;
 
71
        b3 = (n >> 16) & 0xff;
 
72
        b4 = (n >> 24) & 0xff;
 
73
 
 
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; }
 
79
 
 
80
        // Put the bytes in the appropriate place in the array.
 
81
        if (big_endian) {
 
82
            arr[pos++] = (byte)b4;
 
83
            arr[pos++] = (byte)b3;
 
84
            arr[pos++] = (byte)b2;
 
85
            arr[pos] = (byte)b1;
 
86
        } else {
 
87
            arr[pos++] = (byte)b1;
 
88
            arr[pos++] = (byte)b2;
 
89
            arr[pos++] = (byte)b3;
 
90
            arr[pos] = (byte)b4;
 
91
        }
 
92
    }
 
93
 
 
94
    private static final boolean big_endian = am_big_endian();
 
95
    private static native boolean am_big_endian();
 
96
}
 
97
 
 
98
// end of DbUtil.java