Java ByteBuffer to Int toInt(ByteBuffer buffer)

Here you can find the source of toInt(ByteBuffer buffer)

Description

Convert buffer to an integer.

License

Open Source License

Parameter

Parameter Description
buffer A buffer containing an integer in flush mode. The position is not changed.

Return

an int

Declaration

public static int toInt(ByteBuffer buffer) 

Method Source Code

//package com.java2s;
//  are made available under the terms of the Eclipse Public License v1.0

import java.nio.ByteBuffer;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class Main {
    static final byte SPACE = 0x20;
    static final byte MINUS = '-';

    /**/*from  w  w w.  j  a  v a  2s  .  c  o  m*/
     * Convert buffer to an integer. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown
     *
     * @param buffer
     *            A buffer containing an integer in flush mode. The position is not changed.
     * @return an int
     */
    public static int toInt(ByteBuffer buffer) {
        return toInt(buffer, buffer.position(), buffer.remaining());
    }

    /**
     * Convert buffer to an integer. Parses up to the first non-numeric character. If no number is found an
     * IllegalArgumentException is thrown
     *
     * @param buffer
     *            A buffer containing an integer in flush mode. The position is not changed.
     * @param position
     *            the position in the buffer to start reading from
     * @param length
     *            the length of the buffer to use for conversion
     * @return an int of the buffer bytes
     */
    public static int toInt(ByteBuffer buffer, int position, int length) {
        int val = 0;
        boolean started = false;
        boolean minus = false;

        int limit = position + length;

        if (length <= 0)
            throw new NumberFormatException(toString(buffer, position, length, StandardCharsets.UTF_8));

        for (int i = position; i < limit; i++) {
            byte b = buffer.get(i);
            if (b <= SPACE) {
                if (started)
                    break;
            } else if (b >= '0' && b <= '9') {
                val = val * 10 + (b - '0');
                started = true;
            } else if (b == MINUS && !started) {
                minus = true;
            } else
                break;
        }

        if (started)
            return minus ? (-val) : val;
        throw new NumberFormatException(toString(buffer));
    }

    /** Convert the buffer to an ISO-8859-1 String
     * @param buffer The buffer to convert in flush mode. The buffer is unchanged
     * @return The buffer as a string.
     */
    public static String toString(ByteBuffer buffer) {
        return toString(buffer, StandardCharsets.ISO_8859_1);
    }

    /** Convert the buffer to an ISO-8859-1 String
     * @param buffer  The buffer to convert in flush mode. The buffer is unchanged
     * @param charset The {@link Charset} to use to convert the bytes
     * @return The buffer as a string.
     */
    public static String toString(ByteBuffer buffer, Charset charset) {
        if (buffer == null)
            return null;
        byte[] array = buffer.hasArray() ? buffer.array() : null;
        if (array == null) {
            byte[] to = new byte[buffer.remaining()];
            buffer.slice().get(to);
            return new String(to, 0, to.length, charset);
        }
        return new String(array, buffer.arrayOffset() + buffer.position(), buffer.remaining(), charset);
    }

    /** Convert a partial buffer to a String.
     * 
     * @param buffer the buffer to convert 
     * @param position The position in the buffer to start the string from
     * @param length The length of the buffer
     * @param charset The {@link Charset} to use to convert the bytes
     * @return  The buffer as a string.
     */
    public static String toString(ByteBuffer buffer, int position, int length, Charset charset) {
        if (buffer == null)
            return null;
        byte[] array = buffer.hasArray() ? buffer.array() : null;
        if (array == null) {
            ByteBuffer ro = buffer.asReadOnlyBuffer();
            ro.position(position);
            ro.limit(position + length);
            byte[] to = new byte[length];
            ro.get(to);
            return new String(to, 0, to.length, charset);
        }
        return new String(array, buffer.arrayOffset() + position, length, charset);
    }
}

Related

  1. readVarint(ByteBuffer buffer)
  2. readVarInt(ByteBuffer buffer)
  3. readVarInt(ByteBuffer stream)
  4. readVarIntRest(ByteBuffer buff, int b)
  5. toInt(ByteBuffer buffer)
  6. toInt(ByteBuffer bytes)
  7. toInt(ByteBuffer value)
  8. toInt16BE(ByteBuffer buf, int[] out)