PageRenderTime 65ms CodeModel.GetById 30ms RepoModel.GetById 7ms app.codeStats 0ms

/src/sys/dotnet/fan/sys/Buf.cs

https://bitbucket.org/bedlaczech/fan-1.0
C# | 479 lines | 313 code | 116 blank | 50 comment | 35 complexity | 8bb5c0dff3858decb5b9abc057ccebd0 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2007, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 26 Feb 07 Andy Frank Creation
  7. //
  8. using System;
  9. using System.IO;
  10. using System.Text;
  11. namespace Fan.Sys
  12. {
  13. /// <summary>
  14. /// Buf
  15. /// </summary>
  16. public abstract class Buf : FanObj
  17. {
  18. //////////////////////////////////////////////////////////////////////////
  19. // Constructor
  20. //////////////////////////////////////////////////////////////////////////
  21. public static Buf make() { return new MemBuf(1024); }
  22. public static Buf make(long capacity) { return new MemBuf((int)capacity); }
  23. public static Buf random(long s)
  24. {
  25. int size = (int)s;
  26. byte[] buf = new byte[size];
  27. Random random = new Random();
  28. for (int i=0; i<size;)
  29. {
  30. int x = random.Next();
  31. buf[i++] = (byte)(x >> 24);
  32. if (i < size)
  33. {
  34. buf[i++] = (byte)(x >> 16);
  35. if (i < size)
  36. {
  37. buf[i++] = (byte)(x >> 8);
  38. if (i < size) buf[i++] = (byte)x;
  39. }
  40. }
  41. }
  42. return new MemBuf(buf);
  43. }
  44. //////////////////////////////////////////////////////////////////////////
  45. // Obj
  46. //////////////////////////////////////////////////////////////////////////
  47. public override string toStr()
  48. {
  49. return @typeof().name() + "(pos=" + getPos() + " size=" + getSize() + ")";
  50. }
  51. public override Type @typeof() { return Sys.BufType; }
  52. //////////////////////////////////////////////////////////////////////////
  53. // Support
  54. //////////////////////////////////////////////////////////////////////////
  55. internal abstract long getSize();
  56. internal abstract void setSize(long x);
  57. internal abstract long getPos();
  58. internal abstract void setPos(long x);
  59. internal abstract int getByte(long pos);
  60. internal abstract void getBytes(long pos, byte[] dst, int off, int len);
  61. internal abstract void setByte(long pos, int x);
  62. internal abstract void pipeTo(byte[] dst, int dstPos, int len);
  63. internal abstract void pipeTo(Stream dst, long len);
  64. //internal abstract void pipeTo(ByteBuffer dst, int len);
  65. internal abstract void pipeFrom(byte[] src, int srcPos, int len);
  66. internal abstract long pipeFrom(Stream src, long len);
  67. //internal abstract void pipeTo(ByteBuffer dst, int len);
  68. //////////////////////////////////////////////////////////////////////////
  69. // Access
  70. //////////////////////////////////////////////////////////////////////////
  71. public bool isEmpty()
  72. {
  73. return getSize() == 0;
  74. }
  75. public virtual long capacity()
  76. {
  77. return FanInt.m_maxVal;
  78. }
  79. public virtual void capacity(long c)
  80. {
  81. }
  82. public long size()
  83. {
  84. return getSize();
  85. }
  86. public void size(long s)
  87. {
  88. setSize(s);
  89. }
  90. public long pos()
  91. {
  92. return getPos();
  93. }
  94. public long remaining()
  95. {
  96. return getSize()-getPos();
  97. }
  98. public bool more()
  99. {
  100. return getSize()-getPos() > 0;
  101. }
  102. public Buf seek(long pos)
  103. {
  104. long p = pos;
  105. long size = getSize();
  106. if (p < 0) p = size + p;
  107. if (p < 0 || p > size) throw IndexErr.make(pos).val;
  108. setPos(p);
  109. return this;
  110. }
  111. public Buf flip()
  112. {
  113. setSize(getPos());
  114. setPos(0);
  115. return this;
  116. }
  117. public long get(long pos)
  118. {
  119. long size = getSize();
  120. if (pos < 0) pos = size + pos;
  121. if (pos < 0 || pos >= size) throw IndexErr.make(pos).val;
  122. return getByte(pos);
  123. }
  124. public Buf getRange(Range range)
  125. {
  126. long size = getSize();
  127. long s = range.start(size);
  128. long e = range.end(size);
  129. int n = (int)(e - s + 1);
  130. if (n < 0) throw IndexErr.make(range).val;
  131. byte[] slice = new byte[n];
  132. getBytes(s, slice, 0, n);
  133. Buf result = new MemBuf(slice, n);
  134. result.charset(charset());
  135. return result;
  136. }
  137. public Buf dup()
  138. {
  139. int size = (int)this.size();
  140. byte[] copy = new byte[size];
  141. getBytes(0, copy, 0, size);
  142. Buf result = new MemBuf(copy, size);
  143. result.charset(charset());
  144. return result;
  145. }
  146. //////////////////////////////////////////////////////////////////////////
  147. // Modification
  148. //////////////////////////////////////////////////////////////////////////
  149. public Buf set(long pos, long b)
  150. {
  151. long size = getSize();
  152. if (pos < 0) pos = size + pos;
  153. if (pos < 0 || pos >= size) throw IndexErr.make(pos).val;
  154. setByte(pos, (int)b);
  155. return this;
  156. }
  157. public virtual Buf trim()
  158. {
  159. return this;
  160. }
  161. public Buf clear()
  162. {
  163. setPos(0);
  164. setSize(0);
  165. return this;
  166. }
  167. public virtual Buf flush()
  168. {
  169. return this;
  170. }
  171. public virtual bool close()
  172. {
  173. return true;
  174. }
  175. public Endian endian()
  176. {
  177. return m_out.endian();
  178. }
  179. public void endian(Endian endian)
  180. {
  181. m_out.endian(endian);
  182. m_in.endian(endian);
  183. }
  184. public Charset charset()
  185. {
  186. return m_out.charset();
  187. }
  188. public void charset(Charset charset)
  189. {
  190. m_out.charset(charset);
  191. m_in.charset(charset);
  192. }
  193. public Buf fill(long b, long times)
  194. {
  195. if (capacity() < size()+times) capacity(size()+times);
  196. int t = (int)times;
  197. for (int i=0; i<t; ++i) m_out.write(b);
  198. return this;
  199. }
  200. //////////////////////////////////////////////////////////////////////////
  201. // OutStream
  202. //////////////////////////////////////////////////////////////////////////
  203. public OutStream @out() { return m_out; }
  204. public Buf write(long b) { m_out.write(b); return this; }
  205. public Buf writeBuf(Buf other) { m_out.writeBuf(other); return this; }
  206. public Buf writeBuf(Buf other, long n) { m_out.writeBuf(other, n); return this; }
  207. public Buf writeI2(long x) { m_out.writeI2(x); return this; }
  208. public Buf writeI4(long x) { m_out.writeI4(x); return this; }
  209. public Buf writeI8(long x) { m_out.writeI8(x); return this; }
  210. public Buf writeF4(double x) { m_out.writeF4(x); return this; }
  211. public Buf writeF8(double x) { m_out.writeF8(x); return this; }
  212. public Buf writeDecimal(BigDecimal x) { m_out.writeDecimal(x); return this; }
  213. public Buf writeBool(bool x) { m_out.writeBool(x); return this; }
  214. public Buf writeUtf(string x) { m_out.writeUtf(x); return this; }
  215. public Buf writeChar(long c) { m_out.writeChar(c); return this; }
  216. public Buf writeChars(string s) { m_out.writeChars(s); return this; }
  217. public Buf writeChars(string s, long off) { m_out.writeChars(s, off); return this; }
  218. public Buf writeChars(string s, long off, long len) { m_out.writeChars(s, off, len); return this; }
  219. public Buf print(object obj) { m_out.print(obj); return this; }
  220. public Buf printLine() { m_out.printLine(); return this; }
  221. public Buf printLine(object obj) { m_out.printLine(obj); return this; }
  222. public Buf writeProps(Map props) { m_out.writeProps(props); return this; }
  223. public Buf writeObj(object obj) { m_out.writeObj(obj); return this; }
  224. public Buf writeObj(object obj, Map opt) { m_out.writeObj(obj, opt); return this; }
  225. public Buf writeXml(string s) { m_out.writeXml(s, 0); return this; }
  226. public Buf writeXml(string s, long flags) { m_out.writeXml(s, flags); return this; }
  227. //////////////////////////////////////////////////////////////////////////
  228. // InStream
  229. //////////////////////////////////////////////////////////////////////////
  230. public InStream @in() { return m_in; }
  231. public Long read() { return m_in.read(); }
  232. public Long readBuf(Buf other, long n) { return m_in.readBuf(other, n); }
  233. public Buf unread(long n) { m_in.unread(n); return this; }
  234. public Buf readBufFully(Buf buf, long n) { return m_in.readBufFully(buf, n); }
  235. public Buf readAllBuf() { return m_in.readAllBuf(); }
  236. public Long peek() { return m_in.peek(); }
  237. public long readU1() { return m_in.readU1(); }
  238. public long readS1() { return m_in.readS1(); }
  239. public long readU2() { return m_in.readU2(); }
  240. public long readS2() { return m_in.readS2(); }
  241. public long readU4() { return m_in.readU4(); }
  242. public long readS4() { return m_in.readS4(); }
  243. public long readS8() { return m_in.readS8(); }
  244. public double readF4() { return m_in.readF4(); }
  245. public double readF8() { return m_in.readF8(); }
  246. public BigDecimal readDecimal() { return m_in.readDecimal(); }
  247. public bool readBool() { return m_in.readBool(); }
  248. public string readUtf() { return m_in.readUtf(); }
  249. public Long readChar() { return m_in.readChar(); }
  250. public Buf unreadChar(long c) { m_in.unreadChar(c); return this; }
  251. public Long peekChar() { return m_in.peekChar(); }
  252. public string readChars(long n) { return m_in.readChars(n); }
  253. public string readLine() { return m_in.readLine(); }
  254. public string readLine(Long max) { return m_in.readLine(max); }
  255. public string readStrToken() { return m_in.readStrToken(); }
  256. public string readStrToken(Long max) { return m_in.readStrToken(max); }
  257. public string readStrToken(Long max, Func f) { return m_in.readStrToken(FanInt.Chunk, f); }
  258. public List readAllLines() { return m_in.readAllLines(); }
  259. public void eachLine(Func f) { m_in.eachLine(f); }
  260. public string readAllStr() { return m_in.readAllStr(); }
  261. public string readAllStr(bool normalizeNewlines) { return m_in.readAllStr(normalizeNewlines); }
  262. public Map readProps() { return m_in.readProps(); }
  263. public object readObj() { return m_in.readObj(); }
  264. public object readObj(Map opt) { return m_in.readObj(opt); }
  265. //////////////////////////////////////////////////////////////////////////
  266. // Hex
  267. //////////////////////////////////////////////////////////////////////////
  268. public virtual string toHex()
  269. {
  270. throw UnsupportedErr.make(@typeof()+".toHex").val;
  271. }
  272. public static Buf fromHex(string s)
  273. {
  274. int slen = s.Length;
  275. byte[] buf = new byte[slen/2];
  276. int[] hexInv = Buf.hexInv;
  277. int size = 0;
  278. for (int i=0; i<slen; ++i)
  279. {
  280. int c0 = s[i];
  281. int n0 = c0 < 128 ? hexInv[c0] : -1;
  282. if (n0 < 0) continue;
  283. int n1 = -1;
  284. if (++i < slen)
  285. {
  286. int c1 = s[i];
  287. n1 = c1 < 128 ? hexInv[c1] : -1;
  288. }
  289. if (n1 < 0) throw IOErr.make("Invalid hex str").val;
  290. buf[size++] = (byte)((n0 << 4) | n1);
  291. }
  292. return new MemBuf(buf, size);
  293. }
  294. //////////////////////////////////////////////////////////////////////////
  295. // Base64
  296. //////////////////////////////////////////////////////////////////////////
  297. public virtual string toBase64()
  298. {
  299. throw UnsupportedErr.make(@typeof()+".toBase64").val;
  300. }
  301. public static Buf fromBase64(string s)
  302. {
  303. int slen = s.Length;
  304. int si = 0;
  305. int max = slen * 6 / 8;
  306. byte[] buf = new byte[max];
  307. int size = 0;
  308. while (si < slen)
  309. {
  310. int n = 0;
  311. int v = 0;
  312. for (int j=0; j<4 && si<slen;)
  313. {
  314. int ch = s[si++];
  315. int c = ch < 128 ? base64inv[ch] : -1;
  316. if (c >= 0)
  317. {
  318. n |= c << (18 - j++ * 6);
  319. if (ch != '=') v++;
  320. }
  321. }
  322. if (v > 1) buf[size++] = (byte)(n >> 16);
  323. if (v > 2) buf[size++] = (byte)(n >> 8);
  324. if (v > 3) buf[size++] = (byte)n;
  325. }
  326. return new MemBuf(buf, size);
  327. }
  328. //////////////////////////////////////////////////////////////////////////
  329. // Digest
  330. //////////////////////////////////////////////////////////////////////////
  331. public virtual Buf toDigest(string algorithm)
  332. {
  333. throw UnsupportedErr.make(@typeof()+".toDigest").val;
  334. }
  335. public virtual Buf hmac(String algorithm, Buf key)
  336. {
  337. throw UnsupportedErr.make(@typeof()+".hmac").val;
  338. }
  339. //////////////////////////////////////////////////////////////////////////
  340. // Static
  341. //////////////////////////////////////////////////////////////////////////
  342. internal static readonly char[] hexChars = "0123456789abcdef".ToCharArray();
  343. internal static readonly int[] hexInv = new int[128];
  344. internal static readonly char[] base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".ToCharArray();
  345. internal static readonly int[] base64inv = new int[128];
  346. static Buf()
  347. {
  348. // base64
  349. for (int i=0; i<hexInv.Length; ++i) hexInv[i] = -1;
  350. for (int i=0; i<10; ++i) hexInv['0'+i] = i;
  351. for (int i=10; i<16; ++i) hexInv['a'+i-10] = hexInv['A'+i-10] = i;
  352. // hex
  353. for (int i=0; i<base64inv.Length; ++i) base64inv[i] = -1;
  354. for (int i=0; i<base64chars.Length; ++i) base64inv[base64chars[i]] = i;
  355. base64inv['='] = 0;
  356. }
  357. //////////////////////////////////////////////////////////////////////////
  358. // Fields
  359. //////////////////////////////////////////////////////////////////////////
  360. internal OutStream m_out;
  361. internal InStream m_in;
  362. }
  363. }