You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/06/21 13:30:19 UTC

[commons-io] branch master updated (950fc0a4 -> fffa8406)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


    from 950fc0a4 Inline single constant reference to other constant
     new 92a67fcc Format tweak
     new 99106fd1 Whitespace and name tweaks
     new e2d44764 Reuse StandardCharsets
     new eda19377 Fix exception message typo
     new fffa8406 Use Objects.requireNonNull()

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../java/org/apache/commons/io/LineIterator.java   |  5 +-
 .../apache/commons/io/input/BOMInputStream.java    |  2 +-
 .../apache/commons/io/input/XmlStreamReader.java   | 53 ++++++++++------------
 .../io/output/DeferredFileOutputStream.java        | 19 ++++----
 .../org/apache/commons/io/LineIteratorTest.java    | 31 +++----------
 .../io/output/DeferredFileOutputStreamTest.java    |  2 +-
 6 files changed, 43 insertions(+), 69 deletions(-)


[commons-io] 05/05: Use Objects.requireNonNull()

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git

commit fffa8406955ca838a53e5092a6300df4d579baa9
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jun 21 09:30:12 2022 -0400

    Use Objects.requireNonNull()
    
    Javadoc. Reuse own API.
---
 .../java/org/apache/commons/io/LineIterator.java   |  5 ++--
 .../io/output/DeferredFileOutputStream.java        | 19 ++++++-------
 .../org/apache/commons/io/LineIteratorTest.java    | 31 +++++-----------------
 .../io/output/DeferredFileOutputStreamTest.java    |  2 +-
 4 files changed, 17 insertions(+), 40 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/LineIterator.java b/src/main/java/org/apache/commons/io/LineIterator.java
