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 the buffer to dump
63       * @param lengthLimit the limit at which hex dumping will stop
64       * @return a hex formatted string representation of the <i>in</i> {@link IoBuffer}.
65       */
66      public static String getHexdump(IoBuffer in, int lengthLimit) {
67          if (lengthLimit == 0) {
68              throw new IllegalArgumentException("lengthLimit: " + lengthLimit + " (expected: 1+)");
69          }
70  
71          int limit = in.limit();
72          int pos = in.position();
73          
74          boolean truncate = limit - pos > lengthLimit;
75          int size;
76          if (truncate) {
77              size = lengthLimit;
78          } else {
79              size = limit - pos;
80          }
81  
82          if (size == 0) {
83              return "empty";
84          }
85  
86          StringBuilder out = new StringBuilder(size * 3 + 3);
87  
88          // fill the first
89          int byteValue = in.get(pos++) & 0xFF;
90          out.append((char) highDigits[byteValue]);
91          out.append((char) lowDigits[byteValue]);
92  
93          // and the others, too
94          for (; pos < limit; ) {
95              out.append(' ');
96              byteValue = in.get(pos++) & 0xFF;
97              out.append((char) highDigits[byteValue]);
98              out.append((char) lowDigits[byteValue]);
99          }
100 
101         if (truncate) {
102             out.append("...");
103         }
104 
105         return out.toString();
106     }
107 }