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