This is an automated email from the ASF dual-hosted git repository. mattsicker pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 2156eee8d1b24f6c98745b70193c93391d03a2b7 Author: Matt Sicker <[email protected]> AuthorDate: Sun Oct 15 20:00:41 2023 -0500 Replace IOUtils with standard Java functions Signed-off-by: Matt Sicker <[email protected]> --- .../java/org/apache/log4j/xml/DOMConfigurator.java | 16 +-- ...llingAppenderDirectWriteWithHtmlLayoutTest.java | 26 ++--- .../appender/rolling/RollingFileManagerTest.java | 10 +- .../core/appender/HttpURLConnectionManager.java | 11 +- .../apache/logging/log4j/core/util/IOUtils.java | 130 --------------------- .../apache/logging/log4j/script/ScriptFile.java | 6 +- 6 files changed, 32 insertions(+), 167 deletions(-) diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/xml/DOMConfigurator.java b/log4j-1.2-api/src/main/java/org/apache/log4j/xml/DOMConfigurator.java index 9e5d9374b3..fb914fcedb 100644 --- a/log4j-1.2-api/src/main/java/org/apache/log4j/xml/DOMConfigurator.java +++ b/log4j-1.2-api/src/main/java/org/apache/log4j/xml/DOMConfigurator.java @@ -17,10 +17,11 @@ package org.apache.log4j.xml; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStreamWriter; import java.io.Reader; -import java.io.StringWriter; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; @@ -40,7 +41,6 @@ import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.ConfigurationSource; import org.apache.logging.log4j.core.config.Configurator; import org.apache.logging.log4j.core.net.UrlConnectionFactory; -import org.apache.logging.log4j.core.util.IOUtils; import org.w3c.dom.Element; /** @@ -74,7 +74,7 @@ public class DOMConfigurator { final Path path = Paths.get(fileName); try (final InputStream inputStream = Files.newInputStream(path)) { final ConfigurationSource source = new ConfigurationSource(inputStream, path); - final LoggerContext context = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false); + final LoggerContext context = LoggerContext.getContext(false); Configuration configuration; configuration = new XmlConfigurationFactory().getConfiguration(context, source); LogManager.getRootLogger().removeAllAppenders(); @@ -112,7 +112,7 @@ public class DOMConfigurator { } private void doConfigure(final ConfigurationSource source) { - final LoggerContext context = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false); + final LoggerContext context = LoggerContext.getContext(false); Configuration configuration; configuration = new XmlConfigurationFactory().getConfiguration(context, source); Configurator.reconfigure(configuration); @@ -131,9 +131,11 @@ public class DOMConfigurator { public void doConfigure(final Reader reader, final LoggerRepository repository) throws FactoryConfigurationError { try { - final StringWriter sw = new StringWriter(); - IOUtils.copy(reader, sw); - doConfigure(new ConfigurationSource(new ByteArrayInputStream(sw.toString().getBytes(StandardCharsets.UTF_8)))); + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + final OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8); + reader.transferTo(writer); + final ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray()); + doConfigure(new ConfigurationSource(inputStream)); } catch (final IOException e) { throw new FactoryConfigurationError(e); } diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectWriteWithHtmlLayoutTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectWriteWithHtmlLayoutTest.java index e051a6794a..23e87a4631 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectWriteWithHtmlLayoutTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectWriteWithHtmlLayoutTest.java @@ -16,10 +16,9 @@ */ package org.apache.logging.log4j.core.appender.rolling; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; import java.io.IOException; +import java.nio.file.Files; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -28,10 +27,8 @@ import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.impl.Log4jLogEvent; import org.apache.logging.log4j.core.layout.HtmlLayout; import org.apache.logging.log4j.core.test.junit.LoggerContextSource; -import org.apache.logging.log4j.core.util.IOUtils; import org.apache.logging.log4j.message.SimpleMessage; import org.apache.logging.log4j.test.junit.CleanUpDirectories; -import org.hamcrest.Matchers; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -40,7 +37,10 @@ import static org.apache.logging.log4j.core.test.hamcrest.FileMatchers.hasName; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.hasItemInArray; -import static org.junit.jupiter.api.Assertions.*; +import static org.hamcrest.Matchers.startsWith; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Tests for LOG4J2-2760 @@ -98,15 +98,13 @@ public class RollingAppenderDirectWriteWithHtmlLayoutTest { for (File file : files) { if (!file.getName().startsWith(prefix)) continue; - try (final BufferedReader reader = new BufferedReader(new FileReader(file))) { - final String data = IOUtils.toString(reader).trim(); - // check that every file starts with the header - assertThat("header in file " + file, data, Matchers.startsWith("<!DOCTYPE")); - assertThat("footer in file " + file, data, endsWith("</html>")); - final Matcher matcher = eventMatcher.matcher(data); - while (matcher.find()) { - foundEvents++; - } + final String data = Files.readString(file.toPath()); + // check that every file starts with the header + assertThat("header in file " + file, data, startsWith("<!DOCTYPE")); + assertThat("footer in file " + file, data, endsWith("</html>")); + final Matcher matcher = eventMatcher.matcher(data); + while (matcher.find()) { + foundEvents++; } } assertEquals(count, foundEvents, "Incorrect number of events read."); diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManagerTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManagerTest.java index b6ae28ec5c..80bec2fdd3 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManagerTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManagerTest.java @@ -16,14 +16,15 @@ */ package org.apache.logging.log4j.core.appender.rolling; -import java.io.*; +import java.io.File; +import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.appender.RollingFileAppender; import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.lookup.StrSubstitutor; -import org.apache.logging.log4j.core.util.IOUtils; import org.junit.Assert; import org.junit.Test; @@ -79,9 +80,8 @@ public class RollingFileManagerTest { Assert.assertEquals(file.getAbsolutePath(), manager.getFileName()); manager.writeToDestination(testContent.getBytes(StandardCharsets.US_ASCII), 0, testContent.length()); } - try (final Reader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.US_ASCII)) { - Assert.assertEquals(testContent, IOUtils.toString(reader)); - } + final String actualContents = Files.readString(file.toPath(), StandardCharsets.US_ASCII); + Assert.assertEquals(testContent, actualContents); } } } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java index 1ab9009480..d603fb0c40 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java @@ -35,7 +35,6 @@ import org.apache.logging.log4j.core.config.ConfigurationException; import org.apache.logging.log4j.core.config.Property; import org.apache.logging.log4j.core.net.ssl.LaxHostnameVerifier; import org.apache.logging.log4j.core.net.ssl.SslConfiguration; -import org.apache.logging.log4j.core.util.IOUtils; public class HttpURLConnectionManager extends HttpManager { @@ -108,11 +107,8 @@ public class HttpURLConnectionManager extends HttpManager { os.write(msg); } - final byte[] buffer = new byte[1024]; try (final InputStream is = urlConnection.getInputStream()) { - while (IOUtils.EOF != is.read(buffer)) { - // empty - } + is.readAllBytes(); } catch (final IOException e) { final StringBuilder errorMessage = new StringBuilder(); try (final InputStream es = urlConnection.getErrorStream()) { @@ -122,10 +118,7 @@ public class HttpURLConnectionManager extends HttpManager { } if (es != null) { errorMessage.append(" - "); - int n; - while (IOUtils.EOF != (n = es.read(buffer))) { - errorMessage.append(new String(buffer, 0, n, CHARSET)); - } + errorMessage.append(new String(es.readAllBytes(), CHARSET)); } } if (urlConnection.getResponseCode() > -1) { diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/IOUtils.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/IOUtils.java deleted file mode 100644 index d152d46689..0000000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/IOUtils.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.core.util; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Reader; -import java.io.Writer; - -/** - * Copied from Apache Commons IO revision 1686747. - */ -public class IOUtils { - - /** - * The default buffer size ({@value}) to use for - * {@link #copyLarge(InputStream, OutputStream)} - * and - * {@link #copyLarge(Reader, Writer)} - */ - private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; - - /** - * Represents the end-of-file (or stream). - */ - public static final int EOF = -1; - - /** - * Copies chars from a <code>Reader</code> to a <code>Writer</code>. - * <p/> - * This method buffers the input internally, so there is no need to use a - * <code>BufferedReader</code>. - * <p/> - * Large streams (over 2GB) will return a chars copied value of - * <code>-1</code> after the copy has completed since the correct - * number of chars cannot be returned as an int. For large streams - * use the <code>copyLarge(Reader, Writer)</code> method. - * - * @param input the <code>Reader</code> to read from - * @param output the <code>Writer</code> to write to - * @return the number of characters copied, or -1 if > Integer.MAX_VALUE - * @throws NullPointerException if the input or output is null - * @throws IOException if an I/O error occurs - * @since 1.1 - */ - public static int copy(final Reader input, final Writer output) throws IOException { - final long count = copyLarge(input, output); - if (count > Integer.MAX_VALUE) { - return -1; - } - return (int) count; - } - - /** - * Copies chars from a large (over 2GB) <code>Reader</code> to a <code>Writer</code>. - * <p/> - * This method buffers the input internally, so there is no need to use a - * <code>BufferedReader</code>. - * <p/> - * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}. - * - * @param input the <code>Reader</code> to read from - * @param output the <code>Writer</code> to write to - * @return the number of characters copied - * @throws NullPointerException if the input or output is null - * @throws IOException if an I/O error occurs - * @since 1.3 - */ - public static long copyLarge(final Reader input, final Writer output) throws IOException { - return copyLarge(input, output, new char[DEFAULT_BUFFER_SIZE]); - } - - /** - * Copies chars from a large (over 2GB) <code>Reader</code> to a <code>Writer</code>. - * <p/> - * This method uses the provided buffer, so there is no need to use a - * <code>BufferedReader</code>. - * <p/> - * - * @param input the <code>Reader</code> to read from - * @param output the <code>Writer</code> to write to - * @param buffer the buffer to be used for the copy - * @return the number of characters copied - * @throws NullPointerException if the input or output is null - * @throws IOException if an I/O error occurs - * @since 2.2 - */ - public static long copyLarge(final Reader input, final Writer output, final char[] buffer) throws IOException { - long count = 0; - int n; - while (EOF != (n = input.read(buffer))) { - output.write(buffer, 0, n); - count += n; - } - return count; - } - - /** - * Gets the contents of a <code>Reader</code> as a String. - * <p/> - * This method buffers the input internally, so there is no need to use a - * <code>BufferedReader</code>. - * - * @param input the <code>Reader</code> to read from - * @return the requested String - * @throws NullPointerException if the input is null - * @throws IOException if an I/O error occurs - */ - public static String toString(final Reader input) throws IOException { - final StringBuilderWriter sw = new StringBuilderWriter(); - copy(input, sw); - return sw.toString(); - } - -} diff --git a/log4j-script/src/main/java/org/apache/logging/log4j/script/ScriptFile.java b/log4j-script/src/main/java/org/apache/logging/log4j/script/ScriptFile.java index fb7617ce97..edc42a6f52 100644 --- a/log4j-script/src/main/java/org/apache/logging/log4j/script/ScriptFile.java +++ b/log4j-script/src/main/java/org/apache/logging/log4j/script/ScriptFile.java @@ -21,6 +21,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; +import java.io.StringWriter; import java.net.URI; import java.nio.charset.Charset; import java.nio.file.Path; @@ -28,7 +29,6 @@ import java.nio.file.Paths; import org.apache.logging.log4j.core.util.ExtensionLanguageMapping; import org.apache.logging.log4j.core.util.FileUtils; -import org.apache.logging.log4j.core.util.IOUtils; import org.apache.logging.log4j.core.util.NetUtils; import org.apache.logging.log4j.plugins.Configurable; import org.apache.logging.log4j.plugins.Plugin; @@ -96,7 +96,9 @@ public class ScriptFile extends AbstractScript { final String scriptText; try (final Reader reader = new InputStreamReader( file != null ? new FileInputStream(file) : uri.toURL().openStream(), actualCharset)) { - scriptText = IOUtils.toString(reader); + final StringWriter writer = new StringWriter(); + reader.transferTo(writer); + scriptText = writer.toString(); } catch (final IOException e) { LOGGER.error("{}: language={}, path={}, actualCharset={}", e.getClass().getSimpleName(), language, filePathOrUri, actualCharset);
