Java Convert via ByteBuffer intToBytes(final int i)

Here you can find the source of intToBytes(final int i)

Description

Converts integer to big-endian byte array.

License

Apache License

Exception

Parameter Description
IllegalArgumentException if integer would need more thanNUM_BYTES_PER_INT to represent correctly.

Declaration

@SuppressWarnings("unused")
public static byte[] intToBytes(final int i) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/**/*w  ww  .  j a v a  2 s .  com*/
 *    Copyright 2014 Galois, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

import java.nio.ByteBuffer;

public class Main {
    private static final int NUM_BYTES_PER_INT = 4;
    private static final int MAX_INT_SIZE = 4;

    /**
     * Converts integer to big-endian byte array. The integer must be less than
     * max 2^*(8bits * {@code NUM_BYTES_PER_INT}) for conversion to work and
     * less than {@code Integer.MAX_VALUE}.
     *
     * @throws IllegalArgumentException if integer is negative.
     * @throws IllegalArgumentException if integer would need more than
     * NUM_BYTES_PER_INT to represent correctly.
     */
    @SuppressWarnings("unused")
    public static byte[] intToBytes(final int i) throws IllegalArgumentException {
        if (i < 0) {
            throw new IllegalArgumentException("Cannot convert negative numbers.");
        }
        // When using fewer than 4 bytes to represent an integer, check
        // that converted integer will fit into expected byte array length.
        if (NUM_BYTES_PER_INT < MAX_INT_SIZE && (i > (2 ^ (8 * NUM_BYTES_PER_INT)))) {
            // We suppress dead-code warning since someone could change
            // NUM_BYTES_PER_INT to be less than MAX_INT_SIZE.
            throw new IllegalArgumentException("Byte array too large");
        }
        // Write bytes corresponding to integer, i, into buffer,
        // making sure to reset position before reading
        ByteBuffer bb = ByteBuffer.allocate(MAX_INT_SIZE).putInt(i);
        bb.rewind();

        byte[] result = new byte[NUM_BYTES_PER_INT];
        bb.get(result, MAX_INT_SIZE - NUM_BYTES_PER_INT, MAX_INT_SIZE);

        return result;
    }
}

Related

  1. intToByte(int[] intArray)
  2. intToByteArray(int integer)
  3. intToByteArray(int l)
  4. intToByteArray(int number)
  5. intToByteArray(int someInt, int byteSize)
  6. intToBytes(final int integer)
  7. intToBytes(final int value)
  8. intToBytes(final int x)
  9. intToBytes(int i, byte[] backingStore, int offset)