This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-repoinit-parser.git
The following commit(s) were added to refs/heads/master by this push:
new 05cae0c SLING-10215 don't stop reading when Reader.read(...) returns
0. (#7)
05cae0c is described below
commit 05cae0cce036f83494f3722a6b693abbc6e182ca
Author: Konrad Windszus <[email protected]>
AuthorDate: Tue Mar 16 16:14:17 2021 +0100
SLING-10215 don't stop reading when Reader.read(...) returns 0. (#7)
Improve memory consumption by preventing the copy of the full repoinit
section
---
pom.xml | 6 +++-
.../parser/impl/RepoInitParserService.java | 39 ++++++++++++++--------
2 files changed, 30 insertions(+), 15 deletions(-)
diff --git a/pom.xml b/pom.xml
index d8d234b..0d78786 100644
--- a/pom.xml
+++ b/pom.xml
@@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.sling</groupId>
<artifactId>sling-bundle-parent</artifactId>
- <version>40</version>
+ <version>41</version>
<relativePath />
</parent>
@@ -39,6 +39,10 @@
<tag>HEAD</tag>
</scm>
+ <properties>
+
<project.build.outputTimestamp>2020-01-22T15:10:15Z</project.build.outputTimestamp>
+ </properties>
+
<build>
<plugins>
<plugin>
diff --git
a/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java
b/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java
index 8f3935e..4fdc817 100644
---
a/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java
+++
b/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java
@@ -18,10 +18,9 @@
*/
package org.apache.sling.repoinit.parser.impl;
+import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
-import java.io.StringReader;
-import java.io.StringWriter;
import java.util.List;
import org.apache.sling.repoinit.parser.RepoInitParser;
@@ -40,14 +39,9 @@ public class RepoInitParserService implements RepoInitParser
{
@Override
public List<Operation> parse(final Reader r) throws
RepoInitParsingException {
// in order to avoid parsing problems with trailing comments we add a
line feed at the end
- try ( final StringWriter sw = new StringWriter()) {
- final char[] buf = new char[2048];
- int l;
- while ( (l = r.read(buf)) > 0 ) {
- sw.write(buf, 0, l);
- }
- try (final StringReader sr = new
StringReader(sw.toString().concat("\n")) ){
- return new RepoInitParserImpl(sr).parse();
+ try (final Reader readerWrapper = new
AddTailingLinefeedFilterReader(r)) {
+ try {
+ return new RepoInitParserImpl(readerWrapper).parse();
} catch (TokenMgrError tme) {
throw new RepoInitParsingException(tme.getMessage(), tme);
} catch (ParseException pe) {
@@ -55,11 +49,28 @@ public class RepoInitParserService implements
RepoInitParser {
}
} catch ( final IOException ioe ) {
throw new RepoInitParsingException(ioe.getMessage(), ioe);
- } finally {
- try {
- r.close();
- } catch(IOException ignore) {
+ }
+ }
+
+ private static final class AddTailingLinefeedFilterReader extends
FilterReader {
+
+ private boolean alreadyAddedNewline;
+
+ protected AddTailingLinefeedFilterReader(Reader in) {
+ super(in);
+ }
+
+ @Override
+ public int read(char[] cbuf, int off, int len) throws IOException {
+ int result = super.read(cbuf, off, len);
+ if (result == -1 && !alreadyAddedNewline) {
+ cbuf[off] = '\n';
+ alreadyAddedNewline = true;
+ return 1;
+ } else {
+ return result;
}
}
+
}
}