public FieldBufferParser( final FieldBuffer buffer ) { fields = buffer.getFields().iterator(); }
@Override public ByteBuffer toByteBuffer( final FieldBuffer obj ) { //create an output stream final List<ByteBuffer> fields = obj.getFields(); final int fieldCount = fields.size(); //we start with 9. 1 byte for version, 4 bytes for total length, 4 bytes for the field count int totalLength = 9; for ( ByteBuffer fieldData : fields ) { final int bufferLength = fieldData.remaining(); totalLength += 4 + bufferLength; } //now we have our total length, allocate it. ByteBuffer buffer = ByteBuffer.allocate( totalLength ); buffer.put( VERSION ); buffer.putInt( totalLength ); buffer.putInt( fieldCount ); for ( ByteBuffer fieldData : fields ) { final int bufferLength = fieldData.limit(); buffer.putInt( bufferLength ); buffer.put( fieldData ); } buffer.rewind(); return buffer; }