View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
    6    * use this file except in compliance with the License. You may obtain a copy of
    7    * the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    14   * License for the specific language governing permissions and limitations under
    15   * the License.
    16   */
    17  package org.activeio.packet;
    18
    19  import java.io.DataOutput;
    20  import java.io.IOException;
    21  import java.io.OutputStream;
    22
    23  import org.activeio.Packet;
    24
    25  /***
    26   * Provides a Packet implementation that filters operations to another packet.
    27   *
    28   * Used to make it easier to augment the {@see #narrow(Class)}method.
    29   *
    30   * @version $Revision$
    31   */
    32  public abstract class FilterPacket implements Packet {
    33      final protected Packet next;
    34
    35      public FilterPacket(Packet next) {
    36          this.next = next;
    37      }
    38
    39      public ByteSequence asByteSequence() {
    40          return next.asByteSequence();
    41      }
    42
    43      public int capacity() {
    44          return next.capacity();
    45      }
    46
    47      public void clear() {
    48          next.clear();
    49      }
    50
    51      public void flip() {
    52          next.flip();
    53      }
    54
    55      public boolean hasRemaining() {
    56          return next.hasRemaining();
    57      }
    58
    59      public int limit() {
    60          return next.limit();
    61      }
    62
    63      public void limit(int limit) {
    64          next.limit(limit);
    65      }
    66
    67      public Object narrow(Class target) {
    68          if( target.isAssignableFrom(getClass()) ) {
    69              return this;
    70          }
    71          return next.narrow(target);
    72      }
    73
    74      public int position() {
    75          return next.position();
    76      }
    77
    78      public void position(int position) {
    79          next.position(position);
    80      }
    81
    82      public int read() {
    83          return next.read();
    84      }
    85
    86      public int read(byte[] data, int offset, int length) {
    87          return next.read(data, offset, length);
    88      }
    89
    90      public int read(Packet dest) {
    91          return next.read(dest);
    92      }
    93
    94      public int remaining() {
    95          return next.remaining();
    96      }
    97
    98      public void rewind() {
    99          next.rewind();
    100     }
    101
    102     public byte[] sliceAsBytes() {
    103         return next.sliceAsBytes();
    104     }
    105
    106     public int write(byte[] data, int offset, int length) {
    107         return next.write(data, offset, length);
    108     }
    109
    110     public boolean write(int data) {
    111         return next.write(data);
    112     }
    113
    114     public void writeTo(OutputStream out) throws IOException {
    115         next.writeTo(out);
    116     }
    117     public void writeTo(DataOutput out) throws IOException {
    118         next.writeTo(out);
    119     }
    120
    121     public Object duplicate(ClassLoader cl) throws IOException {
    122         return next.duplicate(cl);
    123     }
    124
    125     public Packet duplicate() {
    126         return filter(next.duplicate());
    127     }
    128
    129     public Packet slice() {
    130         return filter(next.slice());
    131     }
    132
    133     public void dispose() {
    134         next.dispose();
    135     }
    136
    137     abstract public Packet filter(Packet packet);
    138 }