public byte[] writePhotoshopApp13Segment(final PhotoshopApp13Data data) throws IOException, ImagingException {
        try (ByteArrayOutputStream os = new ByteArrayOutputStream();
                AbstractBinaryOutputStream bos = AbstractBinaryOutputStream.bigEndian(os)) {

            JpegConstants.PHOTOSHOP_IDENTIFICATION_STRING.writeTo(bos);

            final List<IptcBlock> blocks = data.getRawBlocks();
            for (final IptcBlock block : blocks) {
                bos.write4Bytes(JpegConstants.CONST_8BIM);

                if (block.getBlockType() < 0 || block.getBlockType() > 0xffff) {
                    throw new ImagingException("Invalid IPTC block type.");
                }
                bos.write2Bytes(block.getBlockType());

                final byte[] blockNameBytes = block.getBlockNameBytes();
                if (blockNameBytes.length > 255) {
                    throw new ImagingException("IPTC block name is too long: " + blockNameBytes.length);
                }
                bos.write(blockNameBytes.length);
                bos.write(blockNameBytes);
                if (blockNameBytes.length % 2 == 0) {
                    bos.write(0); // pad to even size, including length byte.
                }

                final byte[] blockData = block.getBlockData();
                if (blockData.length > IptcConstants.IPTC_NON_EXTENDED_RECORD_MAXIMUM_SIZE) {
                    throw new ImagingException("IPTC block data is too long: " + blockData.length);
                }
                bos.write4Bytes(blockData.length);
                bos.write(blockData);
                if (blockData.length % 2 == 1) {
                    bos.write(0); // pad to even size
                }
            }

            bos.flush();
            return os.toByteArray();
        }
    }
Constructs a new instance with the default, big-endian, byte order.
src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java