Example usage for org.apache.commons.codec.binary Base64OutputStream Base64OutputStream

List of usage examples for org.apache.commons.codec.binary Base64OutputStream Base64OutputStream

Introduction

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

Prototype

public Base64OutputStream(OutputStream out) 

Source Link

Usage

From source file:fr.dutra.confluence2wordpress.util.CodecUtils.java

public static String compressAndEncode(String text) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = new GZIPOutputStream(new Base64OutputStream(baos));
    try {//ww w  .  j  a  va2 s  .com
        gzos.write(text.getBytes(UTF_8));
    } finally {
        baos.close();
        gzos.close();
    }
    return new String(baos.toByteArray(), UTF_8);
}

From source file:com.mirth.connect.donkey.util.Base64Util.java

/**
 * Encodes binary data using the base64 algorithm and chunks the encoded output into 76
 * character blocks.//from   w  ww.ja v a  2s. c o m
 * This method sets the initial output buffer to a value that is "guaranteed" to be slightly
 * larger than necessary.
 * Therefore the buffer should never need to be expanded, making the maximum memory requirements
 * much lower than
 * using Base64.encodeBase64Chunked.
 * 
 * @param bytes
 * @param chunked
 * @return
 * @throws IOException
 */
public static byte[] encodeBase64(byte[] bytes, boolean chunked) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    // Set the size of the buffer to minimize peak memory usage
    // A Base64 encoded message takes roughly 1.333 times the memory of the raw binary, so use 1.4 to be safe.
    ByteArrayOutputStream baos = new ByteArrayOutputStream((int) (bytes.length * 1.4));
    Base64OutputStream b64os = null;
    if (chunked) {
        b64os = new Base64OutputStream(baos);
    } else {
        b64os = new Base64OutputStream(baos, true, 0, null);
    }

    // Perform the encoding
    IOUtils.copy(bais, b64os);

    // Free up any memory from the input
    b64os.close();

    return baos.toByteArray();
}

From source file:com.scorpio4.util.io.IOStreamCrypto.java

public CipherOutputStream encrypt(OutputStream out) throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidAlgorithmParameterException, InvalidKeyException {
    final SecretKey key = new SecretKeySpec(bytePassword, cipherSpec);
    final IvParameterSpec IV = new IvParameterSpec(ivBytes);
    final Cipher cipher = Cipher.getInstance(cipherTransformation);
    cipher.init(Cipher.ENCRYPT_MODE, key, IV);
    return new CipherOutputStream(new Base64OutputStream(out), cipher);
}

From source file:com.asksunny.tool.Base64Tool.java

protected void encode() throws IOException {

    try (FileInputStream fin = new FileInputStream(pathToInput);
            FileOutputStream fout = new FileOutputStream(this.pathToOutput);
            Base64OutputStream b64out = new Base64OutputStream(fout)) {

        byte[] buf = new byte[BUFFER_SIZE];
        int len = 0;
        while ((len = fin.read(buf)) != -1) {
            b64out.write(buf, 0, len);/*from  w  w w  .  j a  v a  2s. c  om*/
        }
        fout.flush();
    }
}

From source file:com.brif.nix.parser.BinaryTempFileBody.java

public void writeTo(OutputStream out) throws IOException, MessagingException {
    InputStream in = getInputStream();
    Base64OutputStream base64Out = new Base64OutputStream(out);
    IOUtils.copy(in, base64Out);/*from w w  w  .  ja  v  a2s.  com*/
    base64Out.close();
    mFile.delete();
}

From source file:gobblin.crypto.EncodingBenchmark.java

@Benchmark
public byte[] write1KRecordsBase64Only(EncodingBenchmarkState state) throws IOException {
    ByteArrayOutputStream sink = new ByteArrayOutputStream();
    OutputStream os = new Base64OutputStream(sink);
    os.write(state.OneKBytes);/*  w  ww.j  a va  2s.com*/
    os.close();

    return sink.toByteArray();
}

From source file:com.amalto.core.storage.hibernate.TypeMapping.java

