⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bytebuffer.java

📁 移动Agent编程工具Naplet
💻 JAVA
字号:
package naplet.nsocket;

/**
 * A buffer hold undilivered data when connection migration
 */
class ByteBuffer
    implements java.io.Serializable
{
  private static final int EMPTY = 0;
  private static final int FULL = 1;

  private int head = 0;
  private int tail = 0;
  private int status = 0;
  byte[] buf;

  public ByteBuffer( int i )
  {
    buf = new byte[i + 1];
  }

  public int getStatus()
  {
    return status;
  }

  public boolean isEmpty()
  {
    if ( status == EMPTY )
    {
      return true;
    }
    return false;
  }

  public boolean isFull()
  {
    if ( status == FULL )
    {
      return true;
    }
    return false;
  }

  /**
   * Reads the next byte of data from the buffer stream. The value byte is
   * returned as an int in the range 0 to 255. If no byte is available because
   * the end of the stream has been reached, the value -1 is returned.
   *
   * @return
   */
  public synchronized int read()
  {
    if ( status == EMPTY )
    {
      return -1;
    }
    if ( head >= buf.length )
    {
      System.out.println( "head is out of bounds" );
      return -1;
    }
    if ( tail >= buf.length )
    {
      System.out.println( "tail is out of bounds" );
      return -1;
    }
    int i = head;
    head++;
    if ( head >= buf.length )
    {
      head = EMPTY;
    }
    if ( head == tail )
    {
      status = EMPTY;
    }
    else
    {
      status = 2;
    }
    return buf[i];
  }

  /**
   * Reads up to len bytes of data from the buffer into an array of bytes from
   * offset specified by off.
   * Read from buffer from off to off+len
   * @param is
   * @param off
   * @param len
   * @return
   */
  public synchronized int read( byte[] is, int off, int len )
  {
    if ( off >= is.length )
    {
      throw new IllegalArgumentException
          ( "offset must be less than the length of the byte array" );
    }
    if ( len < 0 )
    {
      throw new IllegalArgumentException
          ( "length must be greater than or equal to 0" );
    }
    if ( len + off > is.length )
    {
      throw new IllegalArgumentException
          ( "len + off must be less than the length of the byte array" );
    }
    if ( len == 0 )
    {
      return 0;
    }
    if ( status == EMPTY )
    {
      System.out.println( "buffer empty, read nothing" );
      return -1;
    }
    if ( head >= buf.length )
    {
      System.out.println( "head is out of bounds" );
      return -1;
    }
    if ( tail >= buf.length )
    {
      System.out.println( "tail is out of bounds" );
      return -1;
    }
    for ( int index = off; index < len + off; index++ )
    {
      if ( status == EMPTY )
      { //empty
        return index - off;
      }
      int hd = head;
      head++;
      if ( head >= buf.length )
      {
        head = 0;
      }
      if ( head == tail )
      {
        status = EMPTY;
      }
      else
      {
        status = 2;
      }
      is[index] = buf[hd];
    }
    return len;
  }

  /**
   * change buffer size to a new one
   * @param i
   */
  public synchronized void resize( int i )
  {
    if ( i <= buf.length )
    {
      throw new IllegalArgumentException( "Wrong Size" );
    }
    byte[] is = buf;
    buf = new byte[i];
    int ind = 0;
    if ( status != EMPTY )
    {
      for ( int ii = 0; ii < is.length; ii++ )
      {
        buf[ind] = is[head];
        ind++;
        head++;
        if ( head >= is.length )
        {
          head = 0;
        }
        if ( head == tail )
        {
          break;
        }
      }
    }
    head = 0;
    tail = ind;
    if ( status == FULL )
    {
      status = 2;
    }
  }

  /**
   * return size of the buffer
   * @return
   */
  public int size()
  {
    return buf.length - 1;
  }

  public String toString()
  {
    String string = null;
    string += new String( buf );
    return string;
  }

  /**
   * Writes len bytes from the input array starting at offset off to the
   * underlying buffer.
   * Writes buffer from off to off+len
   * @param is
   * @param off
   * @param len
   * @return
   */
  public synchronized int write( byte[] is, int off, int len )
  {
    if ( off >= is.length )
    {
      throw new IllegalArgumentException
          ( "off must be less than the length of the byte array" );
    }
    if ( len < 0 )
    {
      throw new IllegalArgumentException
          ( "len must be greater than or equal to 0" );
    }
    if ( len + off > is.length )
    {
      throw new IllegalArgumentException
          ( "len + off must be less than the length of the byte array" );
    }
    if ( status == FULL )
    {
      System.out.println( "buffer full, write nothing" );
      return 0;
    }
    if ( head >= buf.length )
    {
      System.out.println( "head is out of bounds" );
      return -1;
    }
    if ( tail >= buf.length )
    {
      System.out.println( "tail is out of bounds" );
      return -1;
    }
    for ( int index = off; index < len + off; index++ )
    {
      if ( status == FULL )
      {
        return index - off;
      }
      buf[tail] = is[index];
      tail++;
      if ( tail >= buf.length )
      {
        tail = 0;
      }
      if ( tail < head && tail + 1 == head )
      {
        status = FULL;
      }
      else if ( tail > head && head == 0 && tail == buf.length - 1 )
      {
        status = FULL;
      }
      else
      {
        status = 2;
      }
    }
    return len;
  }

  /**
   * Test for ByteBuffer
   * @param args
   */
  public static void main( String[] args )
  {
    ByteBuffer by = new ByteBuffer( 30 );
    byte[] b1 = new byte[10];
    for ( int i = 0; i < b1.length; i++ )
    {
      b1[i] = ( byte ) i;
    }
    by.write( b1, 0, b1.length );

    byte[] b2 = new byte[10];
    for ( int i = 0; i < b1.length; i++ )
    {
      b2[i] = ( byte ) ( i + 100 );
    }
    by.write( b2, 0, b2.length );
    by.write( b2, 0, b2.length );
    by.write( b2, 0, b2.length );

    for ( int i = 0; i < by.size(); i++ )
    {
      System.out.println( by.buf[i] );
    }
    System.out.println();

    /*by.read(b2);
         for (int i=0;i<by.size();i++) {
      System.out.println(by.buf[i]);
         }
         System.out.println(by.isEmpty());
         by.read(b2);
         for (int i=0;i<by.size();i++) {
      System.out.println(by.buf[i]);
         }
         System.out.println(by.isEmpty());*/

  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -