// float public static bool read(bintalk.IReader r, ref float v, uint maxValue) { byte[] data; int startId; if (!r.read(4, out data, out startId)) return false; v = BitConverter.ToSingle(data, startId); return true; }
// uint16 public static bool read(bintalk.IReader r, ref ushort v, uint maxValue) { byte[] data; int startId; if (!r.read(2, out data, out startId)) return false; v = BitConverter.ToUInt16(data, startId); return true; }
// int8 public static bool read(bintalk.IReader r, ref sbyte v, uint maxValue) { byte[] data; int startId; if (!r.read(1, out data, out startId)) return false; v = (sbyte)data[startId]; return true; }
// string public static void write(bintalk.IWriter w, string v) { if (v == null || v.Length == 0) writeDynSize(w, 0); else { byte[] str = Encoding.UTF8.GetBytes(v); uint len = (uint)str.Length; writeDynSize(w, len); if (len > 0) w.write(str); } }
public static bool readMid(bintalk.IReader r, ref ushort v) { return read(r, ref v, 0); }
// dynamic size. public static bool readDynSize(bintalk.IReader r, out uint s) { s = 0; byte b = 0; if (!read(r, ref b, 0)) return false; uint n = (uint)((b & 0XC0) >> 6); s = (uint)(b & 0X3F); for (int i = 0; i < n; i++) { if (!read(r, ref b, 0)) return false; s = (s << 8) | b; } return true; }
// binary. public static bool read(bintalk.IReader r, ref byte[] v, uint maxValue) { uint s; if (!readDynSize(r, out s) || s > maxValue) return false; v = new byte[s]; byte[] data; int startId; if(!r.read(s, out data, out startId)) return false; Array.Copy(data, startId, v, 0, s); return true; }
// string. public static bool read(bintalk.IReader r, ref string v, uint maxValue) { uint s; if (!readDynSize(r, out s) || s > maxValue) return false; byte[] data; int startId; if(!r.read(s, out data, out startId)) return false; v = Encoding.UTF8.GetString(data, startId, (int)s); return true; }
// binary. public static void write(bintalk.IWriter w, byte[] v) { if (v == null || v.Length == 0) writeDynSize(w, 0); else { uint s = (uint)v.Length; writeDynSize(w, s); w.write(v); } }
// bool public static void write(bintalk.IWriter w, bool v) { write(w, (byte)(v?1:0)); }
// uint8 public static void write(bintalk.IWriter w, byte v) { w.write(new byte[1]{v}); }
// uint16 public static void write(bintalk.IWriter w, ushort v) { w.write(BitConverter.GetBytes(v)); }
public static void writeMid(bintalk.IWriter w, int v) { write(w, (ushort)v); }
// dynamic size. public static void writeDynSize(bintalk.IWriter w, uint s) { byte[] b = BitConverter.GetBytes(s); int n = 0; if (s <= 0X3F) n = 0; else if (s <= 0X3FFF) n = 1; else if(s <= 0X3FFFFF) n = 2; else if(s <= 0X3FFFFFFF) n = 3; b[n] |= (byte)(n << 6); for(int i = n; i >= 0; i--) write(w, b[i]); }