001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.core.io.buffer;
018
019import java.io.InputStream;
020import java.io.OutputStream;
021import java.nio.ByteBuffer;
022import java.nio.charset.Charset;
023import java.util.function.IntPredicate;
024
025import org.springframework.util.Assert;
026
027/**
028 * Provides a convenient implementation of the {@link DataBuffer} interface
029 * that can be overridden to adapt the delegate.
030 *
031 * <p>These methods default to calling through to the wrapped delegate object.
032 *
033 * @author Arjen Poutsma
034 * @since 5.2
035 */
036public class DataBufferWrapper implements DataBuffer {
037
038        private final DataBuffer delegate;
039
040
041        /**
042         * Create a new {@code DataBufferWrapper} that wraps the given buffer.
043         * @param delegate the buffer to wrap
044         */
045        public DataBufferWrapper(DataBuffer delegate) {
046                Assert.notNull(delegate, "Delegate must not be null");
047                this.delegate = delegate;
048        }
049
050        /**
051         * Return the wrapped delegate.
052         */
053        public DataBuffer dataBuffer() {
054                return this.delegate;
055        }
056
057        @Override
058        public DataBufferFactory factory() {
059                return this.delegate.factory();
060        }
061
062        @Override
063        public int indexOf(IntPredicate predicate, int fromIndex) {
064                return this.delegate.indexOf(predicate, fromIndex);
065        }
066
067        @Override
068        public int lastIndexOf(IntPredicate predicate, int fromIndex) {
069                return this.delegate.lastIndexOf(predicate, fromIndex);
070        }
071
072        @Override
073        public int readableByteCount() {
074                return this.delegate.readableByteCount();
075        }
076
077        @Override
078        public int writableByteCount() {
079                return this.delegate.writableByteCount();
080        }
081
082        @Override
083        public int capacity() {
084                return this.delegate.capacity();
085        }
086
087        @Override
088        public DataBuffer capacity(int capacity) {
089                return this.delegate.capacity(capacity);
090        }
091
092        @Override
093        public DataBuffer ensureCapacity(int capacity) {
094                return this.delegate.ensureCapacity(capacity);
095        }
096
097        @Override
098        public int readPosition() {
099                return this.delegate.readPosition();
100        }
101
102        @Override
103        public DataBuffer readPosition(int readPosition) {
104                return this.delegate.readPosition(readPosition);
105        }
106
107        @Override
108        public int writePosition() {
109                return this.delegate.writePosition();
110        }
111
112        @Override
113        public DataBuffer writePosition(int writePosition) {
114                return this.delegate.writePosition(writePosition);
115        }
116
117        @Override
118        public byte getByte(int index) {
119                return this.delegate.getByte(index);
120        }
121
122        @Override
123        public byte read() {
124                return this.delegate.read();
125        }
126
127        @Override
128        public DataBuffer read(byte[] destination) {
129                return this.delegate.read(destination);
130        }
131
132        @Override
133        public DataBuffer read(byte[] destination, int offset, int length) {
134                return this.delegate.read(destination, offset, length);
135        }
136
137        @Override
138        public DataBuffer write(byte b) {
139                return this.delegate.write(b);
140        }
141
142        @Override
143        public DataBuffer write(byte[] source) {
144                return this.delegate.write(source);
145        }
146
147        @Override
148        public DataBuffer write(byte[] source, int offset, int length) {
149                return this.delegate.write(source, offset, length);
150        }
151
152        @Override
153        public DataBuffer write(DataBuffer... buffers) {
154                return this.delegate.write(buffers);
155        }
156
157        @Override
158        public DataBuffer write(ByteBuffer... buffers) {
159                return this.delegate.write(buffers);
160        }
161
162        @Override
163        public DataBuffer write(CharSequence charSequence,
164                        Charset charset) {
165                return this.delegate.write(charSequence, charset);
166        }
167
168        @Override
169        public DataBuffer slice(int index, int length) {
170                return this.delegate.slice(index, length);
171        }
172
173        @Override
174        public DataBuffer retainedSlice(int index, int length) {
175                return this.delegate.retainedSlice(index, length);
176        }
177
178        @Override
179        public ByteBuffer asByteBuffer() {
180                return this.delegate.asByteBuffer();
181        }
182
183        @Override
184        public ByteBuffer asByteBuffer(int index, int length) {
185                return this.delegate.asByteBuffer(index, length);
186        }
187
188        @Override
189        public InputStream asInputStream() {
190                return this.delegate.asInputStream();
191        }
192
193        @Override
194        public InputStream asInputStream(boolean releaseOnClose) {
195                return this.delegate.asInputStream(releaseOnClose);
196        }
197
198        @Override
199        public OutputStream asOutputStream() {
200                return this.delegate.asOutputStream();
201        }
202
203        @Override
204        public String toString(Charset charset) {
205                return this.delegate.toString(charset);
206        }
207
208        @Override
209        public String toString(int index, int length, Charset charset) {
210                return this.delegate.toString(index, length, charset);
211        }
212
213}