01: /*
02: * Enhydra Java Application Server Project
03: *
04: * The contents of this file are subject to the Enhydra Public License
05: * Version 1.1 (the "License"); you may not use this file except in
06: * compliance with the License. You may obtain a copy of the License on
07: * the Enhydra web site ( http://www.enhydra.org/ ).
08: *
09: * Software distributed under the License is distributed on an "AS IS"
10: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11: * the License for the specific terms governing rights and limitations
12: * under the License.
13: *
14: * The Initial Developer of the Enhydra Application Server is Lutris
15: * Technologies, Inc. The Enhydra Application Server and portions created
16: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17: * All Rights Reserved.
18: *
19: * Contributor(s):
20: *
21: * $Id: BytesToString.java,v 1.3 2007-10-19 10:05:39 sinisa Exp $
22: */
23:
24: package com.lutris.util;
25:
26: /**
27: This class has static methods which convert arrays of bytes to
28: a strings. It remaps non-ascii bytes to ".". One use for this class is
29: to convert raw data read in from a file or the net to a human-readable
30: form, probably for debugging.
31:
32: @author Andy John
33: */
34: public class BytesToString {
35:
36: /**
37: Converts the array of bytes to a string. Non-ascii characters
38: are converted to ".".
39:
40: @param b The array of bytes to convert.
41: @return The bytes converted into a string.
42: */
43: public static String conv(byte[] b) {
44: return conv(b, b.length);
45: }
46:
47: /**
48: Converts the array of bytes to a string. Non-ascii characters
49: are converted to ".". Only the first len bytes are used.
50:
51: @param b The array of bytes to convert.
52: @param len Only uses bytes b[0]..b[len-1].
53: @return The bytes converted into a string.
54: */
55: public static String conv(byte[] b, int len) {
56: byte[] c = new byte[len];
57: for (int i = 0; i < len; i++) {
58: byte n = b[i];
59: if ((n < 32)/* Alex || (n > 128)*/)
60: n = 65;
61: c[i] = n;
62: }
63: return new String(c);
64: }
65: }
|