SimpleMapSerializer { public byte[] encode(Map<String, String> map) throws SerializationException { if (map == null || map.isEmpty()) { return null; } UnsafeByteArrayOutputStream out = new UnsafeByteArrayOutputStream(64); try { for (Map.Entry<String, String> entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (key != null && value != null) { writeString(out, key); writeString(out, value); } } return out.toByteArray(); } catch (IOException ex) { throw new SerializationException(ex.getMessage(), ex); } }  byte[] encode(Map<String, String> map); Map<String, String> decode(byte[] bytes); int readInt(InputStream in);  }
@Test public void encode() throws Exception { SimpleMapSerializer simpleMapSerializer = new SimpleMapSerializer(); Map<String, String> map = null; byte[] bs = simpleMapSerializer.encode(map); Assert.assertEquals(bs, null); map = new HashMap<String, String>(); bs = simpleMapSerializer.encode(map); Assert.assertEquals(bs, null); map.put("1", "2"); map.put("", "x"); map.put("a", ""); map.put("b", null); bs = simpleMapSerializer.encode(map); Assert.assertEquals(28, bs.length); Map<String, String> map1 = simpleMapSerializer.decode(bs); Assert.assertNotNull(map1); Assert.assertEquals(3, map1.size()); Assert.assertEquals("2", map1.get("1")); Assert.assertEquals("x", map1.get("")); Assert.assertEquals("", map1.get("a")); Assert.assertNull(map1.get("b")); map1 = simpleMapSerializer.decode(null); Assert.assertNotNull(map1); Assert.assertEquals(0, map1.size()); map1 = simpleMapSerializer.decode(new byte[0]); Assert.assertNotNull(map1); Assert.assertEquals(0, map1.size()); }