1. /*
  2. * @(#)ASCII_CharStream.java 4.16 04/07/26
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /* Generated By:JavaCC: Do not edit this line. ASCII_CharStream.java Version 0.7pre6 */
  8. package com.sun.jmx.snmp.IPAcl;
  9. /**
  10. * An implementation of interface CharStream, where the stream is assumed to
  11. * contain only ASCII characters (without unicode processing).
  12. */
  13. final class ASCII_CharStream
  14. {
  15. public static final boolean staticFlag = false;
  16. int bufsize;
  17. int available;
  18. int tokenBegin;
  19. public int bufpos = -1;
  20. private int bufline[];
  21. private int bufcolumn[];
  22. private int column = 0;
  23. private int line = 1;
  24. private boolean prevCharIsCR = false;
  25. private boolean prevCharIsLF = false;
  26. private java.io.Reader inputStream;
  27. private char[] buffer;
  28. private int maxNextCharInd = 0;
  29. private int inBuf = 0;
  30. private final void ExpandBuff(boolean wrapAround)
  31. {
  32. char[] newbuffer = new char[bufsize + 2048];
  33. int newbufline[] = new int[bufsize + 2048];
  34. int newbufcolumn[] = new int[bufsize + 2048];
  35. try
  36. {
  37. if (wrapAround)
  38. {
  39. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  40. System.arraycopy(buffer, 0, newbuffer,
  41. bufsize - tokenBegin, bufpos);
  42. buffer = newbuffer;
  43. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  44. System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
  45. bufline = newbufline;
  46. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  47. System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
  48. bufcolumn = newbufcolumn;
  49. maxNextCharInd = (bufpos += (bufsize - tokenBegin));
  50. }
  51. else
  52. {
  53. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  54. buffer = newbuffer;
  55. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  56. bufline = newbufline;
  57. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  58. bufcolumn = newbufcolumn;
  59. maxNextCharInd = (bufpos -= tokenBegin);
  60. }
  61. }
  62. catch (Throwable t)
  63. {
  64. throw new Error(t.getMessage());
  65. }
  66. bufsize += 2048;
  67. available = bufsize;
  68. tokenBegin = 0;
  69. }
  70. private final void FillBuff() throws java.io.IOException
  71. {
  72. if (maxNextCharInd == available)
  73. {
  74. if (available == bufsize)
  75. {
  76. if (tokenBegin > 2048)
  77. {
  78. bufpos = maxNextCharInd = 0;
  79. available = tokenBegin;
  80. }
  81. else if (tokenBegin < 0)
  82. bufpos = maxNextCharInd = 0;
  83. else
  84. ExpandBuff(false);
  85. }
  86. else if (available > tokenBegin)
  87. available = bufsize;
  88. else if ((tokenBegin - available) < 2048)
  89. ExpandBuff(true);
  90. else
  91. available = tokenBegin;
  92. }
  93. int i;
  94. try {
  95. if ((i = inputStream.read(buffer, maxNextCharInd,
  96. available - maxNextCharInd)) == -1)
  97. {
  98. inputStream.close();
  99. throw new java.io.IOException();
  100. }
  101. else
  102. maxNextCharInd += i;
  103. return;
  104. }
  105. catch(java.io.IOException e) {
  106. --bufpos;
  107. backup(0);
  108. if (tokenBegin == -1)
  109. tokenBegin = bufpos;
  110. throw e;
  111. }
  112. }
  113. public final char BeginToken() throws java.io.IOException
  114. {
  115. tokenBegin = -1;
  116. char c = readChar();
  117. tokenBegin = bufpos;
  118. return c;
  119. }
  120. private final void UpdateLineColumn(char c)
  121. {
  122. column++;
  123. if (prevCharIsLF)
  124. {
  125. prevCharIsLF = false;
  126. line += (column = 1);
  127. }
  128. else if (prevCharIsCR)
  129. {
  130. prevCharIsCR = false;
  131. if (c == '\n')
  132. {
  133. prevCharIsLF = true;
  134. }
  135. else
  136. line += (column = 1);
  137. }
  138. switch (c)
  139. {
  140. case '\r' :
  141. prevCharIsCR = true;
  142. break;
  143. case '\n' :
  144. prevCharIsLF = true;
  145. break;
  146. case '\t' :
  147. column--;
  148. column += (8 - (column & 07));
  149. break;
  150. default :
  151. break;
  152. }
  153. bufline[bufpos] = line;
  154. bufcolumn[bufpos] = column;
  155. }
  156. public final char readChar() throws java.io.IOException
  157. {
  158. if (inBuf > 0)
  159. {
  160. --inBuf;
  161. return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]);
  162. }
  163. if (++bufpos >= maxNextCharInd)
  164. FillBuff();
  165. char c = (char)((char)0xff & buffer[bufpos]);
  166. UpdateLineColumn(c);
  167. return (c);
  168. }
  169. /**
  170. * @deprecated
  171. * @see #getEndColumn
  172. */
  173. @Deprecated
  174. public final int getColumn() {
  175. return bufcolumn[bufpos];
  176. }
  177. /**
  178. * @deprecated
  179. * @see #getEndLine
  180. */
  181. @Deprecated
  182. public final int getLine() {
  183. return bufline[bufpos];
  184. }
  185. public final int getEndColumn() {
  186. return bufcolumn[bufpos];
  187. }
  188. public final int getEndLine() {
  189. return bufline[bufpos];
  190. }
  191. public final int getBeginColumn() {
  192. return bufcolumn[tokenBegin];
  193. }
  194. public final int getBeginLine() {
  195. return bufline[tokenBegin];
  196. }
  197. public final void backup(int amount) {
  198. inBuf += amount;
  199. if ((bufpos -= amount) < 0)
  200. bufpos += bufsize;
  201. }
  202. public ASCII_CharStream(java.io.Reader dstream, int startline,
  203. int startcolumn, int buffersize)
  204. {
  205. inputStream = dstream;
  206. line = startline;
  207. column = startcolumn - 1;
  208. available = bufsize = buffersize;
  209. buffer = new char[buffersize];
  210. bufline = new int[buffersize];
  211. bufcolumn = new int[buffersize];
  212. }
  213. public ASCII_CharStream(java.io.Reader dstream, int startline,
  214. int startcolumn)
  215. {
  216. this(dstream, startline, startcolumn, 4096);
  217. }
  218. public void ReInit(java.io.Reader dstream, int startline,
  219. int startcolumn, int buffersize)
  220. {
  221. inputStream = dstream;
  222. line = startline;
  223. column = startcolumn - 1;
  224. if (buffer == null || buffersize != buffer.length)
  225. {
  226. available = bufsize = buffersize;
  227. buffer = new char[buffersize];
  228. bufline = new int[buffersize];
  229. bufcolumn = new int[buffersize];
  230. }
  231. prevCharIsLF = prevCharIsCR = false;
  232. tokenBegin = inBuf = maxNextCharInd = 0;
  233. bufpos = -1;
  234. }
  235. public void ReInit(java.io.Reader dstream, int startline,
  236. int startcolumn)
  237. {
  238. ReInit(dstream, startline, startcolumn, 4096);
  239. }
  240. public ASCII_CharStream(java.io.InputStream dstream, int startline,
  241. int startcolumn, int buffersize)
  242. {
  243. this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
  244. }
  245. public ASCII_CharStream(java.io.InputStream dstream, int startline,
  246. int startcolumn)
  247. {
  248. this(dstream, startline, startcolumn, 4096);
  249. }
  250. public void ReInit(java.io.InputStream dstream, int startline,
  251. int startcolumn, int buffersize)
  252. {
  253. ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
  254. }
  255. public void ReInit(java.io.InputStream dstream, int startline,
  256. int startcolumn)
  257. {
  258. ReInit(dstream, startline, startcolumn, 4096);
  259. }
  260. public final String GetImage()
  261. {
  262. if (bufpos >= tokenBegin)
  263. return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
  264. else
  265. return new String(buffer, tokenBegin, bufsize - tokenBegin) +
  266. new String(buffer, 0, bufpos + 1);
  267. }
  268. public final char[] GetSuffix(int len)
  269. {
  270. char[] ret = new char[len];
  271. if ((bufpos + 1) >= len)
  272. System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
  273. else
  274. {
  275. System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
  276. len - bufpos - 1);
  277. System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
  278. }
  279. return ret;
  280. }
  281. public void Done()
  282. {
  283. buffer = null;
  284. bufline = null;
  285. bufcolumn = null;
  286. }
  287. /**
  288. * Method to adjust line and column numbers for the start of a token.
  289. */
  290. public void adjustBeginLineColumn(int newLine, int newCol)
  291. {
  292. int start = tokenBegin;
  293. int len;
  294. if (bufpos >= tokenBegin)
  295. {
  296. len = bufpos - tokenBegin + inBuf + 1;
  297. }
  298. else
  299. {
  300. len = bufsize - tokenBegin + bufpos + 1 + inBuf;
  301. }
  302. int i = 0, j = 0, k = 0;
  303. int nextColDiff = 0, columnDiff = 0;
  304. while (i < len &&
  305. bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
  306. {
  307. bufline[j] = newLine;
  308. nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
  309. bufcolumn[j] = newCol + columnDiff;
  310. columnDiff = nextColDiff;
  311. i++;
  312. }
  313. if (i < len)
  314. {
  315. bufline[j] = newLine++;
  316. bufcolumn[j] = newCol + columnDiff;
  317. while (i++ < len)
  318. {
  319. if (bufline[j = start % bufsize] != bufline[++start % bufsize])
  320. bufline[j] = newLine++;
  321. else
  322. bufline[j] = newLine;
  323. }
  324. }
  325. line = bufline[j];
  326. column = bufcolumn[j];
  327. }
  328. }