From c5e6e4bd95d02c84be86bcc590f0addcb8b1b7ad Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 18 Feb 2003 14:06:22 +0000 Subject: [PATCH] Replace algorithm with a less recursive one - avoid StackOverflow on large files. PR: 15528 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274120 13f79535-47bb-0310-9956-ffa450edef68 --- .../tools/ant/filters/LineContains.java | 6 ++--- .../tools/ant/filters/LineContainsRegExp.java | 26 ++++++++++++------- .../tools/ant/filters/StripLineComments.java | 19 ++++++++++---- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/main/org/apache/tools/ant/filters/LineContains.java b/src/main/org/apache/tools/ant/filters/LineContains.java index 3a3916b17..d749153b6 100644 --- a/src/main/org/apache/tools/ant/filters/LineContains.java +++ b/src/main/org/apache/tools/ant/filters/LineContains.java @@ -71,9 +71,9 @@ import org.apache.tools.ant.types.Parameter; * * Or: * - *
<filterreader classname="org.apache.tools.ant.filters.LineContains">
- *    <param type="contains" value="foo"/>
- *    <param type="contains" value="bar"/>
+ * 
<filterreader classname="org.apache.tools.ant.filters.LineContains">
+ *    <param type="contains" value="foo"/>
+ *    <param type="contains" value="bar"/>
  * </filterreader>
* * This will include only those lines that contain foo and diff --git a/src/main/org/apache/tools/ant/filters/LineContainsRegExp.java b/src/main/org/apache/tools/ant/filters/LineContainsRegExp.java index 0eea46fa0..67d8fd2e6 100644 --- a/src/main/org/apache/tools/ant/filters/LineContainsRegExp.java +++ b/src/main/org/apache/tools/ant/filters/LineContainsRegExp.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2002 The Apache Software Foundation. All rights + * Copyright (c) 2002-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -88,7 +88,7 @@ public final class LineContainsRegExp /** Vector that holds the expressions that input lines must contain. */ private Vector regexps = new Vector(); - /** + /** * Remaining line to be read from this filter, or null if * the next call to read() should read the original stream * to find the next matching line. @@ -97,7 +97,7 @@ public final class LineContainsRegExp /** * Constructor for "dummy" instances. - * + * * @see BaseFilterReader#BaseFilterReader() */ public LineContainsRegExp() { @@ -121,9 +121,9 @@ public final class LineContainsRegExp * * @return the next character in the resulting stream, or -1 * if the end of the resulting stream has been reached - * + * * @exception IOException if the underlying stream throws an IOException - * during reading + * during reading */ public final int read() throws IOException { if (!getInitialized()) { @@ -142,10 +142,9 @@ public final class LineContainsRegExp } } else { line = readLine(); - if (line == null) { - ch = -1; - } else { - final int regexpsSize = regexps.size(); + final int regexpsSize = regexps.size(); + + while (line != null) { for (int i = 0; i < regexpsSize; i++) { RegularExpression regexp = (RegularExpression) regexps.elementAt(i); @@ -157,6 +156,15 @@ public final class LineContainsRegExp } } + if (line == null) { + // line didn't match + line = readLine(); + } else { + break; + } + } + + if (line != null) { return read(); } } diff --git a/src/main/org/apache/tools/ant/filters/StripLineComments.java b/src/main/org/apache/tools/ant/filters/StripLineComments.java index 60ae53cec..f7ad7a746 100644 --- a/src/main/org/apache/tools/ant/filters/StripLineComments.java +++ b/src/main/org/apache/tools/ant/filters/StripLineComments.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2002 The Apache Software Foundation. All rights + * Copyright (c) 2002-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -142,10 +142,9 @@ public final class StripLineComments } } else { line = readLine(); - if (line == null) { - ch = -1; - } else { - int commentsSize = comments.size(); + final int commentsSize = comments.size(); + + while (line != null) { for (int i = 0; i < commentsSize; i++) { String comment = (String) comments.elementAt(i); if (line.startsWith(comment)) { @@ -153,6 +152,16 @@ public final class StripLineComments break; } } + + if (line == null) { + // line started with comment + line = readLine(); + } else { + break; + } + } + + if (line != null) { return read(); } }