public void write(SerializedFile serialized) throws IOException {
        this.serialized = serialized;

        // header is always big endian
        out.order(ByteOrder.BIG_ENDIAN);

        writeHeader(out);

        SerializedFileHeader header = serialized.header();

        // newer formats use little endian for the rest of the file
        if (header.version() > 5) {
            out.order(ByteOrder.LITTLE_ENDIAN);
        }

        // older formats store the object data before the structure data
        if (header.version() < 9) {
            header.dataOffset(0);

            writeObjects(out);
            out.writeUnsignedByte(header.version() > 5 ? 0 : 1);

            writeMetadata(out);
            out.writeUnsignedByte(0);
        } else {
            writeMetadata(out);

            long dataOffset = out.position();

            // calculate padding
            if (dataOffset < META_PADDING) {
                dataOffset = META_PADDING;
            } else {
                dataOffset += META_ALIGN - (dataOffset % META_ALIGN);
            }

            header.dataOffset(dataOffset);

            out.position(dataOffset);
            writeObjects(out);

            // write updated path table
            out.position(serialized.metadata().objectInfoBlock().offset());
            out.writeStruct(serialized.metadata().objectInfoTable());
        }

        // update header
        header.fileSize(out.size());

        // FIXME: the metadata size is slightly off in comparison to original files
        int metadataOffset = header.version() < 9 ? 2 : 1;

        header.metadataSize(serialized.metadataBlock().length() + metadataOffset);

        // write updated header
        out.order(ByteOrder.BIG_ENDIAN);
        out.position(serialized.headerBlock().offset());
        out.writeStruct(header);
    }
@author Nico Bergemann <barracuda415 at yahoo.de>
disunity-core/src/main/java/info/ata4/junity/serialize/SerializedFileWriter.java