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/01/11 14:22:29 UTC

[commons-io] branch master updated (932ca4b -> f3823cb)

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 932ca4b  CharSequenceInputStream maps null Charset and Charset name to the platform default instead of throwing a NullPointerException.
     new 240eda8  Sort members.
     new 5444ad3  Sort members.
     new da09365  Add CharsetDecoders.
     new f3823cb  WriterOutputStream maps null Charset, Charset name, and CharsetEncoder name to the platform default instead of throwing a NullPointerException.

The 4 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:
 src/changes/changes.xml                            |  8 +++-
 .../{CharsetEncoders.java => CharsetDecoders.java} | 20 ++++----
 .../apache/commons/io/charset/CharsetEncoders.java |  6 ++-
 .../org/apache/commons/io/function/IOStreams.java  | 28 ++++++------
 .../commons/io/output/WriterOutputStream.java      | 19 ++++----
 .../org/apache/commons/io/DeleteDirectoryTest.java | 10 ++--
 .../commons/io/FileUtilsCleanDirectoryTest.java    | 30 ++++++------
 .../org/apache/commons/io/IOUtilsCopyTest.java     | 34 +++++++-------
 ...etEncodersTest.java => CharsetDecdersTest.java} | 18 ++++----
 .../io/input/CharSequenceInputStreamTest.java      | 32 ++++++-------
 .../commons/io/input/ReaderInputStreamTest.java    | 46 +++++++++----------
 .../commons/io/output/BrokenOutputStreamTest.java  | 30 ++++++------
 .../apache/commons/io/output/BrokenWriterTest.java | 30 ++++++------
 .../commons/io/output/WriterOutputStreamTest.java  | 53 +++++++++++++++++++++-
 14 files changed, 216 insertions(+), 148 deletions(-)
 copy src/main/java/org/apache/commons/io/charset/{CharsetEncoders.java => CharsetDecoders.java} (62%)
 copy src/test/java/org/apache/commons/io/charset/{CharsetEncodersTest.java => CharsetDecdersTest.java} (71%)

[commons-io] 03/04: Add CharsetDecoders.

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 da09365587fa86b488bcbaecc5abe5bbdc62afd5
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jan 11 09:13:20 2022 -0500

    Add CharsetDecoders.
---
 src/changes/changes.xml                            |  3 ++
 .../{CharsetEncoders.java => CharsetDecoders.java} | 20 ++++----
 .../apache/commons/io/charset/CharsetEncoders.java |  6 ++-
 .../commons/io/charset/CharsetDecdersTest.java     | 54 ++++++++++++++++++++++
 4 files changed, 74 insertions(+), 9 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 2229fd3..151b755 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -302,6 +302,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add CharsetEncoders.
       </action>
+     <action dev="ggregory" type="add" due-to="Gary Gregory">
+        Add CharsetDecoders.
+      </action>
       <!-- UPDATE -->
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Update FileEntry to use FileTime instead of long for file time stamps.
diff --git a/src/main/java/org/apache/commons/io/charset/CharsetEncoders.java b/src/main/java/org/apache/commons/io/charset/CharsetDecoders.java
similarity index 62%
copy from src/main/java/org/apache/commons/io/charset/CharsetEncoders.java
copy to src/main/java/org/apache/commons/io/charset/CharsetDecoders.java
index 815aaef..afb7977 100644
--- a/src/main/java/org/apache/commons/io/charset/CharsetEncoders.java
+++ b/src/main/java/org/apache/commons/io/charset/CharsetDecoders.java
@@ -18,19 +18,23 @@
 package org.apache.commons.io.charset;
 
 import java.nio.charset.Charset;
-import java.nio.charset.CharsetEncoder;
+import java.nio.charset.CharsetDecoder;
 
