com.tera.common.util.ConsolePrinter.java Source code

Java tutorial

Introduction

Here is the source code for com.tera.common.util.ConsolePrinter.java

Source

/**
 * This file is part of tera-api.
 * 
 * tera-api is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * tera-api is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with tera-api.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.tera.common.util;

import java.nio.ByteBuffer;

import org.apache.commons.lang.StringUtils;
import org.jboss.netty.buffer.ChannelBuffer;

/**
 * @author ATracer
 */
public class ConsolePrinter {

    /**
     * @param sectionName
     */
    public static void printSection(String sectionName) {
        System.out.println();
        sectionName = "-[ " + sectionName + " ]";
        while (sectionName.length() < 79)
            sectionName = "=" + sectionName;
        System.out.println(sectionName);
    }

    /**
     * @param data ByteBuffer or ChannelBuffer
     * @return
     */
    public static String toHex(Object data) {
        if (data instanceof ByteBuffer) {
            return toHex((ByteBuffer) data);
        } else if (data instanceof ChannelBuffer) {
            return toHex((ChannelBuffer) data);
        }
        return StringUtils.EMPTY;
    }

    /**
     * Convert data from given ByteBuffer to hex
     * 
     * @param data
     * @return hex
     */
    public static String toHex(ByteBuffer data) {
        StringBuilder result = new StringBuilder();
        int counter = 0;
        int b;
        while (data.hasRemaining()) {
            if (counter % 16 == 0)
                result.append(String.format("%04X: ", counter));

            b = data.get() & 0xff;
            result.append(String.format("%02X ", b));

            counter++;
            if (counter % 16 == 0) {
                result.append("  ");
                toText(data, result, 16);
                result.append("\n");
            }
        }
        int rest = counter % 16;
        if (rest > 0) {
            for (int i = 0; i < 17 - rest; i++) {
                result.append("   ");
            }
            toText(data, result, rest);
        }
        return result.toString();
    }

    /**
     * Convert data from given ChannelBuffer to hex
     * 
     * @param data
     * @return
     */
    public static String toHex(ChannelBuffer data) {
        StringBuilder result = new StringBuilder();
        int counter = 0;
        int b;
        while (data.readable()) {
            if (counter % 16 == 0)
                result.append(String.format("%04X: ", counter));

            b = data.readByte() & 0xff;
            result.append(String.format("%02X ", b));

            counter++;
            if (counter % 16 == 0) {
                result.append("  ");
                toText(data, result, 16);
                result.append("\n");
            }
        }
        int rest = counter % 16;
        if (rest > 0) {
            for (int i = 0; i < 17 - rest; i++) {
                result.append("   ");
            }
            toText(data, result, rest);
        }
        return result.toString();
    }

    /**
     * Gets last <tt>cnt</tt> read bytes from the <tt>data</tt> buffer and puts
     * into <tt>result</tt> buffer in special format:
     * <ul>
     * <li>if byte represents char from partition 0x1F to 0x80 (which are normal
     * ascii chars) then it's put into buffer as it is</li>
     * <li>otherwise dot is put into buffer</li>
     * </ul>
     * 
     * @param data
     * @param result
     * @param cnt
     */
    private static void toText(ByteBuffer data, StringBuilder result, int cnt) {
        int charPos = data.position() - cnt;
        for (int a = 0; a < cnt; a++) {
            int c = data.get(charPos++);
            if (c > 0x1f && c < 0x80)
                result.append((char) c);
            else
                result.append('.');
        }
    }

    /**
     * @param data
     * @param result
     * @param cnt
     */
    private static void toText(ChannelBuffer data, StringBuilder result, int cnt) {
        int charPos = data.readerIndex() - cnt;
        for (int a = 0; a < cnt; a++) {
            int c = data.getByte(charPos++);
            if (c > 0x1f && c < 0x80)
                result.append((char) c);
            else
                result.append('.');
        }
    }
}