1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.ByteBufAllocator;
20 import io.netty.buffer.ByteBufProcessor;
21 import io.netty.buffer.SwappedByteBuf;
22 import io.netty.buffer.Unpooled;
23 import io.netty.util.Signal;
24 import io.netty.util.internal.StringUtil;
25
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.nio.ByteBuffer;
29 import java.nio.ByteOrder;
30 import java.nio.channels.GatheringByteChannel;
31 import java.nio.channels.ScatteringByteChannel;
32 import java.nio.charset.Charset;
33
34
35
36
37 final class ReplayingDecoderByteBuf extends ByteBuf {
38
39 private static final Signal REPLAY = ReplayingDecoder.REPLAY;
40
41 private ByteBuf buffer;
42 private boolean terminated;
43 private SwappedByteBuf swapped;
44
45 static final ReplayingDecoderByteBuf EMPTY_BUFFER = new ReplayingDecoderByteBuf(Unpooled.EMPTY_BUFFER);
46
47 static {
48 EMPTY_BUFFER.terminate();
49 }
50
51 ReplayingDecoderByteBuf() { }
52
53 ReplayingDecoderByteBuf(ByteBuf buffer) {
54 setCumulation(buffer);
55 }
56
57 void setCumulation(ByteBuf buffer) {
58 this.buffer = buffer;
59 }
60
61 void terminate() {
62 terminated = true;
63 }
64
65 @Override
66 public int capacity() {
67 if (terminated) {
68 return buffer.capacity();
69 } else {
70 return Integer.MAX_VALUE;
71 }
72 }
73
74 @Override
75 public ByteBuf capacity(int newCapacity) {
76 reject();
77 return this;
78 }
79
80 @Override
81 public int maxCapacity() {
82 return capacity();
83 }
84
85 @Override
86 public ByteBufAllocator alloc() {
87 return buffer.alloc();
88 }
89
90 @Override
91 public boolean isDirect() {
92 return buffer.isDirect();
93 }
94
95 @Override
96 public boolean hasArray() {
97 return false;
98 }
99
100 @Override
101 public byte[] array() {
102 throw new UnsupportedOperationException();
103 }
104
105 @Override
106 public int arrayOffset() {
107 throw new UnsupportedOperationException();
108 }
109
110 @Override
111 public boolean hasMemoryAddress() {
112 return false;
113 }
114
115 @Override
116 public long memoryAddress() {
117 throw new UnsupportedOperationException();
118 }
119
120 @Override
121 public ByteBuf clear() {
122 reject();
123 return this;
124 }
125
126 @Override
127 public boolean equals(Object obj) {
128 return this == obj;
129 }
130
131 @Override
132 public int compareTo(ByteBuf buffer) {
133 reject();
134 return 0;
135 }
136
137 @Override
138 public ByteBuf copy() {
139 reject();
140 return this;
141 }
142
143 @Override
144 public ByteBuf copy(int index, int length) {
145 checkIndex(index, length);
146 return buffer.copy(index, length);
147 }
148
149 @Override
150 public ByteBuf discardReadBytes() {
151 reject();
152 return this;
153 }
154
155 @Override
156 public ByteBuf ensureWritable(int writableBytes) {
157 reject();
158 return this;
159 }
160
161 @Override
162 public int ensureWritable(int minWritableBytes, boolean force) {
163 reject();
164 return 0;
165 }
166
167 @Override
168 public ByteBuf duplicate() {
169 reject();
170 return this;
171 }
172
173 @Override
174 public boolean getBoolean(int index) {
175 checkIndex(index, 1);
176 return buffer.getBoolean(index);
177 }
178
179 @Override
180 public byte getByte(int index) {
181 checkIndex(index, 1);
182 return buffer.getByte(index);
183 }
184
185 @Override
186 public short getUnsignedByte(int index) {
187 checkIndex(index, 1);
188 return buffer.getUnsignedByte(index);
189 }
190
191 @Override
192 public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
193 checkIndex(index, length);
194 buffer.getBytes(index, dst, dstIndex, length);
195 return this;
196 }
197
198 @Override
199 public ByteBuf getBytes(int index, byte[] dst) {
200 checkIndex(index, dst.length);
201 buffer.getBytes(index, dst);
202 return this;
203 }
204
205 @Override
206 public ByteBuf getBytes(int index, ByteBuffer dst) {
207 reject();
208 return this;
209 }
210
211 @Override
212 public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
213 checkIndex(index, length);
214 buffer.getBytes(index, dst, dstIndex, length);
215 return this;
216 }
217
218 @Override
219 public ByteBuf getBytes(int index, ByteBuf dst, int length) {
220 reject();
221 return this;
222 }
223
224 @Override
225 public ByteBuf getBytes(int index, ByteBuf dst) {
226 reject();
227 return this;
228 }
229
230 @Override
231 public int getBytes(int index, GatheringByteChannel out, int length) {
232 reject();
233 return 0;
234 }
235
236 @Override
237 public ByteBuf getBytes(int index, OutputStream out, int length) {
238 reject();
239 return this;
240 }
241
242 @Override
243 public int getInt(int index) {
244 checkIndex(index, 4);
245 return buffer.getInt(index);
246 }
247
248 @Override
249 public long getUnsignedInt(int index) {
250 checkIndex(index, 4);
251 return buffer.getUnsignedInt(index);
252 }
253
254 @Override
255 public long getLong(int index) {
256 checkIndex(index, 8);
257 return buffer.getLong(index);
258 }
259
260 @Override
261 public int getMedium(int index) {
262 checkIndex(index, 3);
263 return buffer.getMedium(index);
264 }
265
266 @Override
267 public int getUnsignedMedium(int index) {
268 checkIndex(index, 3);
269 return buffer.getUnsignedMedium(index);
270 }
271
272 @Override
273 public short getShort(int index) {
274 checkIndex(index, 2);
275 return buffer.getShort(index);
276 }
277
278 @Override
279 public int getUnsignedShort(int index) {
280 checkIndex(index, 2);
281 return buffer.getUnsignedShort(index);
282 }
283
284 @Override
285 public char getChar(int index) {
286 checkIndex(index, 2);
287 return buffer.getChar(index);
288 }
289
290 @Override
291 public float getFloat(int index) {
292 checkIndex(index, 4);
293 return buffer.getFloat(index);
294 }
295
296 @Override
297 public double getDouble(int index) {
298 checkIndex(index, 8);
299 return buffer.getDouble(index);
300 }
301
302 @Override
303 public int hashCode() {
304 reject();
305 return 0;
306 }
307
308 @Override
309 public int indexOf(int fromIndex, int toIndex, byte value) {
310 if (fromIndex == toIndex) {
311 return -1;
312 }
313
314 if (Math.max(fromIndex, toIndex) > buffer.writerIndex()) {
315 throw REPLAY;
316 }
317
318 return buffer.indexOf(fromIndex, toIndex, value);
319 }
320
321 @Override
322 public int bytesBefore(byte value) {
323 int bytes = buffer.bytesBefore(value);
324 if (bytes < 0) {
325 throw REPLAY;
326 }
327 return bytes;
328 }
329
330 @Override
331 public int bytesBefore(int length, byte value) {
332 final int readerIndex = buffer.readerIndex();
333 return bytesBefore(readerIndex, buffer.writerIndex() - readerIndex, value);
334 }
335
336 @Override
337 public int bytesBefore(int index, int length, byte value) {
338 final int writerIndex = buffer.writerIndex();
339 if (index >= writerIndex) {
340 throw REPLAY;
341 }
342
343 if (index <= writerIndex - length) {
344 return buffer.bytesBefore(index, length, value);
345 }
346
347 int res = buffer.bytesBefore(index, writerIndex - index, value);
348 if (res < 0) {
349 throw REPLAY;
350 } else {
351 return res;
352 }
353 }
354
355 @Override
356 public int forEachByte(ByteBufProcessor processor) {
357 int ret = buffer.forEachByte(processor);
358 if (ret < 0) {
359 throw REPLAY;
360 } else {
361 return ret;
362 }
363 }
364
365 @Override
366 public int forEachByte(int index, int length, ByteBufProcessor processor) {
367 final int writerIndex = buffer.writerIndex();
368 if (index >= writerIndex) {
369 throw REPLAY;
370 }
371
372 if (index <= writerIndex - length) {
373 return buffer.forEachByte(index, length, processor);
374 }
375
376 int ret = buffer.forEachByte(index, writerIndex - index, processor);
377 if (ret < 0) {
378 throw REPLAY;
379 } else {
380 return ret;
381 }
382 }
383
384 @Override
385 public int forEachByteDesc(ByteBufProcessor processor) {
386 if (terminated) {
387 return buffer.forEachByteDesc(processor);
388 } else {
389 reject();
390 return 0;
391 }
392 }
393
394 @Override
395 public int forEachByteDesc(int index, int length, ByteBufProcessor processor) {
396 if (index + length > buffer.writerIndex()) {
397 throw REPLAY;
398 }
399
400 return buffer.forEachByteDesc(index, length, processor);
401 }
402
403 @Override
404 public ByteBuf markReaderIndex() {
405 buffer.markReaderIndex();
406 return this;
407 }
408
409 @Override
410 public ByteBuf markWriterIndex() {
411 reject();
412 return this;
413 }
414
415 @Override
416 public ByteOrder order() {
417 return buffer.order();
418 }
419
420 @Override
421 public ByteBuf order(ByteOrder endianness) {
422 if (endianness == null) {
423 throw new NullPointerException("endianness");
424 }
425 if (endianness == order()) {
426 return this;
427 }
428
429 SwappedByteBuf swapped = this.swapped;
430 if (swapped == null) {
431 this.swapped = swapped = new SwappedByteBuf(this);
432 }
433 return swapped;
434 }
435
436 @Override
437 public boolean isReadable() {
438 return terminated? buffer.isReadable() : true;
439 }
440
441 @Override
442 public boolean isReadable(int size) {
443 return terminated? buffer.isReadable(size) : true;
444 }
445
446 @Override
447 public int readableBytes() {
448 if (terminated) {
449 return buffer.readableBytes();
450 } else {
451 return Integer.MAX_VALUE - buffer.readerIndex();
452 }
453 }
454
455 @Override
456 public boolean readBoolean() {
457 checkReadableBytes(1);
458 return buffer.readBoolean();
459 }
460
461 @Override
462 public byte readByte() {
463 checkReadableBytes(1);
464 return buffer.readByte();
465 }
466
467 @Override
468 public short readUnsignedByte() {
469 checkReadableBytes(1);
470 return buffer.readUnsignedByte();
471 }
472
473 @Override
474 public ByteBuf readBytes(byte[] dst, int dstIndex, int length) {
475 checkReadableBytes(length);
476 buffer.readBytes(dst, dstIndex, length);
477 return this;
478 }
479
480 @Override
481 public ByteBuf readBytes(byte[] dst) {
482 checkReadableBytes(dst.length);
483 buffer.readBytes(dst);
484 return this;
485 }
486
487 @Override
488 public ByteBuf readBytes(ByteBuffer dst) {
489 reject();
490 return this;
491 }
492
493 @Override
494 public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) {
495 checkReadableBytes(length);
496 buffer.readBytes(dst, dstIndex, length);
497 return this;
498 }
499
500 @Override
501 public ByteBuf readBytes(ByteBuf dst, int length) {
502 reject();
503 return this;
504 }
505
506 @Override
507 public ByteBuf readBytes(ByteBuf dst) {
508 checkReadableBytes(dst.writableBytes());
509 buffer.readBytes(dst);
510 return this;
511 }
512
513 @Override
514 public int readBytes(GatheringByteChannel out, int length) {
515 reject();
516 return 0;
517 }
518
519 @Override
520 public ByteBuf readBytes(int length) {
521 checkReadableBytes(length);
522 return buffer.readBytes(length);
523 }
524
525 @Override
526 public ByteBuf readSlice(int length) {
527 checkReadableBytes(length);
528 return buffer.readSlice(length);
529 }
530
531 @Override
532 public ByteBuf readBytes(OutputStream out, int length) {
533 reject();
534 return this;
535 }
536
537 @Override
538 public int readerIndex() {
539 return buffer.readerIndex();
540 }
541
542 @Override
543 public ByteBuf readerIndex(int readerIndex) {
544 buffer.readerIndex(readerIndex);
545 return this;
546 }
547
548 @Override
549 public int readInt() {
550 checkReadableBytes(4);
551 return buffer.readInt();
552 }
553
554 @Override
555 public long readUnsignedInt() {
556 checkReadableBytes(4);
557 return buffer.readUnsignedInt();
558 }
559
560 @Override
561 public long readLong() {
562 checkReadableBytes(8);
563 return buffer.readLong();
564 }
565
566 @Override
567 public int readMedium() {
568 checkReadableBytes(3);
569 return buffer.readMedium();
570 }
571
572 @Override
573 public int readUnsignedMedium() {
574 checkReadableBytes(3);
575 return buffer.readUnsignedMedium();
576 }
577
578 @Override
579 public short readShort() {
580 checkReadableBytes(2);
581 return buffer.readShort();
582 }
583
584 @Override
585 public int readUnsignedShort() {
586 checkReadableBytes(2);
587 return buffer.readUnsignedShort();
588 }
589
590 @Override
591 public char readChar() {
592 checkReadableBytes(2);
593 return buffer.readChar();
594 }
595
596 @Override
597 public float readFloat() {
598 checkReadableBytes(4);
599 return buffer.readFloat();
600 }
601
602 @Override
603 public double readDouble() {
604 checkReadableBytes(8);
605 return buffer.readDouble();
606 }
607
608 @Override
609 public ByteBuf resetReaderIndex() {
610 buffer.resetReaderIndex();
611 return this;
612 }
613
614 @Override
615 public ByteBuf resetWriterIndex() {
616 reject();
617 return this;
618 }
619
620 @Override
621 public ByteBuf setBoolean(int index, boolean value) {
622 reject();
623 return this;
624 }
625
626 @Override
627 public ByteBuf setByte(int index, int value) {
628 reject();
629 return this;
630 }
631
632 @Override
633 public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
634 reject();
635 return this;
636 }
637
638 @Override
639 public ByteBuf setBytes(int index, byte[] src) {
640 reject();
641 return this;
642 }
643
644 @Override
645 public ByteBuf setBytes(int index, ByteBuffer src) {
646 reject();
647 return this;
648 }
649
650 @Override
651 public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
652 reject();
653 return this;
654 }
655
656 @Override
657 public ByteBuf setBytes(int index, ByteBuf src, int length) {
658 reject();
659 return this;
660 }
661
662 @Override
663 public ByteBuf setBytes(int index, ByteBuf src) {
664 reject();
665 return this;
666 }
667
668 @Override
669 public int setBytes(int index, InputStream in, int length) {
670 reject();
671 return 0;
672 }
673
674 @Override
675 public ByteBuf setZero(int index, int length) {
676 reject();
677 return this;
678 }
679
680 @Override
681 public int setBytes(int index, ScatteringByteChannel in, int length) {
682 reject();
683 return 0;
684 }
685
686 @Override
687 public ByteBuf setIndex(int readerIndex, int writerIndex) {
688 reject();
689 return this;
690 }
691
692 @Override
693 public ByteBuf setInt(int index, int value) {
694 reject();
695 return this;
696 }
697
698 @Override
699 public ByteBuf setLong(int index, long value) {
700 reject();
701 return this;
702 }
703
704 @Override
705 public ByteBuf setMedium(int index, int value) {
706 reject();
707 return this;
708 }
709
710 @Override
711 public ByteBuf setShort(int index, int value) {
712 reject();
713 return this;
714 }
715
716 @Override
717 public ByteBuf setChar(int index, int value) {
718 reject();
719 return this;
720 }
721
722 @Override
723 public ByteBuf setFloat(int index, float value) {
724 reject();
725 return this;
726 }
727
728 @Override
729 public ByteBuf setDouble(int index, double value) {
730 reject();
731 return this;
732 }
733
734 @Override
735 public ByteBuf skipBytes(int length) {
736 checkReadableBytes(length);
737 buffer.skipBytes(length);
738 return this;
739 }
740
741 @Override
742 public ByteBuf slice() {
743 reject();
744 return this;
745 }
746
747 @Override
748 public ByteBuf slice(int index, int length) {
749 checkIndex(index, length);
750 return buffer.slice(index, length);
751 }
752
753 @Override
754 public int nioBufferCount() {
755 return buffer.nioBufferCount();
756 }
757
758 @Override
759 public ByteBuffer nioBuffer() {
760 reject();
761 return null;
762 }
763
764 @Override
765 public ByteBuffer nioBuffer(int index, int length) {
766 checkIndex(index, length);
767 return buffer.nioBuffer(index, length);
768 }
769
770 @Override
771 public ByteBuffer[] nioBuffers() {
772 reject();
773 return null;
774 }
775
776 @Override
777 public ByteBuffer[] nioBuffers(int index, int length) {
778 checkIndex(index, length);
779 return buffer.nioBuffers(index, length);
780 }
781
782 @Override
783 public ByteBuffer internalNioBuffer(int index, int length) {
784 checkIndex(index, length);
785 return buffer.internalNioBuffer(index, length);
786 }
787
788 @Override
789 public String toString(int index, int length, Charset charset) {
790 checkIndex(index, length);
791 return buffer.toString(index, length, charset);
792 }
793
794 @Override
795 public String toString(Charset charsetName) {
796 reject();
797 return null;
798 }
799
800 @Override
801 public String toString() {
802 return StringUtil.simpleClassName(this) + '(' +
803 "ridx=" +
804 readerIndex() +
805 ", " +
806 "widx=" +
807 writerIndex() +
808 ')';
809 }
810
811 @Override
812 public boolean isWritable() {
813 return false;
814 }
815
816 @Override
817 public boolean isWritable(int size) {
818 return false;
819 }
820
821 @Override
822 public int writableBytes() {
823 return 0;
824 }
825
826 @Override
827 public int maxWritableBytes() {
828 return 0;
829 }
830
831 @Override
832 public ByteBuf writeBoolean(boolean value) {
833 reject();
834 return this;
835 }
836
837 @Override
838 public ByteBuf writeByte(int value) {
839 reject();
840 return this;
841 }
842
843 @Override
844 public ByteBuf writeBytes(byte[] src, int srcIndex, int length) {
845 reject();
846 return this;
847 }
848
849 @Override
850 public ByteBuf writeBytes(byte[] src) {
851 reject();
852 return this;
853 }
854
855 @Override
856 public ByteBuf writeBytes(ByteBuffer src) {
857 reject();
858 return this;
859 }
860
861 @Override
862 public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) {
863 reject();
864 return this;
865 }
866
867 @Override
868 public ByteBuf writeBytes(ByteBuf src, int length) {
869 reject();
870 return this;
871 }
872
873 @Override
874 public ByteBuf writeBytes(ByteBuf src) {
875 reject();
876 return this;
877 }
878
879 @Override
880 public int writeBytes(InputStream in, int length) {
881 reject();
882 return 0;
883 }
884
885 @Override
886 public int writeBytes(ScatteringByteChannel in, int length) {
887 reject();
888 return 0;
889 }
890
891 @Override
892 public ByteBuf writeInt(int value) {
893 reject();
894 return this;
895 }
896
897 @Override
898 public ByteBuf writeLong(long value) {
899 reject();
900 return this;
901 }
902
903 @Override
904 public ByteBuf writeMedium(int value) {
905 reject();
906 return this;
907 }
908
909 @Override
910 public ByteBuf writeZero(int length) {
911 reject();
912 return this;
913 }
914
915 @Override
916 public int writerIndex() {
917 return buffer.writerIndex();
918 }
919
920 @Override
921 public ByteBuf writerIndex(int writerIndex) {
922 reject();
923 return this;
924 }
925
926 @Override
927 public ByteBuf writeShort(int value) {
928 reject();
929 return this;
930 }
931
932 @Override
933 public ByteBuf writeChar(int value) {
934 reject();
935 return this;
936 }
937
938 @Override
939 public ByteBuf writeFloat(float value) {
940 reject();
941 return this;
942 }
943
944 @Override
945 public ByteBuf writeDouble(double value) {
946 reject();
947 return this;
948 }
949
950 private void checkIndex(int index, int length) {
951 if (index + length > buffer.writerIndex()) {
952 throw REPLAY;
953 }
954 }
955
956 private void checkReadableBytes(int readableBytes) {
957 if (buffer.readableBytes() < readableBytes) {
958 throw REPLAY;
959 }
960 }
961
962 @Override
963 public ByteBuf discardSomeReadBytes() {
964 reject();
965 return this;
966 }
967
968 @Override
969 public int refCnt() {
970 return buffer.refCnt();
971 }
972
973 @Override
974 public ByteBuf retain() {
975 reject();
976 return this;
977 }
978
979 @Override
980 public ByteBuf retain(int increment) {
981 reject();
982 return this;
983 }
984
985 @Override
986 public boolean release() {
987 reject();
988 return false;
989 }
990
991 @Override
992 public boolean release(int decrement) {
993 reject();
994 return false;
995 }
996
997 @Override
998 public ByteBuf unwrap() {
999 reject();
1000 return this;
1001 }
1002
1003 private static void reject() {
1004 throw new UnsupportedOperationException("not a replayable operation");
1005 }
1006 }