Java Type Size sizeOfPrimitiveArray(Object object)

Here you can find the source of sizeOfPrimitiveArray(Object object)

Description

size Of Primitive Array

License

Open Source License

Declaration


private static long sizeOfPrimitiveArray(Object object) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {

    private static long sizeOfPrimitiveArray(Object object) {
        long total = 0L;

        if (object instanceof Byte[]) {
            for (Byte b : (Byte[]) object)
                if (b != null)
                    total += 1;/*from w  ww . ja v a2 s  .c  om*/
        }

        else if (object instanceof Short[]) {
            for (Short s : (Short[]) object)
                if (s != null)
                    total += 2;
        }

        else if (object instanceof Integer[]) {
            for (Integer i : (Integer[]) object)
                if (i != null)
                    total += 4;
        }

        else if (object instanceof Long[]) {
            for (Long l : (Long[]) object)
                if (l != null)
                    total += 1;
        }

        else if (object instanceof Float[]) {
            for (Float f : (Float[]) object)
                if (f != null)
                    total += 4;
        }

        else if (object instanceof Double[]) {
            for (Double d : (Double[]) object)
                if (d != null)
                    total += 8;
        }

        else if (object instanceof Character[]) {
            for (Character c : (Character[]) object)
                if (c != null)
                    total += c < 255 ? 1 : 2;
        }

        else if (object instanceof String[]) {
            for (String s : (String[]) object)
                if (s != null)
                    total += s.getBytes().length;
        }

        else if (object instanceof Boolean[]) {
            for (Boolean b : (Boolean[]) object)
                if (b != null)
                    total += 1;
        }

        return total;
    }
}

Related

  1. sizeOfFloat(float f)
  2. sizeOfInt()
  3. sizeOfInt(int x)
  4. sizeOfInVarInt32(int length)
  5. sizeOfPrimitive(Object object)
  6. sizeOfTreeMap(int size)
  7. sizeOfVarint(int value)
  8. sizeOfWriteLength(int len)