public static byte[] intToByteArray(int value, int len) {
        if (len == 2 || len == 4) {
            if (len == 2 && (value < Short.MIN_VALUE || value > Short.MAX_VALUE)) {
                throw new IllegalArgumentException("Value outside range for signed short int.");
            } else {
                byte[] b = new byte[len];
                for (int i = 0; i < len; i++) {
                    int offset = (b.length - 1 - i) * 8;
                    b[i] = (byte) ((value >>> offset) & 0xFF);
                }
                return b;
            }
        } else {
            throw new IllegalArgumentException(
                    "Length must be specified as either 2 or 4.");
        }
    }
@Test
    public void testIntToByteArray() throws Exception {
        byte[] ba;
        int len = 2;
        ba = TCPClientDecorator.intToByteArray(0, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(0, ba[1]);

        ba = TCPClientDecorator.intToByteArray(15, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(15, ba[1]);

        ba = TCPClientDecorator.intToByteArray(255, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(-1, ba[1]);

        ba = TCPClientDecorator.intToByteArray(256, len);
        assertEquals(len, ba.length);
        assertEquals(1, ba[0]);
        assertEquals(0, ba[1]);

        ba = TCPClientDecorator.intToByteArray(-1, len);
        assertEquals(len, ba.length);
        assertEquals(-1, ba[0]);
        assertEquals(-1, ba[1]);

        ba = TCPClientDecorator.intToByteArray(Short.MAX_VALUE, len);
        assertEquals(len, ba.length);
        assertEquals(127, ba[0]);
        assertEquals(-1, ba[1]);

        ba = TCPClientDecorator.intToByteArray(Short.MIN_VALUE, len);
        assertEquals(len, ba.length);
        assertEquals(-128, ba[0]);
        assertEquals(0, ba[1]);

        try {
            ba = TCPClientDecorator.intToByteArray(Short.MIN_VALUE-1, len);
            fail();
        } catch (IllegalArgumentException iae) {
        }

        try {
            ba = TCPClientDecorator.intToByteArray(Short.MAX_VALUE+1, len);
            fail();
        } catch (IllegalArgumentException iae) {
        }

        len = 4;
        ba = TCPClientDecorator.intToByteArray(0, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(0, ba[1]);
        assertEquals(0, ba[2]);
        assertEquals(0, ba[3]);

        ba = TCPClientDecorator.intToByteArray(15, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(0, ba[1]);
        assertEquals(0, ba[2]);
        assertEquals(15, ba[3]);

        ba = TCPClientDecorator.intToByteArray(255, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(0, ba[1]);
        assertEquals(0, ba[2]);
        assertEquals(-1, ba[3]);

        ba = TCPClientDecorator.intToByteArray(-1, len);
        assertEquals(len, ba.length);
        assertEquals(-1, ba[0]);
        assertEquals(-1, ba[1]);
        assertEquals(-1, ba[2]);
        assertEquals(-1, ba[3]);

        ba = TCPClientDecorator.intToByteArray(256, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(0, ba[1]);
        assertEquals(1, ba[2]);
        assertEquals(0, ba[3]);

        ba = TCPClientDecorator.intToByteArray(65535, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(0, ba[1]);
        assertEquals(-1, ba[2]);
        assertEquals(-1, ba[3]);

        ba = TCPClientDecorator.intToByteArray(65536, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(1, ba[1]);
        assertEquals(0, ba[2]);
        assertEquals(0, ba[3]);

        ba = TCPClientDecorator.intToByteArray(Integer.MIN_VALUE, len);
        assertEquals(len, ba.length);
        assertEquals(-128, ba[0]);
        assertEquals(0, ba[1]);
        assertEquals(0, ba[2]);
        assertEquals(0, ba[3]);

        ba = TCPClientDecorator.intToByteArray(Integer.MAX_VALUE, len);
        assertEquals(len, ba.length);
        assertEquals(127, ba[0]);
        assertEquals(-1, ba[1]);
        assertEquals(-1, ba[2]);
        assertEquals(-1, ba[3]);

        // Check illegal array lengths
        try {
            ba = TCPClientDecorator.intToByteArray(0, 0);
            fail();
        } catch (IllegalArgumentException iae) {
        }

        try {
            ba = TCPClientDecorator.intToByteArray(0, 1);
            fail();
        } catch (IllegalArgumentException iae) {
        }

        try {
            ba = TCPClientDecorator.intToByteArray(0, 3);
            fail();
        } catch (IllegalArgumentException iae) {
        }
        try {
            TCPClientDecorator.intToByteArray(0, 5);
            fail("Expected IllegalArgumentException");
        } catch (IllegalArgumentException iae) {
        }
    }