/interpreter/tags/at2-build270707/src/edu/vub/util/regexp/CharIndexedInputStream.java

http://ambienttalk.googlecode.com/ · Java · 159 lines · 95 code · 20 blank · 44 comment · 32 complexity · 38b02fafe663767fbd4236314b540f9b MD5 · raw file

  1. /* gnu/regexp/CharIndexedInputStream.java
  2. Copyright (C) 1998-2001, 2004, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package edu.vub.util.regexp;
  32. import java.io.BufferedInputStream;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. // TODO: move(x) shouldn't rely on calling next() x times
  36. class CharIndexedInputStream implements CharIndexed {
  37. private static final int BUFFER_INCREMENT = 1024;
  38. private static final int UNKNOWN = Integer.MAX_VALUE; // value for end
  39. private BufferedInputStream br;
  40. // so that we don't try to reset() right away
  41. private int index = -1;
  42. private int bufsize = BUFFER_INCREMENT;
  43. private int end = UNKNOWN;
  44. private char cached = OUT_OF_BOUNDS;
  45. // Big enough for a \r\n pair
  46. // lookBehind[0] = most recent
  47. // lookBehind[1] = second most recent
  48. private char[] lookBehind = new char[] { OUT_OF_BOUNDS, OUT_OF_BOUNDS };
  49. CharIndexedInputStream(InputStream str, int index) {
  50. if (str instanceof BufferedInputStream) br = (BufferedInputStream) str;
  51. else br = new BufferedInputStream(str,BUFFER_INCREMENT);
  52. next();
  53. if (index > 0) move(index);
  54. }
  55. private boolean next() {
  56. if (end == 1) return false;
  57. end--; // closer to end
  58. try {
  59. if (index != -1) {
  60. br.reset();
  61. }
  62. int i = br.read();
  63. br.mark(bufsize);
  64. if (i == -1) {
  65. end = 1;
  66. cached = OUT_OF_BOUNDS;
  67. return false;
  68. }
  69. cached = (char) i;
  70. index = 1;
  71. } catch (IOException e) {
  72. e.printStackTrace();
  73. cached = OUT_OF_BOUNDS;
  74. return false;
  75. }
  76. return true;
  77. }
  78. public char charAt(int index) {
  79. if (index == 0) {
  80. return cached;
  81. } else if (index >= end) {
  82. return OUT_OF_BOUNDS;
  83. } else if (index == -1) {
  84. return lookBehind[0];
  85. } else if (index == -2) {
  86. return lookBehind[1];
  87. } else if (index < -2) {
  88. return OUT_OF_BOUNDS;
  89. } else if (index >= bufsize) {
  90. // Allocate more space in the buffer.
  91. try {
  92. while (bufsize <= index) bufsize += BUFFER_INCREMENT;
  93. br.reset();
  94. br.mark(bufsize);
  95. br.skip(index-1);
  96. } catch (IOException e) { }
  97. } else if (this.index != index) {
  98. try {
  99. br.reset();
  100. br.skip(index-1);
  101. } catch (IOException e) { }
  102. }
  103. char ch = OUT_OF_BOUNDS;
  104. try {
  105. int i = br.read();
  106. this.index = index+1; // this.index is index of next pos relative to charAt(0)
  107. if (i == -1) {
  108. // set flag that next should fail next time?
  109. end = index;
  110. return ch;
  111. }
  112. ch = (char) i;
  113. } catch (IOException ie) { }
  114. return ch;
  115. }
  116. public boolean move(int index) {
  117. // move read position [index] clicks from 'charAt(0)'
  118. boolean retval = true;
  119. while (retval && (index-- > 0)) retval = next();
  120. return retval;
  121. }
  122. public boolean isValid() {
  123. return (cached != OUT_OF_BOUNDS);
  124. }
  125. public CharIndexed lookBehind(int index, int length) {
  126. throw new UnsupportedOperationException(
  127. "difficult to look behind for an input stream");
  128. }
  129. public int length() {
  130. throw new UnsupportedOperationException(
  131. "difficult to tell the length for an input stream");
  132. }
  133. }