summaryrefslogtreecommitdiff
path: root/lang/java/src/com/sleepycat/db/internal/DbUtil.java
blob: 83d6d65eb0e9e7da7f972e08675ce8eb2c4c7497 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2001, 2012 Oracle and/or its affiliates.  All rights reserved.
 *
 * $Id$
 */
package com.sleepycat.db.internal;

/**
 *  DbUtil is a simple class that holds a few static utility functions other
 *  parts of the package share and that don't have a good home elsewhere. (For
 *  now, that's limited to byte-array-to-int conversion and back.)
 */

public class DbUtil {
    /**
     *  Get the u_int32_t stored beginning at offset "offset" into
     *  array "arr". We have to do the conversion manually since it's
     *  a C-native int, and we're not really supposed to make this
     *  kind of cast in Java.
     *
     * @return    Description of the Return Value
     */
    public static int array2int(byte[] arr, int offset) {
        int b1;
        int b2;
        int b3;
        int b4;
        int pos = offset;

        // Get the component bytes;  b4 is most significant, b1 least.
        if (big_endian) {
            b4 = arr[pos++];
            b3 = arr[pos++];
            b2 = arr[pos++];
            b1 = arr[pos];
        } else {
            b1 = arr[pos++];
            b2 = arr[pos++];
            b3 = arr[pos++];
            b4 = arr[pos];
        }

        // Bytes are signed.  Convert [-128, -1] to [128, 255].
        if (b1 < 0) {
            b1 += 256;
        }
        if (b2 < 0) {
            b2 += 256;
        }
        if (b3 < 0) {
            b3 += 256;
        }
        if (b4 < 0) {
            b4 += 256;
        }

        // Put the bytes in their proper places in an int.
        b2 <<= 8;
        b3 <<= 16;
        b4 <<= 24;

        // Return their sum.
        return (b1 + b2 + b3 + b4);
    }


    /**
     *  Store the specified u_int32_t, with endianness appropriate to
     *  the platform we're running on, into four consecutive bytes of
     *  the specified byte array, starting from the specified offset.
     */
    public static void int2array(int n, byte[] arr, int offset) {
        int b1;
        int b2;
        int b3;
        int b4;
        int pos = offset;

        b1 = n & 0xff;
        b2 = (n >> 8) & 0xff;
        b3 = (n >> 16) & 0xff;
        b4 = (n >> 24) & 0xff;

        // Bytes are signed.  Convert [128, 255] to [-128, -1].
        if (b1 >= 128) {
            b1 -= 256;
        }
        if (b2 >= 128) {
            b2 -= 256;
        }
        if (b3 >= 128) {
            b3 -= 256;
        }
        if (b4 >= 128) {
            b4 -= 256;
        }

        // Put the bytes in the appropriate place in the array.
        if (big_endian) {
            arr[pos++] = (byte) b4;
            arr[pos++] = (byte) b3;
            arr[pos++] = (byte) b2;
            arr[pos] = (byte) b1;
        } else {
            arr[pos++] = (byte) b1;
            arr[pos++] = (byte) b2;
            arr[pos++] = (byte) b3;
            arr[pos] = (byte) b4;
        }
    }


    /**
     *  Convert a byte array to a concise, readable string suitable
     *  for use in toString methods of the *Stat classes.
     *
     * @return    Description of the Return Value
     */
    public static String byteArrayToString(byte[] barr) {
        if (barr == null) {
            return "null";
        }

        StringBuffer sb = new StringBuffer();
        int len = barr.length;
        for (int i = 0; i < len; i++) {
            sb.append('x');
            int val = (barr[i] >> 4) & 0xf;
            if (val < 10) {
                sb.append((char) ('0' + val));
            } else {
                sb.append((char) ('a' + val - 10));
            }
            val = barr[i] & 0xf;
            if (val < 10) {
                sb.append((char) ('0' + val));
            } else {
                sb.append((char) ('a' + val - 10));
            }
        }
        return sb.toString();
    }


    /**
     *  Convert an object array to a string, suitable for use in
     *  toString methods of the *Stat classes.
     *
     * @return    Description of the Return Value
     */
    public static String objectArrayToString(Object[] arr, String name) {
        if (arr == null) {
            return "null";
        }

        StringBuffer sb = new StringBuffer();
        int len = arr.length;
        for (int i = 0; i < len; i++) {
            sb.append("\n    " + name + "[" + i + "]:\n");
            sb.append("    " + arr[i].toString());
        }
        return sb.toString();
    }

    public static int default_lorder() {
        return big_endian ? 4321 : 1234;
    }

    private final static boolean big_endian = is_big_endian();

    /**
     * @return    Description of the Return Value
     */
    private native static boolean is_big_endian();
}