Example usage for org.apache.commons.codec.binary BinaryCodec toAsciiString

List of usage examples for org.apache.commons.codec.binary BinaryCodec toAsciiString

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary BinaryCodec toAsciiString.

Prototype

public static String toAsciiString(byte[] raw) 

Source Link

Document

Converts an array of raw binary data into a String of ascii 0 and 1 characters.

Usage

From source file:com.nebkat.plugin.text.TextPlugin.java

@EventHandler
@CommandFilter("encode")
public void onEncodeCommand(CommandEvent e) {
    if (e.getParams().length < 2) {
        e.showUsage(getBot());// w w w  . ja  v  a  2 s  . com
        return;
    }
    String algorithm = e.getParams()[0];
    String text = e.getRawParams().substring(algorithm.length() + 1);
    boolean decode = e.getCommandString().equals("decode");
    byte[] bytes = text.getBytes(Charset.defaultCharset());
    String result = "Algorithm unsupported";
    if (algorithm.equalsIgnoreCase("base64")) {
        result = !decode ? Base64.encodeBase64String(bytes)
                : new String(Base64.decodeBase64(text), Charset.defaultCharset());
    } else if (algorithm.equals("hex")) {
        if (!decode) {
            result = Hex.encodeHexString(bytes);
        } else {
            try {
                result = new String(Hex.decodeHex(text.toCharArray()), Charset.defaultCharset());
            } catch (DecoderException de) {
                result = "Invalid hex input";
            }
        }
    } else if (algorithm.equalsIgnoreCase("binary")) {
        result = !decode ? BinaryCodec.toAsciiString(bytes)
                : new String(BinaryCodec.fromAscii(text.toCharArray()), Charset.defaultCharset());
    } else if (algorithm.equalsIgnoreCase("morse")) {
        result = !decode ? Morse.stringToMorse(text) : Morse.stringFromMorse(text);
    } else if (algorithm.equalsIgnoreCase("vigenere")) {
        String key = e.getParams()[1];
        result = !decode ? Vigenere.encrypt(e.getRawParamsAfter(2), key)
                : Vigenere.decrypt(e.getRawParamsAfter(2), key);
    } else if (algorithm.equalsIgnoreCase("rot")) {
        try {
            result = Rot.encrypt(e.getRawParamsAfter(2).toUpperCase(),
                    (!decode ? 1 : -1) * Integer.parseInt(e.getParams()[1]));
        } catch (NumberFormatException nfe) {
            result = "Invalid rot input";

        }
    }
    Irc.message(e.getSession(), e.getTarget(), e.getSource().getNick() + ": " + result);
}