KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > util > BytesToString


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: BytesToString.java,v 1.1 2004/04/21 19:31:58 slobodan Exp $
23  */

24
25
26
27
28
29 package com.lutris.util;
30
31 /**
32     This class has static methods which convert arrays of bytes to
33     a strings. It remaps non-ascii bytes to ".". One use for this class is
34     to convert raw data read in from a file or the net to a human-readable
35     form, probably for debugging.
36
37     @author Andy John
38 */

39 public class BytesToString {
40
41     /**
42         Converts the array of bytes to a string. Non-ascii characters
43         are converted to ".".
44
45         @param b The array of bytes to convert.
46         @return The bytes converted into a string.
47     */

48     public static String JavaDoc conv(byte[] b) {
49         return conv(b, b.length);
50     }
51
52
53     /**
54         Converts the array of bytes to a string. Non-ascii characters
55         are converted to ".". Only the first len bytes are used.
56
57         @param b The array of bytes to convert.
58         @param len Only uses bytes b[0]..b[len-1].
59         @return The bytes converted into a string.
60     */

61     public static String JavaDoc conv(byte[] b, int len) {
62         byte[] c = new byte[len];
63         for(int i=0; i<len; i++) {
64             byte n = b[i];
65             if ((n < 32) || (n > 128))
66                 n = 65;
67             c[i] = n;
68         }
69         return new String JavaDoc(c);
70     }
71 }
72     
73
Popular Tags