Java ByteBuffer Put putDecInt(ByteBuffer buffer, int n)

Here you can find the source of putDecInt(ByteBuffer buffer, int n)

Description

put Dec Int

License

Open Source License

Declaration

public static void putDecInt(ByteBuffer buffer, int n) 

Method Source Code

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

import java.nio.ByteBuffer;

public class Main {
    static final byte[] DIGIT = { (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5',
            (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D',
            (byte) 'E', (byte) 'F' };
    private final static int[] decDivisors = { 1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100,
            10, 1 };//from   ww w  . j a  va2s .  c  o m

    public static void putDecInt(ByteBuffer buffer, int n) {
        if (n < 0) {
            buffer.put((byte) '-');

            if (n == Integer.MIN_VALUE) {
                buffer.put((byte) '2');
                n = 147483648;
            } else
                n = -n;
        }

        if (n < 10) {
            buffer.put(DIGIT[n]);
        } else {
            boolean started = false;
            // This assumes constant time int arithmatic
            for (int decDivisor : decDivisors) {
                if (n < decDivisor) {
                    if (started)
                        buffer.put((byte) '0');
                    continue;
                }

                started = true;
                int d = n / decDivisor;
                buffer.put(DIGIT[d]);
                n = n - d * decDivisor;
            }
        }
    }

    /**
     * Put data from one buffer into another, avoiding over/under flows
     * @param from Buffer to take bytes from in flush mode
     * @param to   Buffer to put bytes to in fill mode.
     * @return number of bytes moved
     */
    public static int put(ByteBuffer from, ByteBuffer to) {
        int put;
        int remaining = from.remaining();
        if (remaining > 0) {
            if (remaining <= to.remaining()) {
                to.put(from);
                put = remaining;
                from.position(from.limit());
            } else if (from.hasArray()) {
                put = to.remaining();
                to.put(from.array(), from.arrayOffset() + from.position(), put);
                from.position(from.position() + put);
            } else {
                put = to.remaining();
                ByteBuffer slice = from.slice();
                slice.limit(put);
                to.put(slice);
                from.position(from.position() + put);
            }
        } else
            put = 0;

        return put;
    }
}

Related

  1. putByteArray(ByteBuffer byteBuffer, byte[] array)
  2. putByteAsString(ByteBuffer buffer, byte value)
  3. putByteBuffer(ByteBuffer source, ByteBuffer target)
  4. putBytesUnescaped(ByteBuffer buffer, byte[] data)
  5. putCharSequence(ByteBuffer buf, Charset charset, CharSequence value)
  6. putInt(ByteBuffer bb, long value)
  7. putInt(ByteBuffer buffer, int val, ByteOrder order)
  8. putInt(final ByteBuffer buffer, int value)
  9. putInt(int i, ByteBuffer buffer)