@Override
    public void write(FieldBase field, Document doc) {
        buf.putShort(Document.SERIALIZED_VERSION);

        //save the position of the length in the buffer
        int lenPos = buf.position();
        // Temporary length, fill in after serialization is done.
        buf.putInt(0);

        doc.getId().serialize(this);

        boolean hasHead = (doc.getFieldCount() != 0);

        byte contents = 0x01; // Indicating we have document type which we always have
        if (hasHead) {
            contents |= 0x2; // Indicate we have header
        }

        buf.put(contents);

        doc.getDataType().serialize(this);
        if (hasHead) {
            write(null, (StructuredFieldValue) doc);
        }

        int finalPos = buf.position();
        buf.position(lenPos);
        buf.putInt(finalPos - lenPos - 4); // Don't include the length itself or the version
        buf.position(finalPos);
    }
Class used for serializing documents on the Vespa 6.x document format.

@author baldersheim
document/src/main/java/com/yahoo/document/serialization/VespaDocumentSerializer6.java