Source code: com/lutris/util/BytesToString.java
1 /*
2 * Enhydra Java Application Server Project
3 *
4 * The contents of this file are subject to the Enhydra Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License on
7 * the Enhydra web site ( http://www.enhydra.org/ ).
8 *
9 * 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.9.12.1 2000/10/19 17:58:53 jasona Exp $
22 */
23
24
25
26
27
28 package com.lutris.util;
29
30 /**
31 This class has static methods which convert arrays of bytes to
32 a strings. It remaps non-ascii bytes to ".". One use for this class is
33 to convert raw data read in from a file or the net to a human-readable
34 form, probably for debugging.
35
36 @author Andy John
37 */
38 public class BytesToString {
39
40 /**
41 Converts the array of bytes to a string. Non-ascii characters
42 are converted to ".".
43
44 @param b The array of bytes to convert.
45 @return The bytes converted into a string.
46 */
47 public static String conv(byte[] b) {
48 return conv(b, b.length);
49 }
50
51
52 /**
53 Converts the array of bytes to a string. Non-ascii characters
54 are converted to ".". Only the first len bytes are used.
55
56 @param b The array of bytes to convert.
57 @param len Only uses bytes b[0]..b[len-1].
58 @return The bytes converted into a string.
59 */
60 public static String conv(byte[] b, int len) {
61 byte[] c = new byte[len];
62 for(int i=0; i<len; i++) {
63 byte n = b[i];
64 if ((n < 32) || (n > 128))
65 n = 65;
66 c[i] = n;
67 }
68 return new String(c);
69 }
70 }
71