/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.dataformat.base64;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.camel.Exchange;
import org.apache.camel.spi.DataFormat;
import org.apache.camel.util.ExchangeHelper;
import org.apache.camel.util.IOHelper;
import org.apache.commons.codec.binary.Base64;

public class Base64DataFormat implements DataFormat {

    private int lineLength = Base64.MIME_CHUNK_SIZE;
    private byte[] lineSeparator = {'\r', '\n'};
    private boolean urlSafe;

    @Override
    public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception {
        byte[] decoded = ExchangeHelper.convertToMandatoryType(exchange, byte[].class, graph);

        Base64 codec = createCodec();
        byte[] encoded = codec.encode(decoded);

        stream.write(encoded);
    }

    @Override
    public Object unmarshal(Exchange exchange, InputStream input) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOHelper.copyAndCloseInput(input, baos);
        byte[] encoded = baos.toByteArray();
        baos.close();

        Base64 codec = createCodec();
        byte[] decoded = codec.decode(encoded);

        return decoded;
    }

    private Base64 createCodec() {
        return new Base64(lineLength, lineSeparator, urlSafe);
    }

    public int getLineLength() {
        return lineLength;
    }

    public void setLineLength(int lineLength) {
        this.lineLength = lineLength;
    }

    public byte[] getLineSeparator() {
        return lineSeparator;
    }

    public void setLineSeparator(byte[] lineSeparator) {
        this.lineSeparator = lineSeparator;
    }

    public boolean isUrlSafe() {
        return urlSafe;
    }

    public void setUrlSafe(boolean urlSafe) {
        this.urlSafe = urlSafe;
    }
}

@@ -846,46 +846,8 @@
 4;%0A%0A
-import java.io.ByteArrayOutputStream;%0A
 impo
@@ -870,16 +870,16 @@
 Stream;%0A
+
 import j
@@ -1104,16 +1104,133 @@
 .Base64;
+%0Aimport org.apache.commons.codec.binary.Base64InputStream;%0Aimport org.apache.commons.codec.binary.Base64OutputStream;
 %0A%0Apublic
@@ -1513,32 +1513,774 @@
 ows Exception %7B%0A
+        if (urlSafe) %7B%0A            marshalUrlSafe(exchange, graph, stream);%0A        %7D else %7B%0A            marshalStreaming(exchange, graph, stream);%0A        %7D%0A    %7D%0A%0A    private void marshalStreaming(Exchange exchange, Object graph, OutputStream stream) throws Exception %7B%0A        InputStream decoded = ExchangeHelper.convertToMandatoryType(exchange, InputStream.class, graph);%0A%0A        Base64OutputStream base64Output = new Base64OutputStream(stream, true, lineLength, lineSeparator);%0A        try %7B%0A            IOHelper.copy(decoded, base64Output);%0A        %7D finally %7B%0A            IOHelper.close(decoded, base64Output);%0A        %7D%0A    %7D%0A%0A    private void marshalUrlSafe(Exchange exchange, Object graph, OutputStream stream) throws Exception %7B%0A
         byte%5B%5D d
@@ -2378,36 +2378,66 @@
 e64 codec = 
-createCodec(
+new Base64(lineLength, lineSeparator, true
 );%0A        b
@@ -2507,16 +2507,40 @@
 coded);%0A
+        stream.flush();%0A
     %7D%0A%0A 
@@ -2649,362 +2649,51 @@
     
-ByteArrayOutputStream baos = new ByteArrayOutputStream();%0A        IOHelper.copyAndCloseInput(input, baos);%0A        byte%5B%5D encoded = baos.toByteArray();%0A        baos.close();%0A%0A        Base64 codec = createCodec();%0A        byte%5B%5D decoded = codec.decode(encoded);%0A%0A        return decoded;%0A    %7D%0A%0A    private Base64 createCodec() %7B%0A        return new Base64(
+return new Base64InputStream(input, false, 
 line
@@ -2717,17 +2717,8 @@
 ator
-, urlSafe
 );%0A 

Use streaming support in base64. Thanks to Stephan Siano for the patch.