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   */
27  class IoBufferHexDumper {
28  
29      /**
30       * The high digits lookup table.
31       */
32      private static final byte[] highDigits;
33  
34      /**
35       * The low digits lookup table.
36       */
37      private static final byte[] lowDigits;
38  
39      /**
40       * Initialize lookup tables.
41       */
42      static {
43          final byte[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
44                  '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
69                      + " (expected: 1+)");
70          }
71  
72          boolean truncate = in.remaining() > lengthLimit;
73          int size;
74          if (truncate) {
75              size = lengthLimit;
76          } else {
77              size = in.remaining();
78          }
79  
80          if (size == 0) {
81              return "empty";
82          }
83  
84          StringBuilder out = new StringBuilder(size * 3 + 3);
85  
86          int mark = in.position();
87  
88          // fill the first
89          int byteValue = in.get() & 0xFF;
90          out.append((char) highDigits[byteValue]);
91          out.append((char) lowDigits[byteValue]);
92          size--;
93  
94          // and the others, too
95          for (; size > 0; size--) {
96              out.append(' ');
97              byteValue = in.get() & 0xFF;
98              out.append((char) highDigits[byteValue]);
99              out.append((char) lowDigits[byteValue]);
100         }
101 
102         in.position(mark);
103 
104         if (truncate) {
105             out.append("...");
106         }
107 
108         return out.toString();
109     }
110 }