private static Object _serializeValue(Object value, FieldMetadata sourceField, FieldMetadata targetField,
        Session session) {//from  w  ww  . j av a  2s .  co  m
    if (targetField == null) {
        return value;
    }
    if (!targetField.isMany()) {
        Boolean targetZipped = targetField.<Boolean>getData(MetadataRepository.DATA_ZIPPED);
        Boolean sourceZipped = sourceField.<Boolean>getData(MetadataRepository.DATA_ZIPPED);
        if (sourceZipped == null && Boolean.TRUE.equals(targetZipped)) {
            try {
                ByteArrayOutputStream characters = new ByteArrayOutputStream();
                OutputStream bos = new Base64OutputStream(characters);
                ZipOutputStream zos = new ZipOutputStream(bos);
                ZipEntry zipEntry = new ZipEntry("content"); //$NON-NLS-1$
                zos.putNextEntry(zipEntry);
                zos.write(String.valueOf(value).getBytes("UTF-8")); //$NON-NLS-1$
                zos.closeEntry();
                zos.flush();
                zos.close();
                return new String(characters.toByteArray());
            } catch (IOException e) {
                throw new RuntimeException("Unexpected compression exception", e); //$NON-NLS-1$
            }
        }
        String targetSQLType = targetField.getType().getData(TypeMapping.SQL_TYPE);
        if (targetSQLType != null && SQL_TYPE_CLOB.equals(targetSQLType)) {
            if (value != null) {
                return Hibernate.getLobCreator(session).createClob(String.valueOf(value));
            } else {
                return null;
            }
        }
    }
    return value;
}

From source file:com.asakusafw.runtime.stage.output.BridgeOutputFormat.java

private static void save(Configuration conf, List<OutputSpec> specs) {
    assert conf != null;
    assert specs != null;
    for (OutputSpec spec : specs) {
        if (spec.resolved) {
            throw new IllegalStateException();
        }/*  w  w  w  .j  a  va2 s.  com*/
    }
    ByteArrayOutputStream sink = new ByteArrayOutputStream();
    try (DataOutputStream output = new DataOutputStream(new GZIPOutputStream(new Base64OutputStream(sink)))) {
        WritableUtils.writeVLong(output, SERIAL_VERSION);
        WritableUtils.writeVInt(output, specs.size());
        for (OutputSpec spec : specs) {
            WritableUtils.writeString(output, spec.basePath);
            WritableUtils.writeVInt(output, spec.deletePatterns.size());
            for (String pattern : spec.deletePatterns) {
                WritableUtils.writeString(output, pattern);
            }
        }
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    conf.set(KEY, new String(sink.toByteArray(), ASCII));
}

From source file:com.asakusafw.runtime.stage.input.StageInputDriver.java

private static String encode(List<StageInput> inputList) throws IOException {
    assert inputList != null;
    String[] dictionary = buildDictionary(inputList);
    ByteArrayOutputStream sink = new ByteArrayOutputStream();
    try (DataOutputStream output = new DataOutputStream(new GZIPOutputStream(new Base64OutputStream(sink)))) {
        WritableUtils.writeVLong(output, SERIAL_VERSION);
        WritableUtils.writeStringArray(output, dictionary);
        WritableUtils.writeVInt(output, inputList.size());
        for (StageInput input : inputList) {
            writeEncoded(output, dictionary, input.getPathString());
            writeEncoded(output, dictionary, input.getFormatClass().getName());
            writeEncoded(output, dictionary, input.getMapperClass().getName());
            WritableUtils.writeVInt(output, input.getAttributes().size());
            for (Map.Entry<String, String> attribute : input.getAttributes().entrySet()) {
                writeEncoded(output, dictionary, attribute.getKey());
                writeEncoded(output, dictionary, attribute.getValue());
            }//from   w w w  . j  a  v a  2s  .  co m
        }
    }
    return new String(sink.toByteArray(), ASCII);
}

From source file:com.xpn.xwiki.internal.xml.XMLWriter.java

/**
 * Writes the <code>{@link Element}</code>, including its <code>{@link
 * Attribute}</code>s, using the <code>{@link InputStream}</code> encoded in Base64 for its content.
 * //from ww  w  . ja  v  a 2 s  .c  om
 * @param element <code>{@link Element}</code> to output.
 * @param is <code>{@link InputStream}</code> that will be fully read and encoded in Base64 into the element
 *            content.
 * @throws IOException a problem occurs during reading or writing.
 */
public void writeBase64(Element element, InputStream is) throws IOException {
    writeOpen(element);
    flush();
    Base64OutputStream base64 = new Base64OutputStream(new CloseShieldOutputStream(this.out));
    IOUtils.copy(is, base64);
    base64.close();
    writeClose(element);
}