Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package util; import io.netty.buffer.ByteBuf; import java.io.IOException; import org.jboss.marshalling.ByteOutput; import org.jboss.marshalling.Marshaller; /** * * @author allen */ public class MarshallingEncoder { private static final byte[] LENGTH_PLACEHOLDER = new byte[4]; private Marshaller marshaller; public MarshallingEncoder() throws IOException { marshaller = MarshallingCodecFactory.buildMarshalling(); } //MarshallingEncoder() protected void encode(Object msg, ByteBuf out) throws Exception { try { int lengthPos = out.writerIndex(); out.writeBytes(LENGTH_PLACEHOLDER); ByteOutput output = new ChannelBufferByteOutput(out); marshaller.start(output); marshaller.writeObject(msg); marshaller.finish(); out.setInt(lengthPos, out.writerIndex() - lengthPos - 4); } finally { marshaller.close(); } //try-finally } //encode() } //MarshallingEncoder