index 67fe5728..b1b05d5c 100644
--- a/src/main/java/org/apache/commons/io/LineIterator.java
+++ b/src/main/java/org/apache/commons/io/LineIterator.java
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.io.Reader;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
+import java.util.Objects;
 
 /**
  * An Iterator over the lines in a {@link Reader}.
@@ -78,9 +79,7 @@ public class LineIterator implements Iterator<String>, Closeable {
      * @throws IllegalArgumentException if the reader is null
      */
     public LineIterator(final Reader reader) throws IllegalArgumentException {
-        if (reader == null) {
-            throw new IllegalArgumentException("Reader must not be null");
-        }
+        Objects.requireNonNull(reader, "reader");
         if (reader instanceof BufferedReader) {
             bufferedReader = (BufferedReader) reader;
         } else {
diff --git a/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java b/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
index 143587a4..4e419fab 100644
--- a/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
+++ b/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
@@ -22,6 +22,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.util.Objects;
 import java.util.function.Supplier;
 
 import org.apache.commons.io.file.PathUtils;
@@ -140,9 +141,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
     public DeferredFileOutputStream(final int threshold, final int initialBufferSize, final String prefix,
         final String suffix, final File directory) {
         this(threshold, null, prefix, suffix, directory, initialBufferSize);
-        if (prefix == null) {
-            throw new IllegalArgumentException("Temporary file prefix is missing");
-        }
+        Objects.requireNonNull("prefix", prefix);
         if (initialBufferSize < 0) {
             throw new IllegalArgumentException("Initial buffer size must be atleast 0.");
         }
@@ -163,9 +162,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
     public DeferredFileOutputStream(final int threshold, final String prefix, final String suffix,
         final File directory) {
         this(threshold, null, prefix, suffix, directory, AbstractByteArrayOutputStream.DEFAULT_SIZE);
-        if (prefix == null) {
-            throw new IllegalArgumentException("Temporary file prefix is missing");
-        }
+        Objects.requireNonNull(prefix, "prefix");
     }
 
     /**
@@ -180,7 +177,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
     }
 
     /**
-     * Returns the data for this output stream as an array of bytes, assuming that the data has been retained in memory.
+     * Gets the data for this output stream as an array of bytes, assuming that the data has been retained in memory.
      * If the data was written to disk, this method returns {@code null}.
      *
      * @return The data for this output stream, or {@code null} if no such data is available.
@@ -190,7 +187,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
     }
 
     /**
-     * Returns either the output file specified in the constructor or the temporary file created or null.
+     * Gets either the output file specified in the constructor or the temporary file created or null.
      * <p>
      * If the constructor specifying the file is used then it returns that same output file, even when threshold has not
      * been reached.
@@ -205,7 +202,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
     }
 
     /**
-     * Returns the current output stream. This may be memory based or disk based, depending on the current state with
+     * Gets the current output stream. This may be memory based or disk based, depending on the current state with
      * respect to the threshold.
      *
      * @return The underlying output stream.
@@ -218,7 +215,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
     }
 
     /**
-     * Determines whether or not the data for this output stream has been retained in memory.
+     * Tests whether or not the data for this output stream has been retained in memory.
      *
      * @return {@code true} if the data is available in memory; {@code false} otherwise.
      */
@@ -251,7 +248,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
     }
 
     /**
-     * Gets the current contents of this byte stream as an {@link InputStream}.
+     * Converts the current contents of this byte stream to an {@link InputStream}.
      * If the data for this output stream has been retained in memory, the
      * returned stream is backed by buffers of {@code this} stream,
      * avoiding memory allocation and copy, thus saving space and time.<br>
diff --git a/src/test/java/org/apache/commons/io/LineIteratorTest.java b/src/test/java/org/apache/commons/io/LineIteratorTest.java
index e6cb8fde..a8c1ea5f 100644
--- a/src/test/java/org/apache/commons/io/LineIteratorTest.java
+++ b/src/test/java/org/apache/commons/io/LineIteratorTest.java
@@ -38,8 +38,7 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
 
 /**
- * This is used to test LineIterator for correctness.
- *
+ * Tests {@link LineIterator}.
  */
 public class LineIteratorTest {
 
@@ -54,11 +53,7 @@ public class LineIteratorTest {
             }
             assertFalse(iterator.hasNext(), "No more expected");
         } finally {
-            try {
-                IOUtils.close(iterator);
-            } catch (final IOException ignored) {
-                // Ignored
-            }
+            IOUtils.closeQuietly(iterator);
         }
     }
 
@@ -105,9 +100,6 @@ public class LineIteratorTest {
         return lines;
     }
 
-
-    // -----------------------------------------------------------------------
-
     /**
      * Utility method to create and test a file with a specified number of lines.
      *
@@ -166,21 +158,17 @@ public class LineIteratorTest {
 
     @Test
     public void testConstructor() {
-        assertThrows(IllegalArgumentException.class, () -> {
-            try (LineIterator li = new LineIterator(null)) {
-            }
-        });
+        assertThrows(NullPointerException.class, () -> new LineIterator(null));
     }
 
-    private void testFiltering(final List<String> lines, final Reader reader) {
-        final LineIterator iterator = new LineIterator(reader) {
+    private void testFiltering(final List<String> lines, final Reader reader) throws IOException {
+        try (LineIterator iterator = new LineIterator(reader) {
             @Override
             protected boolean isValidLine(final String line) {
                 final char c = line.charAt(line.length() - 1);
                 return (c - 48) % 3 != 1;
             }
-        };
-        try {
+        }) {
             assertThrows(UnsupportedOperationException.class, iterator::remove);
 
             int idx = 0;
@@ -202,12 +190,6 @@ public class LineIteratorTest {
             // try calling next() after file processed
             assertThrows(NoSuchElementException.class, iterator::next);
             assertThrows(NoSuchElementException.class, iterator::nextLine);
-        } finally {
-            try {
-                IOUtils.close(iterator);
-            } catch (final IOException ignored) {
-                // Ignored
-            }
         }
     }
 
@@ -223,7 +205,6 @@ public class LineIteratorTest {
         this.testFiltering(lines, reader);
     }
 
-    // -----------------------------------------------------------------------
     @Test
     public void testFilteringFileReader() throws Exception {
         final String encoding = "UTF-8";
diff --git a/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
index 30a8ac30..0dfb4ecf 100644
--- a/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
@@ -244,7 +244,7 @@ public class DeferredFileOutputStreamTest {
         final String prefix = null;
         final String suffix = ".out";
         final File tempDir = FileUtils.current();
-        assertThrows(IllegalArgumentException.class, () -> new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir).close());
+        assertThrows(NullPointerException.class, () -> new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir).close());
     }
 
     /**


[commons-io] 03/05: Reuse StandardCharsets

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git

commit e2d4476418ec53037fbbf0c36ff35f5185ed1895
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jun 21 08:11:42 2022 -0400

    Reuse StandardCharsets
    
    Javadoc
---
 .../java/org/apache/commons/io/input/XmlStreamReader.java | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/input/XmlStreamReader.java b/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
index 0dec7002..2efeb39b 100644
--- a/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
+++ b/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
@@ -27,6 +27,7 @@ import java.io.StringReader;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.text.MessageFormat;
@@ -72,19 +73,19 @@ import org.apache.commons.io.IOUtils;
  */
 public class XmlStreamReader extends Reader {
 
-    private static final String UTF_8 = "UTF-8";
+    private static final String UTF_8 = StandardCharsets.UTF_8.name();
 
-    private static final String US_ASCII = "US-ASCII";
+    private static final String US_ASCII = StandardCharsets.US_ASCII.name();
 
-    private static final String UTF_16BE = "UTF-16BE";
+    private static final String UTF_16BE = StandardCharsets.UTF_16BE.name();
 
-    private static final String UTF_16LE = "UTF-16LE";
+    private static final String UTF_16LE = StandardCharsets.UTF_16LE.name();
 
     private static final String UTF_32BE = "UTF-32BE";
 
     private static final String UTF_32LE = "UTF-32LE";
 
-    private static final String UTF_16 = "UTF-16";
+    private static final String UTF_16 = StandardCharsets.UTF_16.name();
 
     private static final String UTF_32 = "UTF-32";
 
@@ -446,7 +447,7 @@ public class XmlStreamReader extends Reader {
      *        relaxed.
      * @throws IOException thrown if there is a problem reading the file.
      * @throws XmlStreamReaderException thrown if the charset encoding could not
-     *         be determined according to the specs.
+     *         be determined according to the specification.
      */
     public XmlStreamReader(final InputStream inputStream, final String httpContentType,
             final boolean lenient) throws IOException {
@@ -492,7 +493,7 @@ public class XmlStreamReader extends Reader {
      * @param defaultEncoding The default encoding
      * @throws IOException thrown if there is a problem reading the file.
      * @throws XmlStreamReaderException thrown if the charset encoding could not
-     *         be determined according to the specs.
+     *         be determined according to the specification.
      */
     @SuppressWarnings("resource") // InputStream is managed through a InputStreamReader in this instance.
     public XmlStreamReader(final InputStream inputStream, final String httpContentType,


[commons-io] 04/05: Fix exception message typo

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git

commit eda19377969aa38a88a5ea989ad7170172578f82
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jun 21 08:12:01 2022 -0400

    Fix exception message typo
---
 src/main/java/org/apache/commons/io/input/BOMInputStream.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/io/input/BOMInputStream.java b/src/main/java/org/apache/commons/io/input/BOMInputStream.java
index ec7f8207..5df9c943 100644
--- a/src/main/java/org/apache/commons/io/input/BOMInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/BOMInputStream.java
@@ -246,7 +246,7 @@ public class BOMInputStream extends ProxyInputStream {
      */
     public boolean hasBOM(final ByteOrderMark bom) throws IOException {
         if (!boms.contains(bom)) {
-            throw new IllegalArgumentException("Stream not configure to detect " + bom);
+            throw new IllegalArgumentException("Stream not configured to detect " + bom);
         }
         getBOM();
         return byteOrderMark != null && byteOrderMark.equals(bom);


[commons-io] 02/05: Whitespace and name tweaks

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git

commit 99106fd1670193815411b931556bdc39f86ba9bd
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jun 21 08:03:20 2022 -0400

    Whitespace and name tweaks
---
 .../apache/commons/io/input/XmlStreamReader.java   | 33 ++++++++++------------
 1 file changed, 15 insertions(+), 18 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/input/XmlStreamReader.java b/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
index 7d666758..0dec7002 100644
--- a/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
+++ b/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
@@ -98,7 +98,7 @@ public class XmlStreamReader extends Reader {
         ByteOrderMark.UTF_32LE
     };
 
-    // UTF_16LE and UTF_32LE have the same two starting BOM bytes.
+    /** UTF_16LE and UTF_32LE have the same two starting BOM bytes. */
     private static final ByteOrderMark[] XML_GUESS_BYTES = {
         new ByteOrderMark(UTF_8,    0x3C, 0x3F, 0x78, 0x6D),
         new ByteOrderMark(UTF_16BE, 0x00, 0x3C, 0x00, 0x3F),
@@ -453,7 +453,6 @@ public class XmlStreamReader extends Reader {
         this(inputStream, httpContentType, lenient, null);
     }
 
-
     /**
      * Constructs a Reader using an InputStream and the associated content-type
      * header. This constructor is lenient regarding the encoding detection.
@@ -547,7 +546,6 @@ public class XmlStreamReader extends Reader {
      *         the URL.
      */
     public XmlStreamReader(final URL url) throws IOException {
-        // TODO URLConnection leak.
         this(Objects.requireNonNull(url, "url").openConnection(), null);
     }
 
@@ -580,14 +578,14 @@ public class XmlStreamReader extends Reader {
         final String contentType = urlConnection.getContentType();
         final InputStream inputStream = urlConnection.getInputStream();
         @SuppressWarnings("resource") // managed by the InputStreamReader tracked by this instance
-        final BOMInputStream bom = new BOMInputStream(new BufferedInputStream(inputStream, IOUtils.DEFAULT_BUFFER_SIZE), false, BOMS);
-        final BOMInputStream pis = new BOMInputStream(bom, true, XML_GUESS_BYTES);
+        final BOMInputStream bomInput = new BOMInputStream(new BufferedInputStream(inputStream, IOUtils.DEFAULT_BUFFER_SIZE), false, BOMS);
+        final BOMInputStream piInput = new BOMInputStream(bomInput, true, XML_GUESS_BYTES);
         if (urlConnection instanceof HttpURLConnection || contentType != null) {
-            this.encoding = processHttpStream(bom, pis, contentType, lenient);
+            this.encoding = processHttpStream(bomInput, piInput, contentType, lenient);
         } else {
-            this.encoding = doRawStream(bom, pis, lenient);
+            this.encoding = doRawStream(bomInput, piInput, lenient);
         }
-        this.reader = new InputStreamReader(pis, encoding);
+        this.reader = new InputStreamReader(piInput, encoding);
     }
 
     /**
@@ -826,21 +824,20 @@ public class XmlStreamReader extends Reader {
     }
 
     /**
-     * Processes a HTTP stream.
+     * Processes an HTTP stream.
      *
-     * @param bom BOMInputStream to detect byte order marks
-     * @param pis BOMInputStream to guess XML encoding
+     * @param bomInput BOMInputStream to detect byte order marks
+     * @param piInput BOMInputStream to guess XML encoding
      * @param httpContentType The HTTP content type
-     * @param lenient indicates if the charset encoding detection should be
-     *        relaxed.
+     * @param lenient indicates if the charset encoding detection should be relaxed.
      * @return the encoding to be used
      * @throws IOException thrown if there is a problem reading the stream.
      */
-    private String processHttpStream(final BOMInputStream bom, final BOMInputStream pis, final String httpContentType,
-        final boolean lenient) throws IOException {
-        final String bomEnc = bom.getBOMCharsetName();
-        final String xmlGuessEnc = pis.getBOMCharsetName();
-        final String xmlEnc = getXmlProlog(pis, xmlGuessEnc);
+    private String processHttpStream(final BOMInputStream bomInput, final BOMInputStream piInput, final String httpContentType, final boolean lenient)
+        throws IOException {
+        final String bomEnc = bomInput.getBOMCharsetName();
+        final String xmlGuessEnc = piInput.getBOMCharsetName();
+        final String xmlEnc = getXmlProlog(piInput, xmlGuessEnc);
         try {
             return calculateHttpEncoding(httpContentType, bomEnc, xmlGuessEnc, xmlEnc, lenient);
         } catch (final XmlStreamReaderException ex) {


[commons-io] 01/05: Format tweak

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git

commit 92a67fccf7396845b27801cd07480cfed30589b9
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jun 21 07:55:42 2022 -0400

    Format tweak
---
 src/main/java/org/apache/commons/io/input/XmlStreamReader.java | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/input/XmlStreamReader.java b/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
index d1e39f49..7d666758 100644
--- a/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
+++ b/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
@@ -789,9 +789,8 @@ public class XmlStreamReader extends Reader {
      * @return the encoding to be used
      * @throws IOException thrown if there is a problem reading the stream.
      */
-    private String doRawStream(final BOMInputStream bom, final BOMInputStream pis, final boolean lenient)
-            throws IOException {
-        final String bomEnc      = bom.getBOMCharsetName();
+    private String doRawStream(final BOMInputStream bom, final BOMInputStream pis, final boolean lenient) throws IOException {
+        final String bomEnc = bom.getBOMCharsetName();
         final String xmlGuessEnc = pis.getBOMCharsetName();
         final String xmlEnc = getXmlProlog(pis, xmlGuessEnc);
         try {