View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.core.buffer;
21  
22  /**
23   * Provides utility methods to dump an {@link IoBuffer} into a hex formatted
24   * string.
25   *
26   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
27   */
28  class IoBufferHexDumper {
29  
30      /**
31       * The high digits lookup table.
32       */
33      private static final byte[] highDigits;
34  
35      /**
36       * The low digits lookup table.
37       */
38      private static final byte[] lowDigits;
39  
40      /**
41       * Initialize lookup tables.
42       */
43      static {
44  	final byte[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
45  
46  	int i;
47  	byte[] high = new byte[256];
48  	byte[] low = new byte[256];
49  
50  	for (i = 0; i < 256; i++) {
51  	    high[i] = digits[i >>> 4];
52  	    low[i] = digits[i & 0x0F];
53  	}
54  
55  	highDigits = high;
56  	lowDigits = low;
57      }
58  
59      /**
60       * Dumps an {@link IoBuffer} to a hex formatted string.
61       * 
62       * @param in
63       *            the buffer to dump
64       * @param length
65       *            the limit at which hex dumping will stop
66       * @return a hex formatted string representation of the <i>in</i>
67       *         {@link IoBuffer}.
68       */
69      public static String getHexdump(IoBuffer in, int length) {
70  	if (length < 0) {
71  	    throw new IllegalArgumentException("length: " + length + " must be non-negative number");
72  	}
73  
74  	int pos = in.position();
75  	int rem = in.limit() - pos;
76  	int items = Math.min(rem, length);
77  
78  	if (items == 0) {
79  	    return "";
80  	}
81  
82  	int lim = pos + items;
83  
84  	StringBuilder out = new StringBuilder((items * 3) + 6);
85  
86  	/* first sequence to align the spaces */{
87  	    int byteValue = in.get(pos++) & 0xFF;
88  	    out.append((char) highDigits[byteValue]);
89  	    out.append((char) lowDigits[byteValue]);
90  	}
91  
92  	/* loop remainder */for (; pos < lim;) {
93  	    out.append(' ');
94  	    int byteValue = in.get(pos++) & 0xFF;
95  	    out.append((char) highDigits[byteValue]);
96  	    out.append((char) lowDigits[byteValue]);
97  	}
98  
99  	if (items != rem) {
100 	    out.append("...");
101 	}
102 
103 	return out.toString();
104     }
105 }