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 string.
24   *
25   * @author The Apache MINA Project (dev@mina.apache.org)
26   * @version $Rev: 686598 $, $Date: 2008-08-17 12:58:23 +0200 (Sun, 17 Aug 2008) $
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',
45                  '9', 'A', 'B', 'C', 'D', 'E', 'F' };
46  
47          int i;
48          byte[] high = new byte[256];
49          byte[] low = new byte[256];
50  
51          for (i = 0; i < 256; i++) {
52              high[i] = digits[i >>> 4];
53              low[i] = digits[i & 0x0F];
54          }
55  
56          highDigits = high;
57          lowDigits = low;
58      }
59  
60      /**
61       * Dumps an {@link IoBuffer} to a hex formatted string.
62       * 
63       * @param in the buffer to dump
64       * @param lengthLimit the limit at which hex dumping will stop
65       * @return a hex formatted string representation of the <i>in</i> {@link Iobuffer}.
66       */
67      public static String getHexdump(IoBuffer in, int lengthLimit) {
68          if (lengthLimit == 0) {
69              throw new IllegalArgumentException("lengthLimit: " + lengthLimit
70                      + " (expected: 1+)");
71          }
72  
73          boolean truncate = in.remaining() > lengthLimit;
74          int size;
75          if (truncate) {
76              size = lengthLimit;
77          } else {
78              size = in.remaining();
79          }
80  
81          if (size == 0) {
82              return "empty";
83          }
84  
85          StringBuilder out = new StringBuilder(in.remaining() * 3 - 1);
86  
87          int mark = in.position();
88  
89          // fill the first
90          int byteValue = in.get() & 0xFF;
91          out.append((char) highDigits[byteValue]);
92          out.append((char) lowDigits[byteValue]);
93          size--;
94  
95          // and the others, too
96          for (; size > 0; size--) {
97              out.append(' ');
98              byteValue = in.get() & 0xFF;
99              out.append((char) highDigits[byteValue]);
100             out.append((char) lowDigits[byteValue]);
101         }
102 
103         in.position(mark);
104 
105         if (truncate) {
106             out.append("...");
107         }
108 
109         return out.toString();
110     }
111 }