PageRenderTime 85ms CodeModel.GetById 27ms RepoModel.GetById 10ms app.codeStats 0ms

/src/sys/java/fan/sys/Buf.java

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