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.nio.ByteBuffer; 022import java.nio.channels.WritableByteChannel; 023 024import org.apache.commons.crypto.stream.CryptoOutputStream; 025 026/** 027 * The ChannelOutput class takes a {@link WritableByteChannel} object and 028 * wraps it as {@code Output} object acceptable by 029 * {@link CryptoOutputStream} as the output target. 030 */ 031public class ChannelOutput implements Output { 032 033 private final WritableByteChannel channel; 034 035 /** 036 * Constructs a 037 * {@link org.apache.commons.crypto.stream.output.ChannelOutput}. 038 * 039 * @param channel the WritableByteChannel object. 040 */ 041 public ChannelOutput(final WritableByteChannel channel) { 042 this.channel = channel; 043 } 044 045 /** 046 * Overrides the {@link Output#close()}. Closes this output and releases any 047 * system resources associated with the under layer output. 048 * 049 * @throws IOException if an I/O error occurs. 050 */ 051 @Override 052 public void close() throws IOException { 053 channel.close(); 054 } 055 056 /** 057 * Overrides the {@link Output#flush()}. Flushes this output and forces any 058 * buffered output bytes to be written out if the under layer output method 059 * support. 060 * 061 * @throws IOException if an I/O error occurs. 062 */ 063 @Override 064 public void flush() throws IOException { 065 // noop 066 } 067 068 /** 069 * Overrides the 070 * {@link org.apache.commons.crypto.stream.output.Output#write(ByteBuffer)}. 071 * Writes a sequence of bytes to this output from the given buffer. 072 * 073 * @param src The buffer from which bytes are to be retrieved. 074 * 075 * @return The number of bytes written, possibly zero. 076 * @throws IOException if an I/O error occurs. 077 */ 078 @Override 079 public int write(final ByteBuffer src) throws IOException { 080 return channel.write(src); 081 } 082}