PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/gnu/regexp/REFilterInputStream.java

#
Java | 121 lines | 57 code | 12 blank | 52 comment | 12 complexity | d2a3c6ccbc6374e999b23b44164738d3 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/REFilterInputStream.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.FilterInputStream;
  21. import java.io.InputStream;
  22. /**
  23. * Replaces instances of a given RE found within an InputStream
  24. * with replacement text. The replacements are interpolated into the
  25. * stream when a match is found.
  26. *
  27. * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
  28. * @deprecated This class cannot properly handle all character
  29. * encodings. For proper handling, use the REFilterReader
  30. * class instead.
  31. */
  32. public class REFilterInputStream extends FilterInputStream {
  33. private RE expr;
  34. private String replace;
  35. private String buffer;
  36. private int bufpos;
  37. private int offset;
  38. private CharIndexedInputStream stream;
  39. /**
  40. * Creates an REFilterInputStream. When reading from this stream,
  41. * occurrences of patterns matching the supplied regular expression
  42. * will be replaced with the supplied replacement text (the
  43. * metacharacters $0 through $9 may be used to refer to the full
  44. * match or subexpression matches).
  45. *
  46. * @param stream The InputStream to be filtered.
  47. * @param expr The regular expression to search for.
  48. * @param replace The text pattern to replace matches with.
  49. */
  50. public REFilterInputStream(InputStream stream, RE expr, String replace) {
  51. super(stream);
  52. this.stream = new CharIndexedInputStream(stream,0);
  53. this.expr = expr;
  54. this.replace = replace;
  55. }
  56. /**
  57. * Reads the next byte from the stream per the general contract of
  58. * InputStream.read(). Returns -1 on error or end of stream.
  59. */
  60. public int read() {
  61. // If we have buffered replace data, use it.
  62. if ((buffer != null) && (bufpos < buffer.length())) {
  63. return (int) buffer.charAt(bufpos++);
  64. }
  65. // check if input is at a valid position
  66. if (!stream.isValid()) return -1;
  67. REMatch mymatch = new REMatch(expr.getNumSubs(),offset,0);
  68. if (expr.match(stream, mymatch)) {
  69. mymatch.end[0] = mymatch.index;
  70. mymatch.finish(stream);
  71. stream.move(mymatch.toString().length());
  72. offset += mymatch.toString().length();
  73. buffer = mymatch.substituteInto(replace);
  74. bufpos = 1;
  75. // This is prone to infinite loops if replace string turns out empty.
  76. if (buffer.length() > 0) {
  77. return buffer.charAt(0);
  78. }
  79. }
  80. char ch = stream.charAt(0);
  81. if (ch == CharIndexed.OUT_OF_BOUNDS) return -1;
  82. stream.move(1);
  83. offset++;
  84. return ch;
  85. }
  86. /**
  87. * Returns false. REFilterInputStream does not support mark() and
  88. * reset() methods.
  89. */
  90. public boolean markSupported() {
  91. return false;
  92. }
  93. /** Reads from the stream into the provided array. */
  94. public int read(byte[] b, int off, int len) {
  95. int i;
  96. int ok = 0;
  97. while (len-- > 0) {
  98. i = read();
  99. if (i == -1) return (ok == 0) ? -1 : ok;
  100. b[off++] = (byte) i;
  101. ok++;
  102. }
  103. return ok;
  104. }
  105. /** Reads from the stream into the provided array. */
  106. public int read(byte[] b) {
  107. return read(b,0,b.length);
  108. }
  109. }