SocketAddressTemplate extends AbstractTemplate<SocketAddress> { @Override public void write(Packer packer, SocketAddress endpoint, boolean required) throws IOException { if (!InetSocketAddress.class.isAssignableFrom(endpoint.getClass())) { throw new IllegalArgumentException(endpoint.getClass().getSimpleName() + " can not be encoded by " + SocketAddressTemplate.class.getSimpleName()); } InetSocketAddress inetEndpoint = (InetSocketAddress) endpoint; packer.writeArrayBegin(2); packer.write(inetEndpoint.getHostName()); packer.write(inetEndpoint.getPort()); packer.writeArrayEnd(); } private  SocketAddressTemplate(); static Template<SocketAddress> getInstance(); @Override void write(Packer packer, SocketAddress endpoint, boolean required); @Override SocketAddress read(Unpacker unpacker, SocketAddress endpoint, boolean required);  }
@Test public void write() throws IOException { String hostname = "localhost"; int port = 3456; MessagePack pack = new MessagePack(); byte[] bytes = pack.write(Arrays.asList(hostname, port)); byte[] result = pack.write(new InetSocketAddress(hostname, port), SocketAddressTemplate.getInstance()); Assert.assertArrayEquals(bytes, result); }