blob: 777dc617135cbbed6b5e3b40b92ee87fa693ab54 [file] [log] [blame]
Yuta HIGUCHI385348e2017-05-21 00:30:16 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Yuta HIGUCHI385348e2017-05-21 00:30:16 -07003 *
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.onosproject.bgp.controller.impl;
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 {
Ray Milkey9c9cde42018-01-12 14:22:06 -080026 private static final Logger log = LoggerFactory.getLogger(HexDump.class);
Yuta HIGUCHI385348e2017-05-21 00:30:16 -070027
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 buff.markReaderIndex();
38 try {
39 do {
40 StringBuilder sb = new StringBuilder();
41 for (int k = 0; (k < 16) && (buff.readableBytes() != 0); ++k) {
42 if (0 == k % 4) {
Ray Milkey42b62032018-02-01 11:15:31 -080043 sb.append(" "); // blank after 4 bytes
Yuta HIGUCHI385348e2017-05-21 00:30:16 -070044 }
45 sb.append(String.format("%02X ", buff.readByte()));
46 }
47 log.debug(sb.toString());
48 } while (buff.readableBytes() != 0);
49 } catch (Exception e) {
50 log.error("[HexDump] Invalid buffer: " + e.toString());
51 }
52 buff.resetReaderIndex();
53 }
54}