001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.commons.crypto.stream.output;
019
020import java.io.IOException;
021import java.io.OutputStream;
022import java.nio.ByteBuffer;
023
024import org.apache.commons.crypto.stream.CryptoOutputStream;
025
026/**
027 * The StreamOutput class takes a {@link OutputStream} object and wraps it
028 * as {@link Output} object acceptable by {@link CryptoOutputStream}
029 * as the output target.
030 */
031public class StreamOutput implements Output {
032    private final byte[] buf;
033    private final int bufferSize;
034    private final OutputStream out;
035
036    /**
037     * Constructs a new instance.
038     *
039     * @param out the OutputStream object.
040     * @param bufferSize the buffer size.
041     */
042    public StreamOutput(final OutputStream out, final int bufferSize) {
043        this.out = out;
044        this.bufferSize = bufferSize;
045        buf = new byte[bufferSize];
046    }
047
048    /**
049     * Overrides the {@link Output#close()}. Closes this output and releases any
050     * system resources associated with the under layer output.
051     *
052     * @throws IOException if an I/O error occurs.
053     */
054    @Override
055    public void close() throws IOException {
056        out.close();
057    }
058
059    /**
060     * Overrides the {@link Output#flush()}. Flushes this output and forces any
061     * buffered output bytes to be written out if the under layer output method
062     * support.
063     *
064     * @throws IOException if an I/O error occurs.
065     */
066    @Override
067    public void flush() throws IOException {
068        out.flush();
069    }
070
071    /**
072     * Gets the output stream.
073     *
074     * @return the output stream.
075     */
076    protected OutputStream getOut() {
077        return out;
078    }
079
080    /**
081     * Overrides the
082     * {@link org.apache.commons.crypto.stream.output.Output#write(ByteBuffer)}.
083     * Writes a sequence of bytes to this output from the given buffer.
084     *
085     * @param src The buffer from which bytes are to be retrieved.
086     *
087     * @return The number of bytes written, possibly zero.
088     * @throws IOException if an I/O error occurs.
089     */
090    @Override
091    public int write(final ByteBuffer src) throws IOException {
092        final int len = src.remaining();
093
094        int remaining = len;
095        while (remaining > 0) {
096            final int n = Math.min(remaining, bufferSize);
097            src.get(buf, 0, n);
098            out.write(buf, 0, n);
099            remaining = src.remaining();
100        }
101
102        return len;
103    }
104}