Java Byte Array Save to File nio writeIntChars(int value, int index, byte[] buf)

Here you can find the source of writeIntChars(int value, int index, byte[] buf)

Description

Write the characters that make up this integer into the buffer Note: index is the ending location in the buffer.

License

Apache License

Declaration

final public static void writeIntChars(int value, int index, byte[] buf) 

Method Source Code


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

import static java.nio.charset.StandardCharsets.UTF_8;

public class Main {
    /**//from  www.j av  a  2s  . c o m
     * All possible chars for representing a number as a String
     */
    final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
            'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
    final static char[] DigitTens = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1',
            '1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3',
            '3', '3', '3', '3', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5', '5',
            '5', '5', '5', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '7', '7', '7', '7', '7', '7', '7', '7',
            '7', '7', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '9', '9', '9', '9', '9', '9', '9', '9', '9',
            '9', };
    final static char[] DigitOnes = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4',
            '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8',
            '9', };
    final static byte[] INT_MIN_VALUE_BYTES = Integer.toString(Integer.MIN_VALUE).getBytes(UTF_8);

    /**
     * Write the characters that make up this integer into the buffer
     * 
     * Note: index is the ending location in the buffer.  Bytes will be written backwards into the buffer.
     */
    final public static void writeIntChars(int value, int index, byte[] buf) {
        if (value == Integer.MIN_VALUE) {
            System.arraycopy(INT_MIN_VALUE_BYTES, 0, buf, index - INT_MIN_VALUE_BYTES.length,
                    INT_MIN_VALUE_BYTES.length);
        } else {
            getIntChars(value, index, buf);
        }
    }

    /**
     * Places characters representing the integer i into the
     * character array buf. The characters are placed into
     * the buffer backwards starting with the least significant
     * digit at the specified index (exclusive), and working
     * backwards from there.
     *
     * Will fail if i == Integer.MIN_VALUE
     */
    final static void getIntChars(int i, int index, byte[] buf) {
        int q, r;
        int charPos = index;
        char sign = 0;

        if (i < 0) {
            sign = '-';
            i = -i;
        }

        // Generate two digits per iteration
        while (i >= 65536) {
            q = i / 100;
            // really: r = i - (q * 100);
            r = i - ((q << 6) + (q << 5) + (q << 2));
            i = q;
            buf[--charPos] = (byte) DigitOnes[r];
            buf[--charPos] = (byte) DigitTens[r];
        }

        // Fall thru to fast mode for smaller numbers
        // assert(i <= 65536, i);
        for (;;) {
            q = (i * 52429) >>> (16 + 3);
            r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
            buf[--charPos] = (byte) digits[r];
            i = q;
            if (i == 0)
                break;
        }
        if (sign != 0) {
            buf[--charPos] = (byte) sign;
        }
    }
}

Related

  1. fileWriteBytes(byte[] data, String file)
  2. writeBytes(final byte[] bytes, final OutputStream output)
  3. writeHeader(File input, ByteArrayOutputStream headerStream)
  4. writeToBinaryFile(String filename, byte[] data)
  5. writeToFile(File file, byte[] content)