-public class CharsetEncoders {
+/**
+ * Works with {@link CharsetDecoder}.
+ *
+ * @since 2.12.0
+ */
+public class CharsetDecoders {
 
     /**
-     * Returns the given non-null CharsetEncoder or a new default CharsetEncoder.
+     * Returns the given non-null CharsetDecoder or a new default CharsetDecoder.
      *
-     * @param charsetEncoder The CharsetEncoder to test.
-     * @return the given non-null CharsetEncoder or a new default CharsetEncoder.
-     * @since 2.12.0
+     * @param charsetDecoder The CharsetDecoder to test.
+     * @return the given non-null CharsetDecoder or a new default CharsetDecoder.
      */
-    public static CharsetEncoder toCharsetEncoder(CharsetEncoder charsetEncoder) {
-        return charsetEncoder != null ? charsetEncoder : Charset.defaultCharset().newEncoder();
+    public static CharsetDecoder toCharsetDecoder(CharsetDecoder charsetDecoder) {
+        return charsetDecoder != null ? charsetDecoder : Charset.defaultCharset().newDecoder();
     }
 
 }
diff --git a/src/main/java/org/apache/commons/io/charset/CharsetEncoders.java b/src/main/java/org/apache/commons/io/charset/CharsetEncoders.java
index 815aaef..c3ddcad 100644
--- a/src/main/java/org/apache/commons/io/charset/CharsetEncoders.java
+++ b/src/main/java/org/apache/commons/io/charset/CharsetEncoders.java
@@ -20,6 +20,11 @@ package org.apache.commons.io.charset;
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetEncoder;
 
+/**
+ * Works with {@link CharsetEncoder}.
+ *
+ * @since 2.12.0
+ */
 public class CharsetEncoders {
 
     /**
@@ -27,7 +32,6 @@ public class CharsetEncoders {
      *
      * @param charsetEncoder The CharsetEncoder to test.
      * @return the given non-null CharsetEncoder or a new default CharsetEncoder.
-     * @since 2.12.0
      */
     public static CharsetEncoder toCharsetEncoder(CharsetEncoder charsetEncoder) {
         return charsetEncoder != null ? charsetEncoder : Charset.defaultCharset().newEncoder();
diff --git a/src/test/java/org/apache/commons/io/charset/CharsetDecdersTest.java b/src/test/java/org/apache/commons/io/charset/CharsetDecdersTest.java
new file mode 100644
index 0000000..2595419
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/charset/CharsetDecdersTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.commons.io.charset;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.StandardCharsets;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link CharsetDecoders}.
+ */
+public class CharsetDecdersTest {
+
+    @Test
+    public void testToCharsetDecoders_default() {
+        final CharsetDecoder charsetEncoder = CharsetDecoders.toCharsetDecoder(Charset.defaultCharset().newDecoder());
+        assertNotNull(charsetEncoder);
+        assertEquals(Charset.defaultCharset(), charsetEncoder.charset());
+    }
+
+    @Test
+    public void testToCharsetDecoders_ISO_8859_1() {
+        final CharsetDecoder charsetEncoder = CharsetDecoders.toCharsetDecoder(StandardCharsets.ISO_8859_1.newDecoder());
+        assertNotNull(charsetEncoder);
+        assertEquals(StandardCharsets.ISO_8859_1, charsetEncoder.charset());
+    }
+
+    @Test
+    public void testToCharsetDecoders_null() {
+        final CharsetDecoder charsetEncoder = CharsetDecoders.toCharsetDecoder(null);
+        assertNotNull(charsetEncoder);
+        assertEquals(Charset.defaultCharset(), charsetEncoder.charset());
+    }
+}

[commons-io] 01/04: Sort members.

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 240eda8bd8c1165def702bb53f749a3c42158d87
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jan 11 08:52:16 2022 -0500

    Sort members.
---
 .../org/apache/commons/io/DeleteDirectoryTest.java | 10 ++---
 .../commons/io/FileUtilsCleanDirectoryTest.java    | 30 +++++++-------
 .../org/apache/commons/io/IOUtilsCopyTest.java     | 34 ++++++++--------
 .../io/input/CharSequenceInputStreamTest.java      | 32 +++++++--------
 .../commons/io/input/ReaderInputStreamTest.java    | 46 +++++++++++-----------
 .../commons/io/output/BrokenOutputStreamTest.java  | 30 +++++++-------
 .../apache/commons/io/output/BrokenWriterTest.java | 30 +++++++-------
 7 files changed, 106 insertions(+), 106 deletions(-)

diff --git a/src/test/java/org/apache/commons/io/DeleteDirectoryTest.java b/src/test/java/org/apache/commons/io/DeleteDirectoryTest.java
index 2442f2d..871bb75 100644
--- a/src/test/java/org/apache/commons/io/DeleteDirectoryTest.java
+++ b/src/test/java/org/apache/commons/io/DeleteDirectoryTest.java
@@ -87,6 +87,11 @@ public class DeleteDirectoryTest extends AbstractTempDirTest {
     }
 
     @Test
+    public void testDeleteDirectoryWithPathUtilsOverrideReadOnly() throws IOException {
+        testDeleteDirectory(dir -> PathUtils.deleteDirectory(dir, StandardDeleteOption.OVERRIDE_READ_ONLY));
+    }
+
+    @Test
     @DisabledOnOs(OS.LINUX) // TODO
     public void testDeleteFileCheckParentAccess() throws IOException {
         // Create a test directory
@@ -117,9 +122,4 @@ public class DeleteDirectoryTest extends AbstractTempDirTest {
         assertFalse(Files.isWritable(testDir));
         assertFalse(Files.isExecutable(testDir));
     }
-
-    @Test
-    public void testDeleteDirectoryWithPathUtilsOverrideReadOnly() throws IOException {
-        testDeleteDirectory(dir -> PathUtils.deleteDirectory(dir, StandardDeleteOption.OVERRIDE_READ_ONLY));
-    }
 }
diff --git a/src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTest.java b/src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTest.java
index 8d20782..04ea31f 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTest.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTest.java
@@ -62,6 +62,21 @@ public class FileUtilsCleanDirectoryTest extends AbstractTempDirTest {
         return proc.waitFor() == 0;
     }
 
+    @DisabledOnOs(OS.WINDOWS)
+    @Test
+    public void testCleanDirectoryToForceDelete() throws Exception {
+        final File file = new File(tempDirFile, "restricted");
+        FileUtils.touch(file);
+
+        // 300 = owner: WE.
+        // 500 = owner: RE.
+        // 700 = owner: RWE.
+        assumeTrue(chmod(tempDirFile, 700, false));
+
+        // cleanDirectory calls forceDelete
+        FileUtils.cleanDirectory(tempDirFile);
+    }
+
     @Test
     public void testCleanEmpty() throws Exception {
         assertEquals(0, tempDirFile.list().length);
@@ -100,21 +115,6 @@ public class FileUtilsCleanDirectoryTest extends AbstractTempDirTest {
 
     @DisabledOnOs(OS.WINDOWS)
     @Test
-    public void testCleanDirectoryToForceDelete() throws Exception {
-        final File file = new File(tempDirFile, "restricted");
-        FileUtils.touch(file);
-
-        // 300 = owner: WE.
-        // 500 = owner: RE.
-        // 700 = owner: RWE.
-        assumeTrue(chmod(tempDirFile, 700, false));
-
-        // cleanDirectory calls forceDelete
-        FileUtils.cleanDirectory(tempDirFile);
-    }
-
-    @DisabledOnOs(OS.WINDOWS)
-    @Test
     public void testThrowsOnNullList() throws Exception {
         // test wont work if we can't restrict permissions on the
         // directory, so skip it.
diff --git a/src/test/java/org/apache/commons/io/IOUtilsCopyTest.java b/src/test/java/org/apache/commons/io/IOUtilsCopyTest.java
index 3da83d0..e8ac84d 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsCopyTest.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsCopyTest.java
@@ -65,23 +65,6 @@ public class IOUtilsCopyTest {
 
     @SuppressWarnings("resource") // 'in' is deliberately not closed
     @Test
-    public void testCopy_inputStreamToOutputStream() throws Exception {
-        InputStream in = new ByteArrayInputStream(inData);
-        in = new ThrowOnCloseInputStream(in);
-
-        final ByteArrayOutputStream baout = new ByteArrayOutputStream();
-        final OutputStream out = new ThrowOnFlushAndCloseOutputStream(baout, false, true);
-
-        final int count = IOUtils.copy(in, out);
-
-        assertEquals(0, in.available(), "Not all bytes were read");
-        assertEquals(inData.length, baout.size(), "Sizes differ");
-        assertArrayEquals(inData, baout.toByteArray(), "Content differs");
-        assertEquals(inData.length,count);
-    }
-
-    @SuppressWarnings("resource") // 'in' is deliberately not closed
-    @Test
     public void testCopy_byteArrayOutputStreamToInputStream() throws Exception {
         final java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
         out.write(inData);
@@ -101,6 +84,23 @@ public class IOUtilsCopyTest {
         assertThrows(NullPointerException.class, () -> IOUtils.copy(null));
     }
 
+    @SuppressWarnings("resource") // 'in' is deliberately not closed
+    @Test
+    public void testCopy_inputStreamToOutputStream() throws Exception {
+        InputStream in = new ByteArrayInputStream(inData);
+        in = new ThrowOnCloseInputStream(in);
+
+        final ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        final OutputStream out = new ThrowOnFlushAndCloseOutputStream(baout, false, true);
+
+        final int count = IOUtils.copy(in, out);
+
+        assertEquals(0, in.available(), "Not all bytes were read");
+        assertEquals(inData.length, baout.size(), "Sizes differ");
+        assertArrayEquals(inData, baout.toByteArray(), "Content differs");
+        assertEquals(inData.length,count);
+    }
+
     /**
      * Test Copying file > 2GB  - see issue# IO-84
      */
diff --git a/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java b/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
index 2d04daa..0f34c07 100644
--- a/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
@@ -75,22 +75,6 @@ public class CharSequenceInputStreamTest {
     }
 
     @Test
-    public void testNullCharset() throws IOException {
-        try (CharSequenceInputStream in = new CharSequenceInputStream("A", (Charset) null)) {
-            IOUtils.toByteArray(in);
-            assertEquals(Charset.defaultCharset(), in.getCharsetEncoder().charset());
-        }
-    }
-
-    @Test
-    public void testNullCharsetName() throws IOException {
-        try (CharSequenceInputStream in = new CharSequenceInputStream("A", (String) null)) {
-            IOUtils.toByteArray(in);
-            assertEquals(Charset.defaultCharset(), in.getCharsetEncoder().charset());
-        }
-    }
-
-    @Test
     public void testAvailable() throws Exception {
         for (final String csName : Charset.availableCharsets().keySet()) {
             // prevent java.lang.UnsupportedOperationException at sun.nio.cs.ext.ISO2022_CN.newEncoder.
@@ -357,6 +341,22 @@ public class CharSequenceInputStreamTest {
         }
     }
 
+    @Test
+    public void testNullCharset() throws IOException {
+        try (CharSequenceInputStream in = new CharSequenceInputStream("A", (Charset) null)) {
+            IOUtils.toByteArray(in);
+            assertEquals(Charset.defaultCharset(), in.getCharsetEncoder().charset());
+        }
+    }
+
+    @Test
+    public void testNullCharsetName() throws IOException {
+        try (CharSequenceInputStream in = new CharSequenceInputStream("A", (String) null)) {
+            IOUtils.toByteArray(in);
+            assertEquals(Charset.defaultCharset(), in.getCharsetEncoder().charset());
+        }
+    }
+
     private void testReadZero(final String csName) throws Exception {
         try (InputStream r = new CharSequenceInputStream("test", csName)) {
             final byte[] bytes = new byte[30];
diff --git a/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java b/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java
index f6750ff..ccff6de 100644
--- a/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java
@@ -53,19 +53,6 @@ public class ReaderInputStreamTest {
         LARGE_TEST_STRING = buffer.toString();
     }
 
-    private final Random random = new Random();
-
-    @ParameterizedTest
-    @MethodSource("charsetData")
-    public void testCharsetEncoderFlush(final String charsetName, final String data) throws IOException {
-        final Charset charset = Charset.forName(charsetName);
-        final byte[] expected = data.getBytes(charset);
-        try (InputStream in = new ReaderInputStream(new StringReader(data), charset)) {
-            final byte[] actual = IOUtils.toByteArray(in);
-            assertEquals(Arrays.toString(expected), Arrays.toString(actual));
-        }
-    }
-
     static Stream<Arguments> charsetData() {
         // @formatter:off
         return Stream.of(
@@ -75,12 +62,7 @@ public class ReaderInputStreamTest {
         // @formatter:on
     }
 
-    @Test
-    public void testBufferTooSmall() throws IOException {
-        assertThrows(IllegalArgumentException.class, () -> new ReaderInputStream(new StringReader("\uD800"), StandardCharsets.UTF_8, -1));
-        assertThrows(IllegalArgumentException.class, () -> new ReaderInputStream(new StringReader("\uD800"), StandardCharsets.UTF_8, 0));
-        assertThrows(IllegalArgumentException.class, () -> new ReaderInputStream(new StringReader("\uD800"), StandardCharsets.UTF_8, 1));
-    }
+    private final Random random = new Random();
 
     @Test
     @Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
@@ -91,6 +73,24 @@ public class ReaderInputStreamTest {
         }
     }
 
+    @Test
+    public void testBufferTooSmall() throws IOException {
+        assertThrows(IllegalArgumentException.class, () -> new ReaderInputStream(new StringReader("\uD800"), StandardCharsets.UTF_8, -1));
+        assertThrows(IllegalArgumentException.class, () -> new ReaderInputStream(new StringReader("\uD800"), StandardCharsets.UTF_8, 0));
+        assertThrows(IllegalArgumentException.class, () -> new ReaderInputStream(new StringReader("\uD800"), StandardCharsets.UTF_8, 1));
+    }
+
+    @ParameterizedTest
+    @MethodSource("charsetData")
+    public void testCharsetEncoderFlush(final String charsetName, final String data) throws IOException {
+        final Charset charset = Charset.forName(charsetName);
+        final byte[] expected = data.getBytes(charset);
+        try (InputStream in = new ReaderInputStream(new StringReader(data), charset)) {
+            final byte[] actual = IOUtils.toByteArray(in);
+            assertEquals(Arrays.toString(expected), Arrays.toString(actual));
+        }
+    }
+
     /*
      * Tests https://issues.apache.org/jira/browse/IO-277
      */
@@ -122,9 +122,9 @@ public class ReaderInputStreamTest {
 
     @Test
     @Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
-    public void testConstructNullCharsetEncoder() throws IOException {
+    public void testConstructNullCharset() throws IOException {
         final Charset charset = Charset.defaultCharset();
-        final CharsetEncoder encoder = null;
+        final Charset encoder = null;
         try (ReaderInputStream in = new ReaderInputStream(new StringReader("ABC"), encoder, (int) ReaderInputStream.minBufferSize(charset.newEncoder()))) {
             IOUtils.toByteArray(in);
             assertEquals(Charset.defaultCharset(), in.getCharsetEncoder().charset());
@@ -133,9 +133,9 @@ public class ReaderInputStreamTest {
 
     @Test
     @Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
-    public void testConstructNullCharset() throws IOException {
+    public void testConstructNullCharsetEncoder() throws IOException {
         final Charset charset = Charset.defaultCharset();
-        final Charset encoder = null;
+        final CharsetEncoder encoder = null;
         try (ReaderInputStream in = new ReaderInputStream(new StringReader("ABC"), encoder, (int) ReaderInputStream.minBufferSize(charset.newEncoder()))) {
             IOUtils.toByteArray(in);
             assertEquals(Charset.defaultCharset(), in.getCharsetEncoder().charset());
diff --git a/src/test/java/org/apache/commons/io/output/BrokenOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/BrokenOutputStreamTest.java
index 7750362..1f56c27 100644
--- a/src/test/java/org/apache/commons/io/output/BrokenOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/BrokenOutputStreamTest.java
@@ -51,21 +51,6 @@ public class BrokenOutputStreamTest {
     }
 
     @Test
-    public void testWriteByteArray() {
-        assertEquals(exception, assertThrows(IOException.class, () -> stream.write(new byte[1])));
-    }
-
-    @Test
-    public void testWriteByteArrayIndexed() {
-        assertEquals(exception, assertThrows(IOException.class, () -> stream.write(new byte[1], 0, 1)));
-    }
-
-    @Test
-    public void testWriteInt() {
-        assertEquals(exception, assertThrows(IOException.class, () -> stream.write(1)));
-    }
-
-    @Test
     public void testTryWithResources() {
         final IOException thrown = assertThrows(IOException.class, () -> {
             try (OutputStream newStream = new BrokenOutputStream()) {
@@ -80,4 +65,19 @@ public class BrokenOutputStreamTest {
         assertEquals("Broken output stream", suppressed[0].getMessage());
     }
 
+    @Test
+    public void testWriteByteArray() {
+        assertEquals(exception, assertThrows(IOException.class, () -> stream.write(new byte[1])));
+    }
+
+    @Test
+    public void testWriteByteArrayIndexed() {
+        assertEquals(exception, assertThrows(IOException.class, () -> stream.write(new byte[1], 0, 1)));
+    }
+
+    @Test
+    public void testWriteInt() {
+        assertEquals(exception, assertThrows(IOException.class, () -> stream.write(1)));
+    }
+
 }
diff --git a/src/test/java/org/apache/commons/io/output/BrokenWriterTest.java b/src/test/java/org/apache/commons/io/output/BrokenWriterTest.java
index e6f2d62..f508b24 100644
--- a/src/test/java/org/apache/commons/io/output/BrokenWriterTest.java
+++ b/src/test/java/org/apache/commons/io/output/BrokenWriterTest.java
@@ -85,6 +85,21 @@ public class BrokenWriterTest {
     }
 
     @Test
+    public void testTryWithResources() {
+        final IOException thrown = assertThrows(IOException.class, () -> {
+            try (Writer newWriter = new BrokenWriter()) {
+                newWriter.write(1);
+            }
+        });
+        assertEquals("Broken writer", thrown.getMessage());
+
+        final Throwable[] suppressed = thrown.getSuppressed();
+        assertEquals(1, suppressed.length);
+        assertEquals(IOException.class, suppressed[0].getClass());
+        assertEquals("Broken writer", suppressed[0].getMessage());
+    }
+
+    @Test
     public void testWriteCharArray() {
         assertEquals(exception, assertThrows(IOException.class, () -> brokenWriter.write(new char[1])));
     }
@@ -109,19 +124,4 @@ public class BrokenWriterTest {
         assertEquals(exception, assertThrows(IOException.class, () -> brokenWriter.write("01", 0, 1)));
     }
 
-    @Test
-    public void testTryWithResources() {
-        final IOException thrown = assertThrows(IOException.class, () -> {
-            try (Writer newWriter = new BrokenWriter()) {
-                newWriter.write(1);
-            }
-        });
-        assertEquals("Broken writer", thrown.getMessage());
-
-        final Throwable[] suppressed = thrown.getSuppressed();
-        assertEquals(1, suppressed.length);
-        assertEquals(IOException.class, suppressed[0].getClass());
-        assertEquals("Broken writer", suppressed[0].getMessage());
-    }
-
 }

[commons-io] 04/04: WriterOutputStream maps null Charset, Charset name, and CharsetEncoder name to the platform default instead of throwing a NullPointerException.

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 f3823cb310c3409c87d6f4979f616c8650381766
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jan 11 09:22:27 2022 -0500

    WriterOutputStream maps null Charset, Charset name, and CharsetEncoder
    name to the platform default instead of throwing a NullPointerException.
---
 src/changes/changes.xml                            |  5 +-
 .../commons/io/output/WriterOutputStream.java      | 19 ++++----
 .../commons/io/output/WriterOutputStreamTest.java  | 53 +++++++++++++++++++++-
 3 files changed, 67 insertions(+), 10 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 151b755..0720199 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -127,11 +127,14 @@ The <action> type attribute can be add,update,fix,remove.
         When deleting symlinks, File/PathUtils.deleteDirectory() changes file permissions of the target.
       </action>
       <action dev="ggregory" type="fix" due-to="Gary Gregory">
-        ReaderInputStream maps null Charset, Charset name, and CharsetEmcoder to the platform default instead of throwing a NullPointerException.
+        ReaderInputStream maps null Charset, Charset name, and CharsetEncoder to the platform default instead of throwing a NullPointerException.
       </action>
       <action dev="ggregory" type="fix" due-to="Gary Gregory">
         CharSequenceInputStream maps null Charset and Charset name to the platform default instead of throwing a NullPointerException.
       </action>
+      <action dev="ggregory" type="fix" due-to="Gary Gregory">
+        WriterOutputStream maps null Charset, Charset name, and CharsetEncoder name to the platform default instead of throwing a NullPointerException.
+      </action>
       <!-- ADD -->
       <action issue="IO-726" dev="ggregory" type="fix" due-to="shollander, Gary Gregory">
         Add MemoryMappedFileInputStream #215.
diff --git a/src/main/java/org/apache/commons/io/output/WriterOutputStream.java b/src/main/java/org/apache/commons/io/output/WriterOutputStream.java
index ae6d1a5..b5d6f46 100644
--- a/src/main/java/org/apache/commons/io/output/WriterOutputStream.java
+++ b/src/main/java/org/apache/commons/io/output/WriterOutputStream.java
@@ -26,6 +26,9 @@ import java.nio.charset.CharsetDecoder;
 import java.nio.charset.CoderResult;
 import java.nio.charset.CodingErrorAction;
 
+import org.apache.commons.io.Charsets;
+import org.apache.commons.io.charset.CharsetDecoders;
+
 /**
  * {@link OutputStream} implementation that transforms a byte stream to a
  * character stream using a specified charset encoding and writes the resulting
@@ -164,15 +167,16 @@ public class WriterOutputStream extends OutputStream {
      *                         output buffer will only be flushed when it overflows or when
      *                         {@link #flush()} or {@link #close()} is called.
      */
-    public WriterOutputStream(final Writer writer, final Charset charset, final int bufferSize,
-                              final boolean writeImmediately) {
+    public WriterOutputStream(final Writer writer, final Charset charset, final int bufferSize, final boolean writeImmediately) {
+        // @formatter:off
         this(writer,
-             charset.newDecoder()
+            Charsets.toCharset(charset).newDecoder()
                     .onMalformedInput(CodingErrorAction.REPLACE)
                     .onUnmappableCharacter(CodingErrorAction.REPLACE)
                     .replaceWith("?"),
              bufferSize,
              writeImmediately);
+        // @formatter:on
     }
 
     /**
@@ -201,11 +205,10 @@ public class WriterOutputStream extends OutputStream {
      *                         {@link #flush()} or {@link #close()} is called.
      * @since 2.1
      */
-    public WriterOutputStream(final Writer writer, final CharsetDecoder decoder, final int bufferSize,
-                              final boolean writeImmediately) {
-        checkIbmJdkWithBrokenUTF16( decoder.charset());
+    public WriterOutputStream(final Writer writer, final CharsetDecoder decoder, final int bufferSize, final boolean writeImmediately) {
+        checkIbmJdkWithBrokenUTF16(CharsetDecoders.toCharsetDecoder(decoder).charset());
         this.writer = writer;
-        this.decoder = decoder;
+        this.decoder = CharsetDecoders.toCharsetDecoder(decoder);
         this.writeImmediately = writeImmediately;
         decoderOut = CharBuffer.allocate(bufferSize);
     }
@@ -236,7 +239,7 @@ public class WriterOutputStream extends OutputStream {
      */
     public WriterOutputStream(final Writer writer, final String charsetName, final int bufferSize,
                               final boolean writeImmediately) {
-        this(writer, Charset.forName(charsetName), bufferSize, writeImmediately);
+        this(writer, Charsets.toCharset(charsetName), bufferSize, writeImmediately);
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/io/output/WriterOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/WriterOutputStreamTest.java
index 61da71c..50c840d 100644
--- a/src/test/java/org/apache/commons/io/output/WriterOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/WriterOutputStreamTest.java
@@ -21,9 +21,13 @@ import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.IOException;
 import java.io.StringWriter;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
 import java.nio.charset.StandardCharsets;
 import java.util.Random;
 
+import org.apache.commons.io.Charsets;
+import org.apache.commons.io.charset.CharsetDecoders;
 import org.junit.jupiter.api.Test;
 
 public class WriterOutputStreamTest {
@@ -52,6 +56,16 @@ public class WriterOutputStreamTest {
     }
 
     @Test
+    public void testLargeUTF8CharsetWithBufferedWrite() throws IOException {
+        testWithBufferedWrite(LARGE_TEST_STRING, "UTF-8");
+    }
+
+    @Test
+    public void testLargeUTF8CharsetWithSingleByteWrite() throws IOException {
+        testWithSingleByteWrite(LARGE_TEST_STRING, StandardCharsets.UTF_8);
+    }
+
+    @Test
     public void testLargeUTF8WithBufferedWrite() throws IOException {
         testWithBufferedWrite(LARGE_TEST_STRING, "UTF-8");
     }
@@ -62,6 +76,21 @@ public class WriterOutputStreamTest {
     }
 
     @Test
+    public void testNullCharsetNameWithSingleByteWrite() throws IOException {
+        testWithSingleByteWrite(TEST_STRING, (String) null);
+    }
+
+    @Test
+    public void testNullCharsetWithSingleByteWrite() throws IOException {
+        testWithSingleByteWrite(TEST_STRING, (Charset) null);
+    }
+
+    @Test
+    public void testNullCharsetDecoderWithSingleByteWrite() throws IOException {
+        testWithSingleByteWrite(TEST_STRING, (CharsetDecoder) null);
+    }
+
+    @Test
     public void testUTF16BEWithBufferedWrite() throws IOException {
         testWithBufferedWrite(TEST_STRING, "UTF-16BE");
     }
@@ -128,8 +157,30 @@ public class WriterOutputStreamTest {
     }
 
 
+    private void testWithSingleByteWrite(final String testString, final Charset charset) throws IOException {
+        final byte[] bytes = testString.getBytes(Charsets.toCharset(charset));
+        final StringWriter writer = new StringWriter();
+        try (final WriterOutputStream out = new WriterOutputStream(writer, charset)) {
+            for (final byte b : bytes) {
+                out.write(b);
+            }
+        }
+        assertEquals(testString, writer.toString());
+    }
+
+    private void testWithSingleByteWrite(final String testString, final CharsetDecoder charsetDecoder) throws IOException {
+        final byte[] bytes = testString.getBytes(CharsetDecoders.toCharsetDecoder(charsetDecoder).charset());
+        final StringWriter writer = new StringWriter();
+        try (final WriterOutputStream out = new WriterOutputStream(writer, charsetDecoder)) {
+            for (final byte b : bytes) {
+                out.write(b);
+            }
+        }
+        assertEquals(testString, writer.toString());
+    }
+
     private void testWithSingleByteWrite(final String testString, final String charsetName) throws IOException {
-        final byte[] bytes = testString.getBytes(charsetName);
+        final byte[] bytes = testString.getBytes(Charsets.toCharset(charsetName));
         final StringWriter writer = new StringWriter();
         try (final WriterOutputStream out = new WriterOutputStream(writer, charsetName)) {
             for (final byte b : bytes) {

[commons-io] 02/04: Sort members.

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 5444ad3de9d01d8a7f6999472f9b973ebba02acf
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jan 11 08:52:31 2022 -0500

    Sort members.
---
 .../org/apache/commons/io/function/IOStreams.java  | 28 +++++++++++-----------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/function/IOStreams.java b/src/main/java/org/apache/commons/io/function/IOStreams.java
index 7437ec3..74581c0 100644
--- a/src/main/java/org/apache/commons/io/function/IOStreams.java
+++ b/src/main/java/org/apache/commons/io/function/IOStreams.java
@@ -32,20 +32,6 @@ import org.apache.commons.io.IOExceptionList;
  */
 class IOStreams {
 
-    /**
-     * Null-safe version of {@link Stream#of(Object[])}.
-     *
-     * Copied from Apache Commons Lang.
-     *
-     * @param <T> the type of stream elements.
-     * @param values the elements of the new stream, may be {@code null}.
-     * @return the new stream on {@code values} or {@link Stream#empty()}.
-     */
-    @SafeVarargs // Creating a stream from an array is safe
-    static <T> Stream<T> of(final T... values) {
-        return values == null ? Stream.empty() : Stream.of(values);
-    }
-
     static <T> void forEach(final Stream<T> stream, final IOConsumer<T> action) throws IOException {
         forEachIndexed(stream, action, (i, e) -> e);
     }
@@ -68,4 +54,18 @@ class IOStreams {
         IOExceptionList.checkEmpty(causeList.get(), null);
     }
 
+    /**
+     * Null-safe version of {@link Stream#of(Object[])}.
+     *
+     * Copied from Apache Commons Lang.
+     *
+     * @param <T> the type of stream elements.
+     * @param values the elements of the new stream, may be {@code null}.
+     * @return the new stream on {@code values} or {@link Stream#empty()}.
+     */
+    @SafeVarargs // Creating a stream from an array is safe
+    static <T> Stream<T> of(final T... values) {
+        return values == null ? Stream.empty() : Stream.of(values);
+    }
+
 }