PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/gnu/regexp/CharIndexedReader.java

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