Tuesday, 18 December 2012

My java experience

1. In JUnit, we have assertTrue & assertFalse for evaluating conditions

2. How to split array into array of arrays & join them back

    public static ArrayList<byte[]> Split(byte[] bytes, int size)
    {
        if(bytes == null || bytes.length <= 0) return null;

        int offset = 0;
        ArrayList
<byte[]> splittedByteArray = new ArrayList();

        while (offset < bytes.length)
        {
            byte[] outputBytes;

            if(bytes.length - offset < size )
            {
                outputBytes = new byte[bytes.length - offset];
                System.arraycopy(bytes, offset, outputBytes, 0, bytes.length - offset);
                splittedByteArray.add(outputBytes);

                break;
            }

            outputBytes = new byte[size];
            System.arraycopy(bytes, offset, outputBytes, 0, size);
            splittedByteArray.add(outputBytes);
            offset += size ;
        }

        return splittedByteArray;
     }


     public static byte[] Join(ArrayList<byte[]> bytes)
    {
        if(bytes == null || bytes.size() <= 0) return null;

        int finalByteSize = 0;

        for(int index = 0; index < bytes.size(); index++)
        {
            finalByteSize += bytes.get(index).length;
        }

        byte[] outputBytes = new byte[finalByteSize];
        int desPos = 0;

        for(int index = 0; index < bytes.size(); index++)
        {
            byte[] data = bytes.get(index);

            System.arraycopy(bytes.get(index), 0, outputBytes, desPos, data.length);
            desPos += data.length;
        }

        return outputBytes;
    }

3. ConvertToBase64 & ConvertFromBase64

     public static ArrayList<String> ConvertToBase64(ArrayList<byte[]> byteArray)
    {
        if(byteArray == null || byteArray.isEmpty()) return null;

        ArrayList
<String> base64EncodedStrings = new ArrayList<String>();

        for(int index = 0; index < byteArray.size(); index++)
        {
            String base64encoded = new String(com.lowagie.text.pdf.codec.Base64.encodeBytes(byteArray.get(index)));
            base64EncodedStrings.add(base64encoded);
        }

        return base64EncodedStrings;
    }

    public static ArrayList
<byte[]> CovertFromBase64(ArrayList<String> base64encodedArray)
    {
        if(base64encodedArray == null || base64encodedArray.isEmpty()) return null;

        ArrayList
<byte[]> base64decodedArrays = new ArrayList<byte[]>();

        for(int index = 0; index < base64encodedArray.size(); index++)
        {
            byte[] base64decodedArray = com.lowagie.text.pdf.codec.Base64.decode(base64encodedArray.get(index));
            base64decodedArrays.add(base64decodedArray);
        }

        return base64decodedArrays;
    }


4. How to convert InputStream into byte[], we can use IOUtils.toByteArray(stream). This is part of apache sdk.

5.  ASCII encoding

     String data = "This is to test splitting the bytes";
      byte[] bytes = data.getBytes("US-ASCII");


6. Arrays.equals helps compare two arrays are equal or not(similar to SequenceEqual in c#)