001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.io.channels;
019
020import java.io.FilterInputStream;
021import java.io.FilterOutputStream;
022import java.io.FilterReader;
023import java.io.IOException;
024import java.nio.ByteBuffer;
025import java.nio.channels.WritableByteChannel;
026
027import org.apache.commons.io.input.ProxyInputStream;
028import org.apache.commons.io.input.ProxyReader;
029import org.apache.commons.io.output.ProxyOutputStream;
030import org.apache.commons.io.output.ProxyWriter;
031
032/**
033 * A {@link WritableByteChannel} filter which delegates to the wrapped {@link WritableByteChannel}.
034 * <p>
035 * A {@code FilterWritableByteChannel} wraps some other channel, which it uses as its basic source of data, possibly transforming the data along the way or
036 * providing additional functionality. The class {@code FilterWritableByteChannel} itself simply overrides methods of {@code WritableByteChannel} with versions
037 * that pass all requests to the wrapped channel. Subclasses of {@code FilterWritableByteChannel} may of course override any methods declared or inherited by
038 * {@code WritableByteChannel}, and may also provide additional fields and methods.
039 * </p>
040 * <p>
041 * You construct s simple instance with the {@link FilterWritableByteChannel#FilterWritableByteChannel(WritableByteChannel) Channel constructor} and more
042 * advanced instances through the {@link Builder}.
043 * </p>
044 *
045 * @param <C> the {@link WritableByteChannel} type.
046 * @see FilterInputStream
047 * @see FilterOutputStream
048 * @see FilterReader
049 * @see FilterWritableByteChannel
050 * @see ProxyInputStream
051 * @see ProxyOutputStream
052 * @see ProxyReader
053 * @see ProxyWriter
054 * @since 2.22.0
055 */
056public class FilterWritableByteChannel<C extends WritableByteChannel> extends FilterChannel<C> implements WritableByteChannel {
057
058    /**
059     * Builds instances of {@link FilterWritableByteChannel} for subclasses.
060     *
061     * @param <F> The {@link FilterWritableByteChannel} type.
062     * @param <C> The {@link WritableByteChannel} type wrapped by the FilterChannel.
063     * @param <B> The builder type.
064     */
065    public abstract static class AbstractBuilder<F extends FilterWritableByteChannel<C>, C extends WritableByteChannel, B extends AbstractBuilder<F, C, B>>
066            extends FilterChannel.AbstractBuilder<F, C, B> {
067
068        /**
069         * Constructs a new builder for {@link FilterWritableByteChannel}.
070         */
071        public AbstractBuilder() {
072            // empty
073        }
074    }
075
076    /**
077     * Builds instances of {@link FilterByteChannel}.
078     */
079    public static class Builder extends AbstractBuilder<FilterWritableByteChannel<WritableByteChannel>, WritableByteChannel, Builder> {
080
081        /**
082         * Builds instances of {@link FilterByteChannel}.
083         */
084        protected Builder() {
085            // empty
086        }
087
088        @Override
089        public FilterWritableByteChannel<WritableByteChannel> get() throws IOException {
090            return new FilterWritableByteChannel<>(this);
091        }
092    }
093
094    /**
095     * Creates a new {@link Builder}.
096     *
097     * @return a new {@link Builder}.
098     */
099    public static Builder forWritableByteChannel() {
100        return new Builder();
101    }
102
103    FilterWritableByteChannel(final Builder builder) throws IOException {
104        super(builder);
105    }
106
107    /**
108     * Constructs a new instance.
109     *
110     * @param channel The channel to wrap.
111     */
112    public FilterWritableByteChannel(final C channel) {
113        super(channel);
114    }
115
116    @Override
117    public int write(final ByteBuffer src) throws IOException {
118        return channel.write(src);
119    }
120}