private int writeClass(Class<?> type) throws IOException { SerializeCache cache = getSerializeCache(type, mode); if (cache == null) { cache = new SerializeCache(); ByteArrayOutputStream cachestream = new ByteArrayOutputStream(); Map<String, MemberAccessor> members = HproseHelper.getMembers(type, mode); int count = members.size(); cachestream.write(HproseTags.TagClass); writeUTF8String(HproseHelper.getClassName(type), cachestream); if (count > 0) { writeInt(count, cachestream); } cachestream.write(HproseTags.TagOpenbrace); for (Entry<String, MemberAccessor> member : members.entrySet()) { cachestream.write(HproseTags.TagString); writeUTF8String(member.getKey(), cachestream); ++cache.refcount; } cachestream.write(HproseTags.TagClosebrace); cache.data = cachestream.toByteArray(); putSerializeCache(type, mode, cache); } stream.write(cache.data); lastref += cache.refcount; int cr = lastclassref++; classref.put(type, cr); return cr; }
public void writeObject(Object object) throws IOException { Class<?> type = object.getClass(); int cr = classref.get(type); if (cr < 0) { cr = writeClass(type); } ref.put(object, lastref++); Map<String, MemberAccessor> members = HproseHelper.getMembers(type, mode); stream.write(HproseTags.TagObject); writeInt(cr); stream.write(HproseTags.TagOpenbrace); for (Entry<String, MemberAccessor> entry : members.entrySet()) { MemberAccessor member = entry.getValue(); Object value; try { value = member.get(object); } catch (Exception e) { throw new HproseException(e.getMessage()); } serialize(value, member.typecode); } stream.write(HproseTags.TagClosebrace); }