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