パッケージ org.piax.util

クラス ByteBufferUtil

java.lang.Object
org.piax.util.ByteBufferUtil

public class ByteBufferUtil
extends Object
Messaging Framework で使用するByteBufferに関するユーティティクラス。

Messaging Framework では、先頭部分に補助情報を付与することを行う。 このため、ByteBufferを割り当てる際には、先頭部分に空きを設けて、途中の offsetから使用することを行う。

ByteBuffer では、position=0 が先頭バイトとなることを仮定してメソッドが 用意されている。(例:clear(), flip(), rewind()) ここでは、先頭バイトを指すindexとしてmarkを使用する。既存のメソッドに 代わるものとして、positionを0にする代わりに、mark値にセットする、 clear(), flip(), rewind(), flop() を用意する。

read状態は次のような状態である。ここで、+ は読み込むべきデータ。mはmark, pはposition, lはlimit [---m=p++++++l----] この状態にするために、flipを用いる。再読み込みするためには、rewindを用いる。 write状態は次のような状態である。 [---m++++++p----l] この状態にするために、flopを用いる。

  • コンストラクタの概要

    コンストラクタ 
    コンストラクタ 説明
    ByteBufferUtil()  
  • メソッドの概要

    修飾子とタイプ メソッド 説明
    static byte[] buffer2Bytes​(ByteBuffer bbuf)
    ByteBufferのpositionからlimitで指定されたbyte列を新規のbyte配列として返す。
    static ByteBuffer byte2Buffer​(byte[] data)
    指定されたbyte列を値として持つByteBufferを生成する。
    static ByteBuffer byte2Buffer​(byte[] buf, int offset, int len)
    指定されたbyte列を値として持つByteBufferを生成する。
    static ByteBuffer byte2Buffer​(int margin, byte[] buf, int offset, int len)  
    static void clear​(ByteBuffer b)
    指定されたByteBufferをclearする。
    static ByteBuffer concat​(byte[] pre, ByteBuffer bbuf)
    bbufの持つbyte列の先頭にpreで指定されたbyte列を結合する。
    static ByteBuffer concat​(ByteBuffer pre, ByteBuffer bbuf)
    bbufの持つbyte列の先頭にpreで指定されたByteBufferを結合する。
    static void copy2Buffer​(byte[] src, int srcOff, int srcLen, ByteBuffer dst, int dstOff)
    指定されたbyte列を指定されたByteBufferの指定位置にコピーする。
    static ByteBuffer enhance​(int size, ByteBuffer bbuf)
    ByteBufferのヘッダマージンをsizeのbyte分だけ拡張する。
    static ByteBuffer enhance​(ByteBuffer bbuf, int size)
    ByteBufferのbodyをsizeのbyte分だけ拡張する。
    static void flip​(ByteBuffer b)
    指定されたByteBufferを反転する。
    static void flop​(ByteBuffer b)
    read状態から再びデータをputする状態に変える。
    static void mark​(ByteBuffer b)
    指定されたByteBufferの先頭マークを設定する。
    static ByteBuffer newByteBuffer​(int capacity)
    指定された容量capacityを持つByteBufferを生成する。
    static ByteBuffer newByteBuffer​(int margin, int capacity)
    指定されたmarginと容量capacityを持つByteBufferを生成する。
    static ByteBuffer put​(ByteBuffer bbuf, byte[] post)
    bbufにpostで指定されたbyte列を追加する。
    static ByteBuffer put​(ByteBuffer bbuf, ByteBuffer post)
    bbufにpostで指定されたByteBufferを追加する。
    static ByteBuffer reserve​(int preLen, ByteBuffer bbuf)
    bbufの持つbyte列の前方にpreLenで指定されたbyte長だけ領域確保する。
    static void reset​(int offset, ByteBuffer b)
    指定されたByteBufferの先頭をoffsetとしてresetする。
    static void rewind​(ByteBuffer b)
    指定されたByteBufferを巻き戻す。
    static boolean startsWith​(byte[] prefix, ByteBuffer bbuf)
    bbufの持つbyte列の先頭が、prefixと等しいか判定する。
    static void strip​(int preLen, ByteBuffer bbuf)
    bbufの持つbyte列から先頭部preLen分のbyte列を削る。

    クラスから継承されたメソッド java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • コンストラクタの詳細

  • メソッドの詳細

    • clear

      public static void clear​(ByteBuffer b)
      指定されたByteBufferをclearする。 positionはmarkの値に、limitはcapacityの値にセットされる。
      パラメータ:
      b - 対象となるByteBuffer
      例外:
      InvalidMarkException - markがセットされてなかった場合
    • flip

      public static void flip​(ByteBuffer b)
      指定されたByteBufferを反転する。 putされたデータをreadするための準備のための作業。 limitはpositionの値に、positionはmarkの値にセットされる。
      パラメータ:
      b - 対象となるByteBuffer
      例外:
      InvalidMarkException - markがセットされてなかった場合
    • rewind

      public static void rewind​(ByteBuffer b)
      指定されたByteBufferを巻き戻す。 readの操作によって起こったpositionの変化を元に戻す。 positionはmarkの値にセットされる。
      パラメータ:
      b - 対象となるByteBuffer
      例外:
      InvalidMarkException - markがセットされてなかった場合
    • flop

      public static void flop​(ByteBuffer b)
      read状態から再びデータをputする状態に変える。 positionはlimitの値に、limitはcapacityにセットされる。
      パラメータ:
      b - 対象となるByteBuffer
    • mark

      public static void mark​(ByteBuffer b)
      指定されたByteBufferの先頭マークを設定する。 markはpositionの値にセットされる。
      パラメータ:
      b - 対象となるByteBuffer
    • reset

      public static void reset​(int offset, ByteBuffer b)
      指定されたByteBufferの先頭をoffsetとしてresetする。 mark, positionともoffset値にセットされる。
      パラメータ:
      offset - reset時のoffset値
      b - 対象となるByteBuffer
    • newByteBuffer

      public static ByteBuffer newByteBuffer​(int margin, int capacity)
      指定されたmarginと容量capacityを持つByteBufferを生成する。
      パラメータ:
      margin - 先頭部の余白
      capacity - 容量
      戻り値:
      新規に生成されたByteBuffer
    • newByteBuffer

      public static ByteBuffer newByteBuffer​(int capacity)
      指定された容量capacityを持つByteBufferを生成する。 marginは、DEFAULT_HEADER_MARGIN の値がセットされる。
      パラメータ:
      capacity - 容量
      戻り値:
      新規に生成されたByteBuffer
    • copy2Buffer

      public static void copy2Buffer​(byte[] src, int srcOff, int srcLen, ByteBuffer dst, int dstOff)
      指定されたbyte列を指定されたByteBufferの指定位置にコピーする。
      パラメータ:
      src - コピー元のbyte配列
      srcOff - コピー元となるbyte配列のoffset
      srcLen - コピー元となるbyte列の長さ
      dst - コピー先のByteBuffer
      dstOff - コピー先のoffset
      例外:
      BufferOverflowException - ByteBuffer内に残っている容量が不足している場合
      IndexOutOfBoundsException - srcOffとsrcLengthパラメータの前提条件が満たされていない場合
    • byte2Buffer

      public static ByteBuffer byte2Buffer​(int margin, byte[] buf, int offset, int len)
    • byte2Buffer

      public static ByteBuffer byte2Buffer​(byte[] data)
      指定されたbyte列を値として持つByteBufferを生成する。 生成されたByteBufferの先頭には、DEFAULT_HEADER_MARGINで指定されたbyte数の 領域が確保される。
      パラメータ:
      data - byte列全体を格納するbyte配列
      戻り値:
      新規に生成されたByteBuffer
    • byte2Buffer

      public static ByteBuffer byte2Buffer​(byte[] buf, int offset, int len)
      指定されたbyte列を値として持つByteBufferを生成する。 生成されたByteBufferの先頭には、DEFAULT_HEADER_MARGINで指定されたbyte数の 領域が確保される。
      パラメータ:
      buf - byte列を格納するbyte配列
      offset - 格納するbyte列の先頭を示すoffset
      len - 格納するbyte列の長さ
      戻り値:
      新規に生成したByteBuffer
    • buffer2Bytes

      public static byte[] buffer2Bytes​(ByteBuffer bbuf)
      ByteBufferのpositionからlimitで指定されたbyte列を新規のbyte配列として返す。 尚、この操作によって、ByteBufferのpositionは変化しない。
      パラメータ:
      bbuf - 対象となるByteBuffer
      戻り値:
      bbufによって指定されるbyte列を値として持つbyte配列
    • enhance

      public static ByteBuffer enhance​(int size, ByteBuffer bbuf)
      ByteBufferのヘッダマージンをsizeのbyte分だけ拡張する。 但し、対象となるByteBufferは、read状態でないといけない。(呼び出し側の条件から)
      パラメータ:
      size - 拡張するbyte数
      bbuf - 対象となるByteBuffer
      戻り値:
      ヘッダマージンを拡張したByteBuffer
    • enhance

      public static ByteBuffer enhance​(ByteBuffer bbuf, int size)
      ByteBufferのbodyをsizeのbyte分だけ拡張する。 enhance(int size, ByteBuffer bbuf) と異なり、bbufは任意の状態でよい。
      パラメータ:
      bbuf - 対象となるByteBuffer
      size - 拡張するbyte数
      戻り値:
      bodyを拡張したByteBuffer
    • reserve

      public static ByteBuffer reserve​(int preLen, ByteBuffer bbuf)
      bbufの持つbyte列の前方にpreLenで指定されたbyte長だけ領域確保する。 bbufの先頭に十分なマージンがない場合は、bbufが拡張されて後、結合を行う。 但し、対象となるByteBufferは、read状態でないといけない。 領域確保された後、markとpositionは確保された領域の先頭に移動する。
      パラメータ:
      preLen - 前方に確保する領域のbyte長
      bbuf - 対象となるByteBuffer
      戻り値:
      確保後のByteBuffer
    • concat

      public static ByteBuffer concat​(byte[] pre, ByteBuffer bbuf)
      bbufの持つbyte列の先頭にpreで指定されたbyte列を結合する。 bbufの先頭に十分なマージンがない場合は、bbufが拡張されて後、結合を行う。 ByteBufferのpositionとmarkは、結合後のpreを含めたbyte列の先頭にセットされる。
      パラメータ:
      pre - 先頭に結合するbyte列
      bbuf - 対象となるByteBuffer
      戻り値:
      結合後のByteBuffer
    • concat

      public static ByteBuffer concat​(ByteBuffer pre, ByteBuffer bbuf)
      bbufの持つbyte列の先頭にpreで指定されたByteBufferを結合する。 bbufの先頭に十分なマージンがない場合は、bbufが拡張されて後、結合を行う。 ByteBufferのpositionとmarkは、結合後のpreを含めたbyte列の先頭にセットされる。
      パラメータ:
      pre - 先頭に結合するByteBuffer
      bbuf - 対象となるByteBuffer
      戻り値:
      結合後のByteBuffer
    • put

      public static ByteBuffer put​(ByteBuffer bbuf, byte[] post)
      bbufにpostで指定されたbyte列を追加する。 bbufは追記のため、write状態でないといけない。(limit=capacity) bbufに空き領域がない場合、ByteBufferの領域が拡張され、返り値には、 領域拡張されたByteBufferが返る。
      パラメータ:
      bbuf - ByteBuffer
      post - 追加するbyte列
      戻り値:
      post追加後のByteBuffer
    • put

      public static ByteBuffer put​(ByteBuffer bbuf, ByteBuffer post)
      bbufにpostで指定されたByteBufferを追加する。 bbufは追記のため、write状態でないといけない。(limit=capacity) bbufに空き領域がない場合、ByteBufferの領域が拡張され、返り値には、 領域拡張されたByteBufferが返る。
      パラメータ:
      bbuf - ByteBuffer
      post - 追加するByteBuffer
      戻り値:
      post追加後のByteBuffer
    • strip

      public static void strip​(int preLen, ByteBuffer bbuf)
      bbufの持つbyte列から先頭部preLen分のbyte列を削る。 bbufはread状態でないといけない。
      パラメータ:
      preLen - 先頭部から削るbyte数
      bbuf - 対象となるByteBuffer
      例外:
      IllegalArgumentException - 保持するbyte列より削るbyte数が同じであるか大きい場合
    • startsWith

      public static boolean startsWith​(byte[] prefix, ByteBuffer bbuf)
      bbufの持つbyte列の先頭が、prefixと等しいか判定する。
      パラメータ:
      prefix - 比較するbyte列
      bbuf - 対象となるByteBuffer
      戻り値:
      先頭がprefixと等しい場合true、そうでない場合false