static Span readSpan(MessageUnpacker unpacker) throws IOException {
  int numEntries=unpacker.unpackMapHeader();
  MilliSpan.Builder builder=new MilliSpan.Builder();
  while (--numEntries >= 0) {
    String key=unpacker.unpackString();
    if (key.length() != 1) {
      throw new IOException("Unknown key " + key);
    }
switch (key.charAt(0)) {
case 'a':
      builder.spanId(readSpanId(unpacker));
    break;
case 'b':
  builder.begin(unpacker.unpackLong());
break;
case 'e':
builder.end(unpacker.unpackLong());
break;
case 'd':
builder.description(unpacker.unpackString());
break;
case 'r':
builder.tracerId(unpacker.unpackString());
break;
case 'p':
int numParents=unpacker.unpackArrayHeader();
SpanId[] parents=new SpanId[numParents];
for (int i=0; i < numParents; i++) {
parents[i]=readSpanId(unpacker);
}
builder.parents(parents);
break;
case 'n':
int mapEntries=unpacker.unpackMapHeader();
HashMap<String,String> entries=new HashMap<String,String>(mapEntries);
for (int i=0; i < mapEntries; i++) {
String k=unpacker.unpackString();
String v=unpacker.unpackString();
entries.put(k,v);
}
builder.traceInfo(entries);
break;
case 't':
int listEntries=unpacker.unpackArrayHeader();
ArrayList<TimelineAnnotation> list=new ArrayList<TimelineAnnotation>(listEntries);
for (int i=0; i < listEntries; i++) {
int timelineObjectSize=unpacker.unpackMapHeader();
long time=0;
String msg="";
for (int j=0; j < timelineObjectSize; j++) {
String tlKey=unpacker.unpackString();
if (tlKey.length() != 1) {
throw new IOException("Unknown timeline map key " + tlKey);
}
switch (tlKey.charAt(0)) {
case 't':
time=unpacker.unpackLong();
break;
case 'm':
msg=unpacker.unpackString();
break;
default :
throw new IOException("Unknown timeline map key " + tlKey);
}
}
list.add(new TimelineAnnotation(time,msg));
}
builder.timeline(list);
break;
default :
throw new IOException("Unknown key " + key);
}
}
return builder.build();
}

static Span readSpan ( MessageUnpacker unpacker ) throws IOException { int numEntries = unpacker . unpackMapHeader ( ) ; MilliSpan . Builder builder = new MilliSpan . Builder ( ) ; while ( -- numEntries >= 0 ) { String key = unpacker . unpackString ( ) ; if ( key . length ( ) != 1 ) { throw new IOException ( "Unknown key " + key ) ; } switch ( key . charAt ( 0 ) ) { case 'a' : builder . spanId ( readSpanId ( unpacker ) ) ; break ; case 'b' : builder . begin ( unpacker . unpackLong ( ) ) ; break ; case 'e' : builder . end ( unpacker . unpackLong ( ) ) ; break ; case 'd' : builder . description ( unpacker . unpackString ( ) ) ; break ; case 'r' : builder . tracerId ( unpacker . unpackString ( ) ) ; break ; case 'p' : int numParents = unpacker . unpackArrayHeader ( ) ; SpanId [ ] parents = new SpanId [ numParents ] ; for ( int i = 0 ; i < numParents ; i ++ ) { parents [ i ] = readSpanId ( unpacker ) ; } builder . parents ( parents ) ; break ; case 'n' : int mapEntries = unpacker . unpackMapHeader ( ) ; HashMap < String , String > entries = new HashMap < String , String > ( mapEntries ) ; for ( int i = 0 ; i < mapEntries ; i ++ ) { String k = unpacker . unpackString ( ) ; String v = unpacker . unpackString ( ) ; entries . put ( k , v ) ; } builder . traceInfo ( entries ) ; break ; case 't' : int listEntries = unpacker . unpackArrayHeader ( ) ; ArrayList < TimelineAnnotation > list = new ArrayList < TimelineAnnotation > ( listEntries ) ; for ( int i = 0 ; i < listEntries ; i ++ ) { int timelineObjectSize = unpacker . unpackMapHeader ( ) ; long time = 0 ; String msg = "" ; for ( int j = 0 ; j < timelineObjectSize ; j ++ ) { String tlKey = unpacker . unpackString ( ) ; if ( tlKey . length ( ) != 1 ) { throw new IOException ( "Unknown timeline map key " + tlKey ) ; } switch ( tlKey . charAt ( 0 ) ) { case 't' : time = unpacker . unpackLong ( ) ; break ; case 'm' : msg = unpacker . unpackString ( ) ; break ; default : throw new IOException ( "Unknown timeline map key " + tlKey ) ; } } list . add ( new TimelineAnnotation ( time , msg ) ) ; } builder . timeline ( list ) ; break ; default : throw new IOException ( "Unknown key " + key ) ; } } return builder . build ( ) ; }
read a span . used in unit tests . not optimized .
