LoginSignup
1
1

More than 5 years have passed since last update.

ストリームにint値を書いたり、読んだり

Posted at
    /**
     * OutputStreamの現在位置にint値(4バイト)を書き込む。
     * @param out
     * @param i
     * @throws IOException
     */
    protected static void writeInt(OutputStream out, int i) throws IOException {
        byte[] bytes = {
                  (byte) (i >> 24)
                , (byte) (i >> 16)
                , (byte) (i >> 8)
                , (byte) i
        };
        out.write(bytes);
    }

    /**
     * InputStreamからint値(4バイト)を読み込む
     * @param in
     * @return
     * @throws IOException
     */
    protected static int readInt(InputStream in) throws IOException {
        byte[] bytes = new byte[4];
        in.read(bytes);
        return (0xff & bytes[0]) << 24
                | (0xff & bytes[1]) << 16
                | (0xff & bytes[2]) << 8
                | (0xff & bytes[3]);
    }

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1