blob: cfb793908824a8c5aafa84fca0cefec0cec01786 [file] [log] [blame]
Satish Ke107e662015-09-21 19:00:17 +05301/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onlab.util;
17
18import org.jboss.netty.buffer.ChannelBuffer;
19import org.slf4j.Logger;
20import org.slf4j.LoggerFactory;
21
22/**
23 * HexDump class an utility to dump buffer in hex format.
24 */
25public final class HexDump {
26 protected static final Logger log = LoggerFactory.getLogger(HexDump.class);
27
28 private HexDump() {
29 }
30
31 /**
32 * Dump the buffer content in hex format.
33 *
34 * @param buff buffer content to dump in hex format
35 */
36 public static void dump(ChannelBuffer buff) {
37 try {
38 byte[] yTemp;
39 yTemp = buff.array();
40
41 int iStartIndex = buff.readerIndex();
42 int iEndIndex = buff.writerIndex();
43 do {
44 StringBuilder sb = new StringBuilder();
45 for (int k = 0; (k < 16) && (iStartIndex < iEndIndex); ++k) {
46 if (0 == k % 4) {
47 sb.append(String.format(" ")); // blank after 4 bytes
48 }
49 sb.append(String.format("%02X ", yTemp[iStartIndex++]));
50 }
51 log.debug(sb.toString());
52 } while (iStartIndex < iEndIndex);
53 } catch (Exception e) {
54 log.error("[HexDump] Invalid buffer: " + e.toString());
55 }
56 }
57}