cvs commit: spice/sandbox/packet/src/java/org/realityforge/packet/encoder BasicEncoder.java

Peter Donald <[email protected]> Tue, 11 Nov 2003 01:36:20 -0800
Newsgroups gmane.comp.java.spice.cvs
Message-ID <[email protected]>
donaldp     03/11/11 01:36:20

  Added:       sandbox/packet/src/java/org/realityforge/packet/encoder
                        BasicEncoder.java
  Log:
  Add in basic encoder
  
  Revision  Changes    Path
  1.1                  spice/sandbox/packet/src/java/org/realityforge/packet/encoder/BasicEncoder.java
  
  Index: BasicEncoder.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.realityforge.packet.encoder;
  
  import org.realityforge.packet.Packet;
  import java.nio.ByteBuffer;
  
  /**
   * A basic encoder that writes packet using simple mechanisms.
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/11/11 09:36:20 $
   */
  public class BasicEncoder
      implements Encoder
  {
      /**
       * The number of bytes in packets header.
       */
      private static final int SIZEOF_HEADER = 2 + 2;
  
      /**
       * @see Encoder#encode
       */
      public boolean encode( final Packet packet,
                             final ByteBuffer output )
      {
          final int space = output.capacity() - output.limit();
          final ByteBuffer data = packet.getData();
          final int size = data.limit();
          if( space < SIZEOF_HEADER + size )
          {
              return false;
          }
          output.putShort( (short)size );
          output.putShort( packet.getSequence() );
          output.put( data.array(), 0, size );
          return true;
      }
  
      /**
       * @see Encoder#decode
       */
      public Packet decode( final ByteBuffer input )
      {
          final int index = input.position();
          input.position( 0 );
          if( input.remaining() < SIZEOF_HEADER )
          {
              return null;
          }
          final short messageSize = input.getShort();
          final short sequence = input.getShort();
          final int available = input.remaining();
          if( available >= messageSize )
          {
              final ByteBuffer byteBuffer = ByteBuffer.allocate( available );
              System.arraycopy( input.array(), SIZEOF_HEADER,
                                byteBuffer.array(), 0,
                                messageSize );
              final int end = messageSize + SIZEOF_HEADER;
              input.position( end );
              input.compact();
              return new Packet( sequence, 0, byteBuffer );
          }
          else
          {
              input.position( index );
              return null;
          }
      }
  }
  
  
  


-------------------------------------------------------
This SF.Net email sponsored by: ApacheCon 2003,
16-19 November in Las Vegas. Learn firsthand the latest
developments in Apache, PHP, Perl, XML, Java, MySQL,
WebDAV, and more! http://www.apachecon.com/