List of usage examples for java.lang System arraycopy
@HotSpotIntrinsicCandidate public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
From source file:ArrayExpander.java
public static Object merge(Object array1, Object array2) { if (array1 == null) { return null; }/*from w w w . j a v a 2 s .com*/ if (array2 == null) { return array1; } Class c = array1.getClass(); if (c.isArray() && array2.getClass().isArray()) { Class cc = c.getComponentType(); Object newArray = Array.newInstance(cc, Array.getLength(array1) + Array.getLength(array2)); System.arraycopy(array1, 0, newArray, 0, Array.getLength(array1)); System.arraycopy(array2, 0, newArray, Array.getLength(array1), Array.getLength(array2)); return newArray; } else { throw new ClassCastException("need array"); } }
From source file:Utils.java
/** * Gets the subarray of length <tt>length</tt> from <tt>array</tt> * that starts at <tt>offset</tt>. *//*from w w w. j a v a 2 s .com*/ public static byte[] get(byte[] array, int offset, int length) { byte[] result = new byte[length]; System.arraycopy(array, offset, result, 0, length); return result; }
From source file:Main.java
/** * A generic method for gluing lists of objects. * * @param first//from w w w . java2 s . c o m * the first array. * @param rest * the rest of the arrays. * @return */ public static <T> T[] arrCat(T[] first, T[]... rest) { int ttlLen = first.length; for (T[] arr : rest) { ttlLen += arr.length; } T[] result = Arrays.copyOf(first, ttlLen); int currOfst = first.length; for (T[] arr : rest) { System.arraycopy(arr, 0, result, currOfst, arr.length); currOfst += arr.length; } return result; }
From source file:Main.java
public static <T> T[] addAll(T[]... arrays) { if (arrays.length == 1) { return arrays[0]; }//from w ww . j ava 2 s . c om int length = 0; for (T[] array : arrays) { if (array == null) { continue; } length += array.length; } T[] result = newArray(arrays.getClass().getComponentType().getComponentType(), length); length = 0; for (T[] array : arrays) { if (array == null) { continue; } System.arraycopy(array, 0, result, length, array.length); length += array.length; } return result; }
From source file:Main.java
static public final String readPassword(OutputStream out, InputStream in) throws IOException { int rd = in.read(); if (rd == -1) return null; byte r = (byte) rd; int i = 0;//w ww. java2 s. c o m int l = 50; byte[] buf = new byte[l]; while (r != '\n') { if (i >= l - 1) { l += 50; byte[] old = buf; buf = new byte[l]; System.arraycopy(old, 0, buf, 0, old.length); } if (r != '\r') { buf[i++] = r; out.write('\b'); out.write('*'); out.flush(); } rd = in.read(); if (rd == -1) break; r = (byte) rd; } return new String(buf, 0, i); }
From source file:Main.java
/** * Returns the remainder byte array from the given index. The byte value on the * given index is <b>not</b> part of the result array. * * Returns an empty array, if the index is not within the array or it points to * the last element in the array./*from w w w . j a va 2 s . c om*/ * * @param array * @param fromIndex * @return */ public static byte[] subByteArray(byte[] array, int fromIndex) { if (fromIndex >= 0 && fromIndex < array.length - 1) { byte[] output = new byte[array.length - fromIndex - 1]; System.arraycopy(array, fromIndex + 1, output, 0, array.length - fromIndex - 1); return output; } return new byte[0]; }
From source file:com.ericgithinji.java.algorithms.sort.MergeSort.java
public static int[] mergeSort(int[] array) { if (array.length == 1) return array; int newLength = array.length / 2; int[] leftArray = new int[newLength]; int[] rightArray = new int[newLength]; System.arraycopy(array, 0, leftArray, 0, newLength); System.arraycopy(array, newLength, rightArray, 0, newLength); leftArray = mergeSort(leftArray);/*from ww w .ja va 2 s . co m*/ rightArray = mergeSort(rightArray); return merge(leftArray, rightArray); }
From source file:Main.java
/** * Returns the given array if newsize is the same as existing. * Returns a new array of given size, containing as many elements of * the original array as it can hold./*from w ww . j a va2 s .c o m*/ */ public static Object resizeArrayIfDifferent(Object source, int newsize) { int oldsize = Array.getLength(source); if (oldsize == newsize) { return source; } Object newarray = Array.newInstance(source.getClass().getComponentType(), newsize); if (oldsize < newsize) { newsize = oldsize; } System.arraycopy(source, 0, newarray, 0, newsize); return newarray; }
From source file:Main.java
public static byte[] duplicateArray(byte[] array) { byte[] copy = new byte[array.length]; System.arraycopy(array, 0, copy, 0, array.length); return copy;/*from w w w. jav a2 s. c o m*/ }
From source file:Main.java
public static byte[] prependXmlDeclaration(byte[] doc) throws Exception { if (!hasValidXmlDeclaration(doc)) { byte[] newdoc = new byte[doc.length + XML_DECL.length]; System.arraycopy(XML_DECL, 0, newdoc, 0, XML_DECL.length); System.arraycopy(doc, 0, newdoc, XML_DECL.length, doc.length); return newdoc; } else {/*from www .j ava 2s . com*/ return doc; } }