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.ByteChannel; 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 ByteChannel} filter which delegates to the wrapped {@link ByteChannel}. 034 * <p> 035 * A {@code FilterByteChannel} wraps some other channel, which it uses as its basic source of data, possibly transforming the data along the way or providing 036 * additional functionality. The class {@code FilterByteChannel} itself simply overrides methods of {@code ByteChannel} with versions that pass all requests to 037 * the wrapped channel. Subclasses of {@code FilterByteChannel} may of course override any methods declared or inherited by {@code FilterByteChannel}, and may 038 * also provide additional fields and methods. 039 * </p> 040 * <p> 041 * You construct s simple instance with the {@link FilterByteChannel#FilterByteChannel(ByteChannel) channel constructor} and more advanced instances through the 042 * {@link Builder}. 043 * </p> 044 * 045 * @param <C> the {@link ByteChannel} 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 FilterByteChannel<C extends ByteChannel> extends FilterChannel<C> implements ByteChannel { 057 058 /** 059 * Builds instances of {@link FilterByteChannel} for subclasses. 060 * 061 * @param <F> The {@link FilterByteChannel} type. 062 * @param <C> The {@link ByteChannel} type wrapped by the FilterChannel. 063 * @param <B> The builder type. 064 */ 065 public abstract static class AbstractBuilder<F extends FilterByteChannel<C>, C extends ByteChannel, B extends AbstractBuilder<F, C, B>> 066 extends FilterChannel.AbstractBuilder<F, C, B> { 067 068 /** 069 * Constructs a new builder for {@link FilterByteChannel}. 070 */ 071 protected AbstractBuilder() { 072 // empty 073 } 074 } 075 076 /** 077 * Builds instances of {@link FilterByteChannel}. 078 */ 079 public static class Builder extends AbstractBuilder<FilterByteChannel<ByteChannel>, ByteChannel, Builder> { 080 081 /** 082 * Builds instances of {@link FilterByteChannel}. 083 */ 084 protected Builder() { 085 // empty 086 } 087 088 @Override 089 public FilterByteChannel<ByteChannel> get() throws IOException { 090 return new FilterByteChannel<>(this); 091 } 092 } 093 094 /** 095 * Creates a new {@link Builder}. 096 * 097 * @return a new {@link Builder}. 098 */ 099 public static Builder forByteChannel() { 100 return new Builder(); 101 } 102 103 FilterByteChannel(final AbstractBuilder<?, ?, ?> builder) throws IOException { 104 super(builder); 105 } 106 107 /** 108 * Constructs a new instance. 109 * 110 * @param byteChannel The channel to wrap. 111 */ 112 public FilterByteChannel(final C byteChannel) { 113 super(byteChannel); 114 } 115 116 @Override 117 public int read(final ByteBuffer dst) throws IOException { 118 return channel.read(dst); 119 } 120 121 @Override 122 public int write(final ByteBuffer src) throws IOException { 123 return channel.write(src); 124 } 125}