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