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.common;
21  
22  
23  /**
24   * Provides utility methods for an {@link IoBuffer}.
25   *
26   * @author The Apache MINA Project (dev@mina.apache.org)
27   * @version $Rev: 581239 $, $Date: 2007-10-02 07:51:26 -0600 (Tue, 02 Oct 2007) $
28   */
29  class IoBufferHexDumper {
30      private static final byte[] highDigits;
31  
32      private static final byte[] lowDigits;
33  
34      // initialize lookup tables
35      static {
36          final byte[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
37                  '9', 'A', 'B', 'C', 'D', 'E', 'F' };
38  
39          int i;
40          byte[] high = new byte[256];
41          byte[] low = new byte[256];
42  
43          for (i = 0; i < 256; i++) {
44              high[i] = digits[i >>> 4];
45              low[i] = digits[i & 0x0F];
46          }
47  
48          highDigits = high;
49          lowDigits = low;
50      }
51  
52      public static String getHexdump(IoBuffer in, int lengthLimit) {
53          if (lengthLimit == 0) {
54              throw new IllegalArgumentException("lengthLimit: " + lengthLimit
55                      + " (expected: 1+)");
56          }
57  
58          boolean truncate = in.remaining() > lengthLimit;
59          int size;
60          if (truncate) {
61              size = lengthLimit;
62          } else {
63              size = in.remaining();
64          }
65  
66          if (size == 0) {
67              return "empty";
68          }
69  
70          StringBuffer out = new StringBuffer(in.remaining() * 3 - 1);
71  
72          int mark = in.position();
73  
74          // fill the first
75          int byteValue = in.get() & 0xFF;
76          out.append((char) highDigits[byteValue]);
77          out.append((char) lowDigits[byteValue]);
78          size--;
79  
80          // and the others, too
81          for (; size > 0; size--) {
82              out.append(' ');
83              byteValue = in.get() & 0xFF;
84              out.append((char) highDigits[byteValue]);
85              out.append((char) lowDigits[byteValue]);
86          }
87  
88          in.position(mark);
89  
90          if (truncate) {
91              out.append("...");
92          }
93  
94          return out.toString();
95      }
96  }