blob: b40cf6eff43676a891bc17c0301490fbbf86b7fe [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) {
Shashikanth VHe7eacfd2015-12-28 14:32:56 +053037 buff.markReaderIndex();
Satish Ke107e662015-09-21 19:00:17 +053038 try {
Satish Ke107e662015-09-21 19:00:17 +053039 do {
40 StringBuilder sb = new StringBuilder();
Shashikanth VHe7eacfd2015-12-28 14:32:56 +053041 for (int k = 0; (k < 16) && (buff.readableBytes() != 0); ++k) {
Satish Ke107e662015-09-21 19:00:17 +053042 if (0 == k % 4) {
43 sb.append(String.format(" ")); // blank after 4 bytes
44 }
Shashikanth VHe7eacfd2015-12-28 14:32:56 +053045 sb.append(String.format("%02X ", buff.readByte()));
Satish Ke107e662015-09-21 19:00:17 +053046 }
47 log.debug(sb.toString());
Shashikanth VHe7eacfd2015-12-28 14:32:56 +053048 } while (buff.readableBytes() != 0);
Satish Ke107e662015-09-21 19:00:17 +053049 } catch (Exception e) {
50 log.error("[HexDump] Invalid buffer: " + e.toString());
51 }
Shashikanth VHe7eacfd2015-12-28 14:32:56 +053052 buff.resetReaderIndex();
Satish Ke107e662015-09-21 19:00:17 +053053 }
54}