パッケージ org.piax.util

クラス ByteUtil

java.lang.Object
org.piax.util.ByteUtil

public class ByteUtil
extends Object
byte配列を処理するためのユーティリティ。

Javaのクラスライブラリには、byte配列を扱うものが少ないので、自分で作りました。 必要に応じて、機能を足しているので、次第にメソッドが増えていきます。

あくまでも、piaxの中で内部処理を分かって用いられることを前提にしているため、 引数チェックはしていません。

メモ:
可変長byte配列を扱うクラスライブラリが望まれる。 ByteArrayOutputStream をこの用途で使っている箇所もあるが、 ByteArrayOutputStream が行う処理は byte配列を頻繁にreallocateしていて 必ずしも効率の良いコードになっていない。 効率化のためには、apache commons-io の org.apache.commons.io.output.ByteArrayOutputStream を使うように 変更する(または自分で実装する)必要が出てくるかもしれない。

  • コンストラクタの概要

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

    修飾子とタイプ メソッド 説明
    static String byte2Binary​(byte b)
    指定されたbyteデータを2進法の文字列に変換する。
    static String bytes2Binary​(byte[] b)
    指定されたbyte列データを2進法の文字列に変換する。
    static void bytes2File​(byte[] b, File file)  
    static String bytes2Hex​(byte[] b)  
    static int bytes2Int​(byte[] b)  
    static int bytes2Int​(byte[] b, int off)  
    static long bytes2Long​(byte[] b)  
    static long bytes2Long​(byte[] b, int off)  
    static void bytes2Stream​(byte[] b, OutputStream out)  
    static int commonPostfixLen​(byte[] b1, byte[] b2)
    指定された2つのbyte列に共通するpostfixのbit長を返す。
    static int commonPostfixLen​(byte b1, byte b2)
    指定された2つのbyteに共通するpostfixのbit長を返す。
    static int commonPrefixLen​(byte[] b1, byte[] b2)
    指定された2つのbyte列に共通するprefixのbit長を返す。
    static int commonPrefixLen​(byte b1, byte b2)
    指定された2つのbyteに共通するprefixのbit長を返す。
    static int compare​(byte[] b1, byte[] b2)
    指定された2つのbyte列を符号なし整数として比較して、b1がb2より小さい場合は負の整数、 両方が等しい場合は0、b1がb2より大きい場合は正の整数を返す。
    static int compareAsNumber​(byte[] b1, byte[] b2)
    指定された2つのbyte列を符号なし整数として比較して、b1がb2より小さい場合は負の整数、 両方が等しい場合は0、b1がb2より大きい場合は正の整数を返す。
    static byte[] concat​(byte[] b1, byte[] b2)  
    static int decode4escape​(byte[] b, int len, byte[] buf, byte[] escapeAndElimBytes)
    encode4escapeメソッドによってencodeされたbyte配列を 元のbyte配列にdecodeする。
    static String dumpBytes​(byte[] b)  
    static String dumpBytes​(byte[] b, int offset, int len)
    byte列をダンプ用の文字列に変換する。
    static String dumpBytes​(ByteBuffer bbuf)  
    static int encode4escape​(byte[] b, int len, byte[] buf, byte[] escapeAndElimBytes)
    与えられたbyte配列を指定されたbyte値が出現しないようにencodeする。
    static boolean equals​(byte[] b1, byte[] b2)
    指定された 2つのbyte列が同等である場合に trueを返す。
    static byte[] file2Bytes​(File file)  
    static Comparator<byte[]> getComparator()
    byte列を符号なし整数として比較する Comparatorを取得する。
    static byte[] hex2Bytes​(String s)  
    static byte[] hexBytes2Bytes​(byte[] hex)  
    static int indexOf​(byte[] b, byte x)  
    static byte[] int2bytes​(int x)  
    static boolean isASCII​(byte[] b)
    指定されたbyte列が、7bit ASCII文字列である場合はtrueを返す。
    static byte[] long2bytes​(long x)  
    static int reverse32​(int baseno)  
    static byte reverse8​(byte v)  
    static boolean startsWith​(byte[] b, byte[] prefix)  
    static byte[] stream2Bytes​(InputStream in)  
    static boolean testBit​(byte[] b, int ix)
    指定されたbyte列の先頭のbyteを起点として上位からカウントしたixビット目が1か0かを調べる。
    static boolean testBit​(byte b, int ix)
    指定されたbyteの上位からixビット目が1か0かを調べる。
    static byte[] url2Bytes​(URL url)  

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

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

  • メソッドの詳細

    • getComparator

      public static Comparator<byte[]> getComparator()
      byte列を符号なし整数として比較する Comparatorを取得する。 このComparatorは、長さの異なるbyte列であっても、符号なし整数として等しい場合は、同等とみなす。
      戻り値:
      byte列を符号なし整数として比較する Comparator
    • compare

      public static int compare​(byte[] b1, byte[] b2)
      指定された2つのbyte列を符号なし整数として比較して、b1がb2より小さい場合は負の整数、 両方が等しい場合は0、b1がb2より大きい場合は正の整数を返す。

      比較するbyte列の長さが異なる場合は、長さの長いbyte列が大きいとみなされる。 このため、equalsと互換性がある。

      パラメータ:
      b1 - byte列
      b2 - byte列
      戻り値:
      b1がb2より小さい場合は負の整数、両方が等しい場合は0、b1がb2より大きい場合は正の整数
    • compareAsNumber

      public static int compareAsNumber​(byte[] b1, byte[] b2)
      指定された2つのbyte列を符号なし整数として比較して、b1がb2より小さい場合は負の整数、 両方が等しい場合は0、b1がb2より大きい場合は正の整数を返す。

      両方のbyte列の長さが異なる場合は、長さの短いbyte列の上位に0を埋めて同じ長さのbyte列と みなした上で比較する。 このため、equalsと互換性はない。

      パラメータ:
      b1 - byte列
      b2 - byte列
      戻り値:
      b1がb2より小さい場合は負の整数、両方が等しい場合は0、b1がb2より大きい場合は正の整数
    • equals

      public static boolean equals​(byte[] b1, byte[] b2)
      指定された 2つのbyte列が同等である場合に trueを返す。

      2つのbyte列が同等とみなされるのは、同じ数の要素があり、対応する対の要素がすべて同等である場合となる。 compareAsNumberメソッドが異なる長さのbyte列であっても、数値として等しい場合に同等と返す点に注意。

      パラメータ:
      b1 - byte列
      b2 - byte列
      戻り値:
      b1, b2が同等である場合、true
    • concat

      public static byte[] concat​(byte[] b1, byte[] b2)
    • testBit

      public static boolean testBit​(byte b, int ix) throws IllegalArgumentException
      指定されたbyteの上位からixビット目が1か0かを調べる。 1の時はtrue、0の時はfalseが返る。
      パラメータ:
      b - バイトデータ
      ix - 上位から数えたビット数
      戻り値:
      上位ixビット目が1の時はtrue、それ以外はfalse
      例外:
      IllegalArgumentException - 0 <= ix < 8 でない場合
    • testBit

      public static boolean testBit​(byte[] b, int ix) throws IllegalArgumentException
      指定されたbyte列の先頭のbyteを起点として上位からカウントしたixビット目が1か0かを調べる。 1の時はtrue、0の時はfalseが返る。
      パラメータ:
      b - バイト列データ
      ix - 上位から数えたビット数
      戻り値:
      上位ixビット目が1の時はtrue、それ以外はfalse
      例外:
      IllegalArgumentException - 0 <= ix < 8*byte長、 でない場合
    • commonPostfixLen

      public static int commonPostfixLen​(byte b1, byte b2)
      指定された2つのbyteに共通するpostfixのbit長を返す。
      パラメータ:
      b1 - byte
      b2 - byte
      戻り値:
      b1, b2に共通するpostfixのbit長
    • commonPostfixLen

      public static int commonPostfixLen​(byte[] b1, byte[] b2)
      指定された2つのbyte列に共通するpostfixのbit長を返す。
      パラメータ:
      b1 - byte列
      b2 - byte列
      戻り値:
      b1, b2に共通するpostfixのbit長
    • commonPrefixLen

      public static int commonPrefixLen​(byte b1, byte b2)
      指定された2つのbyteに共通するprefixのbit長を返す。
      パラメータ:
      b1 - byte
      b2 - byte
      戻り値:
      b1, b2に共通するprefixのbit長
    • commonPrefixLen

      public static int commonPrefixLen​(byte[] b1, byte[] b2)
      指定された2つのbyte列に共通するprefixのbit長を返す。
      パラメータ:
      b1 - byte列
      b2 - byte列
      戻り値:
      b1, b2に共通するprefixのbit長
    • reverse32

      public static int reverse32​(int baseno)
    • reverse8

      public static byte reverse8​(byte v)
    • bytes2Int

      public static int bytes2Int​(byte[] b)
    • bytes2Int

      public static int bytes2Int​(byte[] b, int off)
    • int2bytes

      public static byte[] int2bytes​(int x)
    • bytes2Long

      public static long bytes2Long​(byte[] b)
    • bytes2Long

      public static long bytes2Long​(byte[] b, int off)
    • long2bytes

      public static byte[] long2bytes​(long x)
    • stream2Bytes

      public static byte[] stream2Bytes​(InputStream in) throws IOException
      例外:
      IOException
    • file2Bytes

      public static byte[] file2Bytes​(File file) throws FileNotFoundException, IOException
      例外:
      FileNotFoundException
      IOException
    • url2Bytes

      public static byte[] url2Bytes​(URL url) throws FileNotFoundException, IOException
      例外:
      FileNotFoundException
      IOException
    • bytes2Stream

      public static void bytes2Stream​(byte[] b, OutputStream out) throws IOException
      例外:
      IOException
    • bytes2File

      public static void bytes2File​(byte[] b, File file) throws FileNotFoundException, IOException
      例外:
      FileNotFoundException
      IOException
    • byte2Binary

      public static String byte2Binary​(byte b)
      指定されたbyteデータを2進法の文字列に変換する。
      パラメータ:
      b - byte
      戻り値:
      2進法の文字列表現
    • bytes2Binary

      public static String bytes2Binary​(byte[] b)
      指定されたbyte列データを2進法の文字列に変換する。 文字列表現において、byteの切れ目には"_"が挿入される。
      パラメータ:
      b - byte列
      戻り値:
      2進法の文字列表現
    • bytes2Hex

      public static String bytes2Hex​(byte[] b)
    • hexBytes2Bytes

      public static byte[] hexBytes2Bytes​(byte[] hex)
    • hex2Bytes

      public static byte[] hex2Bytes​(String s) throws IllegalArgumentException
      例外:
      IllegalArgumentException
    • isASCII

      public static boolean isASCII​(byte[] b)
      指定されたbyte列が、7bit ASCII文字列である場合はtrueを返す。
      パラメータ:
      b - byte列
      戻り値:
      7bit ASCII文字列である場合、true
    • dumpBytes

      public static String dumpBytes​(byte[] b, int offset, int len)
      byte列をダンプ用の文字列に変換する。

      0x20-0x7e はACSII文字として出力し、 他のbyte値については、16進表現+'.' を出力する。(例:".8f")

      パラメータ:
      b - byte列
      offset - offset
      len - 長さ
      戻り値:
      ダンプ用文字列
    • dumpBytes

      public static String dumpBytes​(byte[] b)
    • dumpBytes

      public static String dumpBytes​(ByteBuffer bbuf)
    • startsWith

      public static boolean startsWith​(byte[] b, byte[] prefix)
    • indexOf

      public static int indexOf​(byte[] b, byte x)
    • encode4escape

      public static int encode4escape​(byte[] b, int len, byte[] buf, byte[] escapeAndElimBytes) throws ArrayIndexOutOfBoundsException
      与えられたbyte配列を指定されたbyte値が出現しないようにencodeする。

      元のbyte配列に、指定されたbyte値が出現した場合、1byte目をescape用の byte, 2byte目を対応するescape codeとする2byteの列に置き換える。

      引数 escapeAndElimBytes の1byte目にはescape用のbyte, 2byte目以降に 出現を抑制したいbyte値(複数可)をセットする。

      パラメータ:
      b - ソースとなるbyte配列
      len - ソースとなるbyte配列の長さ
      buf - 変換後のbyte配列を入れるバッファ
      escapeAndElimBytes - escape用のbyteと出現を抑制したいbyte値(複数可)
      戻り値:
      変換後のbyte配列の長さ
      例外:
      ArrayIndexOutOfBoundsException - バッファが十分な容量を持たない場合
    • decode4escape

      public static int decode4escape​(byte[] b, int len, byte[] buf, byte[] escapeAndElimBytes) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
      encode4escapeメソッドによってencodeされたbyte配列を 元のbyte配列にdecodeする。

      decode処理の場合に限り、ソースとして指定する配列を変換後のバッファと して用いる配列に指定してもよい。

      パラメータ:
      b - ソースとなるbyte配列
      len - ソースとなるbyte配列の長さ
      buf - 変換後のbyte配列を入れるバッファ
      escapeAndElimBytes - escape用のbyteと出現を抑制したいbyte値(複数可)
      戻り値:
      変換後のbyte配列の長さ
      例外:
      IllegalArgumentException - ソースとなるbyte配列が正しくencodeされたものでない場合
      ArrayIndexOutOfBoundsException - バッファが十分な容量を持たない場合