Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 1 | /*
|
| 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 | */
|
| 16 | package org.onlab.util;
|
| 17 |
|
| 18 | import org.jboss.netty.buffer.ChannelBuffer;
|
| 19 | import org.slf4j.Logger;
|
| 20 | import org.slf4j.LoggerFactory;
|
| 21 |
|
| 22 | /**
|
| 23 | * HexDump class an utility to dump buffer in hex format.
|
| 24 | */
|
| 25 | public 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 VH | e7eacfd | 2015-12-28 14:32:56 +0530 | [diff] [blame] | 37 | buff.markReaderIndex();
|
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 38 | try {
|
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 39 | do {
|
| 40 | StringBuilder sb = new StringBuilder();
|
Shashikanth VH | e7eacfd | 2015-12-28 14:32:56 +0530 | [diff] [blame] | 41 | for (int k = 0; (k < 16) && (buff.readableBytes() != 0); ++k) {
|
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 42 | if (0 == k % 4) {
|
| 43 | sb.append(String.format(" ")); // blank after 4 bytes
|
| 44 | }
|
Shashikanth VH | e7eacfd | 2015-12-28 14:32:56 +0530 | [diff] [blame] | 45 | sb.append(String.format("%02X ", buff.readByte()));
|
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 46 | }
|
| 47 | log.debug(sb.toString());
|
Shashikanth VH | e7eacfd | 2015-12-28 14:32:56 +0530 | [diff] [blame] | 48 | } while (buff.readableBytes() != 0);
|
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 49 | } catch (Exception e) {
|
| 50 | log.error("[HexDump] Invalid buffer: " + e.toString());
|
| 51 | }
|
Shashikanth VH | e7eacfd | 2015-12-28 14:32:56 +0530 | [diff] [blame] | 52 | buff.resetReaderIndex();
|
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 53 | }
|
| 54 | }
|