public void writeRecord(byte[] record) throws IOException {
        int length = record.length;

        //
        // The record length must be a positive value
        //
        if (length < 1)
            throw new IllegalArgumentException("Record length "+length+" is not valid");

        //
        // Flush the output buffer if there are fewer than 4 bytes remaining
        //
        if (size-next < 4)
            flush();

        //
        // Write the record descriptor
        //
        stream[next++] = (byte)(length>>24);
        stream[next++] = (byte)(length>>16);
        stream[next++] = (byte)(length>>8);
        stream[next++] = (byte)length;

        //
        // Write the record data
        //
        int offset = 0;
        int segment;

        while (offset < length) {
            segment = Math.min(size-next, length-offset);

            if (segment != 0) {
                System.arraycopy(record, offset, stream, next, segment);
                next += segment;
                offset += segment;
            }

            if (offset < length)
                flush();
        }
    }