@@ -23,7 +23,6 @@ import java.util.Collections; | |||
import java.util.HashSet; | |||
import java.util.LinkedHashSet; | |||
import java.util.Map; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.stream.Stream; | |||
@@ -20,6 +20,7 @@ package org.apache.tools.ant.util; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.io.Reader; | |||
import java.nio.charset.Charset; | |||
/** | |||
* Adapts a <code>Reader</code> as an <code>InputStream</code>. | |||
@@ -64,6 +65,22 @@ public class ReaderInputStream extends InputStream { | |||
this.encoding = encoding; | |||
} | |||
/** | |||
* Construct a <code>ReaderInputStream</code> | |||
* for the specified <code>Reader</code>, | |||
* with the specified encoding. | |||
* | |||
* @param reader non-null <code>Reader</code>. | |||
* @param charset non-null <code>Charset</code> charset. | |||
*/ | |||
public ReaderInputStream(Reader reader, Charset charset) { | |||
this(reader); | |||
if (charset == null) { | |||
throw new IllegalArgumentException("encoding must not be null"); | |||
} | |||
this.encoding = charset.name(); | |||
} | |||
/** | |||
* Reads from the <code>Reader</code>, returning the same value. | |||
* | |||
@@ -105,8 +122,7 @@ public class ReaderInputStream extends InputStream { | |||
* @exception IOException if an error occurs | |||
*/ | |||
@Override | |||
public synchronized int read(byte[] b, int off, int len) | |||
throws IOException { | |||
public synchronized int read(byte[] b, int off, int len) throws IOException { | |||
if (in == null) { | |||
throw new IOException("Stream Closed"); | |||
} | |||
@@ -24,15 +24,17 @@ import java.io.FileInputStream; | |||
import java.io.IOException; | |||
import java.io.InputStreamReader; | |||
import java.io.PrintStream; | |||
import java.nio.charset.Charset; | |||
import java.nio.charset.StandardCharsets; | |||
import org.apache.tools.ant.DefaultLogger; | |||
import org.apache.tools.ant.Project; | |||
import org.apache.tools.ant.util.FileUtils; | |||
import org.junit.After; | |||
import org.junit.Before; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.TemporaryFolder; | |||
import static org.apache.tools.ant.util.FileUtils.readFully; | |||
import static org.junit.Assert.assertEquals; | |||
/** | |||
@@ -40,12 +42,13 @@ import static org.junit.Assert.assertEquals; | |||
*/ | |||
public class EchoTest { | |||
@Rule | |||
public TemporaryFolder folder = new TemporaryFolder(); | |||
private EchoTestLogger logger; | |||
private Echo echo; | |||
private File removeThis; | |||
@Before | |||
public void setUp() { | |||
Project p = new Project(); | |||
@@ -56,13 +59,6 @@ public class EchoTest { | |||
echo.setProject(p); | |||
} | |||
@After | |||
public void tearDown() { | |||
if (removeThis != null && removeThis.exists() && !removeThis.delete()) { | |||
removeThis.deleteOnExit(); | |||
} | |||
} | |||
@Test | |||
public void testLogBlankEcho() { | |||
echo.setTaskName("testLogBlankEcho"); | |||
@@ -72,14 +68,17 @@ public class EchoTest { | |||
@Test | |||
public void testLogUTF8Echo() throws IOException { | |||
File removeThis = folder.newFile("abc.txt"); | |||
Charset cs = StandardCharsets.UTF_8; | |||
String msg = "\u00e4\u00a9"; | |||
echo.setTaskName("testLogUTF8Echo"); | |||
echo.setMessage("\u00e4\u00a9"); | |||
removeThis = new File("abc.txt"); | |||
echo.setMessage(msg); | |||
echo.setFile(removeThis); | |||
echo.setEncoding("UTF-8"); | |||
echo.setEncoding(cs.name()); | |||
echo.execute(); | |||
String x = FileUtils.readFully(new InputStreamReader(new FileInputStream(removeThis), StandardCharsets.UTF_8)); | |||
assertEquals(x, "\u00e4\u00a9"); | |||
assertEquals(msg, readFully(new InputStreamReader(new FileInputStream(removeThis), cs))); | |||
} | |||
private class EchoTestLogger extends DefaultLogger { | |||
@@ -89,40 +89,32 @@ public class ReaderInputStreamTest { | |||
@Test | |||
public void testIso88591ToUtf8() throws Exception { | |||
InputStreamReader fin = null; | |||
ReaderInputStream r = null; | |||
FileInputStream utf8 = null; | |||
try { | |||
fin = new InputStreamReader(new FileInputStream(new File(ROOT, | |||
"src/tests/antunit/taskdefs/exec/input/iso8859-1")), "ISO8859_1"); | |||
r = new ReaderInputStream(fin, "UTF8"); | |||
ByteArrayOutputStream actualOS = new ByteArrayOutputStream(); | |||
ByteArrayOutputStream actualOS = new ByteArrayOutputStream(); | |||
try (ReaderInputStream r = new ReaderInputStream(new InputStreamReader(new FileInputStream(new File(ROOT, | |||
"src/tests/antunit/taskdefs/exec/input/iso8859-1")), StandardCharsets.ISO_8859_1), | |||
StandardCharsets.UTF_8)) { | |||
int b = r.read(); | |||
while (b > -1) { | |||
actualOS.write((byte) b); | |||
b = r.read(); | |||
} | |||
} | |||
utf8 = new FileInputStream(new File(ROOT, | |||
"src/tests/antunit/taskdefs/exec/expected/utf-8")); | |||
ByteArrayOutputStream expectedOS = new ByteArrayOutputStream(); | |||
b = utf8.read(); | |||
ByteArrayOutputStream expectedOS = new ByteArrayOutputStream(); | |||
try (FileInputStream utf8 = new FileInputStream(new File(ROOT, | |||
"src/tests/antunit/taskdefs/exec/expected/utf-8"))) { | |||
int b = utf8.read(); | |||
while (b > -1) { | |||
expectedOS.write((byte) b); | |||
b = utf8.read(); | |||
} | |||
} | |||
byte[] expected = expectedOS.toByteArray(); | |||
byte[] actual = actualOS.toByteArray(); | |||
assertEquals("length", expected.length, actual.length); | |||
for (int i = 0; i < actual.length; i++) { | |||
assertEquals("byte " + i, expected[i], actual[i]); | |||
} | |||
} finally { | |||
FileUtils.close(fin); | |||
FileUtils.close(r); | |||
FileUtils.close(utf8); | |||
byte[] expected = expectedOS.toByteArray(); | |||
byte[] actual = actualOS.toByteArray(); | |||
assertEquals("length", expected.length, actual.length); | |||
for (int i = 0; i < actual.length; i++) { | |||
assertEquals("byte " + i, expected[i], actual[i]); | |||
} | |||
} | |||