ServiceInfoTemplate extends AbstractTemplate<ServiceInfo> { @Override public ServiceInfo read(Unpacker unpacker, ServiceInfo service, boolean required) throws IOException { unpacker.readArrayBegin(); SocketAddress endpoint = unpacker.read(SocketAddressTemplate.getInstance()); unpacker.readInt(); Map<Integer, String> api = unpacker.read(Templates.tMap(Templates.TInteger, Templates.TString)); unpacker.readArrayEnd(); return new ServiceInfo(name, endpoint, ServiceApi.of(name, ImmutableBiMap.copyOf(api).inverse())); } private  ServiceInfoTemplate(String name); static Template<ServiceInfo> create(String name); @Override void write(Packer packer, ServiceInfo service, boolean required); @Override ServiceInfo read(Unpacker unpacker, ServiceInfo service, boolean required);  }
@Test public void read() throws IOException { String service = "locator"; SocketAddress endpoint = new InetSocketAddress("localhost", 3456); long session = 0L; Map<Integer, String> api = Collections.singletonMap(0, "invoke"); MessagePack pack = new MessagePack(); pack.register(SocketAddress.class, SocketAddressTemplate.getInstance()); byte[] bytes = pack.write(Arrays.asList(endpoint, session, api)); ServiceInfo result = pack.read(bytes, ServiceInfoTemplate.create(service)); Assert.assertEquals(endpoint, result.getEndpoint()); Assert.assertEquals(0, result.getApi().getMethod("invoke")); }