Example usage for org.apache.commons.codec.binary StringUtils newStringUtf8

List of usage examples for org.apache.commons.codec.binary StringUtils newStringUtf8

Introduction

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

Prototype

public static String newStringUtf8(final byte[] bytes) 

Source Link

Document

Constructs a new String by decoding the specified array of bytes using the UTF-8 charset.

Usage

From source file:com.metamx.milano.proto.MilanoTool.java

/**
 * Get the Base64 representation of this MilanoTool.
 *
 * @return A Base64 encoded string.//from  w w  w  .j  av a2s . c o m
 */
public String getBase64() {
    if (base64 == null) {
        base64 = StringUtils.newStringUtf8(Base64.encodeBase64(typeMetadata.toByteArray()));
    }

    return base64;
}

From source file:com.ibc.util.Base64.java

/**
 * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
 * <p>//from  w w w . ja  v a2 s.co m
 * When encoding the line length and line separator are given in the constructor, and the encoding table is
 * STANDARD_ENCODE_TABLE.
 * </p>
 * <p>
 * Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
 * </p>
 * <p>
 * When decoding all variants are supported.
 * </p>
 * 
 * @param lineLength
 *            Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 4).
 *            If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
 * @param lineSeparator
 *            Each line of encoded data will end with this sequence of bytes.
 * @param urlSafe
 *            Instead of emitting '+' and '/' we emit '-' and '_' respectively. urlSafe is only applied to encode
 *            operations. Decoding seamlessly handles both modes.
 * @throws IllegalArgumentException
 *             The provided lineSeparator included some base64 characters. That's not going to work!
 * @since 1.4
 */
public Base64(int lineLength, byte[] lineSeparator, boolean urlSafe) {
    super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, lineLength,
            lineSeparator == null ? 0 : lineSeparator.length);
    // TODO could be simplified if there is no requirement to reject invalid line sep when length <=0
    // @see test case Base64Test.testConstructors() 
    if (lineSeparator != null) {
        if (containsAlphabetOrPad(lineSeparator)) {
            String sep = StringUtils.newStringUtf8(lineSeparator);
            throw new IllegalArgumentException(
                    "lineSeparator must not contain base64 characters: [" + sep + "]");
        }
        if (lineLength > 0) { // null line-sep forces no chunking rather than throwing IAE
            this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparator.length;
            this.lineSeparator = new byte[lineSeparator.length];
            System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
        } else {
            this.encodeSize = BYTES_PER_ENCODED_BLOCK;
            this.lineSeparator = null;
        }
    } else {
        this.encodeSize = BYTES_PER_ENCODED_BLOCK;
        this.lineSeparator = null;
    }
    this.decodeSize = this.encodeSize - 1;
    this.encodeTable = urlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE;
}

From source file:com.ibc.util.BaseNCodec.java

/**
 * Encodes a byte[] containing binary data, into a String containing characters in the appropriate alphabet.
 * Uses UTF8 encoding.// w  ww  . j  a  v a 2  s. c  o m
 *
 * @param pArray a byte array containing binary data
 * @return String containing only character data in the appropriate alphabet.
*/
public String encodeAsString(byte[] pArray) {
    return StringUtils.newStringUtf8(encode(pArray));
}

From source file:com.smartitengineering.cms.spi.impl.content.GroovyGeneratorTest.java

