View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Hiram Chrino
    4    *
    5    *  Licensed under the Apache License, Version 2.0 (the "License");
    6    *  you may not use this file except in compliance with the License.
    7    *  You may obtain a copy of 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,
    13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   *  See the License for the specific language governing permissions and
    15   *  limitations under the License.
    16   */
    17  package org.activeio;
    18
    19  import java.io.DataOutput;
    20  import java.io.IOException;
    21  import java.io.OutputStream;
    22
    23  /***
    24   * Provides a ByteBuffer like interface to work with IO channel packets of data.
    25   *
    26   * @version $Revision$
    27   */
    28  public interface Packet extends Disposable {
    29
    30      public static class ByteSequence {
    31          private final byte[] data;
    32          private final int offset;
    33          private final int length;
    34
    35          public ByteSequence(byte data[], int offset, int length) {
    36              this.data = data;
    37              this.offset = offset;
    38              this.length = length;
    39          }
    40          public byte[] getData() {
    41              return data;
    42          }
    43          public int getLength() {
    44              return length;
    45          }
    46          public int getOffset() {
    47              return offset;
    48          }
    49
    50      }
    51
    52      public int position();
    53      public void position(int position);
    54      public int limit();
    55      public void limit(int limit);
    56      public void flip();
    57      public int remaining();
    58      public void rewind();
    59      public boolean hasRemaining();
    60      public void clear();
    61      public Packet slice();
    62      public Packet duplicate();
    63      public Object duplicate(ClassLoader cl) throws IOException;
    64      public int capacity();
    65
    66      public ByteSequence asByteSequence();
    67      public byte[] sliceAsBytes();
    68
    69
    70      /***
    71       * Writes the remaing bytes in the packet to the output stream.
    72       *
    73       * @param out
    74       * @return
    75       */
    76      void writeTo(OutputStream out) throws IOException;
    77      void writeTo(DataOutput out) throws IOException;
    78
    79      // To read data out of the packet.
    80      public int read();
    81      public int read(byte data[], int offset, int length);
    82
    83      // To write data into the packet.
    84      public boolean write( int data );
    85      public int write( byte data[], int offset, int length );
    86      public int read(Packet dest);
    87
    88      /***
    89       * Used to get a richer metadata interface to the packet.
    90       */
    91      Object narrow(Class target);
    92
    93  }