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 static String[] split(String s, char c, int limit) { if (s == null) return null; LinkedList pos = new LinkedList(); int i = -1; while ((i = s.indexOf((int) c, i + 1)) > 0) { pos.add(HproseHelper.valueOf(i)); } int n = pos.size(); int[] p = new int[n]; for (i = 0; i < n; i++) { p[i] = ((Integer) pos.get(i)).intValue(); } if ((limit == 0) || (limit > n)) { limit = n + 1; } String[] result = new String[limit]; if (n > 0) { result[0] = s.substring(0, p[0]); } else { result[0] = s; } for (i = 1; i < limit - 1; i++) { result[i] = s.substring(p[i - 1] + 1, p[i]); } if (limit > 1) { result[limit - 1] = s.substring(p[limit - 2] + 1); } return result; }
public static Class getClass(String className) { if (ClassManager.containsClass(className)) { return ClassManager.getClass(className); } StringBuffer cn = new StringBuffer(className); LinkedList al = new LinkedList(); int p = className.indexOf("_"); while (p > -1) { al.add(HproseHelper.valueOf(p)); p = className.indexOf("_", p + 1); } Class type = null; if (al.size() > 0) { try { int size = al.size(); int[] pos = new int[size]; int i = 0; for (Iterator iter = al.iterator(); iter.hasNext(); i++) { pos[i] = ((Integer) iter.next()).intValue(); } type = getClass(cn, pos, 0, '.'); if (type == null) { type = getClass(cn, pos, 0, '_'); } if (type == null) { type = getInnerClass(cn, pos, 0, '$'); } } catch (Exception e) { } } else { try { type = Class.forName(className); } catch (Exception e) { } } ClassManager.register(type, className); return type; }
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); }