Chris Wareham said on 04/12/11 13:15:
Hi folks,

I'd like to commit the following changes if that's OK.

Regards,

Chris


I've also reworked the byte array manipulation methods from the Utility
class to use System.arraycopy() rather than a ByteArrayOutputStream.
I've attached a simple test class with my versions of the methods
inline.

Regards,

Chris

public class ArrayTest {
    public static void main(final String[] args) {
        byte[] src = "0123456789".getBytes();
        byte[] insert = "AB".getBytes();

        System.out.println("delete(0, 0)  : '" + new 
String(byteArrayDelete(src, 0, 0)) + "'");
        System.out.println("delete(0, 2)  : '" + new 
String(byteArrayDelete(src, 0, 2)) + "'");
        System.out.println("delete(2, 2)  : '" + new 
String(byteArrayDelete(src, 2, 2)) + "'");
        System.out.println("delete(8, 2)  : '" + new 
String(byteArrayDelete(src, 8, 2)) + "'");
        System.out.println("delete(10, 0) : '" + new 
String(byteArrayDelete(src, 10, 0)) + "'\n");

        System.out.println("insert(0, 0, 0)  : '" + new 
String(byteArrayInsert(src, 0, insert, 0, 0)) + "'");
        System.out.println("insert(0, 0, 1)  : '" + new 
String(byteArrayInsert(src, 0, insert, 0, 1)) + "'");
        System.out.println("insert(0, 1, 1)  : '" + new 
String(byteArrayInsert(src, 0, insert, 1, 1)) + "'");
        System.out.println("insert(0, 0, 2)  : '" + new 
String(byteArrayInsert(src, 0, insert, 0, 2)) + "'");
        System.out.println("insert(8, 0, 2)  : '" + new 
String(byteArrayInsert(src, 8, insert, 0, 2)) + "'");
        System.out.println("insert(10, 0, 2) : '" + new 
String(byteArrayInsert(src, 10, insert, 0, 2)) + "'\n");

        System.out.println("replace(0, 0, 0, 0)  : '" + new 
String(byteArrayReplace(src, 0, 0, insert, 0, 0)) + "'");
        System.out.println("replace(0, 2, 0, 1)  : '" + new 
String(byteArrayReplace(src, 0, 2, insert, 0, 1)) + "'");
        System.out.println("replace(2, 2, 1, 1)  : '" + new 
String(byteArrayReplace(src, 2, 2, insert, 1, 1)) + "'");
        System.out.println("replace(8, 0, 0, 2)  : '" + new 
String(byteArrayReplace(src, 8, 0, insert, 0, 2)) + "'");
        System.out.println("replace(8, 2, 0, 2)  : '" + new 
String(byteArrayReplace(src, 8, 2, insert, 0, 2)) + "'");
        System.out.println("replace(0, 10, 0, 2) : '" + new 
String(byteArrayReplace(src, 0, 10, insert, 0, 2)) + "'");
    }

    /**
     * Return a copy of a source byte array with a region deleted.
     *
     * @param src the source array to copy
     * @param offset the offset of the region to delete
     * @param len the length in bytes of the region to delete
     * @return a copy of the source array with the region deleted
     */
    public static byte[] byteArrayDelete(final byte[] src, final int offset, 
final int len) {
        byte[] dest = new byte[src.length - len];
        System.arraycopy(src, 0, dest, 0, offset);
        System.arraycopy(src, offset + len, dest, offset, src.length - offset - 
len);
        return dest;
    }

    /**
     * Return a copy of a source byte array with a region inserted.
     *
     * @param src the source array to copy
     * @param offset the offset of the region to insert
     * @param insert the array to insert a region of
     * @param insertOffset the offset of the region to insert
     * @param insertLen the length in bytes of the region to insert
     * @return a copy of the source array with the region inserted
     */
    public static byte[] byteArrayInsert(final byte[] src, final int offset, 
final byte[] insert, final int insertOffset, final int insertLen) {
        byte[] dest = new byte[src.length + insertLen];
        System.arraycopy(src, 0, dest, 0, offset);
        System.arraycopy(insert, insertOffset, dest, offset, insertLen);
        System.arraycopy(src, offset, dest, offset + insertLen, src.length - 
offset);
        return dest;
    }

    /**
     * Return a copy of a source byte array with a region replaced.
     *
     * @param src the source array to copy
     * @param offset the offset of the region to replace
     * @param len the length of the region to replace
     * @param insert the array to insert a region of
     * @param insertOffset the offset of the region to insert
     * @param insertLen the length in bytes of the region to insert
     * @return a copy of the source array with the region replaced
     */
    public static byte[] byteArrayReplace(final byte[] src, final int offset, 
final int len, final byte[] insert, final int insertOffset, final int 
insertLen) {
        byte[] dest = new byte[src.length - len + insertLen];
        System.arraycopy(src, 0, dest, 0, offset);
        System.arraycopy(insert, insertOffset, dest, offset, insertLen);
        System.arraycopy(src, offset + len, dest, offset + insertLen, 
src.length - offset - len);
        return dest;
    }
}
------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure 
contains a definitive record of customers, application performance, 
security threats, fraudulent activity, and more. Splunk takes this 
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Jsynthlib-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jsynthlib-devel

Reply via email to