1/*
2 * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * utility class
26 */
27
28public class TestUtility {
29
30    private static final String DIGITS = "0123456789abcdef";
31
32    private TestUtility() {
33
34    }
35
36    public static String hexDump(byte[] bytes) {
37
38        StringBuilder buf = new StringBuilder(bytes.length * 2);
39        int i;
40
41        buf.append("    "); // four spaces
42        for (i = 0; i < bytes.length; i++) {
43            buf.append(DIGITS.charAt(bytes[i] >> 4 & 0x0f));
44            buf.append(DIGITS.charAt(bytes[i] & 0x0f));
45            if ((i + 1) % 32 == 0) {
46                if (i + 1 != bytes.length) {
47                    buf.append("\n    "); // line after four words
48                }
49            } else if ((i + 1) % 4 == 0) {
50                buf.append(' '); // space between words
51            }
52        }
53        return buf.toString();
54    }
55
56    public static String hexDump(byte[] bytes, int index) {
57        StringBuilder buf = new StringBuilder(bytes.length * 2);
58        int i;
59
60        buf.append("    "); // four spaces
61        buf.append(DIGITS.charAt(bytes[index] >> 4 & 0x0f));
62        buf.append(DIGITS.charAt(bytes[index] & 0x0f));
63        return buf.toString();
64    }
65
66    public static boolean equalsBlock(byte[] b1, byte[] b2) {
67
68        if (b1.length != b2.length) {
69            return false;
70        }
71
72        for (int i = 0; i < b1.length; i++) {
73            if (b1[i] != b2[i]) {
74                return false;
75            }
76        }
77
78        return true;
79    }
80
81    public static boolean equalsBlock(byte[] b1, byte[] b2, int len) {
82
83        for (int i = 0; i < len; i++) {
84            if (b1[i] != b2[i]) {
85                return false;
86            }
87        }
88
89        return true;
90    }
91
92}
93