@Test
public void testGroovyVarGeneration() throws IOException {
    TypeVariationGenerator generator = new GroovyVariationGenerator();
    final VariationTemplate template = mockery.mock(VariationTemplate.class);
    WorkspaceAPIImpl impl = new WorkspaceAPIImpl() {

        @Override/*from   w w  w . ja va  2 s.c o  m*/
        public VariationTemplate getVariationTemplate(WorkspaceId id, String name) {
            return template;
        }
    };
    impl.setVariationGenerators(Collections.singletonMap(TemplateType.GROOVY, generator));
    VariationProvider provider = new VariationProviderImpl();
    registerBeanFactory(impl);
    final Field field = mockery.mock(Field.class, "varField");
    final FieldValue value = mockery.mock(FieldValue.class, "varFieldVal");
    final Content content = mockery.mock(Content.class, "varContent");
    final FieldDef fieldDef = mockery.mock(FieldDef.class);
    final Map<String, VariationDef> vars = mockery.mock(Map.class, "varMap");
    final VariationDef def = mockery.mock(VariationDef.class);
    mockery.checking(new Expectations() {

        {
            exactly(1).of(template).getTemplateType();
            will(returnValue(TemplateType.GROOVY));
            exactly(1).of(template).getTemplate();
            will(returnValue(IOUtils.toByteArray(getClass().getClassLoader()
                    .getResourceAsStream("scripts/groovy/GroovyTestVariationGenerator.groovy"))));
            exactly(1).of(template).getName();
            will(returnValue(REP_NAME));
            exactly(1).of(value).getValue();
            will(returnValue(CONTENT));
            exactly(1).of(field).getValue();
            will(returnValue(value));
            exactly(1).of(field).getFieldDef();
            will(returnValue(fieldDef));
            final ContentId contentId = mockery.mock(ContentId.class, "varId");
            exactly(2).of(content).getContentId();
            will(returnValue(contentId));
            final WorkspaceId wId = mockery.mock(WorkspaceId.class, "varWId");
            exactly(1).of(contentId).getWorkspaceId();
            will(returnValue(wId));
            exactly(1).of(fieldDef).getVariations();
            will(returnValue(vars));
            exactly(1).of(vars).get(with(REP_NAME));
            will(returnValue(def));
            exactly(1).of(def).getParameters();
            will(returnValue(Collections.emptyMap()));
            exactly(1).of(def).getMIMEType();
            will(returnValue(GroovyGeneratorTest.MIME_TYPE));
            final ResourceUri rUri = mockery.mock(ResourceUri.class, "varRUri");
            exactly(1).of(def).getResourceUri();
            will(returnValue(rUri));
            exactly(1).of(rUri).getValue();
            will(returnValue("iUri"));
        }
    });
    Variation representation = provider.getVariation(REP_NAME, content, field);
    Assert.assertNotNull(representation);
    Assert.assertEquals(REP_NAME, representation.getName());
    Assert.assertEquals(GroovyGeneratorTest.MIME_TYPE, representation.getMimeType());
    Assert.assertEquals(CONTENT, StringUtils.newStringUtf8(representation.getVariation()));
}

From source file:com.yenlo.identity.application.authenticator.custom.Base32Vitor.java

/**
 * Creates a Base32 / Base32 Hex codec used for decoding and encoding.
 * <p>/*from  w ww.  j  a v a2  s. c o m*/
 * When encoding the line length and line separator are given in the constructor.
 * </p>
 * <p>
 * Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
 * </p>
 *
 * @param lineLength
 *            Each line of encoded data will be at most of the given length (rounded down to nearest multiple of
 *            8). If lineLength &lt;= 0, then the output will not be divided into lines (chunks). Ignored when
 *            decoding.
 * @param lineSeparator
 *            Each line of encoded data will end with this sequence of bytes.
 * @param useHex
 *            if {@code true}, then use Base32 Hex alphabet, otherwise use Base32 alphabet
 * @param pad byte used as padding byte.
 * @throws IllegalArgumentException
 *             The provided lineSeparator included some Base32 characters. That's not going to work! Or the
 *             lineLength &gt; 0 and lineSeparator is null.
 */
public Base32Vitor(final int lineLength, final byte[] lineSeparator, final boolean useHex, final byte pad) {
    super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, lineLength,
            lineSeparator == null ? 0 : lineSeparator.length, pad);
    if (useHex) {
        this.encodeTable = HEX_ENCODE_TABLE;
        this.decodeTable = HEX_DECODE_TABLE;
    } else {
        this.encodeTable = ENCODE_TABLE;
        this.decodeTable = DECODE_TABLE;
    }
    if (lineLength > 0) {
        if (lineSeparator == null) {
            throw new IllegalArgumentException("lineLength " + lineLength + " > 0, but lineSeparator is null");
        }
        // Must be done after initializing the tables
        if (containsAlphabetOrPad(lineSeparator)) {
            final String sep = StringUtils.newStringUtf8(lineSeparator);
            throw new IllegalArgumentException(
                    "lineSeparator must not contain Base32 characters: [" + sep + "]");
        }
        this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparator.length;
        this.lineSeparator = new byte[lineSeparator.length];
        System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
    } else {
        this.encodeSize = BYTES_PER_ENCODED_BLOCK;
        this.lineSeparator = null;
    }
    this.decodeSize = this.encodeSize - 1;

    if (isInAlphabet(pad) || isWhiteSpace(pad)) {
        throw new IllegalArgumentException("pad must not be in alphabet or whitespace");
    }
}

From source file:com.yenlo.identity.application.authenticator.custom.BaseNCodec.java

/**
 * Encodes a byte[] containing binary data, into a String containing characters in the Base-N alphabet.
 * Uses UTF8 encoding./*  w  ww  .  j  a va  2 s  .  c  om*/
 *
 * @param pArray
 *            a byte array containing binary data
 * @return A String containing only Base-N character data
 */
public String encodeToString(final byte[] pArray) {
    return StringUtils.newStringUtf8(encode(pArray));
}

From source file:com.jason.xpass.util.codec.Base64.java

