KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > util > Ubyte


1 /*
2  * Copyright (C) 2001 - 2004 ScalAgent Distributed Technologies
3  * Copyright (C) 1996 - 2000 BULL
4  * Copyright (C) 1996 - 2000 INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Dyade
22  * Contributor(s): ScalAgent Distributed Technologies
23  */

24 package fr.dyade.aaa.util;
25
26 /**
27  * Provides a kind of unsigned byte functionality.
28  */

29 public class Ubyte {
30   /**
31    * Gives the unsigned value of a byte.
32    */

33   public static int unsignedValue(byte b) {
34     return (b >= 0 ? (int) b : 0x100 + b);
35   }
36
37   /**
38    * Gives the Byte value of an unsigned byte value.
39    */

40   public static byte signedValue(int b) {
41     return (b <= Byte.MAX_VALUE ? (byte) b : (byte) (b - 0x100));
42   }
43
44   /**
45    * Gives the hexa representation of unsigned value of the byte.
46    */

47   public static String JavaDoc toHexString(byte b) {
48     String JavaDoc str = Integer.toHexString(unsignedValue(b));
49     switch (str.length()) {
50     case 2:
51       return str;
52     case 1:
53       return "0" + str;
54     default:
55       // should never occur
56
throw new IllegalArgumentException JavaDoc("error in Ubyte.toHexString(" + b + ")");
57     }
58   }
59
60   /**
61    * Gives the hexa representation of an array of bytes.
62    *
63    * @param buffer array of bytes to print
64    * @param start index of first byte to print in buffer
65    * @param stop index of first byte not to print in buffer
66    * @param bytesInBlock number of bytes to print grouped, or 0
67    * @param blockHeader header for each block of bytes
68    * @param blocksInLine number of blocks to print in a line, or 0
69    * @param lineHeader header for each line
70    * @param current number of bytes already printed in previous calls
71    */

72   static final char hexaDigits[] = {
73     '0', '1', '2', '3', '4', '5', '6', '7',
74     '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
75   };
76   public static String JavaDoc toHexString(
77     byte[] buffer, int start, int stop,
78     int bytesInBlock, String JavaDoc blockHeader,
79     int blocksInLine, String JavaDoc lineHeader,
80     int current) {
81
82     StringBuffer JavaDoc output = new StringBuffer JavaDoc();
83
84     if (bytesInBlock == 0) bytesInBlock = Integer.MAX_VALUE;
85     if (blocksInLine == 0) blocksInLine = Integer.MAX_VALUE;
86
87     // bytes in block counter
88
int k = bytesInBlock - (current % bytesInBlock);
89     // blocks in line counter
90
int j = blocksInLine - ((current / bytesInBlock) % blocksInLine);
91
92     // finds first header
93
String JavaDoc header;
94     if (k != bytesInBlock) {
95       header = "";
96     } else if (j != blocksInLine) {
97       header = blockHeader;
98     } else {
99       header = lineHeader;
100     }
101
102     int i = start;
103   printLoop:
104     while (true) {
105       while (j-- > 0) {
106     output.append(header);
107     while (k-- > 0) {
108       // prints byte
109
int value = unsignedValue(buffer[i]);
110       output.append(hexaDigits[value / 0x10]);
111       output.append(hexaDigits[value % 0x10]);
112       // checks end of buffer
113
if (++i == stop)
114         break printLoop;
115     }
116     k = bytesInBlock;
117     header = blockHeader;
118       }
119       output.append('\n');
120       j = blocksInLine;
121       header = lineHeader;
122     }
123
124     return output.toString();
125   }
126
127   /**
128    * Gives the hexa representation of an array of bytes.
129    *
130    * @param buffer array of bytes to print
131    * @param start index of first byte to print in buffer
132    * @param stop index of first byte not to print in buffer
133    */

134   public static String JavaDoc toHexString(byte[] buffer, int start, int stop) {
135     return toHexString(buffer, start, stop, 2, " ", 8, "\t", 0);
136   }
137 }
138
Popular Tags