View Javadoc
1   /*
2    * Copyright 2012 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    *   http://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.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   * Special {@link ByteBuf} implementation which is used by the {@link ReplayingDecoder}
36   */
37  final class ReplayingDecoderBuffer 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 ReplayingDecoderBuffer EMPTY_BUFFER = new ReplayingDecoderBuffer(Unpooled.EMPTY_BUFFER);
46  
47      static {
48          EMPTY_BUFFER.terminate();
49      }
50  
51      ReplayingDecoderBuffer() { }
52  
53      ReplayingDecoderBuffer(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         return bytesBefore(buffer.readerIndex(), length, value);
333     }
334 
335     @Override
336     public int bytesBefore(int index, int length, byte value) {
337         final int writerIndex = buffer.writerIndex();
338         if (index >= writerIndex) {
339             throw REPLAY;
340         }
341 
342         if (index <= writerIndex - length) {
343             return buffer.bytesBefore(index, length, value);
344         }
345 
346         int res = buffer.bytesBefore(index, writerIndex - index, value);
347         if (res < 0) {
348             throw REPLAY;
349         } else {
350             return res;
351         }
352     }
353 
354     @Override
355     public int forEachByte(ByteBufProcessor processor) {
356         int ret = buffer.forEachByte(processor);
357         if (ret < 0) {
358             throw REPLAY;
359         } else {
360             return ret;
361         }
362     }
363 
364     @Override
365     public int forEachByte(int index, int length, ByteBufProcessor processor) {
366         final int writerIndex = buffer.writerIndex();
367         if (index >= writerIndex) {
368             throw REPLAY;
369         }
370 
371         if (index <= writerIndex - length) {
372             return buffer.forEachByte(index, length, processor);
373         }
374 
375         int ret = buffer.forEachByte(index, writerIndex - index, processor);
376         if (ret < 0) {
377             throw REPLAY;
378         } else {
379             return ret;
380         }
381     }
382 
383     @Override
384     public int forEachByteDesc(ByteBufProcessor processor) {
385         if (terminated) {
386             return buffer.forEachByteDesc(processor);
387         } else {
388             reject();
389             return 0;
390         }
391     }
392 
393     @Override
394     public int forEachByteDesc(int index, int length, ByteBufProcessor processor) {
395         if (index + length > buffer.writerIndex()) {
396             throw REPLAY;
397         }
398 
399         return buffer.forEachByteDesc(index, length, processor);
400     }
401 
402     @Override
403     public ByteBuf markReaderIndex() {
404         buffer.markReaderIndex();
405         return this;
406     }
407 
408     @Override
409     public ByteBuf markWriterIndex() {
410         reject();
411         return this;
412     }
413 
414     @Override
415     public ByteOrder order() {
416         return buffer.order();
417     }
418 
419     @Override
420     public ByteBuf order(ByteOrder endianness) {
421         if (endianness == null) {
422             throw new NullPointerException("endianness");
423         }
424         if (endianness == order()) {
425             return this;
426         }
427 
428         SwappedByteBuf swapped = this.swapped;
429         if (swapped == null) {
430             this.swapped = swapped = new SwappedByteBuf(this);
431         }
432         return swapped;
433     }
434 
435     @Override
436     public boolean isReadable() {
437         return terminated? buffer.isReadable() : true;
438     }
439 
440     @Override
441     public boolean isReadable(int size) {
442         return terminated? buffer.isReadable(size) : true;
443     }
444 
445     @Override
446     public int readableBytes() {
447         if (terminated) {
448             return buffer.readableBytes();
449         } else {
450             return Integer.MAX_VALUE - buffer.readerIndex();
451         }
452     }
453 
454     @Override
455     public boolean readBoolean() {
456         checkReadableBytes(1);
457         return buffer.readBoolean();
458     }
459 
460     @Override
461     public byte readByte() {
462         checkReadableBytes(1);
463         return buffer.readByte();
464     }
465 
466     @Override
467     public short readUnsignedByte() {
468         checkReadableBytes(1);
469         return buffer.readUnsignedByte();
470     }
471 
472     @Override
473     public ByteBuf readBytes(byte[] dst, int dstIndex, int length) {
474         checkReadableBytes(length);
475         buffer.readBytes(dst, dstIndex, length);
476         return this;
477     }
478 
479     @Override
480     public ByteBuf readBytes(byte[] dst) {
481         checkReadableBytes(dst.length);
482         buffer.readBytes(dst);
483         return this;
484     }
485 
486     @Override
487     public ByteBuf readBytes(ByteBuffer dst) {
488         reject();
489         return this;
490     }
491 
492     @Override
493     public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) {
494         checkReadableBytes(length);
495         buffer.readBytes(dst, dstIndex, length);
496         return this;
497     }
498 
499     @Override
500     public ByteBuf readBytes(ByteBuf dst, int length) {
501         reject();
502         return this;
503     }
504 
505     @Override
506     public ByteBuf readBytes(ByteBuf dst) {
507         checkReadableBytes(dst.writableBytes());
508         buffer.readBytes(dst);
509         return this;
510     }
511 
512     @Override
513     public int readBytes(GatheringByteChannel out, int length) {
514         reject();
515         return 0;
516     }
517 
518     @Override
519     public ByteBuf readBytes(int length) {
520         checkReadableBytes(length);
521         return buffer.readBytes(length);
522     }
523 
524     @Override
525     public ByteBuf readSlice(int length) {
526         checkReadableBytes(length);
527         return buffer.readSlice(length);
528     }
529 
530     @Override
531     public ByteBuf readBytes(OutputStream out, int length) {
532         reject();
533         return this;
534     }
535 
536     @Override
537     public int readerIndex() {
538         return buffer.readerIndex();
539     }
540 
541     @Override
542     public ByteBuf readerIndex(int readerIndex) {
543         buffer.readerIndex(readerIndex);
544         return this;
545     }
546 
547     @Override
548     public int readInt() {
549         checkReadableBytes(4);
550         return buffer.readInt();
551     }
552 
553     @Override
554     public long readUnsignedInt() {
555         checkReadableBytes(4);
556         return buffer.readUnsignedInt();
557     }
558 
559     @Override
560     public long readLong() {
561         checkReadableBytes(8);
562         return buffer.readLong();
563     }
564 
565     @Override
566     public int readMedium() {
567         checkReadableBytes(3);
568         return buffer.readMedium();
569     }
570 
571     @Override
572     public int readUnsignedMedium() {
573         checkReadableBytes(3);
574         return buffer.readUnsignedMedium();
575     }
576 
577     @Override
578     public short readShort() {
579         checkReadableBytes(2);
580         return buffer.readShort();
581     }
582 
583     @Override
584     public int readUnsignedShort() {
585         checkReadableBytes(2);
586         return buffer.readUnsignedShort();
587     }
588 
589     @Override
590     public char readChar() {
591         checkReadableBytes(2);
592         return buffer.readChar();
593     }
594 
595     @Override
596     public float readFloat() {
597         checkReadableBytes(4);
598         return buffer.readFloat();
599     }
600 
601     @Override
602     public double readDouble() {
603         checkReadableBytes(8);
604         return buffer.readDouble();
605     }
606 
607     @Override
608     public ByteBuf resetReaderIndex() {
609         buffer.resetReaderIndex();
610         return this;
611     }
612 
613     @Override
614     public ByteBuf resetWriterIndex() {
615         reject();
616         return this;
617     }
618 
619     @Override
620     public ByteBuf setBoolean(int index, boolean value) {
621         reject();
622         return this;
623     }
624 
625     @Override
626     public ByteBuf setByte(int index, int value) {
627         reject();
628         return this;
629     }
630 
631     @Override
632     public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
633         reject();
634         return this;
635     }
636 
637     @Override
638     public ByteBuf setBytes(int index, byte[] src) {
639         reject();
640         return this;
641     }
642 
643     @Override
644     public ByteBuf setBytes(int index, ByteBuffer src) {
645         reject();
646         return this;
647     }
648 
649     @Override
650     public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
651         reject();
652         return this;
653     }
654 
655     @Override
656     public ByteBuf setBytes(int index, ByteBuf src, int length) {
657         reject();
658         return this;
659     }
660 
661     @Override
662     public ByteBuf setBytes(int index, ByteBuf src) {
663         reject();
664         return this;
665     }
666 
667     @Override
668     public int setBytes(int index, InputStream in, int length) {
669         reject();
670         return 0;
671     }
672 
673     @Override
674     public ByteBuf setZero(int index, int length) {
675         reject();
676         return this;
677     }
678 
679     @Override
680     public int setBytes(int index, ScatteringByteChannel in, int length) {
681         reject();
682         return 0;
683     }
684 
685     @Override
686     public ByteBuf setIndex(int readerIndex, int writerIndex) {
687         reject();
688         return this;
689     }
690 
691     @Override
692     public ByteBuf setInt(int index, int value) {
693         reject();
694         return this;
695     }
696 
697     @Override
698     public ByteBuf setLong(int index, long value) {
699         reject();
700         return this;
701     }
702 
703     @Override
704     public ByteBuf setMedium(int index, int value) {
705         reject();
706         return this;
707     }
708 
709     @Override
710     public ByteBuf setShort(int index, int value) {
711         reject();
712         return this;
713     }
714 
715     @Override
716     public ByteBuf setChar(int index, int value) {
717         reject();
718         return this;
719     }
720 
721     @Override
722     public ByteBuf setFloat(int index, float value) {
723         reject();
724         return this;
725     }
726 
727     @Override
728     public ByteBuf setDouble(int index, double value) {
729         reject();
730         return this;
731     }
732 
733     @Override
734     public ByteBuf skipBytes(int length) {
735         checkReadableBytes(length);
736         buffer.skipBytes(length);
737         return this;
738     }
739 
740     @Override
741     public ByteBuf slice() {
742         reject();
743         return this;
744     }
745 
746     @Override
747     public ByteBuf slice(int index, int length) {
748         checkIndex(index, length);
749         return buffer.slice(index, length);
750     }
751 
752     @Override
753     public int nioBufferCount() {
754         return buffer.nioBufferCount();
755     }
756 
757     @Override
758     public ByteBuffer nioBuffer() {
759         reject();
760         return null;
761     }
762 
763     @Override
764     public ByteBuffer nioBuffer(int index, int length) {
765         checkIndex(index, length);
766         return buffer.nioBuffer(index, length);
767     }
768 
769     @Override
770     public ByteBuffer[] nioBuffers() {
771         reject();
772         return null;
773     }
774 
775     @Override
776     public ByteBuffer[] nioBuffers(int index, int length) {
777         checkIndex(index, length);
778         return buffer.nioBuffers(index, length);
779     }
780 
781     @Override
782     public ByteBuffer internalNioBuffer(int index, int length) {
783         checkIndex(index, length);
784         return buffer.internalNioBuffer(index, length);
785     }
786 
787     @Override
788     public String toString(int index, int length, Charset charset) {
789         checkIndex(index, length);
790         return buffer.toString(index, length, charset);
791     }
792 
793     @Override
794     public String toString(Charset charsetName) {
795         reject();
796         return null;
797     }
798 
799     @Override
800     public String toString() {
801         return StringUtil.simpleClassName(this) + '(' +
802                "ridx=" +
803                readerIndex() +
804                ", " +
805                "widx=" +
806                writerIndex() +
807                ')';
808     }
809 
810     @Override
811     public boolean isWritable() {
812         return false;
813     }
814 
815     @Override
816     public boolean isWritable(int size) {
817         return false;
818     }
819 
820     @Override
821     public int writableBytes() {
822         return 0;
823     }
824 
825     @Override
826     public int maxWritableBytes() {
827         return 0;
828     }
829 
830     @Override
831     public ByteBuf writeBoolean(boolean value) {
832         reject();
833         return this;
834     }
835 
836     @Override
837     public ByteBuf writeByte(int value) {
838         reject();
839         return this;
840     }
841 
842     @Override
843     public ByteBuf writeBytes(byte[] src, int srcIndex, int length) {
844         reject();
845         return this;
846     }
847 
848     @Override
849     public ByteBuf writeBytes(byte[] src) {
850         reject();
851         return this;
852     }
853 
854     @Override
855     public ByteBuf writeBytes(ByteBuffer src) {
856         reject();
857         return this;
858     }
859 
860     @Override
861     public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) {
862         reject();
863         return this;
864     }
865 
866     @Override
867     public ByteBuf writeBytes(ByteBuf src, int length) {
868         reject();
869         return this;
870     }
871 
872     @Override
873     public ByteBuf writeBytes(ByteBuf src) {
874         reject();
875         return this;
876     }
877 
878     @Override
879     public int writeBytes(InputStream in, int length) {
880         reject();
881         return 0;
882     }
883 
884     @Override
885     public int writeBytes(ScatteringByteChannel in, int length) {
886         reject();
887         return 0;
888     }
889 
890     @Override
891     public ByteBuf writeInt(int value) {
892         reject();
893         return this;
894     }
895 
896     @Override
897     public ByteBuf writeLong(long value) {
898         reject();
899         return this;
900     }
901 
902     @Override
903     public ByteBuf writeMedium(int value) {
904         reject();
905         return this;
906     }
907 
908     @Override
909     public ByteBuf writeZero(int length) {
910         reject();
911         return this;
912     }
913 
914     @Override
915     public int writerIndex() {
916         return buffer.writerIndex();
917     }
918 
919     @Override
920     public ByteBuf writerIndex(int writerIndex) {
921         reject();
922         return this;
923     }
924 
925     @Override
926     public ByteBuf writeShort(int value) {
927         reject();
928         return this;
929     }
930 
931     @Override
932     public ByteBuf writeChar(int value) {
933         reject();
934         return this;
935     }
936 
937     @Override
938     public ByteBuf writeFloat(float value) {
939         reject();
940         return this;
941     }
942 
943     @Override
944     public ByteBuf writeDouble(double value) {
945         reject();
946         return this;
947     }
948 
949     private void checkIndex(int index, int length) {
950         if (index + length > buffer.writerIndex()) {
951             throw REPLAY;
952         }
953     }
954 
955     private void checkReadableBytes(int readableBytes) {
956         if (buffer.readableBytes() < readableBytes) {
957             throw REPLAY;
958         }
959     }
960 
961     @Override
962     public ByteBuf discardSomeReadBytes() {
963         reject();
964         return this;
965     }
966 
967     @Override
968     public int refCnt() {
969         return buffer.refCnt();
970     }
971 
972     @Override
973     public ByteBuf retain() {
974         reject();
975         return this;
976     }
977 
978     @Override
979     public ByteBuf retain(int increment) {
980         reject();
981         return this;
982     }
983 
984     @Override
985     public ByteBuf touch() {
986         buffer.touch();
987         return this;
988     }
989 
990     @Override
991     public ByteBuf touch(Object hint) {
992         buffer.touch(hint);
993         return this;
994     }
995 
996     @Override
997     public boolean release() {
998         reject();
999         return false;
1000     }
1001 
1002     @Override
1003     public boolean release(int decrement) {
1004         reject();
1005         return false;
1006     }
1007 
1008     @Override
1009     public ByteBuf unwrap() {
1010         reject();
1011         return this;
1012     }
1013 
1014     private static void reject() {
1015         throw new UnsupportedOperationException("not a replayable operation");
1016     }
1017 }