查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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 org.jboss.netty.handler.codec.replay;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  import java.nio.ByteBuffer;
22  import java.nio.ByteOrder;
23  import java.nio.channels.GatheringByteChannel;
24  import java.nio.channels.ScatteringByteChannel;
25  import java.nio.charset.Charset;
26  
27  import org.jboss.netty.buffer.ChannelBuffer;
28  import org.jboss.netty.buffer.ChannelBufferFactory;
29  import org.jboss.netty.buffer.ChannelBufferIndexFinder;
30  
31  class ReplayingDecoderBuffer implements ChannelBuffer {
32  
33      private static final Error REPLAY = new ReplayError();
34  
35      private final ReplayingDecoder<?> parent;
36      private boolean terminated;
37  
38      ReplayingDecoderBuffer(ReplayingDecoder<?> parent) {
39          this.parent = parent;
40      }
41  
42      private ChannelBuffer buf() {
43          return parent.internalBuffer();
44      }
45  
46      void terminate() {
47          terminated = true;
48      }
49  
50      public int capacity() {
51          if (terminated) {
52              return buf().capacity();
53          } else {
54              return Integer.MAX_VALUE;
55          }
56      }
57  
58      public boolean isDirect() {
59          return buf().isDirect();
60      }
61  
62      public boolean hasArray() {
63          return false;
64      }
65  
66      public byte[] array() {
67          throw new UnsupportedOperationException();
68      }
69  
70      public int arrayOffset() {
71          throw new UnsupportedOperationException();
72      }
73  
74      public void clear() {
75          throw new UnreplayableOperationException();
76      }
77  
78      @Override
79      public boolean equals(Object obj) {
80          return this == obj;
81      }
82  
83      public int compareTo(ChannelBuffer buffer) {
84          throw new UnreplayableOperationException();
85      }
86  
87      public ChannelBuffer copy() {
88          throw new UnreplayableOperationException();
89      }
90  
91      public ChannelBuffer copy(int index, int length) {
92          checkIndex(index, length);
93          return buf().copy(index, length);
94      }
95  
96      public void discardReadBytes() {
97          throw new UnreplayableOperationException();
98      }
99  
100     public void ensureWritableBytes(int writableBytes) {
101         throw new UnreplayableOperationException();
102     }
103 
104     public ChannelBuffer duplicate() {
105         throw new UnreplayableOperationException();
106     }
107 
108     public byte getByte(int index) {
109         checkIndex(index, 1);
110         return buf().getByte(index);
111     }
112 
113     public short getUnsignedByte(int index) {
114         checkIndex(index, 1);
115         return buf().getUnsignedByte(index);
116     }
117 
118     public void getBytes(int index, byte[] dst, int dstIndex, int length) {
119         checkIndex(index, length);
120         buf().getBytes(index, dst, dstIndex, length);
121     }
122 
123     public void getBytes(int index, byte[] dst) {
124         checkIndex(index, dst.length);
125         buf().getBytes(index, dst);
126     }
127 
128     public void getBytes(int index, ByteBuffer dst) {
129         throw new UnreplayableOperationException();
130     }
131 
132     public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
133         checkIndex(index, length);
134         buf().getBytes(index, dst, dstIndex, length);
135     }
136 
137     public void getBytes(int index, ChannelBuffer dst, int length) {
138         throw new UnreplayableOperationException();
139     }
140 
141     public void getBytes(int index, ChannelBuffer dst) {
142         throw new UnreplayableOperationException();
143     }
144 
145     public int getBytes(int index, GatheringByteChannel out, int length)
146             throws IOException {
147         throw new UnreplayableOperationException();
148     }
149 
150     public void getBytes(int index, OutputStream out, int length)
151             throws IOException {
152         throw new UnreplayableOperationException();
153     }
154 
155     public int getInt(int index) {
156         checkIndex(index, 4);
157         return buf().getInt(index);
158     }
159 
160     public long getUnsignedInt(int index) {
161         checkIndex(index, 4);
162         return buf().getUnsignedInt(index);
163     }
164 
165     public long getLong(int index) {
166         checkIndex(index, 8);
167         return buf().getLong(index);
168     }
169 
170     public int getMedium(int index) {
171         checkIndex(index, 3);
172         return buf().getMedium(index);
173     }
174 
175     public int getUnsignedMedium(int index) {
176         checkIndex(index, 3);
177         return buf().getUnsignedMedium(index);
178     }
179 
180     public short getShort(int index) {
181         checkIndex(index, 2);
182         return buf().getShort(index);
183     }
184 
185     public int getUnsignedShort(int index) {
186         checkIndex(index, 2);
187         return buf().getUnsignedShort(index);
188     }
189 
190     public char getChar(int index) {
191         checkIndex(index, 2);
192         return buf().getChar(index);
193     }
194 
195     public float getFloat(int index) {
196         checkIndex(index, 4);
197         return buf().getFloat(index);
198     }
199 
200     public double getDouble(int index) {
201         checkIndex(index, 8);
202         return buf().getDouble(index);
203     }
204 
205     @Override
206     public int hashCode() {
207         throw new UnreplayableOperationException();
208     }
209 
210     public int indexOf(int fromIndex, int toIndex, byte value) {
211         int endIndex = buf().indexOf(fromIndex, toIndex, value);
212         if (endIndex < 0) {
213             throw REPLAY;
214         }
215         return endIndex;
216     }
217 
218     public int indexOf(int fromIndex, int toIndex,
219             ChannelBufferIndexFinder indexFinder) {
220         int endIndex = buf().indexOf(fromIndex, toIndex, indexFinder);
221         if (endIndex < 0) {
222             throw REPLAY;
223         }
224         return endIndex;
225     }
226 
227     public int bytesBefore(byte value) {
228         int bytes = buf().bytesBefore(value);
229         if (bytes < 0) {
230             throw REPLAY;
231         }
232         return bytes;
233     }
234 
235     public int bytesBefore(ChannelBufferIndexFinder indexFinder) {
236         int bytes = buf().bytesBefore(indexFinder);
237         if (bytes < 0) {
238             throw REPLAY;
239         }
240         return bytes;
241     }
242 
243     public int bytesBefore(int length, byte value) {
244         checkReadableBytes(length);
245         int bytes = buf().bytesBefore(length, value);
246         if (bytes < 0) {
247             throw REPLAY;
248         }
249         return bytes;
250     }
251 
252     public int bytesBefore(int length, ChannelBufferIndexFinder indexFinder) {
253         checkReadableBytes(length);
254         int bytes = buf().bytesBefore(length, indexFinder);
255         if (bytes < 0) {
256             throw REPLAY;
257         }
258         return bytes;
259     }
260 
261     public int bytesBefore(int index, int length, byte value) {
262         int bytes = buf().bytesBefore(index, length, value);
263         if (bytes < 0) {
264             throw REPLAY;
265         }
266         return bytes;
267     }
268 
269     public int bytesBefore(int index, int length,
270             ChannelBufferIndexFinder indexFinder) {
271         int bytes = buf().bytesBefore(index, length, indexFinder);
272         if (bytes < 0) {
273             throw REPLAY;
274         }
275         return bytes;
276     }
277 
278     public void markReaderIndex() {
279         buf().markReaderIndex();
280     }
281 
282     public void markWriterIndex() {
283         throw new UnreplayableOperationException();
284     }
285 
286     public ChannelBufferFactory factory() {
287         return buf().factory();
288     }
289 
290     public ByteOrder order() {
291         return buf().order();
292     }
293 
294     public boolean readable() {
295         return terminated? buf().readable() : true;
296     }
297 
298     public int readableBytes() {
299         if (terminated) {
300             return buf().readableBytes();
301         } else {
302             return Integer.MAX_VALUE - buf().readerIndex();
303         }
304     }
305 
306     public byte readByte() {
307         checkReadableBytes(1);
308         return buf().readByte();
309     }
310 
311     public short readUnsignedByte() {
312         checkReadableBytes(1);
313         return buf().readUnsignedByte();
314     }
315 
316     public void readBytes(byte[] dst, int dstIndex, int length) {
317         checkReadableBytes(length);
318         buf().readBytes(dst, dstIndex, length);
319     }
320 
321     public void readBytes(byte[] dst) {
322         checkReadableBytes(dst.length);
323         buf().readBytes(dst);
324     }
325 
326     public void readBytes(ByteBuffer dst) {
327         throw new UnreplayableOperationException();
328     }
329 
330     public void readBytes(ChannelBuffer dst, int dstIndex, int length) {
331         checkReadableBytes(length);
332         buf().readBytes(dst, dstIndex, length);
333     }
334 
335     public void readBytes(ChannelBuffer dst, int length) {
336         throw new UnreplayableOperationException();
337     }
338 
339     public void readBytes(ChannelBuffer dst) {
340         throw new UnreplayableOperationException();
341     }
342 
343     public int readBytes(GatheringByteChannel out, int length)
344             throws IOException {
345         throw new UnreplayableOperationException();
346     }
347 
348     public ChannelBuffer readBytes(int length) {
349         checkReadableBytes(length);
350         return buf().readBytes(length);
351     }
352 
353     public ChannelBuffer readSlice(int length) {
354         checkReadableBytes(length);
355         return buf().readSlice(length);
356     }
357 
358     public void readBytes(OutputStream out, int length) throws IOException {
359         throw new UnreplayableOperationException();
360     }
361 
362     public int readerIndex() {
363         return buf().readerIndex();
364     }
365 
366     public void readerIndex(int readerIndex) {
367         buf().readerIndex(readerIndex);
368     }
369 
370     public int readInt() {
371         checkReadableBytes(4);
372         return buf().readInt();
373     }
374 
375     public long readUnsignedInt() {
376         checkReadableBytes(4);
377         return buf().readUnsignedInt();
378     }
379 
380     public long readLong() {
381         checkReadableBytes(8);
382         return buf().readLong();
383     }
384 
385     public int readMedium() {
386         checkReadableBytes(3);
387         return buf().readMedium();
388     }
389 
390     public int readUnsignedMedium() {
391         checkReadableBytes(3);
392         return buf().readUnsignedMedium();
393     }
394 
395     public short readShort() {
396         checkReadableBytes(2);
397         return buf().readShort();
398     }
399 
400     public int readUnsignedShort() {
401         checkReadableBytes(2);
402         return buf().readUnsignedShort();
403     }
404 
405     public char readChar() {
406         checkReadableBytes(2);
407         return buf().readChar();
408     }
409 
410     public float readFloat() {
411         checkReadableBytes(4);
412         return buf().readFloat();
413     }
414 
415     public double readDouble() {
416         checkReadableBytes(8);
417         return buf().readDouble();
418     }
419 
420     public void resetReaderIndex() {
421         buf().resetReaderIndex();
422     }
423 
424     public void resetWriterIndex() {
425         throw new UnreplayableOperationException();
426     }
427 
428     public void setByte(int index, int value) {
429         throw new UnreplayableOperationException();
430     }
431 
432     public void setBytes(int index, byte[] src, int srcIndex, int length) {
433         throw new UnreplayableOperationException();
434     }
435 
436     public void setBytes(int index, byte[] src) {
437         throw new UnreplayableOperationException();
438     }
439 
440     public void setBytes(int index, ByteBuffer src) {
441         throw new UnreplayableOperationException();
442     }
443 
444     public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
445         throw new UnreplayableOperationException();
446     }
447 
448     public void setBytes(int index, ChannelBuffer src, int length) {
449         throw new UnreplayableOperationException();
450     }
451 
452     public void setBytes(int index, ChannelBuffer src) {
453         throw new UnreplayableOperationException();
454     }
455 
456     public int setBytes(int index, InputStream in, int length)
457             throws IOException {
458         throw new UnreplayableOperationException();
459     }
460 
461     public void setZero(int index, int length) {
462         throw new UnreplayableOperationException();
463     }
464 
465     public int setBytes(int index, ScatteringByteChannel in, int length)
466             throws IOException {
467         throw new UnreplayableOperationException();
468     }
469 
470     public void setIndex(int readerIndex, int writerIndex) {
471         throw new UnreplayableOperationException();
472     }
473 
474     public void setInt(int index, int value) {
475         throw new UnreplayableOperationException();
476     }
477 
478     public void setLong(int index, long value) {
479         throw new UnreplayableOperationException();
480     }
481 
482     public void setMedium(int index, int value) {
483         throw new UnreplayableOperationException();
484     }
485 
486     public void setShort(int index, int value) {
487         throw new UnreplayableOperationException();
488     }
489 
490     public void setChar(int index, int value) {
491         throw new UnreplayableOperationException();
492     }
493 
494     public void setFloat(int index, float value) {
495         throw new UnreplayableOperationException();
496     }
497 
498     public void setDouble(int index, double value) {
499         throw new UnreplayableOperationException();
500     }
501 
502     public void skipBytes(int length) {
503         checkReadableBytes(length);
504         buf().skipBytes(length);
505     }
506 
507     public ChannelBuffer slice() {
508         throw new UnreplayableOperationException();
509     }
510 
511     public ChannelBuffer slice(int index, int length) {
512         checkIndex(index, length);
513         return buf().slice(index, length);
514     }
515 
516     public ByteBuffer toByteBuffer() {
517         throw new UnreplayableOperationException();
518     }
519 
520     public ByteBuffer toByteBuffer(int index, int length) {
521         checkIndex(index, length);
522         return buf().toByteBuffer(index, length);
523     }
524 
525     public ByteBuffer[] toByteBuffers() {
526         throw new UnreplayableOperationException();
527     }
528 
529     public ByteBuffer[] toByteBuffers(int index, int length) {
530         checkIndex(index, length);
531         return buf().toByteBuffers(index, length);
532     }
533 
534     public String toString(int index, int length, Charset charset) {
535         checkIndex(index, length);
536         return buf().toString(index, length, charset);
537     }
538 
539     public String toString(Charset charsetName) {
540         throw new UnreplayableOperationException();
541     }
542 
543     @Override
544     public String toString() {
545         return getClass().getSimpleName() + '(' +
546                "ridx=" +
547                readerIndex() +
548                ", " +
549                "widx=" +
550                writerIndex() +
551                ')';
552     }
553 
554     public boolean writable() {
555         return false;
556     }
557 
558     public int writableBytes() {
559         return 0;
560     }
561 
562     public void writeByte(int value) {
563         throw new UnreplayableOperationException();
564     }
565 
566     public void writeBytes(byte[] src, int srcIndex, int length) {
567         throw new UnreplayableOperationException();
568     }
569 
570     public void writeBytes(byte[] src) {
571         throw new UnreplayableOperationException();
572     }
573 
574     public void writeBytes(ByteBuffer src) {
575         throw new UnreplayableOperationException();
576     }
577 
578     public void writeBytes(ChannelBuffer src, int srcIndex, int length) {
579         throw new UnreplayableOperationException();
580     }
581 
582     public void writeBytes(ChannelBuffer src, int length) {
583         throw new UnreplayableOperationException();
584     }
585 
586     public void writeBytes(ChannelBuffer src) {
587         throw new UnreplayableOperationException();
588     }
589 
590     public int writeBytes(InputStream in, int length) throws IOException {
591         throw new UnreplayableOperationException();
592     }
593 
594     public int writeBytes(ScatteringByteChannel in, int length)
595             throws IOException {
596         throw new UnreplayableOperationException();
597     }
598 
599     public void writeInt(int value) {
600         throw new UnreplayableOperationException();
601     }
602 
603     public void writeLong(long value) {
604         throw new UnreplayableOperationException();
605     }
606 
607     public void writeMedium(int value) {
608         throw new UnreplayableOperationException();
609     }
610 
611     public void writeZero(int length) {
612         throw new UnreplayableOperationException();
613     }
614 
615     public int writerIndex() {
616         return buf().writerIndex();
617     }
618 
619     public void writerIndex(int writerIndex) {
620         throw new UnreplayableOperationException();
621     }
622 
623     public void writeShort(int value) {
624         throw new UnreplayableOperationException();
625     }
626 
627     public void writeChar(int value) {
628         throw new UnreplayableOperationException();
629     }
630 
631     public void writeFloat(float value) {
632         throw new UnreplayableOperationException();
633     }
634 
635     public void writeDouble(double value) {
636         throw new UnreplayableOperationException();
637     }
638 
639     private void checkIndex(int index, int length) {
640         if (index + length > buf().writerIndex()) {
641             throw REPLAY;
642         }
643     }
644 
645     private void checkReadableBytes(int readableBytes) {
646         if (buf().readableBytes() < readableBytes) {
647             throw REPLAY;
648         }
649     }
650 }