intbyte[]変換

正確にはDataOutputStream, DataInputStreamを参考にしたので自作ではないですけど。

/**
 * <p>
 * バイト・データをint値に変換します。
 * </p>
 * 
 * @param data
 *            int値を表すバイト・データ。
 * @return int値。
 * @see DataInputStream#readInt()
 */
static int bytesToInt(byte[] data) {
    if (data.length != 4) {
        throw new IllegalArgumentException("data.length != " + 4);
    }

    int ch0 = data[0];
    if (ch0 < 0) {
        ch0 += 256;
    }
    int ch1 = data[1];
    if (ch1 < 0) {
        ch1 += 256;
    }
    int ch2 = data[2];
    if (ch2 < 0) {
        ch2 += 256;
    }
    int ch3 = data[3];
    if (ch3 < 0) {
        ch3 += 256;
    }
    int v = (ch0 << 24) + (ch1 << 16) + (ch2 << 8) + (ch3 << 0);
    return v;
}

/**
 * <p>
 * int値をバイト・データに変換します。
 * </p>
 * 
 * @param value
 *            int値。
 * @return int値を表すバイト・データ。
 * @see DataOutputStream#writeInt(int)
 */
static byte[] intToBytes(int value) {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    byteOut.write((value >>> 24) & BITMASK);
    byteOut.write((value >>> 16) & BITMASK);
    byteOut.write((value >>> 8) & BITMASK);
    byteOut.write((value >>> 0) & BITMASK);

    return byteOut.toByteArray();
}