Java ByteBuffer to Int readInt(ByteBuffer buf)

Here you can find the source of readInt(ByteBuffer buf)

Description

read Int

License

Apache License

Declaration

public static int readInt(ByteBuffer buf) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;

public class Main {
    public static int readInt(ByteBuffer buf) throws IOException {
        if (buf.remaining() < 4) {
            throw new IOException("Not enough bytes in buffer to read integer");
        }/*from   w ww  .ja  v  a 2 s .  co  m*/
        int ch1 = 0x0000ffff & buf.get();
        int ch2 = 0x0000ffff & buf.get();
        int ch3 = 0x0000ffff & buf.get();
        int ch4 = 0x0000ffff & buf.get();
        if ((ch1 | ch2 | ch3 | ch4) < 0)
            throw new EOFException();
        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
    }
}

Related

  1. getUnsignedSmartInt(ByteBuffer buffer)
  2. getUnsignedVarInt(ByteBuffer buffer, int numBytes)
  3. getUShort(ByteBuffer buffer)
  4. getUShort(ByteBuffer buffer)
  5. getUShort(java.nio.ByteBuffer buffer)
  6. readInt(ByteBuffer buf, int length)
  7. readInt(ByteBuffer buf, int pos)
  8. readInt(ByteBuffer buffer)
  9. readInt(ByteBuffer buffer)