View Javadoc

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