001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.converter.stream;
018
019import java.io.ByteArrayOutputStream;
020import java.io.IOException;
021import java.io.OutputStream;
022
023import org.apache.camel.Exchange;
024import org.apache.camel.util.ExchangeHelper;
025
026/**
027 * Utility to hide the complexity of choosing which OutputStream
028 * implementation to choose.
029 * <p/>
030 * Itself masquerades as an OutputStream, but really delegates to a
031 * CachedOutputStream of a ByteArrayOutputStream.
032 */
033public final class OutputStreamBuilder extends OutputStream {
034
035    private final OutputStream outputStream;
036
037    private OutputStreamBuilder(final Exchange exchange) {
038        if (ExchangeHelper.isStreamCachingEnabled(exchange)) {
039            outputStream = new CachedOutputStream(exchange);
040        } else {
041            outputStream = new ByteArrayOutputStream();
042        }
043    }
044
045    /**
046     * Creates a new OutputStreamBuilder with the current exchange
047     * <p/>
048     * Use the {@link #build()} when writing to the stream is finished,
049     * and you need the result of this operation.
050     *
051     * @param exchange the current Exchange
052     * @return the builder
053     */
054    public static OutputStreamBuilder withExchange(final Exchange exchange) {
055        return new OutputStreamBuilder(exchange);
056    }
057
058    @Override
059    public void write(final byte[] b, final int off, final int len) throws IOException {
060        outputStream.write(b, off, len);
061    }
062
063    @Override
064    public void write(final byte[] b) throws IOException {
065        outputStream.write(b);
066    }
067
068    @Override
069    public void write(final int b) throws IOException {
070        outputStream.write(b);
071    }
072
073    @Override
074    public void flush() throws IOException {
075        outputStream.flush();
076    }
077
078    @Override
079    public void close() throws IOException {
080        outputStream.close();
081    }
082
083    /**
084     * Builds the result of using this builder as either a
085     * {@link org.apache.camel.converter.stream.CachedOutputStream} if stream caching is enabled,
086     * otherwise byte[].
087     */
088    public Object build() throws IOException {
089        if (outputStream instanceof CachedOutputStream) {
090            return ((CachedOutputStream)outputStream).newStreamCache();
091        }
092        return ((ByteArrayOutputStream)outputStream).toByteArray();
093    }
094}