public byte[] toByteArray() {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            write(stream);
        } catch (IOException e) {
            // Should not happen as ByteArrayOutputStream does not throw IOException on write
            throw new RuntimeException(e);
        }
        return stream.toByteArray();
    }
@Test
    public void testToByteArray_lt_OP_PUSHDATA1() {
        // < OP_PUSHDATA1
        for (byte len = 1; len < OP_PUSHDATA1; len++) {
            byte[] bytes = new byte[len];
            RANDOM.nextBytes(bytes);
            byte[] expected = ByteUtils.concat(new byte[] { len }, bytes);
            byte[] actual = new ScriptChunk(len, bytes).toByteArray();
            assertArrayEquals(expected, actual);
        }
    }