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:Main.java
/** * Merge byte arraies./*from w w w .j ava 2 s .com*/ * * @param array1 the array1 * @param array2 the array2 * * @return the byte[] */ public static byte[] mergeByteArraies(byte[] array1, byte[] array2) { if (array1 == null) { return array2; } else if (array2 == null) { return array1; } else { byte[] result = new byte[array1.length + array2.length]; System.arraycopy(array1, 0, result, 0, array1.length); System.arraycopy(array2, 0, result, array1.length, array2.length); return result; } }
From source file:ArrayExpander.java
public static Object expand(Object array, int newSize) { if (array == null) { return null; }//from w w w .j av a 2 s . c om Class c = array.getClass(); if (c.isArray()) { int len = Array.getLength(array); if (len >= newSize) { return array; } else { Class cc = c.getComponentType(); Object newArray = Array.newInstance(cc, newSize); System.arraycopy(array, 0, newArray, 0, len); return newArray; } } else { throw new ClassCastException("need array"); } }
From source file:Main.java
public static void runMain(Object main, String[] args, String defCommand) throws Exception { if (args.length > 0) { String command = args[0]; Method[] methods = main.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method.getName().equals(command)) { String[] remaining = new String[args.length - 1]; System.arraycopy(args, 1, remaining, 0, remaining.length); try { method.invoke(main, bindParameters(method, remaining)); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); if (cause instanceof IllegalArgumentException) { System.err.println("Syntax error: " + cause.getMessage()); } else if (cause instanceof Exception) { throw (Exception) cause; } else { throw e; }//from ww w . j ava2s. c o m } return; } } } if (defCommand != null) { runMain(main, new String[] { defCommand }, null); } }
From source file:Main.java
public static String[] addToArray(String[] array, String newElement) { int length = array == null ? 1 : array.length + 1; String[] newArray = new String[length]; if (array != null) { System.arraycopy(array, 0, newArray, 0, array.length); newArray[array.length] = newElement; } else {//from ww w.ja va 2 s .co m newArray[0] = newElement; } return newArray; }
From source file:Main.java
public static void printFile(InputStream is, String path) { FileOutputStream fos = null;//from w w w .j a va2 s. com byte[] temp = null; try { if (isPrint) { File file = new File(path); if (!file.exists()) { file.createNewFile(); } fos = new FileOutputStream(file); temp = new byte[1024]; int i = 0; while ((i = is.read(temp)) > -1) { if (i < temp.length) { byte[] b = new byte[i]; System.arraycopy(temp, 0, b, 0, b.length); fos.write(b); } else { fos.write(temp); } } } } catch (Exception e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } is = null; } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } fos = null; } temp = null; } }
From source file:Main.java
/** * Appends an element to a copy of the array and returns the copy. * @param array The original array, or null to represent an empty array. * @param element The element to add.//from www .jav a 2s. c o m * @return A new array that contains all of the elements of the original array * with the specified element added at the end. */ @SuppressWarnings("unchecked") public static <T> T[] appendElement(Class<T> kind, T[] array, T element) { final T[] result; final int end; if (array != null) { end = array.length; result = (T[]) Array.newInstance(kind, end + 1); System.arraycopy(array, 0, result, 0, end); } else { end = 0; result = (T[]) Array.newInstance(kind, 1); } result[end] = element; return result; }
From source file:Main.java
public static short[] clone(short[] data) { if (data == null) { return null; }/* w w w . j ava 2s . c o m*/ short[] copy = new short[data.length]; System.arraycopy(data, 0, copy, 0, data.length); return copy; }
From source file:Main.java
public static int[] copyOf(int[] data, int newLength) { int[] tmp = new int[newLength]; if (newLength < data.length) { System.arraycopy(data, 0, tmp, 0, newLength); } else {/*from w w w.j a va2 s.c o m*/ System.arraycopy(data, 0, tmp, 0, data.length); } return tmp; }
From source file:Main.java
public static byte[] bmp2bytesStream(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); if (width % 8 != 0) { int adjustWidth = width + (8 - width % 8); final Bitmap.Config config = Bitmap.Config.ARGB_8888; Bitmap whiteBgBitmap = Bitmap.createBitmap(adjustWidth, height, config); Canvas canvas = new Canvas(whiteBgBitmap); canvas.drawColor(Color.WHITE); canvas.drawBitmap(bitmap, 0, 0, null); bitmap = whiteBgBitmap;/*from ww w . j a v a2 s . co m*/ width = bitmap.getWidth(); } int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); byte[] image = new byte[width * height]; final byte WHITE = 0, BLACK = 1; for (int i = 0; i < pixels.length; i++) { image[i] = (pixels[i] != 0xFFFFFFFF) ? BLACK : WHITE; } final int COL = width + width % 2; byte[] row = new byte[COL]; byte[] res = new byte[COL / 8 * height]; for (int i = 0, dex = 0, num = 0; i < height; ++i) { System.arraycopy(image, i * width, row, 0, width); for (byte e : row) { res[dex] += e << (7 - num++); num = 8 == num ? 0 : num; dex = 0 == num ? dex + 1 : dex; } } return res; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <E> E[] array(Class<E> elementClass, Object existingArray, E... elements) { if (existingArray == null) { return elements; }// w w w . ja va2 s . c o m E[] result; if (existingArray.getClass().isArray()) { int length = Array.getLength(existingArray); result = (E[]) Array.newInstance(elementClass, length + elements.length); System.arraycopy(existingArray, 0, result, 0, length); System.arraycopy(elements, 0, result, length, elements.length); } else { result = (E[]) Array.newInstance(elementClass, 1 + elements.length); result[0] = (E) existingArray; System.arraycopy(elements, 0, result, 1, elements.length); } return result; }