java - Issue with toString() Implementation in Android -
i have more advanced class trying write tostring()
for
in order accomplish trying need able change assignment of variables when doing tostring()
.
to make simple going remove bunch of stuff except allows work.
public enum packetelementtype { none((byte)0, "none"), byte((byte)1, "byte"), short((byte)2, "short"), int((byte)3, "int"), long((byte)4, "long"), float((byte)5, "float"), string((byte)6, "string"), bin((byte)7, "bin"); private final byte typevalue; private final string typename; packetelementtype(byte type, string name) { this.typevalue = type; this.typename = name; } public string gettypename() { return typename; } public byte gettypevalue() { return typevalue; } } public class packet { private final int default_size = 1024 * 2; private final int add_size = 1024; private byte[] _buffer = new byte[1]; private int _ptr = 0; private int _bodystart = 0; private int _elements, _bodylen = 0; private int op; private long id; public packet(int op, long id) { setop(op); setid(id); _buffer = new byte[default_size]; } public int getop() { return op; } public void setop(int op) { this.op = op; } public long getid() { return id; } public void setid(long id) { this.id = id; } public packetelementtype peek() { int pie = _ptr; if (pie + 2 > _buffer.length) return packetelementtype.none; return packetelementtype.values()[_buffer[_ptr]]; } protected packet putsimple(packetelementtype type, byte... val) { int len = val.length + 1; this.ensuresize(len); _buffer[++_ptr] = type.gettypevalue(); system.arraycopy(val, 0, _buffer, _ptr, val.length); _ptr += val.length; _elements++; _bodylen += len; return this; } public packet putbyte(byte val) { return this.putsimple(packetelementtype.byte, val); } public packet putbyte(boolean val) { return this.putbyte(val ? (byte) 1 : (byte) 0); } public byte getbyte() throws exception { if (this.peek() != packetelementtype.byte) throw new exception("expected byte, got " + this.peek().gettypename() + "."); _ptr += 1; return _buffer[++_ptr]; } protected void ensuresize(int required) { if (_ptr + required >= _buffer.length) { byte[] b = new byte[_buffer.length + math.max(add_size, required * 2)]; system.arraycopy(_buffer, 0, b, 0, _buffer.length); _buffer = b; } } private boolean isvalidtype(packetelementtype type) { return (type.gettypevalue() >= packetelementtype.byte.gettypevalue() && type.gettypevalue() <= packetelementtype.bin.gettypevalue()); } protected string tostringhack() { stringbuilder result = new stringbuilder(); int prevptu = _ptr; _ptr = _bodystart; try { result.append(string.format("op: %1$08d %3$s, id: %2$016d\r\n", this.getop(), this.getid(), op.getname(this.getop()))); } catch (illegalaccessexception e) { e.printstacktrace(); return result.append("failed convert packet string").tostring(); } packetelementtype type; (int = 1; (this.isvalidtype(type = this.peek()) && _ptr < _buffer.length); ++i) { if (type == packetelementtype.byte) { byte data = 0; try { data = getbyte(); } catch (exception e) { e.printstacktrace(); result.append("failed parse element @ position ").append(i); continue; } result.append(string.format("%1&03d [%2$s] byte : %3$s", i, string.format("%1$016d", data), data)); } } return result.tostring(); } //todo: tostring @override public string tostring() { return tostringhack(); } } public class op { public class msgr { } public static string getname(int op) throws illegalaccessexception { (field field : op.class.getfields()) { if ((int)field.get(null) == op) return field.getname(); } (field field : op.msgr.class.getfields()) { if ((int)field.get(null) == op) return field.getname(); } return "?"; } }
[2
when debugging, _ptr
won't set in tostring()
, when not debugging, _ptr
won't set in putsimple()
.
i'm close pulling hair out, please , thanks, if me glad! please , thank again!
to test bug please review following example:
packet p = new packet(1, 10001).putbyte(true); toast.maketext(this, p.tostring(), toast.length_long).show();
for me throw inside built in test class first, , tried in oncreate main activity.
tostring()
return op , id because _ptr
@ , peek()
attempt read byte starting @ position instead of @ 0 find our 1 byte.
edit
it seems like... _ptr = _bodystart;
being seen other assignment, possible?
result see ok - debugger shows variables before evaluation. ad line after 1 (like log or smth.) , set breakpoint on it.
Comments
Post a Comment