Reallocate a buffer. - Java java.nio

Java examples for java.nio:ByteBuffer

Description

Reallocate a buffer.

Demo Code


//package com.java2s;
import java.lang.reflect.Array;

public class Main {
    public static void main(String[] argv) throws Exception {
        int[] oldBuffer = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        int oldCapacity = 2;
        int newCapacity = 2;
        System.out.println(java.util.Arrays.toString(reallocateBuffer(
                oldBuffer, oldCapacity, newCapacity)));
    }//  w  w  w  .  j  a v a 2  s. c o  m

    /** Reallocate a buffer. */
    public static <T> T[] reallocateBuffer(Class<T> klass, T[] oldBuffer,
            int oldCapacity, int newCapacity) {
        assert (newCapacity > oldCapacity);
        @SuppressWarnings("unchecked")
        T[] newBuffer = (T[]) Array.newInstance(klass, newCapacity);
        if (oldBuffer != null) {
            System.arraycopy(oldBuffer, 0, newBuffer, 0, oldCapacity);
        }
        for (int i = oldCapacity; i < newCapacity; i++) {
            try {
                newBuffer[i] = klass.newInstance();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return newBuffer;
    }

    /** Reallocate a buffer. */
    public static int[] reallocateBuffer(int[] oldBuffer, int oldCapacity,
            int newCapacity) {
        assert (newCapacity > oldCapacity);
        int[] newBuffer = new int[newCapacity];
        if (oldBuffer != null) {
            System.arraycopy(oldBuffer, 0, newBuffer, 0, oldCapacity);
        }
        return newBuffer;
    }

    /** Reallocate a buffer. */
    public static float[] reallocateBuffer(float[] oldBuffer,
            int oldCapacity, int newCapacity) {
        assert (newCapacity > oldCapacity);
        float[] newBuffer = new float[newCapacity];
        if (oldBuffer != null) {
            System.arraycopy(oldBuffer, 0, newBuffer, 0, oldCapacity);
        }
        return newBuffer;
    }

    /**
     * Reallocate a buffer. A 'deferred' buffer is reallocated only if it is not NULL. If
     * 'userSuppliedCapacity' is not zero, buffer is user supplied and must be kept.
     */
    public static <T> T[] reallocateBuffer(Class<T> klass, T[] buffer,
            int userSuppliedCapacity, int oldCapacity, int newCapacity,
            boolean deferred) {
        assert (newCapacity > oldCapacity);
        assert (userSuppliedCapacity == 0 || newCapacity <= userSuppliedCapacity);
        if ((!deferred || buffer != null) && userSuppliedCapacity == 0) {
            buffer = reallocateBuffer(klass, buffer, oldCapacity,
                    newCapacity);
        }
        return buffer;
    }

    /**
     * Reallocate an int buffer. A 'deferred' buffer is reallocated only if it is not NULL. If
     * 'userSuppliedCapacity' is not zero, buffer is user supplied and must be kept.
     */
    public static int[] reallocateBuffer(int[] buffer,
            int userSuppliedCapacity, int oldCapacity, int newCapacity,
            boolean deferred) {
        assert (newCapacity > oldCapacity);
        assert (userSuppliedCapacity == 0 || newCapacity <= userSuppliedCapacity);
        if ((!deferred || buffer != null) && userSuppliedCapacity == 0) {
            buffer = reallocateBuffer(buffer, oldCapacity, newCapacity);
        }
        return buffer;
    }

    /**
     * Reallocate a float buffer. A 'deferred' buffer is reallocated only if it is not NULL. If
     * 'userSuppliedCapacity' is not zero, buffer is user supplied and must be kept.
     */
    public static float[] reallocateBuffer(float[] buffer,
            int userSuppliedCapacity, int oldCapacity, int newCapacity,
            boolean deferred) {
        assert (newCapacity > oldCapacity);
        assert (userSuppliedCapacity == 0 || newCapacity <= userSuppliedCapacity);
        if ((!deferred || buffer != null) && userSuppliedCapacity == 0) {
            buffer = reallocateBuffer(buffer, oldCapacity, newCapacity);
        }
        return buffer;
    }
}

Related Tutorials