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

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

Introduction

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

Prototype

public static byte[] fromAscii(byte[] ascii) 

Source Link

Document

Decodes a byte array where each byte represents an ascii '0' or '1'.

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());//from   w  w w .j a 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);
}

From source file:org.callimachusproject.xproc.DecodeTextStep.java

private String decodeText(XdmNode source_read) throws UnsupportedEncodingException, DecoderException {
    String text = extractText(source_read);
    if ("base64".equals(encoding)) {
        if (charset == null) {
            throw XProcException.stepError(10);
        }//from   w w w .j  a  v a  2 s .c  o  m
        byte[] decoded = Base64.decodeBase64(text);
        return new String(decoded, charset);
    } else if ("base32".equals(encoding)) {
        if (charset == null) {
            throw XProcException.stepError(10);
        }
        byte[] decoded = new Base32().decode(text);
        return new String(decoded, charset);
    } else if ("hex".equals(encoding)) {
        if (charset == null) {
            throw XProcException.stepError(10);
        }
        byte[] decoded = Hex.decodeHex(text.toCharArray());
        return new String(decoded, charset);
    } else if ("binary".equals(encoding)) {
        if (charset == null) {
            throw XProcException.stepError(10);
        }
        byte[] decoded = BinaryCodec.fromAscii(text.toCharArray());
        return new String(decoded, charset);
    } else if ("quoted-printable".equals(encoding)) {
        if (charset == null) {
            throw XProcException.stepError(10);
        }
        return new QuotedPrintableCodec(charset).decode(text);
    } else if ("www-form-urlencoded".equals(encoding)) {
        if (charset == null) {
            throw XProcException.stepError(10);
        }
        return new URLCodec(charset).decode(text);
    } else if (encoding != null && encoding.length() != 0) {
        throw new XProcException(step.getNode(), "Unexpected encoding: " + encoding);
    } else {
        return text;
    }
}

From source file:org.callimachusproject.xproc.DeserializeCascadingStyleSheetStep.java

private String decodeText(XdmNode doc) throws UnsupportedEncodingException, DecoderException {
    String text = extractText(doc);
    if ("base64".equals(encoding)) {
        if (charset == null) {
            throw XProcException.stepError(10);
        }// w w  w. j a  va 2  s  . c  o m
        byte[] decoded = Base64.decodeBase64(text);
        return new String(decoded, charset);
    } else if ("base32".equals(encoding)) {
        if (charset == null) {
            throw XProcException.stepError(10);
        }
        byte[] decoded = new Base32().decode(text);
        return new String(decoded, charset);
    } else if ("hex".equals(encoding)) {
        if (charset == null) {
            throw XProcException.stepError(10);
        }
        byte[] decoded = Hex.decodeHex(text.toCharArray());
        return new String(decoded, charset);
    } else if ("binary".equals(encoding)) {
        if (charset == null) {
            throw XProcException.stepError(10);
        }
        byte[] decoded = BinaryCodec.fromAscii(text.toCharArray());
        return new String(decoded, charset);
    } else if ("quoted-printable".equals(encoding)) {
        if (charset == null) {
            throw XProcException.stepError(10);
        }
        return new QuotedPrintableCodec(charset).decode(text);
    } else if ("www-form-urlencoded".equals(encoding)) {
        if (charset == null) {
            throw XProcException.stepError(10);
        }
        return new URLCodec(charset).decode(text);
    } else if (encoding != null && encoding.length() != 0) {
        throw new XProcException(step.getNode(), "Unexpected encoding: " + encoding);
    } else {
        return text;
    }
}

From source file:org.owasp.jbrofuzz.encode.EncoderHashCore.java

private static String decodeBinary(final String decodeText) {
    return new String(BinaryCodec.fromAscii(decodeText.getBytes()));
}

From source file:tk.jomp16.plugin.codecutils.CodecUtils.java

@Command("decode")
public void decode(CommandEvent commandEvent) throws Exception {
    if (commandEvent.getArgs().size() >= 2) {
        switch (commandEvent.getArgs().get(0).toLowerCase()) {
        case "base64":
            commandEvent.respond(new String(Base64.getDecoder().decode(commandEvent.getArgs().get(1))));
            break;
        case "hex":
            commandEvent.respond(new String(Hex.decodeHex(commandEvent.getArgs().get(1).toCharArray())));
            break;
        case "binary":
            commandEvent.respond(new String(
                    BinaryCodec.fromAscii(commandEvent.getArgs().get(1).replace(" ", "").getBytes())));
            break;
        default:/* ww  w. j a  va 2  s . c  o m*/
            commandEvent.showUsage(this, "decode");
            break;
        }
    } else {
        commandEvent.showUsage(this, "decode");
    }
}

From source file:tk.jomp16.plugin.codecutils.ui.EncodeDecodePanel.java

public EncodeDecodePanel(boolean encode) {
    base64Button.addActionListener(e -> {
        if (encode) {
            if (inputTextArea.getText().length() != 0)
                outputTextArea/*from  www .ja  va2 s  .c  o  m*/
                        .setText(new String(Base64.getEncoder().encode(inputTextArea.getText().getBytes())));
        } else {
            if (inputTextArea.getText().length() != 0)
                outputTextArea.setText(new String(Base64.getDecoder().decode(inputTextArea.getText())));
        }
    });

    hexadecimalButton.addActionListener(e -> {
        if (encode) {
            if (inputTextArea.getText().length() != 0)
                outputTextArea.setText(String.valueOf(Hex.encodeHex(inputTextArea.getText().getBytes())));
        } else {
            if (inputTextArea.getText().length() != 0) {
                try {
                    outputTextArea.setText(new String(Hex.decodeHex(inputTextArea.getText().toCharArray())));
                } catch (DecoderException e1) {
                    e1.printStackTrace();
                }
            }
        }
    });

    binaryButton.addActionListener(e -> {
        if (encode) {
            if (inputTextArea.getText().length() != 0)
                outputTextArea.setText(CodecUtils.getBinary(inputTextArea.getText()));
        } else {
            if (inputTextArea.getText().length() != 0)
                outputTextArea.setText(
                        new String(BinaryCodec.fromAscii(inputTextArea.getText().replace(" ", "").getBytes())));
        }
    });
}