Example usage for java.lang System arraycopy

List of usage examples for java.lang System arraycopy

Introduction

In this page you can find the example usage for java.lang System arraycopy.

Prototype

@HotSpotIntrinsicCandidate
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

Source Link

Document

Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

Usage

From source file:Main.java

/**
 * Removes sub-array.// w w  w .j a  v  a2 s. c  o  m
 */
@SuppressWarnings({ "unchecked" })
public static <T> T[] remove(T[] buffer, int offset, int length, Class<T> componentType) {
    int len2 = buffer.length - length;
    T[] temp = (T[]) Array.newInstance(componentType, len2);
    System.arraycopy(buffer, 0, temp, 0, offset);
    System.arraycopy(buffer, offset + length, temp, offset, len2 - offset);
    return temp;
}

From source file:Main.java

public static char[] copyOf(char[] original, int newLength) {
    char[] copy = new char[newLength];
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;/*ww  w.  j  ava2 s .c o m*/
}

From source file:Main.java

/**Concatenate two arrays of the same type and return it as an new array.
 *<p>/*from   ww w .j  av a  2 s  .com*/
 *If the third parameter is big enough to store the return, it will be stored there; otherwise a new array will be created.
 *@param array1 One of the arrays to concatenate.
 *@param array2 The other one of the arrays to concatenate.
 *@param type The class of the copy to be returned.
 *@return The result of concatenating the array1 with the array2 
 */
@SuppressWarnings("unchecked")
public static <T> T[] concatArrays(T[] array1, T[] array2, T[] type) {
    Object[] ret = new Object[array1.length + array2.length];
    System.arraycopy(array1, 0, ret, 0, array1.length);
    System.arraycopy(array2, 0, ret, array1.length, array2.length);
    return Arrays.asList(ret).toArray(type);
}

From source file:Main.java

public static byte[] copyOf(byte[] data, int newLength) {
    byte[] tmp = new byte[newLength];

    if (newLength < data.length) {
        System.arraycopy(data, 0, tmp, 0, newLength);
    } else {// w  w w.  ja v a  2s .c  o  m
        System.arraycopy(data, 0, tmp, 0, data.length);
    }

    return tmp;
}

From source file:Main.java

/**
 * Inflate the given byte array by {@link #INFLATED_ARRAY_LENGTH}.
 *
 * @param bytes the bytes//w w  w  . j av  a2 s  .co  m
 * @return the array as a string with {@code UTF-8} encoding
 */
public static String inflate(final byte[] bytes) {
    final Inflater inflater = new Inflater(true);
    final byte[] xmlMessageBytes = new byte[INFLATED_ARRAY_LENGTH];

    final byte[] extendedBytes = new byte[bytes.length + 1];
    System.arraycopy(bytes, 0, extendedBytes, 0, bytes.length);
    extendedBytes[bytes.length] = 0;

    inflater.setInput(extendedBytes);

    try {
        final int resultLength = inflater.inflate(xmlMessageBytes);
        inflater.end();

        if (!inflater.finished()) {
            throw new RuntimeException("buffer not large enough.");
        }

        inflater.end();
        return new String(xmlMessageBytes, 0, resultLength, StandardCharsets.UTF_8);
    } catch (final DataFormatException e) {
        return null;
    }
}

From source file:Main.java

public static byte[] copyByteArray(byte[] source) {
    byte[] buf = new byte[source.length];
    System.arraycopy(source, 0, buf, 0, buf.length);
    return buf;/*  ww  w  .java  2s  .co m*/
}

From source file:ArrayUtil.java

/**
 * Removes an element from a an array yielding a new array with that data.
 * /* w w w. jav a  2 s. c  o m*/
 * @param elementData
 *            original array
 * @param index
 *            item to remove
 * @return new array sized to fit
 */
public static String[] removeElement(String[] elementData, int index) {
    int size = elementData.length;
    String[] newData = new String[size - 1];

    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index + 1, newData, index, numMoved);

    elementData = null; // let the gc work
    return newData;
}

From source file:Main.java

public static ArrayList<String> retrieveLinks(String text) {
    ArrayList<String> links = new ArrayList<String>();

    String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(text);//from  ww  w.j a  va 2s  . c  o m
    while (m.find()) {
        String urlStr = m.group();

        if (urlStr.startsWith("(") && urlStr.endsWith(")")) {

            char[] stringArray = urlStr.toCharArray();

            char[] newArray = new char[stringArray.length - 2];
            System.arraycopy(stringArray, 1, newArray, 0, stringArray.length - 2);
            urlStr = new String(newArray);
            System.out.println("Finally Url =" + newArray.toString());

        }
        System.out.println("...Url..." + urlStr);
        links.add(urlStr);
    }
    return links;
}

From source file:Main.java

public static byte[] prepareDataToTransmit(String text) {
    if (text.length() == 0)
        return new byte[0];

    byte[] origData = text.getBytes();
    int len = origData.length;

    if (origData[len - 2] == 0x0D && origData[len - 1] == 0x0A) {
        return origData;
    } else {//from ww w  .  j  ava2 s . c o m
        int newLen = len;
        if (origData[len - 2] == 0x0A && origData[len - 1] == 0x0D) {
            newLen = len - 2;
        } else if (origData[len - 1] == 0x0D || origData[len - 1] == 0x0A) {
            newLen = len - 1;
        }
        byte[] nData = new byte[newLen + 2];
        System.arraycopy(origData, 0, nData, 0, newLen);
        nData[newLen + 0] = 0x0D;
        nData[newLen + 1] = 0x0A;
        return nData;
    }
}

From source file:Main.java

/**
 * Inserts one array into another at given offset.
 *//*from   w w  w .  j av  a  2s  . c o  m*/
@SuppressWarnings({ "unchecked" })
public static <T> T[] insertAt(T[] dest, T[] src, int offset, Class componentType) {
    T[] temp = (T[]) Array.newInstance(componentType, dest.length + src.length - 1);
    System.arraycopy(dest, 0, temp, 0, offset);
    System.arraycopy(src, 0, temp, offset, src.length);
    System.arraycopy(dest, offset + 1, temp, src.length + offset, dest.length - offset - 1);
    return temp;
}