StreamOutput.java

  1.  /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *     http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  */
  18. package org.apache.commons.crypto.stream.output;

  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.nio.ByteBuffer;

  22. import org.apache.commons.crypto.stream.CryptoOutputStream;

  23. /**
  24.  * The StreamOutput class takes a {@link OutputStream} object and wraps it
  25.  * as {@link Output} object acceptable by {@link CryptoOutputStream}
  26.  * as the output target.
  27.  */
  28. public class StreamOutput implements Output {
  29.     private final byte[] buf;
  30.     private final int bufferSize;
  31.     private final OutputStream out;

  32.     /**
  33.      * Constructs a new instance.
  34.      *
  35.      * @param out the OutputStream object.
  36.      * @param bufferSize the buffer size.
  37.      */
  38.     public StreamOutput(final OutputStream out, final int bufferSize) {
  39.         this.out = out;
  40.         this.bufferSize = bufferSize;
  41.         buf = new byte[bufferSize];
  42.     }

  43.     /**
  44.      * Overrides the {@link Output#close()}. Closes this output and releases any
  45.      * system resources associated with the under layer output.
  46.      *
  47.      * @throws IOException if an I/O error occurs.
  48.      */
  49.     @Override
  50.     public void close() throws IOException {
  51.         out.close();
  52.     }

  53.     /**
  54.      * Overrides the {@link Output#flush()}. Flushes this output and forces any
  55.      * buffered output bytes to be written out if the under layer output method
  56.      * support.
  57.      *
  58.      * @throws IOException if an I/O error occurs.
  59.      */
  60.     @Override
  61.     public void flush() throws IOException {
  62.         out.flush();
  63.     }

  64.     /**
  65.      * Gets the output stream.
  66.      *
  67.      * @return the output stream.
  68.      */
  69.     protected OutputStream getOut() {
  70.         return out;
  71.     }

  72.     /**
  73.      * Overrides the
  74.      * {@link org.apache.commons.crypto.stream.output.Output#write(ByteBuffer)}.
  75.      * Writes a sequence of bytes to this output from the given buffer.
  76.      *
  77.      * @param src The buffer from which bytes are to be retrieved.
  78.      *
  79.      * @return The number of bytes written, possibly zero.
  80.      * @throws IOException if an I/O error occurs.
  81.      */
  82.     @Override
  83.     public int write(final ByteBuffer src) throws IOException {
  84.         final int len = src.remaining();

  85.         int remaining = len;
  86.         while (remaining > 0) {
  87.             final int n = Math.min(remaining, bufferSize);
  88.             src.get(buf, 0, n);
  89.             out.write(buf, 0, n);
  90.             remaining = src.remaining();
  91.         }

  92.         return len;
  93.     }
  94. }