Skip to content

Commit 32b9dd2

Browse files
committed
[optimize] Replace StringWriter with StringBuilderWriter
1 parent d0f43e3 commit 32b9dd2

File tree

71 files changed

+1752
-431
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1752
-431
lines changed

exist-core/pom.xml

Lines changed: 88 additions & 2 deletions
Large diffs are not rendered by default.

exist-core/src/main/java/org/exist/backup/ZipWriter.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -21,6 +45,8 @@
2145
*/
2246
package org.exist.backup;
2347

48+
import org.apache.commons.io.output.StringBuilderWriter;
49+
2450
import java.io.*;
2551
import java.nio.charset.StandardCharsets;
2652
import java.nio.file.Files;
@@ -38,7 +64,7 @@ public class ZipWriter implements BackupWriter
3864
{
3965
private String currentPath;
4066
private final ZipOutputStream out;
41-
private StringWriter contents;
67+
private StringBuilderWriter contents;
4268
private boolean dataWritten = false;
4369

4470
public ZipWriter(final String zipFile, final String collection) throws IOException
@@ -53,9 +79,9 @@ public ZipWriter(final Path zipFile, final String collection) throws IOException
5379
currentPath = collection;
5480
}
5581

56-
public Writer newContents() throws IOException
82+
public Writer newContents()
5783
{
58-
contents = new StringWriter();
84+
contents = new StringBuilderWriter();
5985
return( contents );
6086
}
6187

exist-core/src/main/java/org/exist/client/ClientFrame.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*/
4646
package org.exist.client;
4747

