ChannelOutput.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.nio.ByteBuffer;
  21. import java.nio.channels.WritableByteChannel;

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

  23. /**
  24.  * The ChannelOutput class takes a {@link WritableByteChannel} object and
  25.  * wraps it as {@code Output} object acceptable by
  26.  * {@link CryptoOutputStream} as the output target.
  27.  */
  28. public class ChannelOutput implements Output {

  29.     private final WritableByteChannel channel;

  30.     /**
  31.      * Constructs a
  32.      * {@link org.apache.commons.crypto.stream.output.ChannelOutput}.
  33.      *
  34.      * @param channel the WritableByteChannel object.
  35.      */
  36.     public ChannelOutput(final WritableByteChannel channel) {
  37.         this.channel = channel;
  38.     }

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

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

  60.     /**
  61.      * Overrides the
  62.      * {@link org.apache.commons.crypto.stream.output.Output#write(ByteBuffer)}.
  63.      * Writes a sequence of bytes to this output from the given buffer.
  64.      *
  65.      * @param src The buffer from which bytes are to be retrieved.
  66.      *
  67.      * @return The number of bytes written, possibly zero.
  68.      * @throws IOException if an I/O error occurs.
  69.      */
  70.     @Override
  71.     public int write(final ByteBuffer src) throws IOException {
  72.         return channel.write(src);
  73.     }
  74. }