Java Array Size Get arraySizeOf(int[] array)

Here you can find the source of arraySizeOf(int[] array)

Description

array Size Of

License

Apache License

Declaration

public static final long arraySizeOf(int[] array) 

Method Source Code

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

public class Main {
    /** The number of bytes used to represent a byte. */
    public static final int SIZE_OF_BYTE = 1;
    /** The number of bytes used to represent a short. */
    public static final int SIZE_OF_SHORT = 2;
    /** The number of bytes used to represent a int. */
    public static final int SIZE_OF_INT = 4;
    /** The number of bytes used to represent a long. */
    public static final int SIZE_OF_LONG = 8;
    /** The number of bytes used to represent a boolean. */
    public static final int SIZE_OF_BOOLEAN = 1;
    /** The number of bytes used to represent a char. */
    public static final int SIZE_OF_CHAR = 2;
    /** The number of bytes used to represent a byte. */
    public static final int SIZE_OF_FLOAT = 4;
    /** The number of bytes used to represent a byte. */
    public static final int SIZE_OF_DOUBLE = 8;
    /** The number of bytes used to represent a pointer field */
    // 32 bits -> 4; 64 bits -> 8
    public static final int SIZE_OF_REFERENCE = 4 * Integer.getInteger("sun.arch.data.model", 32) / 32;
    /** The number of bytes used to represent an object (with no fields). */
    public static final int SIZE_OF_OBJECT_INSTANCE = 8;

    public static final long arraySizeOf(int[] array) {
        if (array == null)
            return 0;
        return calArraySize(array.length, SIZE_OF_INT);
    }/* w w  w.ja va2 s. co  m*/

    public static final long arraySizeOf(long[] array) {
        if (array == null)
            return 0;
        return calArraySize(array.length, SIZE_OF_LONG);
    }

    public static final long arraySizeOf(byte[] array) {
        if (array == null)
            return 0;
        return calArraySize(array.length, SIZE_OF_BYTE);
    }

    public static final long arraySizeOf(short[] array) {
        if (array == null)
            return 0;
        return calArraySize(array.length, SIZE_OF_SHORT);
    }

    public static final long arraySizeOf(boolean[] array) {
        if (array == null)
            return 0;
        return calArraySize(array.length, SIZE_OF_BOOLEAN);
    }

    public static final long arraySizeOf(char[] array) {
        if (array == null)
            return 0;
        return calArraySize(array.length, SIZE_OF_CHAR);
    }

    public static final long arraySizeOf(float[] array) {
        if (array == null)
            return 0;
        return calArraySize(array.length, SIZE_OF_FLOAT);
    }

    public static final long arraySizeOf(double[] array) {
        if (array == null)
            return 0;
        return calArraySize(array.length, SIZE_OF_DOUBLE);
    }

    public static final long arraySizeOf(Object[] array) {
        if (array == null)
            return 0;
        return calArraySize(array.length, SIZE_OF_REFERENCE);
    }

    static final long calArraySize(int arrayLen, int contentSize) {
        long size = (long) arrayLen * contentSize;
        size += SIZE_OF_INT;
        return asInstance(size);
    }

    public static final long asInstance(long size) {
        size += SIZE_OF_OBJECT_INSTANCE;
        return alignSize(size);
    }

    protected static final long alignSize(long size) {
        long i = size % SIZE_OF_OBJECT_INSTANCE;
        if (i == 0) {
            return size;
        }
        return size + SIZE_OF_OBJECT_INSTANCE - i;
    }
}

Related

  1. arraySize(final int expected, final float f)
  2. arraySize(final int expected, final float f)