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.nio.ByteBuffer;
22 import java.nio.channels.WritableByteChannel;
23
24 import org.apache.commons.crypto.stream.CryptoOutputStream;
25
26 /**
27 * The ChannelOutput class takes a {@link WritableByteChannel} object and
28 * wraps it as {@code Output} object acceptable by
29 * {@link CryptoOutputStream} as the output target.
30 */
31 public class ChannelOutput implements Output {
32
33 private final WritableByteChannel channel;
34
35 /**
36 * Constructs a
37 * {@link org.apache.commons.crypto.stream.output.ChannelOutput}.
38 *
39 * @param channel the WritableByteChannel object.
40 */
41 public ChannelOutput(final WritableByteChannel channel) {
42 this.channel = channel;
43 }
44
45 /**
46 * Overrides the {@link Output#close()}. Closes this output and releases any
47 * system resources associated with the under layer output.
48 *
49 * @throws IOException if an I/O error occurs.
50 */
51 @Override
52 public void close() throws IOException {
53 channel.close();
54 }
55
56 /**
57 * Overrides the {@link Output#flush()}. Flushes this output and forces any
58 * buffered output bytes to be written out if the under layer output method
59 * support.
60 *
61 * @throws IOException if an I/O error occurs.
62 */
63 @Override
64 public void flush() throws IOException {
65 // noop
66 }
67
68 /**
69 * Overrides the
70 * {@link org.apache.commons.crypto.stream.output.Output#write(ByteBuffer)}.
71 * Writes a sequence of bytes to this output from the given buffer.
72 *
73 * @param src The buffer from which bytes are to be retrieved.
74 *
75 * @return The number of bytes written, possibly zero.
76 * @throws IOException if an I/O error occurs.
77 */
78 @Override
79 public int write(final ByteBuffer src) throws IOException {
80 return channel.write(src);
81 }
82 }