public synchronized Buffer marshal()

in openwire-core/src/main/java/org/apache/activemq/openwire/codec/OpenWireFormat.java [114:165]


    public synchronized Buffer marshal(Object command) throws IOException {
        if (cacheEnabled) {
            runMarshallCacheEvictionSweep();
        }

        Buffer sequence = null;
        int size = 1;
        if (command != null) {
            DataStructure c = (DataStructure) command;
            byte type = c.getDataStructureType();
            DataStreamMarshaller dsm = dataMarshallers[type & 0xFF];
            if (dsm == null) {
                throw new IOException("Unknown data type: " + type);
            }
            if (tightEncodingEnabled) {
                BooleanStream bs = new BooleanStream();
                size += dsm.tightMarshal1(this, c, bs);
                size += bs.marshalledSize();

                bytesOut.restart(size);
                if (!sizePrefixDisabled) {
                    bytesOut.writeInt(size);
                }
                bytesOut.writeByte(type);
                bs.marshal(bytesOut);
                dsm.tightMarshal2(this, c, bytesOut, bs);
                sequence = bytesOut.toBuffer();
            } else {
                bytesOut.restart();
                if (!sizePrefixDisabled) {
                    // we don't know the final size yet but write this here for now.
                    bytesOut.writeInt(0);
                }
                bytesOut.writeByte(type);
                dsm.looseMarshal(this, c, bytesOut);

                if (!sizePrefixDisabled) {
                    size = bytesOut.size() - 4;
                    bytesOut.writeInt(0, size);
                }

                sequence = bytesOut.toBuffer();
            }
        } else {
            bytesOut.restart(5);
            bytesOut.writeInt(size);
            bytesOut.writeByte(NULL_TYPE);
            sequence = bytesOut.toBuffer();
        }

        return sequence;
    }