All pastes #402847 Raw Edit

Someone

public java v1 · immutable
·published 2007-03-20 01:01 UTC
rendered paste body
 public void parse(byte[] b) {        int pos = 0; //current byte position        byte[] cur; //current byte chunk        int len = b.length; //packet length        length = len;                LinkedList<Type> copy = new LinkedList<Type>();        for (Type t: struct) {            copy.add(t);        }        Queue<Type> q = (Queue<Type>)copy;                //Not a packet fragment, so get the ID        byte[] idbytes = new byte[2];        System.arraycopy(b, 0, idbytes, 0, 2);        int id = new _Word(null).decode(idbytes).intValue();        pos += 2;                while (pos < len) {            Type t = q.poll();            if (pos + t.size() > len) {                System.err.println("Malformed packet? Breaking early.");                break;            }                        cur = new byte[t.size()];            System.arraycopy(b, pos, cur, 0, t.size());            t.decode(cur);                        pos += t.size();                        if (q.peek() == null && t instanceof DynamicMarker) {                //Push the dynamic marker back in to get more dynamic parts.                q.offer(t);            } else if (q.peek() == null) {                System.err.println("Packet decoder ran out of types!");            }        }            }