Example usage for org.apache.commons.codec BinaryDecoder decode

List of usage examples for org.apache.commons.codec BinaryDecoder decode

Introduction

In this page you can find the example usage for org.apache.commons.codec BinaryDecoder decode.

Prototype

byte[] decode(byte[] source) throws DecoderException;

Source Link

Document

Decodes a byte array and returns the results as a byte array.

Usage

From source file:com.github.jinahya.codec.commons.RareBinaryDecoderProxyTest.java

@Test
public void testAsBinaryDecoder() throws DecoderException {

    final BinaryDecoder decoder = (BinaryDecoder) RareBinaryDecoderProxy.newInstance();

    try {//from  w  w  w .  j a  v  a2  s .c  om
        decoder.decode((byte[]) null);
        Assert.fail("passed.decode((byte[]) null)");
    } catch (final NullPointerException npe) {
        // expected
    }

    final byte[] expected = new byte[0];
    final byte[] actual = decoder.decode(expected);
    Assert.assertEquals(actual, expected);
}

From source file:com.github.jinahya.codec.HexBinaryDecoderProxyTest.java

@Test
public void testAsBinaryDecoder() throws DecoderException {

    final BinaryDecoder decoder = (BinaryDecoder) HexBinaryDecoderProxy.newInstance();

    try {/*  ww  w. j a  v a 2 s. c o  m*/
        decoder.decode((byte[]) null);
        Assert.fail("decode((byte[]) null)");
    } catch (final NullPointerException npe) {
        // expected
    }

    final byte[] encoded = Tests.encodedBytes();
    final byte[] decoded = decoder.decode(encoded);
}

From source file:edu.northwestern.cbits.purple_robot_manager.http.BasicAuthTokenExtractor.java

public String extract(final HttpRequest request) throws HttpException {
    String auth = null;/* ww  w . j a  va2 s.  c  o  m*/
    final Header h = request.getFirstHeader(AUTH.WWW_AUTH_RESP);

    if (h != null) {
        final String s = h.getValue();

        if (s != null) {
            auth = s.trim();
        }
    }

    if (auth != null) {
        final int i = auth.indexOf(' ');

        if (i == -1) {
            throw new ProtocolException("Invalid Authorization header: " + auth);
        }

        final String authscheme = auth.substring(0, i);

        if (authscheme.equalsIgnoreCase("basic")) {
            final String s = auth.substring(i + 1).trim();

            try {
                final byte[] credsRaw = EncodingUtils.getAsciiBytes(s);
                final BinaryDecoder codec = new Base64();
                auth = EncodingUtils.getAsciiString(codec.decode(credsRaw));
            } catch (final DecoderException ex) {
                throw new ProtocolException("Malformed BASIC credentials");
            }
        }
    }
    return auth;
}

From source file:com.github.jinahya.codec.PercentBinaryDecoderProxyTest.java

@Test
public void testAsBinaryDecoder() throws Exception {

    final BinaryDecoder decoder = (BinaryDecoder) PercentBinaryDecoderProxy.newInstance();

    try {// ww w  .ja va 2  s.c  o m
        decoder.decode((byte[]) null);
        Assert.fail("passed: decode((byte[]) null)");
    } catch (NullPointerException npe) {
        // ok
    }

    final byte[] input = PercentCodecTestHelper.encodedBytes(1024);
    final byte[] output = decoder.decode(input);
}

From source file:com.github.jinahya.codec.PercentBinaryDecoderProxyTest.java

@Test(invocationCount = 128)
public void testDecode() throws Exception {

    final BinaryDecoder decoder = (BinaryDecoder) PercentBinaryDecoderProxy.newInstance();

    try {/*from w w w .ja va 2s  . c  o  m*/
        decoder.decode((Object) null);
        Assert.fail("passed: decode((Object) null)");
    } catch (NullPointerException npe) {
        // ok
    }

    try {
        decoder.decode((byte[]) null);
        Assert.fail("passed: decode((byte[]) null)");
    } catch (NullPointerException npe) {
        // ok
    }

    final Random random = ThreadLocalRandom.current();

    final byte[] expected = new byte[random.nextInt(128)];
    random.nextBytes(expected);
    System.out.println("original ----------------------------------------");
    System.out.println(Base64.encodeBase64String(expected));

    final byte[] encoded = PercentEncoder.encodeMultiple(expected);
    System.out.println("encoded -----------------------------------------");
    System.out.println(new String(encoded, "US-ASCII"));

    final byte[] actual = decoder.decode(encoded);
    System.out.println("decoded -----------------------------------------");
    System.out.println(Base64.encodeBase64String(actual));

    Assert.assertEquals(actual, expected);
}

From source file:org.apache.hc.client5.testing.auth.BasicAuthTokenExtractor.java

public String extract(final String challengeResponse) throws HttpException {
    if (challengeResponse != null) {
        final int i = challengeResponse.indexOf(' ');
        if (i == -1) {
            throw new ProtocolException("Invalid challenge response: " + challengeResponse);
        }//from   www .ja v a  2  s.  c  om
        final String authscheme = challengeResponse.substring(0, i);
        if (authscheme.equalsIgnoreCase("basic")) {
            final String s = challengeResponse.substring(i + 1).trim();
            try {
                final byte[] credsRaw = s.getBytes(StandardCharsets.US_ASCII);
                final BinaryDecoder codec = new Base64();
                return new String(codec.decode(credsRaw), StandardCharsets.US_ASCII);
            } catch (final DecoderException ex) {
                throw new ProtocolException("Malformed BASIC credentials");
            }
        }
    }
    return null;
}

From source file:org.apache.http.localserver.BasicAuthTokenExtractor.java

public String extract(final HttpRequest request) throws HttpException {
    String auth = null;//w w w . j  a  va 2  s  . c  o m

    Header h = request.getFirstHeader(AUTH.WWW_AUTH_RESP);
    if (h != null) {
        String s = h.getValue();
        if (s != null) {
            auth = s.trim();
        }
    }

    if (auth != null) {
        int i = auth.indexOf(' ');
        if (i == -1) {
            throw new ProtocolException("Invalid Authorization header: " + auth);
        }
        String authscheme = auth.substring(0, i);
        if (authscheme.equalsIgnoreCase("basic")) {
            String s = auth.substring(i + 1).trim();
            try {
                byte[] credsRaw = EncodingUtils.getAsciiBytes(s);
                BinaryDecoder codec = new Base64();
                auth = EncodingUtils.getAsciiString(codec.decode(credsRaw));
            } catch (DecoderException ex) {
                throw new ProtocolException("Malformed BASIC credentials");
            }
        }
    }
    return auth;
}

From source file:org.apache.http.localserver.RequestBasicAuth.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {

    String auth = null;/*ww w. j av  a 2 s  .  co  m*/

    Header h = request.getFirstHeader(AUTH.WWW_AUTH_RESP);
    if (h != null) {
        String s = h.getValue();
        if (s != null) {
            auth = s.trim();
        }
    }

    if (auth != null) {
        int i = auth.indexOf(' ');
        if (i == -1) {
            throw new ProtocolException("Invalid Authorization header: " + auth);
        }
        String authscheme = auth.substring(0, i);
        if (authscheme.equalsIgnoreCase("basic")) {
            String s = auth.substring(i + 1).trim();
            byte[] credsRaw = s.getBytes(HTTP.ASCII);
            BinaryDecoder codec = new Base64();
            try {
                String creds = new String(codec.decode(credsRaw), HTTP.ASCII);
                context.setAttribute("creds", creds);
            } catch (DecoderException ex) {
                throw new ProtocolException("Malformed BASIC credentials");
            }
        }
    }
}