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.support;
    21
    22  import org.apache.mina.common.ByteBuffer;
    23
    24  /**
    25   * Provides utility methods for ByteBuffers.
    26   *
    27   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    28   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
    29   */
    30  public class ByteBufferHexDumper {
    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(ByteBuffer in) {
    54          int size = in.remaining();
    55
    56          if (size == 0) {
    57              return "empty";
    58          }
    59
    60          StringBuffer out = new StringBuffer((in.remaining() * 3) - 1);
    61
    62          int mark = in.position();
    63
    64          // fill the first
    65          int byteValue = in.get() & 0xFF;
    66          out.append((char) highDigits[byteValue]);
    67          out.append((char) lowDigits[byteValue]);
    68          size--;
    69
    70          // and the others, too
    71          for (; size > 0; size--) {
    72              out.append(' ');
    73              byteValue = in.get() & 0xFF;
    74              out.append((char) highDigits[byteValue]);
    75              out.append((char) lowDigits[byteValue]);
    76          }
    77
    78          in.position(mark);
    79
    80          return out.toString();
    81      }
    82  }