public byte[] writeIPTCBlock(List elements) throws ImageWriteException,
            IOException
    {
        byte blockData[];
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            BinaryOutputStream bos = new BinaryOutputStream(baos,
                    getByteOrder());

            // first, right record version record
            bos.write(IPTC_RECORD_TAG_MARKER);
            bos.write(IPTC_APPLICATION_2_RECORD_NUMBER);
            bos.write(IPTC_TYPE_RECORD_VERSION.type); // record version record
                                                        // type.
            bos.write2Bytes(2); // record version record size
            bos.write2Bytes(2); // record version value

            // make a copy of the list.
            elements = new ArrayList(elements);

            // sort the list. Records must be in numerical order.
            Comparator comparator = new Comparator() {
                public int compare(Object o1, Object o2)
                {
                    IPTCRecord e1 = (IPTCRecord) o1;
                    IPTCRecord e2 = (IPTCRecord) o2;
                    return e2.iptcType.type - e1.iptcType.type;
                }
            };
            Collections.sort(elements, comparator);
            // TODO: make sure order right

            // write the list.
            for (int i = 0; i < elements.size(); i++)
            {
                IPTCRecord element = (IPTCRecord) elements.get(i);

                if (element.iptcType.type == IPTC_TYPE_RECORD_VERSION.type)
                    continue; // ignore

                bos.write(IPTC_RECORD_TAG_MARKER);
                bos.write(IPTC_APPLICATION_2_RECORD_NUMBER);
                if (element.iptcType.type < 0 || element.iptcType.type > 0xff)
                    throw new ImageWriteException("Invalid record type: "
                            + element.iptcType.type);
                bos.write(element.iptcType.type);

                byte recordData[] = element.value.getBytes("ISO-8859-1");
                if (!new String(recordData, "ISO-8859-1").equals(element.value))
                    throw new ImageWriteException(
                            "Invalid record value, not ISO-8859-1");

                bos.write2Bytes(recordData.length);
                bos.write(recordData);
            }

            blockData = baos.toByteArray();
        }

        return blockData;
    }