@Test public void splitBytes() { assertThat(Util.splitBytes(thousandThousand), hasItems(thousandint, thousandlong)); }
public static List<byte[]> splitBytes(byte[] bytes) { if (bytes == null) { throw new IllegalArgumentException("bytes cannot be null"); } List<byte[]> answer = new ArrayList<byte[]>(); if (bytes.length != 0) { if (bytes.length > INT) { int offset = 0; while (offset < bytes.length) { int length = Util.bin2int(bytes, offset); if (bytes.length >= offset + length) { offset += INT; byte[] dest = new byte[length]; System.arraycopy(bytes, offset, dest, 0, length); offset += length; answer.add(dest); } else { throw new IllegalArgumentException( "bytes not long enough to contain as much as it says it does"); } } } else { throw new IllegalArgumentException( "bytes must be at least big enough to have one length field in it or 0 length but was: " + bytes.length); } } return answer; }
Util { public static List<byte[]> splitBytes(byte[] bytes) { if (bytes == null) { throw new IllegalArgumentException("bytes cannot be null"); } List<byte[]> answer = new ArrayList<byte[]>(); if (bytes.length != 0) { if (bytes.length > INT) { int offset = 0; while (offset < bytes.length) { int length = Util.bin2int(bytes, offset); if (bytes.length >= offset + length) { offset += INT; byte[] dest = new byte[length]; System.arraycopy(bytes, offset, dest, 0, length); offset += length; answer.add(dest); } else { throw new IllegalArgumentException( "bytes not long enough to contain as much as it says it does"); } } } else { throw new IllegalArgumentException( "bytes must be at least big enough to have one length field in it or 0 length but was: " + bytes.length); } } return answer; } }
Util { public static List<byte[]> splitBytes(byte[] bytes) { if (bytes == null) { throw new IllegalArgumentException("bytes cannot be null"); } List<byte[]> answer = new ArrayList<byte[]>(); if (bytes.length != 0) { if (bytes.length > INT) { int offset = 0; while (offset < bytes.length) { int length = Util.bin2int(bytes, offset); if (bytes.length >= offset + length) { offset += INT; byte[] dest = new byte[length]; System.arraycopy(bytes, offset, dest, 0, length); offset += length; answer.add(dest); } else { throw new IllegalArgumentException( "bytes not long enough to contain as much as it says it does"); } } } else { throw new IllegalArgumentException( "bytes must be at least big enough to have one length field in it or 0 length but was: " + bytes.length); } } return answer; } private  Util(); }
Util { public static List<byte[]> splitBytes(byte[] bytes) { if (bytes == null) { throw new IllegalArgumentException("bytes cannot be null"); } List<byte[]> answer = new ArrayList<byte[]>(); if (bytes.length != 0) { if (bytes.length > INT) { int offset = 0; while (offset < bytes.length) { int length = Util.bin2int(bytes, offset); if (bytes.length >= offset + length) { offset += INT; byte[] dest = new byte[length]; System.arraycopy(bytes, offset, dest, 0, length); offset += length; answer.add(dest); } else { throw new IllegalArgumentException( "bytes not long enough to contain as much as it says it does"); } } } else { throw new IllegalArgumentException( "bytes must be at least big enough to have one length field in it or 0 length but was: " + bytes.length); } } return answer; } private  Util(); static void int2bin(byte[] out, int offset, int i); static byte[] int2bin(int i); static void long2bin(byte[] out, int offset, long i); static byte[] long2bin(long i); static byte[] double2bin(double d); static void double2bin(byte[] out, int offset, double d); static byte[] boolean2bin(boolean b); static void boolean2bin(byte[] out, int offset, boolean b); static int bin2int(byte[] array, int offset); static long bin2long(byte[] array, int offset); static long bin2long(byte[] array); static double bin2double(byte[] array, int offset); static double bin2double(byte[] array); static boolean bin2boolean(byte[] array, int offset); static boolean bin2boolean(byte[] array); static byte[] twosComplimentToPositiveInt(byte[] input); static byte[] positiveIntToTwosCompliment(byte[] input); static BigInteger byteToBigInt(byte[] input); static byte[] bigIntToByte(BigInteger input); static byte[] joinBytes(byte[]... bytes); static List<byte[]> splitBytes(byte[] bytes); static byte[][] listByte2Array(List<byte[]> bytes); static int compareByteArrays(byte[] first, byte[] second); static void checkPassword(final String password); static void checkUsername(final String username); static boolean deleteDir(File dir); static void addFrom(Throwable t, Throwable from); static byte[] hashKey(byte[] publicKey); }
Util { public static List<byte[]> splitBytes(byte[] bytes) { if (bytes == null) { throw new IllegalArgumentException("bytes cannot be null"); } List<byte[]> answer = new ArrayList<byte[]>(); if (bytes.length != 0) { if (bytes.length > INT) { int offset = 0; while (offset < bytes.length) { int length = Util.bin2int(bytes, offset); if (bytes.length >= offset + length) { offset += INT; byte[] dest = new byte[length]; System.arraycopy(bytes, offset, dest, 0, length); offset += length; answer.add(dest); } else { throw new IllegalArgumentException( "bytes not long enough to contain as much as it says it does"); } } } else { throw new IllegalArgumentException( "bytes must be at least big enough to have one length field in it or 0 length but was: " + bytes.length); } } return answer; } private  Util(); static void int2bin(byte[] out, int offset, int i); static byte[] int2bin(int i); static void long2bin(byte[] out, int offset, long i); static byte[] long2bin(long i); static byte[] double2bin(double d); static void double2bin(byte[] out, int offset, double d); static byte[] boolean2bin(boolean b); static void boolean2bin(byte[] out, int offset, boolean b); static int bin2int(byte[] array, int offset); static long bin2long(byte[] array, int offset); static long bin2long(byte[] array); static double bin2double(byte[] array, int offset); static double bin2double(byte[] array); static boolean bin2boolean(byte[] array, int offset); static boolean bin2boolean(byte[] array); static byte[] twosComplimentToPositiveInt(byte[] input); static byte[] positiveIntToTwosCompliment(byte[] input); static BigInteger byteToBigInt(byte[] input); static byte[] bigIntToByte(BigInteger input); static byte[] joinBytes(byte[]... bytes); static List<byte[]> splitBytes(byte[] bytes); static byte[][] listByte2Array(List<byte[]> bytes); static int compareByteArrays(byte[] first, byte[] second); static void checkPassword(final String password); static void checkUsername(final String username); static boolean deleteDir(File dir); static void addFrom(Throwable t, Throwable from); static byte[] hashKey(byte[] publicKey); static final int INT; static final int LONG; static final int DOUBLE; static final int BOOLEAN; }