Java Array Capacity ensureCapacity(byte[] bytes, int capacity)

Here you can find the source of ensureCapacity(byte[] bytes, int capacity)

Description

Utility method used to ensure the capacity of byte array.

License

Apache License

Parameter

Parameter Description
bytes a parameter
size a parameter
factor a parameter

Declaration

private static byte[] ensureCapacity(byte[] bytes, int capacity) 

Method Source Code

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

import java.util.Arrays;

public class Main {
    /**/*from w w  w  .  j av a2s. co m*/
     * Utility method used to ensure the capacity of byte array.
     * 
     * @param bytes
     * @param size
     * @param factor
     * @return
     */
    private static byte[] ensureCapacity(byte[] bytes, int capacity) {
        if (bytes.length < capacity) {
            int newsize = Math.max(1, bytes.length) << 1;
            bytes = Arrays.copyOf(bytes, newsize);
        }
        return bytes;
    }
}

Related

  1. ensureCapacity(byte array[], int capacity)
  2. ensureCapacity(final double[] array, final int minCapacity)
  3. ensureCapacity(final T[] oldElements, final int requiredLength)
  4. reduceCapacity(Object[] arrays)