/**
 * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
 * <p>/* ww  w.  j a  v  a2s .c o m*/
 * When encoding the line length and line separator are given in the constructor, and the encoding table is
 * STANDARD_ENCODE_TABLE.
 * </p>
 * <p>
 * Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
 * </p>
 * <p>
 * When decoding all variants are supported.
 * </p>
 *
 * @param lineLength
 *            Each line of encoded data will be at most of the given length (rounded down to nearest multiple of
 *            4). If lineLength &lt;= 0, then the output will not be divided into lines (chunks). Ignored when
 *            decoding.
 * @param lineSeparator
 *            Each line of encoded data will end with this sequence of bytes.
 * @param urlSafe
 *            Instead of emitting '+' and '/' we emit '-' and '_' respectively. urlSafe is only applied to encode
 *            operations. Decoding seamlessly handles both modes.
 *            <b>Note: no padding is added when using the URL-safe alphabet.</b>
 * @throws IllegalArgumentException
 *             The provided lineSeparator included some base64 characters. That's not going to work!
 * @since 1.4
 */
public Base64(final int lineLength, final byte[] lineSeparator, final boolean urlSafe) {
    super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, lineLength,
            lineSeparator == null ? 0 : lineSeparator.length);
    // TODO could be simplified if there is no requirement to reject invalid line sep when length <=0
    // @see test case Base64Test.testConstructors()
    if (lineSeparator != null) {
        if (containsAlphabetOrPad(lineSeparator)) {
            final String sep = StringUtils.newStringUtf8(lineSeparator);
            throw new IllegalArgumentException(
                    "lineSeparator must not contain base64 characters: [" + sep + "]");
        }
        if (lineLength > 0) { // null line-sep forces no chunking rather than throwing IAE
            this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparator.length;
            this.lineSeparator = new byte[lineSeparator.length];
            System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
        } else {
            this.encodeSize = BYTES_PER_ENCODED_BLOCK;
            this.lineSeparator = null;
        }
    } else {
        this.encodeSize = BYTES_PER_ENCODED_BLOCK;
        this.lineSeparator = null;
    }
    this.decodeSize = this.encodeSize - 1;
    this.encodeTable = urlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE;
}

From source file:com.yenlo.identity.application.authenticator.custom.BaseNCodec.java

/**
 * Encodes a byte[] containing binary data, into a String containing characters in the appropriate alphabet.
 * Uses UTF8 encoding.//from   w  ww  . ja v  a2  s.c om
 *
 * @param pArray a byte array containing binary data
 * @return String containing only character data in the appropriate alphabet.
*/
public String encodeAsString(final byte[] pArray) {
    return StringUtils.newStringUtf8(encode(pArray));
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.analysis.sensitivity.ResultFileWriter.java

/**
 * Returns the Base64 encoded serialized string representing the variable.
 * /*  www .  j a  va2 s .co m*/
 * @param variable the variable to serialize
 * @return the Base64 encoded serialized string representing the variable
 * @throws IOException if the variable count not be serialized
 */
private String serialize(Variable variable) throws IOException {
    ObjectOutputStream oos = null;

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(variable);

        //encode without calling Base64#encodeBase64String as versions 
        //prior to 1.5 chunked the output
        byte[] encoding = Base64.encodeBase64(baos.toByteArray(), false);
        return StringUtils.newStringUtf8(encoding);
    } finally {
        if (oos != null) {
            oos.close();
        }
    }
}

From source file:com.hp.datahandle.Base64.java

/**
 * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
 * <p>// w  ww.j  av a 2 s .c  om
 * When encoding the line length and line separator are given in the constructor, and the encoding table is
 * STANDARD_ENCODE_TABLE.
 * </p>
 * <p>
 * Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
 * </p>
 * <p>
 * When decoding all variants are supported.
 * </p>
 * 
 * @param lineLength
 *            Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 4).
 *            If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
 * @param lineSeparator
 *            Each line of encoded data will end with this sequence of bytes.
 * @param urlSafe
 *            Instead of emitting '+' and '/' we emit '-' and '_' respectively. urlSafe is only applied to encode
 *            operations. Decoding seamlessly handles both modes.
 * @throws IllegalArgumentException
 *             The provided lineSeparator included some base64 characters. That's not going to work!
 * @since 1.4
 */
public Base64(int lineLength, byte[] lineSeparator, boolean urlSafe) {
    if (lineSeparator == null) {
        lineLength = 0; // disable chunk-separating
        lineSeparator = CHUNK_SEPARATOR; // this just gets ignored
    }
    this.lineLength = lineLength > 0 ? (lineLength / 4) * 4 : 0;
    this.lineSeparator = new byte[lineSeparator.length];
    System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
    if (lineLength > 0) {
        this.encodeSize = 4 + lineSeparator.length;
    } else {
        this.encodeSize = 4;
    }
    this.decodeSize = this.encodeSize - 1;
    if (containsBase64Byte(lineSeparator)) {
        String sep = StringUtils.newStringUtf8(lineSeparator);
        throw new IllegalArgumentException("lineSeperator must not contain base64 characters: [" + sep + "]");
    }
    this.encodeTable = urlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE;
}