48+
import org.apache.commons.io.output.StringBuilderWriter;
4849
import org.exist.SystemProperties;
4950
import org.exist.backup.Backup;
5051
import org.exist.backup.CreateBackupDialog;
@@ -1750,16 +1751,17 @@ public static void showErrorMessage(final String message, final Throwable t) {
17501751
msgArea.setEditable(false);
17511752
msgArea.setBackground(null);
17521753
if (t != null) {
1753-
final StringWriter out = new StringWriter();
1754-
final PrintWriter writer = new PrintWriter(out);
1755-
t.printStackTrace(writer);
1756-
final JTextArea stacktrace = new JTextArea(out.toString(), 20, 50);
1757-
stacktrace.setBackground(null);
1758-
stacktrace.setEditable(false);
1759-
scroll = new JScrollPane(stacktrace);
1760-
scroll.setPreferredSize(new Dimension(250, 300));
1761-
scroll.setBorder(BorderFactory
1754+
try (final StringBuilderWriter out = new StringBuilderWriter();
1755+
final PrintWriter writer = new PrintWriter(out)) {
1756+
t.printStackTrace(writer);
1757+
final JTextArea stacktrace = new JTextArea(out.toString(), 20, 50);
1758+
stacktrace.setBackground(null);
1759+
stacktrace.setEditable(false);
1760+
scroll = new JScrollPane(stacktrace);
1761+
scroll.setPreferredSize(new Dimension(250, 300));
1762+
scroll.setBorder(BorderFactory
17621763
.createTitledBorder(Messages.getString("ClientFrame.215"))); //$NON-NLS-1$
1764+
}
17631765
}
17641766
final JOptionPane optionPane = new JOptionPane();
17651767
optionPane.setMessage(new Object[]{msgArea, scroll});
@@ -1783,7 +1785,7 @@ public static int showErrorMessageQuery(final String message, final Throwable t)
17831785

17841786
JScrollPane scrollStacktrace = null;
17851787
if (t != null) {
1786-
try (final StringWriter out = new StringWriter();
1788+
try (final StringBuilderWriter out = new StringBuilderWriter();
17871789
final PrintWriter writer = new PrintWriter(out)) {
17881790
t.printStackTrace(writer);
17891791
final JTextArea stacktrace = new JTextArea(out.toString(), 20, 50);
@@ -1795,8 +1797,6 @@ public static int showErrorMessageQuery(final String message, final Throwable t)
17951797
scrollStacktrace.setPreferredSize(new Dimension(600, 300));
17961798
scrollStacktrace.setBorder(BorderFactory
17971799
.createTitledBorder(Messages.getString("ClientFrame.218"))); //$NON-NLS-1$
1798-
} catch (final IOException ioe) {
1799-
ioe.printStackTrace();
18001800
}
18011801
}
18021802

exist-core/src/main/java/org/exist/client/DocumentView.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import javax.swing.border.BevelBorder;
8484
import javax.xml.transform.OutputKeys;
8585

86+
import org.apache.commons.io.output.StringBuilderWriter;
8687
import org.exist.security.Account;
8788
import org.exist.storage.ElementIndex;
8889
import org.exist.util.ProgressIndicator;
@@ -186,16 +187,17 @@ private static void showErrorMessage(String message, Throwable t) {
186187
msgArea.setEditable(false);
187188
msgArea.setBackground(null);
188189
if (t != null) {
189-
final StringWriter out = new StringWriter();
190-
final PrintWriter writer = new PrintWriter(out);
191-
t.printStackTrace(writer);
192-
final JTextArea stacktrace = new JTextArea(out.toString(), 20, 50);
193-
stacktrace.setBackground(null);
194-
stacktrace.setEditable(false);
195-
scroll = new JScrollPane(stacktrace);
196-
scroll.setPreferredSize(new Dimension(250, 300));
197-
scroll.setBorder(BorderFactory
190+
try (final StringBuilderWriter out = new StringBuilderWriter();
191+
final PrintWriter writer = new PrintWriter(out)) {
192+
t.printStackTrace(writer);
193+
final JTextArea stacktrace = new JTextArea(out.toString(), 20, 50);
194+
stacktrace.setBackground(null);
195+
stacktrace.setEditable(false);
196+
scroll = new JScrollPane(stacktrace);
197+
scroll.setPreferredSize(new Dimension(250, 300));
198+
scroll.setBorder(BorderFactory
198199
.createTitledBorder("Exception Stacktrace:")); //$NON-NLS-1$
200+
}
199201
}
200202
final JOptionPane optionPane = new JOptionPane();
201203
optionPane.setMessage(new Object[]{msgArea, scroll});

exist-core/src/main/java/org/exist/client/QueryDialog.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import javax.swing.event.PopupMenuListener;
8787
import javax.xml.transform.OutputKeys;
8888

89+
import org.apache.commons.io.output.StringBuilderWriter;
8990
import org.exist.security.PermissionDeniedException;
9091
import org.exist.util.Holder;
9192
import org.exist.xmldb.EXistXQueryService;
@@ -538,9 +539,10 @@ private void compileQuery() {
538539
tCompiled = t1 - t0;
539540

540541
// In this way we can see the parsed structure meanwhile the query is
541-
final StringWriter writer = new StringWriter();
542-
service.dump(compiled, writer);
543-
exprDisplay.setText(writer.toString());
542+
try (final StringBuilderWriter writer = new StringBuilderWriter()) {
543+
service.dump(compiled, writer);
544+
exprDisplay.setText(writer.toString());
545+
}
544546
resultTabs.setSelectedComponent(exprDisplayScrollPane);
545547

546548
statusMessage.setText(Messages.getString(QUERY_DIALOG_COMPILATION) + ": " + tCompiled + "ms");
@@ -610,9 +612,10 @@ public void run() {
610612
tCompiled = t1 - t0;
611613

612614
// In this way we can see the parsed structure meanwhile the query is
613-
StringWriter writer = new StringWriter();
614-
service.dump(compiled, writer);
615-
exprDisplay.setText(writer.toString());
615+
try (final StringBuilderWriter writer = new StringBuilderWriter()) {
616+
service.dump(compiled, writer);
617+
exprDisplay.setText(writer.toString());
618+
}
616619

617620
result = service.execute(compiled);
618621
tResult = System.currentTimeMillis() - t1;
@@ -623,9 +626,10 @@ public void run() {
623626
}
624627

625628
// jmfg: Is this still needed? I don't think so
626-
writer = new StringWriter();
627-
service.dump(compiled, writer);
628-
exprDisplay.setText(writer.toString());
629+
try (final StringBuilderWriter writer = new StringBuilderWriter()) {
630+
service.dump(compiled, writer);
631+
exprDisplay.setText(writer.toString());
632+
}
629633

630634
statusMessage.setText(Messages.getString("QueryDialog.retrievingmessage"));
631635
final int howmany = count.getNumber().intValue();

exist-core/src/main/java/org/exist/config/Configurator.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -47,6 +71,7 @@
4771
import javax.xml.parsers.SAXParser;
4872
import javax.xml.parsers.SAXParserFactory;
4973

74+
import org.apache.commons.io.output.StringBuilderWriter;
5075
import org.apache.logging.log4j.LogManager;
5176
import org.apache.logging.log4j.Logger;
5277
import org.exist.Database;
@@ -1267,24 +1292,22 @@ public static DocumentImpl save(final DBBroker broker, final Configurable instan
12671292
protected static final Set<FullXmldbURI> saving = new ConcurrentSkipListSet<>();
12681293

12691294
public static DocumentImpl save(final Configurable instance, final DBBroker broker, final Collection collection, final XmldbURI uri) throws IOException, ConfigurationException {
1270-
1271-
final StringWriter writer = new StringWriter();
1272-
final SAXSerializer serializer = new SAXSerializer(writer, null);
1273-
1274-
try {
1295+
final String data;
1296+
try (final StringBuilderWriter writer = new StringBuilderWriter()) {
1297+
final SAXSerializer serializer = new SAXSerializer(writer, null);
1298+
12751299
serializer.startDocument();
12761300
serialize(instance, serializer);
12771301
serializer.endDocument();
1278-
1302+
1303+
data = writer.toString();
1304+
if (data == null || data.isEmpty()) {
1305+
return null;
1306+
}
12791307
} catch (final SAXException saxe) {
12801308
throw new ConfigurationException(saxe.getMessage(), saxe);
12811309
}
12821310

1283-
final String data = writer.toString();
1284-
if (data == null || data.length() == 0) {
1285-
return null;
1286-
}
1287-
12881311
FullXmldbURI fullURI = null;
12891312
final BrokerPool pool = broker.getBrokerPool();
12901313
final TransactionManager transact = pool.getTransactionManager();

exist-core/src/main/java/org/exist/dom/persistent/XMLUtil.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -21,6 +45,7 @@
2145
*/
2246
package org.exist.dom.persistent;
2347

48+
import org.apache.commons.io.output.StringBuilderWriter;
2449
import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
2550
import org.apache.logging.log4j.LogManager;
2651
import org.apache.logging.log4j.Logger;
@@ -32,7 +57,6 @@
3257
import javax.xml.transform.TransformerException;
3358
import java.io.IOException;
3459
import java.io.InputStream;
35-
import java.io.StringWriter;
3660
import java.nio.charset.Charset;
3761
import java.nio.file.Files;
3862
import java.nio.file.Path;
@@ -52,14 +76,14 @@ private XMLUtil() {
5276
}
5377

5478
public static final String dump(final DocumentFragment fragment) {
55-
final StringWriter writer = new StringWriter();
56-
final DOMSerializer serializer = new DOMSerializer(writer, null);
57-
try {
79+
try (final StringBuilderWriter writer = new StringBuilderWriter()) {
80+
final DOMSerializer serializer = new DOMSerializer(writer, null);
5881
serializer.serialize(fragment);
82+
return writer.toString();
5983
} catch(final TransformerException e) {
6084
//Nothing to do ?
6185
}
62-
return writer.toString();
86+
return null;
6387
}
6488

6589
public static final String encodeAttrMarkup(final String str) {

exist-core/src/main/java/org/exist/http/RESTServer.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*/
4646
package org.exist.http;
4747

48+
import org.apache.commons.io.output.StringBuilderWriter;
4849
import org.apache.logging.log4j.LogManager;
4950
import org.apache.logging.log4j.Logger;
5051
import org.exist.EXistException;
@@ -1301,15 +1302,16 @@ private String getRequestContent(final HttpServletRequest request) throws IOExce
13011302

13021303
final InputStream is = request.getInputStream();
13031304
final Reader reader = new InputStreamReader(is, encoding);
1304-
final StringWriter content = new StringWriter();
1305-
final char ch[] = new char[4096];
1306-
int len = 0;
1307-
while ((len = reader.read(ch)) > -1) {
1308-
content.write(ch, 0, len);
1309-
}
1305+
try (final StringBuilderWriter content = new StringBuilderWriter()) {
1306+
final char ch[] = new char[4096];
1307+
int len = 0;
1308+
while ((len = reader.read(ch)) > -1) {
1309+
content.write(ch, 0, len);
1310+
}
13101311

1311-
final String xml = content.toString();
1312-
return xml;
1312+
final String xml = content.toString();
1313+
return xml;
1314+
}
13131315
}
13141316

13151317
/**

exist-core/src/main/java/org/exist/management/client/JMXtoXML.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
package org.exist.management.client;
4747

4848
import java.io.IOException;
49-
import java.io.StringWriter;
5049

5150
import static com.evolvedbinary.j8fu.tuple.Tuple.Tuple;
5251
import static java.lang.management.ManagementFactory.CLASS_LOADING_MXBEAN_NAME;
@@ -71,6 +70,7 @@
7170
import javax.xml.transform.TransformerException;
7271

7372
import com.evolvedbinary.j8fu.tuple.Tuple2;
73+
import org.apache.commons.io.output.StringBuilderWriter;
7474
import org.apache.logging.log4j.LogManager;
7575
import org.apache.logging.log4j.Logger;
7676
import org.exist.dom.QName;
@@ -208,10 +208,11 @@ public void connect(final String address, final int port) throws MalformedURLExc
208208
*/
209209
public String generateReport(final String categories[]) throws TransformerException {
210210
final Element root = generateXMLReport(null, categories);
211-
final StringWriter writer = new StringWriter();
212-
final DOMSerializer streamer = new DOMSerializer(writer, defaultProperties);
213-
streamer.serialize(root);
214-
return writer.toString();
211+
try (final StringBuilderWriter writer = new StringBuilderWriter()) {
212+
final DOMSerializer streamer = new DOMSerializer(writer, defaultProperties);
213+
streamer.serialize(root);
214+
return writer.toString();
215+
}
215216
}
216217

217218
/**

0 commit comments

Comments
 (0)