View Javadoc
1   /*
2    * Copyright 2021 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.buffer.api;
17  
18  import io.netty5.buffer.api.ComponentIterator.Next;
19  import io.netty5.util.Send;
20  
21  import java.io.IOException;
22  import java.nio.ByteBuffer;
23  import java.nio.channels.FileChannel;
24  import java.nio.channels.ReadableByteChannel;
25  import java.nio.channels.WritableByteChannel;
26  import java.nio.charset.Charset;
27  
28  /**
29   * A stub of a {@link Buffer} implementation that implements all buffer methods by delegating them to a wrapped buffer
30   * instance.
31   * <p>
32   * This can be used when writing automated tests for code that integrates with {@link Buffer}, but should not be used in
33   * production code.
34   */
35  public class BufferStub implements Buffer {
36      protected final Buffer delegate;
37  
38      /**
39       * Create a new buffer stub that delegates all calls to the given instance.
40       *
41       * @param delegate The buffer instance to delegate all method calls to.
42       */
43      public BufferStub(Buffer delegate) {
44          this.delegate = delegate;
45      }
46  
47      @Override
48      public int capacity() {
49          return delegate.capacity();
50      }
51  
52      @Override
53      public int readerOffset() {
54          return delegate.readerOffset();
55      }
56  
57      @Override
58      public Buffer skipReadableBytes(int delta) {
59          delegate.skipReadableBytes(delta);
60          return this;
61      }
62  
63      @Override
64      public Buffer readerOffset(int offset) {
65          delegate.readerOffset(offset);
66          return this;
67      }
68  
69      @Override
70      public int writerOffset() {
71          return delegate.writerOffset();
72      }
73  
74      @Override
75      public Buffer skipWritableBytes(int delta) {
76          delegate.skipWritableBytes(delta);
77          return this;
78      }
79  
80      @Override
81      public Buffer writerOffset(int offset) {
82          delegate.writerOffset(offset);
83          return this;
84      }
85  
86      @Override
87      public int readableBytes() {
88          return delegate.readableBytes();
89      }
90  
91      @Override
92      public int writableBytes() {
93          return delegate.writableBytes();
94      }
95  
96      @Override
97      public Buffer fill(byte value) {
98          delegate.fill(value);
99          return this;
100     }
101 
102     @Override
103     public Buffer makeReadOnly() {
104         delegate.makeReadOnly();
105         return this;
106     }
107 
108     @Override
109     public boolean readOnly() {
110         return delegate.readOnly();
111     }
112 
113     @Override
114     public boolean isDirect() {
115         return delegate.isDirect();
116     }
117 
118     @Override
119     public Buffer implicitCapacityLimit(int limit) {
120         delegate.implicitCapacityLimit(limit);
121         return this;
122     }
123 
124     @Override
125     public int implicitCapacityLimit() {
126         return delegate.implicitCapacityLimit();
127     }
128 
129     @Override
130     public void copyInto(int srcPos, byte[] dest, int destPos, int length) {
131         delegate.copyInto(srcPos, dest, destPos, length);
132     }
133 
134     @Override
135     public void copyInto(int srcPos, ByteBuffer dest, int destPos, int length) {
136         delegate.copyInto(srcPos, dest, destPos, length);
137     }
138 
139     @Override
140     public void copyInto(int srcPos, Buffer dest, int destPos, int length) {
141         delegate.copyInto(srcPos, dest, destPos, length);
142     }
143 
144     @Override
145     public int transferTo(WritableByteChannel channel, int length) throws IOException {
146         return delegate.transferTo(channel, length);
147     }
148 
149     @Override
150     public int transferFrom(FileChannel channel, long position, int length) throws IOException {
151         return delegate.transferFrom(channel, position, length);
152     }
153 
154     @Override
155     public int transferFrom(ReadableByteChannel channel, int length) throws IOException {
156         return delegate.transferFrom(channel, length);
157     }
158 
159     @Override
160     public Buffer writeBytes(Buffer source) {
161         delegate.writeBytes(source);
162         return this;
163     }
164 
165     @Override
166     public Buffer writeBytes(byte[] source) {
167         delegate.writeBytes(source);
168         return this;
169     }
170 
171     @Override
172     public Buffer resetOffsets() {
173         delegate.resetOffsets();
174         return this;
175     }
176 
177     @Override
178     public int bytesBefore(byte needle) {
179         return delegate.bytesBefore(needle);
180     }
181 
182     @Override
183     public int bytesBefore(Buffer needle) {
184         return delegate.bytesBefore(needle);
185     }
186 
187     @Override
188     public ByteCursor openCursor() {
189         return delegate.openCursor();
190     }
191 
192     @Override
193     public ByteCursor openCursor(int fromOffset, int length) {
194         return delegate.openCursor(fromOffset, length);
195     }
196 
197     @Override
198     public ByteCursor openReverseCursor() {
199         return delegate.openReverseCursor();
200     }
201 
202     @Override
203     public ByteCursor openReverseCursor(int fromOffset, int length) {
204         return delegate.openReverseCursor(fromOffset, length);
205     }
206 
207     @Override
208     public Buffer ensureWritable(int size) {
209         delegate.ensureWritable(size);
210         return this;
211     }
212 
213     @Override
214     public Buffer ensureWritable(int size, int minimumGrowth, boolean allowCompaction) {
215         delegate.ensureWritable(size, minimumGrowth, allowCompaction);
216         return this;
217     }
218 
219     @Override
220     public Buffer copy() {
221         return delegate.copy();
222     }
223 
224     @Override
225     public Buffer copy(int offset, int length, boolean readOnly) {
226         return delegate.copy(offset, length, readOnly);
227     }
228 
229     @Override
230     public Buffer split() {
231         return delegate.split();
232     }
233 
234     @Override
235     public Buffer split(int splitOffset) {
236         return delegate.split(splitOffset);
237     }
238 
239     @Override
240     public Buffer compact() {
241         delegate.compact();
242         return this;
243     }
244 
245     @Override
246     public int countComponents() {
247         return delegate.countComponents();
248     }
249 
250     @Override
251     public int countReadableComponents() {
252         return delegate.countReadableComponents();
253     }
254 
255     @Override
256     public int countWritableComponents() {
257         return delegate.countWritableComponents();
258     }
259 
260     @Override
261     public <E extends Exception> int forEachReadable(int initialIndex,
262                                                      ReadableComponentProcessor<E> processor) throws E {
263         return delegate.forEachReadable(initialIndex, processor);
264     }
265 
266     @Override
267     public <T extends ReadableComponent & Next> ComponentIterator<T> forEachReadable() {
268         return delegate.forEachReadable();
269     }
270 
271     @Override
272     public <E extends Exception> int forEachWritable(int initialIndex,
273                                                      WritableComponentProcessor<E> processor) throws E {
274         return delegate.forEachWritable(initialIndex, processor);
275     }
276 
277     @Override
278     public <T extends WritableComponent & Next> ComponentIterator<T> forEachWritable() {
279         return delegate.forEachWritable();
280     }
281 
282     @Override
283     public byte readByte() {
284         return delegate.readByte();
285     }
286 
287     @Override
288     public byte getByte(int roff) {
289         return delegate.getByte(roff);
290     }
291 
292     @Override
293     public int readUnsignedByte() {
294         return delegate.readUnsignedByte();
295     }
296 
297     @Override
298     public int getUnsignedByte(int roff) {
299         return delegate.getUnsignedByte(roff);
300     }
301 
302     @Override
303     public Buffer writeByte(byte value) {
304         delegate.writeByte(value);
305         return this;
306     }
307 
308     @Override
309     public Buffer setByte(int woff, byte value) {
310         delegate.setByte(woff, value);
311         return this;
312     }
313 
314     @Override
315     public Buffer writeUnsignedByte(int value) {
316         delegate.writeUnsignedByte(value);
317         return this;
318     }
319 
320     @Override
321     public Buffer setUnsignedByte(int woff, int value) {
322         delegate.setUnsignedByte(woff, value);
323         return this;
324     }
325 
326     @Override
327     public char readChar() {
328         return delegate.readChar();
329     }
330 
331     @Override
332     public char getChar(int roff) {
333         return delegate.getChar(roff);
334     }
335 
336     @Override
337     public Buffer writeChar(char value) {
338         delegate.writeChar(value);
339         return this;
340     }
341 
342     @Override
343     public Buffer setChar(int woff, char value) {
344         delegate.setChar(woff, value);
345         return this;
346     }
347 
348     @Override
349     public short readShort() {
350         return delegate.readShort();
351     }
352 
353     @Override
354     public short getShort(int roff) {
355         return delegate.getShort(roff);
356     }
357 
358     @Override
359     public int readUnsignedShort() {
360         return delegate.readUnsignedShort();
361     }
362 
363     @Override
364     public int getUnsignedShort(int roff) {
365         return delegate.getUnsignedShort(roff);
366     }
367 
368     @Override
369     public Buffer writeShort(short value) {
370         delegate.writeShort(value);
371         return this;
372     }
373 
374     @Override
375     public Buffer setShort(int woff, short value) {
376         delegate.setShort(woff, value);
377         return this;
378     }
379 
380     @Override
381     public Buffer writeUnsignedShort(int value) {
382         delegate.writeUnsignedShort(value);
383         return this;
384     }
385 
386     @Override
387     public Buffer setUnsignedShort(int woff, int value) {
388         delegate.setUnsignedShort(woff, value);
389         return this;
390     }
391 
392     @Override
393     public int readMedium() {
394         return delegate.readMedium();
395     }
396 
397     @Override
398     public int getMedium(int roff) {
399         return delegate.getMedium(roff);
400     }
401 
402     @Override
403     public int readUnsignedMedium() {
404         return delegate.readUnsignedMedium();
405     }
406 
407     @Override
408     public int getUnsignedMedium(int roff) {
409         return delegate.getUnsignedMedium(roff);
410     }
411 
412     @Override
413     public Buffer writeMedium(int value) {
414         delegate.writeMedium(value);
415         return this;
416     }
417 
418     @Override
419     public Buffer setMedium(int woff, int value) {
420         delegate.setMedium(woff, value);
421         return this;
422     }
423 
424     @Override
425     public Buffer writeUnsignedMedium(int value) {
426         delegate.writeUnsignedMedium(value);
427         return this;
428     }
429 
430     @Override
431     public Buffer setUnsignedMedium(int woff, int value) {
432         delegate.setUnsignedMedium(woff, value);
433         return this;
434     }
435 
436     @Override
437     public int readInt() {
438         return delegate.readInt();
439     }
440 
441     @Override
442     public int getInt(int roff) {
443         return delegate.getInt(roff);
444     }
445 
446     @Override
447     public long readUnsignedInt() {
448         return delegate.readUnsignedInt();
449     }
450 
451     @Override
452     public long getUnsignedInt(int roff) {
453         return delegate.getUnsignedInt(roff);
454     }
455 
456     @Override
457     public Buffer writeInt(int value) {
458         delegate.writeInt(value);
459         return this;
460     }
461 
462     @Override
463     public Buffer setInt(int woff, int value) {
464         delegate.setInt(woff, value);
465         return this;
466     }
467 
468     @Override
469     public Buffer writeUnsignedInt(long value) {
470         delegate.writeUnsignedInt(value);
471         return this;
472     }
473 
474     @Override
475     public Buffer setUnsignedInt(int woff, long value) {
476         delegate.setUnsignedInt(woff, value);
477         return this;
478     }
479 
480     @Override
481     public float readFloat() {
482         return delegate.readFloat();
483     }
484 
485     @Override
486     public float getFloat(int roff) {
487         return delegate.getFloat(roff);
488     }
489 
490     @Override
491     public Buffer writeFloat(float value) {
492         delegate.writeFloat(value);
493         return this;
494     }
495 
496     @Override
497     public Buffer setFloat(int woff, float value) {
498         delegate.setFloat(woff, value);
499         return this;
500     }
501 
502     @Override
503     public long readLong() {
504         return delegate.readLong();
505     }
506 
507     @Override
508     public long getLong(int roff) {
509         return delegate.getLong(roff);
510     }
511 
512     @Override
513     public Buffer writeLong(long value) {
514         delegate.writeLong(value);
515         return this;
516     }
517 
518     @Override
519     public Buffer setLong(int woff, long value) {
520         delegate.setLong(woff, value);
521         return this;
522     }
523 
524     @Override
525     public double readDouble() {
526         return delegate.readDouble();
527     }
528 
529     @Override
530     public double getDouble(int roff) {
531         return delegate.getDouble(roff);
532     }
533 
534     @Override
535     public Buffer writeDouble(double value) {
536         delegate.writeDouble(value);
537         return this;
538     }
539 
540     @Override
541     public Buffer setDouble(int woff, double value) {
542         delegate.setDouble(woff, value);
543         return this;
544     }
545 
546     @Override
547     public Buffer writeCharSequence(CharSequence source, Charset charset) {
548         delegate.writeCharSequence(source, charset);
549         return this;
550     }
551 
552     @Override
553     public CharSequence readCharSequence(int length, Charset charset) {
554         return delegate.readCharSequence(length, charset);
555     }
556 
557     @Override
558     public Buffer writeBytes(byte[] source, int srcPos, int length) {
559         delegate.writeBytes(source, srcPos, length);
560         return this;
561     }
562 
563     @Override
564     public Buffer readBytes(byte[] destination, int destPos, int length) {
565         delegate.readBytes(destination, destPos, length);
566         return this;
567     }
568 
569     @Override
570     public Buffer copy(int offset, int length) {
571         return delegate.copy(offset, length);
572     }
573 
574     @Override
575     public Buffer readSplit(int length) {
576         return delegate.readSplit(length);
577     }
578 
579     @Override
580     public Buffer writeSplit(int length) {
581         return delegate.writeSplit(length);
582     }
583 
584     @Override
585     public boolean readBoolean() {
586         return delegate.readBoolean();
587     }
588 
589     @Override
590     public boolean getBoolean(int roff) {
591         return delegate.getBoolean(roff);
592     }
593 
594     @Override
595     public Buffer writeBoolean(boolean value) {
596        delegate.writeBoolean(value);
597        return this;
598     }
599 
600     @Override
601     public Buffer setBoolean(int woff, boolean value) {
602        delegate.setBoolean(woff, value);
603        return this;
604     }
605 
606     @Override
607     public Buffer writeBytes(ByteBuffer source) {
608         delegate.writeBytes(source);
609         return this;
610     }
611 
612     @Override
613     public Buffer readBytes(ByteBuffer destination) {
614         delegate.readBytes(destination);
615         return this;
616     }
617 
618     @Override
619     public Buffer copy(boolean readOnly) {
620         return delegate.copy(readOnly);
621     }
622 
623     @Override
624     public Send<Buffer> send() {
625         return delegate.send();
626     }
627 
628     @Override
629     public void close() {
630         delegate.close();
631     }
632 
633     @Override
634     public boolean isAccessible() {
635         return delegate.isAccessible();
636     }
637 
638     @Override
639     public Buffer touch(Object hint) {
640         delegate.touch(hint);
641         return this;
642     }
643 
644     @Override
645     public String toString(Charset charset) {
646         return delegate.toString(charset);
647     }
648 
649     @Override
650     public int hashCode() {
651         return delegate.hashCode();
652     }
653 
654     @Override
655     public boolean equals(Object obj) {
656         return obj instanceof Buffer && delegate.equals(obj);
657     }
658 }