PageRenderTime 29ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/android/titanium/thirdparty/org/apache/james/mime4j/field/language/SimpleCharStream.java

https://github.com/bataboske/titanium_mobile
Java | 457 lines | 353 code | 70 blank | 34 comment | 42 complexity | a687ab4b627ab8d9a980af656e04e7b5 MD5 | raw file
  1. /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */
  2. /****************************************************************
  3. * Licensed to the Apache Software Foundation (ASF) under one *
  4. * or more contributor license agreements. See the NOTICE file *
  5. * distributed with this work for additional information *
  6. * regarding copyright ownership. The ASF licenses this file *
  7. * to you under the Apache License, Version 2.0 (the *
  8. * "License"); you may not use this file except in compliance *
  9. * with the License. You may obtain a copy of the License at *
  10. * *
  11. * http://www.apache.org/licenses/LICENSE-2.0 *
  12. * *
  13. * Unless required by applicable law or agreed to in writing, *
  14. * software distributed under the License is distributed on an *
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
  16. * KIND, either express or implied. See the License for the *
  17. * specific language governing permissions and limitations *
  18. * under the License. *
  19. ****************************************************************/
  20. package org.apache.james.mime4j.field.language;
  21. /**
  22. * An implementation of interface CharStream, where the stream is assumed to
  23. * contain only ASCII characters (without unicode processing).
  24. */
  25. public class SimpleCharStream
  26. {
  27. public static final boolean staticFlag = false;
  28. int bufsize;
  29. int available;
  30. int tokenBegin;
  31. public int bufpos = -1;
  32. protected int bufline[];
  33. protected int bufcolumn[];
  34. protected int column = 0;
  35. protected int line = 1;
  36. protected boolean prevCharIsCR = false;
  37. protected boolean prevCharIsLF = false;
  38. protected java.io.Reader inputStream;
  39. protected char[] buffer;
  40. protected int maxNextCharInd = 0;
  41. protected int inBuf = 0;
  42. protected int tabSize = 8;
  43. protected void setTabSize(int i) { tabSize = i; }
  44. protected int getTabSize(int i) { return tabSize; }
  45. protected void ExpandBuff(boolean wrapAround)
  46. {
  47. char[] newbuffer = new char[bufsize + 2048];
  48. int newbufline[] = new int[bufsize + 2048];
  49. int newbufcolumn[] = new int[bufsize + 2048];
  50. try
  51. {
  52. if (wrapAround)
  53. {
  54. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  55. System.arraycopy(buffer, 0, newbuffer,
  56. bufsize - tokenBegin, bufpos);
  57. buffer = newbuffer;
  58. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  59. System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
  60. bufline = newbufline;
  61. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  62. System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
  63. bufcolumn = newbufcolumn;
  64. maxNextCharInd = (bufpos += (bufsize - tokenBegin));
  65. }
  66. else
  67. {
  68. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  69. buffer = newbuffer;
  70. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  71. bufline = newbufline;
  72. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  73. bufcolumn = newbufcolumn;
  74. maxNextCharInd = (bufpos -= tokenBegin);
  75. }
  76. }
  77. catch (Throwable t)
  78. {
  79. throw new Error(t.getMessage());
  80. }
  81. bufsize += 2048;
  82. available = bufsize;
  83. tokenBegin = 0;
  84. }
  85. protected void FillBuff() throws java.io.IOException
  86. {
  87. if (maxNextCharInd == available)
  88. {
  89. if (available == bufsize)
  90. {
  91. if (tokenBegin > 2048)
  92. {
  93. bufpos = maxNextCharInd = 0;
  94. available = tokenBegin;
  95. }
  96. else if (tokenBegin < 0)
  97. bufpos = maxNextCharInd = 0;
  98. else
  99. ExpandBuff(false);
  100. }
  101. else if (available > tokenBegin)
  102. available = bufsize;
  103. else if ((tokenBegin - available) < 2048)
  104. ExpandBuff(true);
  105. else
  106. available = tokenBegin;
  107. }
  108. int i;
  109. try {
  110. if ((i = inputStream.read(buffer, maxNextCharInd,
  111. available - maxNextCharInd)) == -1)
  112. {
  113. inputStream.close();
  114. throw new java.io.IOException();
  115. }
  116. else
  117. maxNextCharInd += i;
  118. return;
  119. }
  120. catch(java.io.IOException e) {
  121. --bufpos;
  122. backup(0);
  123. if (tokenBegin == -1)
  124. tokenBegin = bufpos;
  125. throw e;
  126. }
  127. }
  128. public char BeginToken() throws java.io.IOException
  129. {
  130. tokenBegin = -1;
  131. char c = readChar();
  132. tokenBegin = bufpos;
  133. return c;
  134. }
  135. protected void UpdateLineColumn(char c)
  136. {
  137. column++;
  138. if (prevCharIsLF)
  139. {
  140. prevCharIsLF = false;
  141. line += (column = 1);
  142. }
  143. else if (prevCharIsCR)
  144. {
  145. prevCharIsCR = false;
  146. if (c == '\n')
  147. {
  148. prevCharIsLF = true;
  149. }
  150. else
  151. line += (column = 1);
  152. }
  153. switch (c)
  154. {
  155. case '\r' :
  156. prevCharIsCR = true;
  157. break;
  158. case '\n' :
  159. prevCharIsLF = true;
  160. break;
  161. case '\t' :
  162. column--;
  163. column += (tabSize - (column % tabSize));
  164. break;
  165. default :
  166. break;
  167. }
  168. bufline[bufpos] = line;
  169. bufcolumn[bufpos] = column;
  170. }
  171. public char readChar() throws java.io.IOException
  172. {
  173. if (inBuf > 0)
  174. {
  175. --inBuf;
  176. if (++bufpos == bufsize)
  177. bufpos = 0;
  178. return buffer[bufpos];
  179. }
  180. if (++bufpos >= maxNextCharInd)
  181. FillBuff();
  182. char c = buffer[bufpos];
  183. UpdateLineColumn(c);
  184. return (c);
  185. }
  186. /**
  187. * @deprecated
  188. * @see #getEndColumn
  189. */
  190. public int getColumn() {
  191. return bufcolumn[bufpos];
  192. }
  193. /**
  194. * @deprecated
  195. * @see #getEndLine
  196. */
  197. public int getLine() {
  198. return bufline[bufpos];
  199. }
  200. public int getEndColumn() {
  201. return bufcolumn[bufpos];
  202. }
  203. public int getEndLine() {
  204. return bufline[bufpos];
  205. }
  206. public int getBeginColumn() {
  207. return bufcolumn[tokenBegin];
  208. }
  209. public int getBeginLine() {
  210. return bufline[tokenBegin];
  211. }
  212. public void backup(int amount) {
  213. inBuf += amount;
  214. if ((bufpos -= amount) < 0)
  215. bufpos += bufsize;
  216. }
  217. public SimpleCharStream(java.io.Reader dstream, int startline,
  218. int startcolumn, int buffersize)
  219. {
  220. inputStream = dstream;
  221. line = startline;
  222. column = startcolumn - 1;
  223. available = bufsize = buffersize;
  224. buffer = new char[buffersize];
  225. bufline = new int[buffersize];
  226. bufcolumn = new int[buffersize];
  227. }
  228. public SimpleCharStream(java.io.Reader dstream, int startline,
  229. int startcolumn)
  230. {
  231. this(dstream, startline, startcolumn, 4096);
  232. }
  233. public SimpleCharStream(java.io.Reader dstream)
  234. {
  235. this(dstream, 1, 1, 4096);
  236. }
  237. public void ReInit(java.io.Reader dstream, int startline,
  238. int startcolumn, int buffersize)
  239. {
  240. inputStream = dstream;
  241. line = startline;
  242. column = startcolumn - 1;
  243. if (buffer == null || buffersize != buffer.length)
  244. {
  245. available = bufsize = buffersize;
  246. buffer = new char[buffersize];
  247. bufline = new int[buffersize];
  248. bufcolumn = new int[buffersize];
  249. }
  250. prevCharIsLF = prevCharIsCR = false;
  251. tokenBegin = inBuf = maxNextCharInd = 0;
  252. bufpos = -1;
  253. }
  254. public void ReInit(java.io.Reader dstream, int startline,
  255. int startcolumn)
  256. {
  257. ReInit(dstream, startline, startcolumn, 4096);
  258. }
  259. public void ReInit(java.io.Reader dstream)
  260. {
  261. ReInit(dstream, 1, 1, 4096);
  262. }
  263. public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
  264. int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  265. {
  266. this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  267. }
  268. public SimpleCharStream(java.io.InputStream dstream, int startline,
  269. int startcolumn, int buffersize)
  270. {
  271. this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
  272. }
  273. public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
  274. int startcolumn) throws java.io.UnsupportedEncodingException
  275. {
  276. this(dstream, encoding, startline, startcolumn, 4096);
  277. }
  278. public SimpleCharStream(java.io.InputStream dstream, int startline,
  279. int startcolumn)
  280. {
  281. this(dstream, startline, startcolumn, 4096);
  282. }
  283. public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  284. {
  285. this(dstream, encoding, 1, 1, 4096);
  286. }
  287. public SimpleCharStream(java.io.InputStream dstream)
  288. {
  289. this(dstream, 1, 1, 4096);
  290. }
  291. public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  292. int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  293. {
  294. ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  295. }
  296. public void ReInit(java.io.InputStream dstream, int startline,
  297. int startcolumn, int buffersize)
  298. {
  299. ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
  300. }
  301. public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  302. {
  303. ReInit(dstream, encoding, 1, 1, 4096);
  304. }
  305. public void ReInit(java.io.InputStream dstream)
  306. {
  307. ReInit(dstream, 1, 1, 4096);
  308. }
  309. public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  310. int startcolumn) throws java.io.UnsupportedEncodingException
  311. {
  312. ReInit(dstream, encoding, startline, startcolumn, 4096);
  313. }
  314. public void ReInit(java.io.InputStream dstream, int startline,
  315. int startcolumn)
  316. {
  317. ReInit(dstream, startline, startcolumn, 4096);
  318. }
  319. public String GetImage()
  320. {
  321. if (bufpos >= tokenBegin)
  322. return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
  323. else
  324. return new String(buffer, tokenBegin, bufsize - tokenBegin) +
  325. new String(buffer, 0, bufpos + 1);
  326. }
  327. public char[] GetSuffix(int len)
  328. {
  329. char[] ret = new char[len];
  330. if ((bufpos + 1) >= len)
  331. System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
  332. else
  333. {
  334. System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
  335. len - bufpos - 1);
  336. System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
  337. }
  338. return ret;
  339. }
  340. public void Done()
  341. {
  342. buffer = null;
  343. bufline = null;
  344. bufcolumn = null;
  345. }
  346. /**
  347. * Method to adjust line and column numbers for the start of a token.
  348. */
  349. public void adjustBeginLineColumn(int newLine, int newCol)
  350. {
  351. int start = tokenBegin;
  352. int len;
  353. if (bufpos >= tokenBegin)
  354. {
  355. len = bufpos - tokenBegin + inBuf + 1;
  356. }
  357. else
  358. {
  359. len = bufsize - tokenBegin + bufpos + 1 + inBuf;
  360. }
  361. int i = 0, j = 0, k = 0;
  362. int nextColDiff = 0, columnDiff = 0;
  363. while (i < len &&
  364. bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
  365. {
  366. bufline[j] = newLine;
  367. nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
  368. bufcolumn[j] = newCol + columnDiff;
  369. columnDiff = nextColDiff;
  370. i++;
  371. }
  372. if (i < len)
  373. {
  374. bufline[j] = newLine++;
  375. bufcolumn[j] = newCol + columnDiff;
  376. while (i++ < len)
  377. {
  378. if (bufline[j = start % bufsize] != bufline[++start % bufsize])
  379. bufline[j] = newLine++;
  380. else
  381. bufline[j] = newLine;
  382. }
  383. }
  384. line = bufline[j];
  385. column = bufcolumn[j];
  386. }
  387. }