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 20 import java.io.IOException; 21 import java.io.OutputStream; 22 import java.nio.ByteBuffer; 23 24 import org.apache.commons.crypto.stream.CryptoOutputStream; 25 26 /** 27 * The StreamOutput class takes a {@link OutputStream} object and wraps it 28 * as {@link Output} object acceptable by {@link CryptoOutputStream} 29 * as the output target. 30 */ 31 public class StreamOutput implements Output { 32 private final byte[] buf; 33 private final int bufferSize; 34 private final OutputStream out; 35 36 /** 37 * Constructs a new instance. 38 * 39 * @param out the OutputStream object. 40 * @param bufferSize the buffer size. 41 */ 42 public StreamOutput(final OutputStream out, final int bufferSize) { 43 this.out = out; 44 this.bufferSize = bufferSize; 45 buf = new byte[bufferSize]; 46 } 47 48 /** 49 * Overrides the {@link Output#close()}. Closes this output and releases any 50 * system resources associated with the under layer output. 51 * 52 * @throws IOException if an I/O error occurs. 53 */ 54 @Override 55 public void close() throws IOException { 56 out.close(); 57 } 58 59 /** 60 * Overrides the {@link Output#flush()}. Flushes this output and forces any 61 * buffered output bytes to be written out if the under layer output method 62 * support. 63 * 64 * @throws IOException if an I/O error occurs. 65 */ 66 @Override 67 public void flush() throws IOException { 68 out.flush(); 69 } 70 71 /** 72 * Gets the output stream. 73 * 74 * @return the output stream. 75 */ 76 protected OutputStream getOut() { 77 return out; 78 } 79 80 /** 81 * Overrides the 82 * {@link org.apache.commons.crypto.stream.output.Output#write(ByteBuffer)}. 83 * Writes a sequence of bytes to this output from the given buffer. 84 * 85 * @param src The buffer from which bytes are to be retrieved. 86 * 87 * @return The number of bytes written, possibly zero. 88 * @throws IOException if an I/O error occurs. 89 */ 90 @Override 91 public int write(final ByteBuffer src) throws IOException { 92 final int len = src.remaining(); 93 94 int remaining = len; 95 while (remaining > 0) { 96 final int n = Math.min(remaining, bufferSize); 97 src.get(buf, 0, n); 98 out.write(buf, 0, n); 99 remaining = src.remaining(); 100 } 101 102 return len; 103 } 104 }