PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/gnu/regexp/CharIndexedInputStream.java

#
Java | 131 lines | 87 code | 18 blank | 26 comment | 32 complexity | 3f8476adc37a8bdbf2999fc0114fee3b MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * gnu/regexp/CharIndexedReader.java
  3. * Copyright (C) 1998-2001 Wes Biggs
  4. *
  5. * This library is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published
  7. * by the Free Software Foundation; either version 2.1 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. package gnu.regexp;
  20. import java.io.InputStream;
  21. import java.io.BufferedInputStream;
  22. import java.io.IOException;
  23. // TODO: move(x) shouldn't rely on calling next() x times
  24. class CharIndexedInputStream implements CharIndexed {
  25. private static final int BUFFER_INCREMENT = 1024;
  26. private static final int UNKNOWN = Integer.MAX_VALUE; // value for end
  27. private BufferedInputStream br;
  28. // so that we don't try to reset() right away
  29. private int index = -1;
  30. private int bufsize = BUFFER_INCREMENT;
  31. private int end = UNKNOWN;
  32. private char cached = OUT_OF_BOUNDS;
  33. // Big enough for a \r\n pair
  34. // lookBehind[0] = most recent
  35. // lookBehind[1] = second most recent
  36. private char[] lookBehind = new char[] { OUT_OF_BOUNDS, OUT_OF_BOUNDS };
  37. CharIndexedInputStream(InputStream str, int index) {
  38. if (str instanceof BufferedInputStream) br = (BufferedInputStream) str;
  39. else br = new BufferedInputStream(str,BUFFER_INCREMENT);
  40. next();
  41. if (index > 0) move(index);
  42. }
  43. private boolean next() {
  44. if (end == 1) return false;
  45. end--; // closer to end
  46. try {
  47. if (index != -1) {
  48. br.reset();
  49. }
  50. int i = br.read();
  51. br.mark(bufsize);
  52. if (i == -1) {
  53. end = 1;
  54. cached = OUT_OF_BOUNDS;
  55. return false;
  56. }
  57. cached = (char) i;
  58. index = 1;
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. cached = OUT_OF_BOUNDS;
  62. return false;
  63. }
  64. return true;
  65. }
  66. public char charAt(int index) {
  67. if (index == 0) {
  68. return cached;
  69. } else if (index >= end) {
  70. return OUT_OF_BOUNDS;
  71. } else if (index == -1) {
  72. return lookBehind[0];
  73. } else if (index == -2) {
  74. return lookBehind[1];
  75. } else if (index < -2) {
  76. return OUT_OF_BOUNDS;
  77. } else if (index >= bufsize) {
  78. // Allocate more space in the buffer.
  79. try {
  80. while (bufsize <= index) bufsize += BUFFER_INCREMENT;
  81. br.reset();
  82. br.mark(bufsize);
  83. br.skip(index-1);
  84. } catch (IOException e) { }
  85. } else if (this.index != index) {
  86. try {
  87. br.reset();
  88. br.skip(index-1);
  89. } catch (IOException e) { }
  90. }
  91. char ch = OUT_OF_BOUNDS;
  92. try {
  93. int i = br.read();
  94. this.index = index+1; // this.index is index of next pos relative to charAt(0)
  95. if (i == -1) {
  96. // set flag that next should fail next time?
  97. end = index;
  98. return ch;
  99. }
  100. ch = (char) i;
  101. } catch (IOException ie) { }
  102. return ch;
  103. }
  104. public boolean move(int index) {
  105. // move read position [index] clicks from 'charAt(0)'
  106. boolean retval = true;
  107. while (retval && (index-- > 0)) retval = next();
  108. return retval;
  109. }
  110. public boolean isValid() {
  111. return (cached != OUT_OF_BOUNDS);
  112. }
  113. }