1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 @Deprecated
344 public ChannelBuffer readBytes(ChannelBufferIndexFinder endIndexFinder) {
345 int endIndex = buf().indexOf(buf().readerIndex(), buf().writerIndex(), endIndexFinder);
346 if (endIndex < 0) {
347 throw REPLAY;
348 }
349 return buf().readBytes(endIndex - buf().readerIndex());
350 }
351
352 public int readBytes(GatheringByteChannel out, int length)
353 throws IOException {
354 throw new UnreplayableOperationException();
355 }
356
357 public ChannelBuffer readBytes(int length) {
358 checkReadableBytes(length);
359 return buf().readBytes(length);
360 }
361
362 @Deprecated
363 public ChannelBuffer readSlice(
364 ChannelBufferIndexFinder endIndexFinder) {
365 int endIndex = buf().indexOf(buf().readerIndex(), buf().writerIndex(), endIndexFinder);
366 if (endIndex < 0) {
367 throw REPLAY;
368 }
369 return buf().readSlice(endIndex - buf().readerIndex());
370 }
371
372 public ChannelBuffer readSlice(int length) {
373 checkReadableBytes(length);
374 return buf().readSlice(length);
375 }
376
377 public void readBytes(OutputStream out, int length) throws IOException {
378 throw new UnreplayableOperationException();
379 }
380
381 public int readerIndex() {
382 return buf().readerIndex();
383 }
384
385 public void readerIndex(int readerIndex) {
386 buf().readerIndex(readerIndex);
387 }
388
389 public int readInt() {
390 checkReadableBytes(4);
391 return buf().readInt();
392 }
393
394 public long readUnsignedInt() {
395 checkReadableBytes(4);
396 return buf().readUnsignedInt();
397 }
398
399 public long readLong() {
400 checkReadableBytes(8);
401 return buf().readLong();
402 }
403
404 public int readMedium() {
405 checkReadableBytes(3);
406 return buf().readMedium();
407 }
408
409 public int readUnsignedMedium() {
410 checkReadableBytes(3);
411 return buf().readUnsignedMedium();
412 }
413
414 public short readShort() {
415 checkReadableBytes(2);
416 return buf().readShort();
417 }
418
419 public int readUnsignedShort() {
420 checkReadableBytes(2);
421 return buf().readUnsignedShort();
422 }
423
424 public char readChar() {
425 checkReadableBytes(2);
426 return buf().readChar();
427 }
428
429 public float readFloat() {
430 checkReadableBytes(4);
431 return buf().readFloat();
432 }
433
434 public double readDouble() {
435 checkReadableBytes(8);
436 return buf().readDouble();
437 }
438
439 public void resetReaderIndex() {
440 buf().resetReaderIndex();
441 }
442
443 public void resetWriterIndex() {
444 throw new UnreplayableOperationException();
445 }
446
447 public void setByte(int index, int value) {
448 throw new UnreplayableOperationException();
449 }
450
451 public void setBytes(int index, byte[] src, int srcIndex, int length) {
452 throw new UnreplayableOperationException();
453 }
454
455 public void setBytes(int index, byte[] src) {
456 throw new UnreplayableOperationException();
457 }
458
459 public void setBytes(int index, ByteBuffer src) {
460 throw new UnreplayableOperationException();
461 }
462
463 public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
464 throw new UnreplayableOperationException();
465 }
466
467 public void setBytes(int index, ChannelBuffer src, int length) {
468 throw new UnreplayableOperationException();
469 }
470
471 public void setBytes(int index, ChannelBuffer src) {
472 throw new UnreplayableOperationException();
473 }
474
475 public int setBytes(int index, InputStream in, int length)
476 throws IOException {
477 throw new UnreplayableOperationException();
478 }
479
480 public void setZero(int index, int length) {
481 throw new UnreplayableOperationException();
482 }
483
484 public int setBytes(int index, ScatteringByteChannel in, int length)
485 throws IOException {
486 throw new UnreplayableOperationException();
487 }
488
489 public void setIndex(int readerIndex, int writerIndex) {
490 throw new UnreplayableOperationException();
491 }
492
493 public void setInt(int index, int value) {
494 throw new UnreplayableOperationException();
495 }
496
497 public void setLong(int index, long value) {
498 throw new UnreplayableOperationException();
499 }
500
501 public void setMedium(int index, int value) {
502 throw new UnreplayableOperationException();
503 }
504
505 public void setShort(int index, int value) {
506 throw new UnreplayableOperationException();
507 }
508
509 public void setChar(int index, int value) {
510 throw new UnreplayableOperationException();
511 }
512
513 public void setFloat(int index, float value) {
514 throw new UnreplayableOperationException();
515 }
516
517 public void setDouble(int index, double value) {
518 throw new UnreplayableOperationException();
519 }
520
521 @Deprecated
522 public int skipBytes(ChannelBufferIndexFinder firstIndexFinder) {
523 int oldReaderIndex = buf().readerIndex();
524 int newReaderIndex = buf().indexOf(oldReaderIndex, buf().writerIndex(), firstIndexFinder);
525 if (newReaderIndex < 0) {
526 throw REPLAY;
527 }
528 buf().readerIndex(newReaderIndex);
529 return newReaderIndex - oldReaderIndex;
530 }
531
532 public void skipBytes(int length) {
533 checkReadableBytes(length);
534 buf().skipBytes(length);
535 }
536
537 public ChannelBuffer slice() {
538 throw new UnreplayableOperationException();
539 }
540
541 public ChannelBuffer slice(int index, int length) {
542 checkIndex(index, length);
543 return buf().slice(index, length);
544 }
545
546 public ByteBuffer toByteBuffer() {
547 throw new UnreplayableOperationException();
548 }
549
550 public ByteBuffer toByteBuffer(int index, int length) {
551 checkIndex(index, length);
552 return buf().toByteBuffer(index, length);
553 }
554
555 public ByteBuffer[] toByteBuffers() {
556 throw new UnreplayableOperationException();
557 }
558
559 public ByteBuffer[] toByteBuffers(int index, int length) {
560 checkIndex(index, length);
561 return buf().toByteBuffers(index, length);
562 }
563
564 public String toString(int index, int length, Charset charset) {
565 checkIndex(index, length);
566 return buf().toString(index, length, charset);
567 }
568
569 public String toString(Charset charsetName) {
570 throw new UnreplayableOperationException();
571 }
572
573 @Deprecated
574 public String toString(int index, int length, String charsetName) {
575 checkIndex(index, length);
576 return buf().toString(index, length, charsetName);
577 }
578
579 @Deprecated
580 public String toString(
581 int index, int length, String charsetName,
582 ChannelBufferIndexFinder terminatorFinder) {
583 checkIndex(index, length);
584 return buf().toString(index, length, charsetName, terminatorFinder);
585 }
586
587 @Deprecated
588 public String toString(String charsetName) {
589 throw new UnreplayableOperationException();
590 }
591
592 @Deprecated
593 public String toString(
594 String charsetName, ChannelBufferIndexFinder terminatorFinder) {
595 throw new UnreplayableOperationException();
596 }
597
598 @Override
599 public String toString() {
600 return getClass().getSimpleName() + '(' +
601 "ridx=" +
602 readerIndex() +
603 ", " +
604 "widx=" +
605 writerIndex() +
606 ')';
607 }
608
609 public boolean writable() {
610 return false;
611 }
612
613 public int writableBytes() {
614 return 0;
615 }
616
617 public void writeByte(int value) {
618 throw new UnreplayableOperationException();
619 }
620
621 public void writeBytes(byte[] src, int srcIndex, int length) {
622 throw new UnreplayableOperationException();
623 }
624
625 public void writeBytes(byte[] src) {
626 throw new UnreplayableOperationException();
627 }
628
629 public void writeBytes(ByteBuffer src) {
630 throw new UnreplayableOperationException();
631 }
632
633 public void writeBytes(ChannelBuffer src, int srcIndex, int length) {
634 throw new UnreplayableOperationException();
635 }
636
637 public void writeBytes(ChannelBuffer src, int length) {
638 throw new UnreplayableOperationException();
639 }
640
641 public void writeBytes(ChannelBuffer src) {
642 throw new UnreplayableOperationException();
643 }
644
645 public int writeBytes(InputStream in, int length) throws IOException {
646 throw new UnreplayableOperationException();
647 }
648
649 public int writeBytes(ScatteringByteChannel in, int length)
650 throws IOException {
651 throw new UnreplayableOperationException();
652 }
653
654 public void writeInt(int value) {
655 throw new UnreplayableOperationException();
656 }
657
658 public void writeLong(long value) {
659 throw new UnreplayableOperationException();
660 }
661
662 public void writeMedium(int value) {
663 throw new UnreplayableOperationException();
664 }
665
666 public void writeZero(int length) {
667 throw new UnreplayableOperationException();
668 }
669
670 public int writerIndex() {
671 return buf().writerIndex();
672 }
673
674 public void writerIndex(int writerIndex) {
675 throw new UnreplayableOperationException();
676 }
677
678 public void writeShort(int value) {
679 throw new UnreplayableOperationException();
680 }
681
682 public void writeChar(int value) {
683 throw new UnreplayableOperationException();
684 }
685
686 public void writeFloat(float value) {
687 throw new UnreplayableOperationException();
688 }
689
690 public void writeDouble(double value) {
691 throw new UnreplayableOperationException();
692 }
693
694 private void checkIndex(int index, int length) {
695 if (index + length > buf().writerIndex()) {
696 throw REPLAY;
697 }
698 }
699
700 private void checkReadableBytes(int readableBytes) {
701 if (buf().readableBytes() < readableBytes) {
702 throw REPLAY;
703 }
704 }
705 }