You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2004/08/14 01:39:01 UTC
cvs commit: jakarta-commons/io/src/java/org/apache/commons/io IOUtils.java
scolebourne 2004/08/13 16:39:01
Modified: io/src/test/org/apache/commons/io IOTestSuite.java
io/src/java/org/apache/commons/io IOUtils.java
Added: io/src/test/org/apache/commons/io IOUtilsCopyTestCase.java
IOUtilsWriteTestCase.java
Log:
Add code from CopyUtils to IOUtils, refactoring to be simpler, clearer and more efficient
Revision Changes Path
1.10 +2 -0 jakarta-commons/io/src/test/org/apache/commons/io/IOTestSuite.java
Index: IOTestSuite.java
===================================================================
RCS file: /home/cvs/jakarta-commons/io/src/test/org/apache/commons/io/IOTestSuite.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- IOTestSuite.java 23 Feb 2004 05:02:25 -0000 1.9
+++ IOTestSuite.java 13 Aug 2004 23:39:01 -0000 1.10
@@ -40,6 +40,8 @@
TestSuite suite = new TestSuite( "IO Utilities" );
suite.addTest( new TestSuite( CopyUtilsTest.class ) );
suite.addTest( new TestSuite( IOUtilsTestCase.class ) );
+ suite.addTest( new TestSuite( IOUtilsCopyTestCase.class ) );
+ suite.addTest( new TestSuite( IOUtilsWriteTestCase.class ) );
suite.addTest( new TestSuite( FileUtilsTestCase.class ) );
suite.addTest( new TestSuite( FileFilterTestCase.class ) );
suite.addTest( new TestSuite( DemuxTestCase.class ) );
1.1 jakarta-commons/io/src/test/org/apache/commons/io/IOUtilsCopyTestCase.java
Index: IOUtilsCopyTestCase.java
===================================================================
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed 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;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Arrays;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.io.testtools.FileBasedTestCase;
import org.apache.commons.io.testtools.YellOnCloseInputStream;
import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream;
/**
* JUnit tests for IOUtils copy methods.
*
* @author Jeff Turner
* @author Matthew Hawthorne
* @author Jeremias Maerki
* @author Stephen Colebourne
* @version $Id: IOUtilsCopyTestCase.java,v 1.1 2004/08/13 23:39:01 scolebourne Exp $
* @see IOUtils
*/
public class IOUtilsCopyTestCase extends FileBasedTestCase {
/*
* NOTE this is not particularly beautiful code. A better way to check for
* flush and close status would be to implement "trojan horse" wrapper
* implementations of the various stream classes, which set a flag when
* relevant methods are called. (JT)
*/
private static final int FILE_SIZE = 1024 * 4 + 1;
private byte[] inData = generateTestData(FILE_SIZE);
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(IOUtilsCopyTestCase.class);
}
public IOUtilsCopyTestCase(String testName) {
super(testName);
}
// ----------------------------------------------------------------
// Setup
// ----------------------------------------------------------------
public void setUp() throws Exception {
}
public void tearDown() throws Exception {
}
//-----------------------------------------------------------------------
public void testCopy_inputStreamToOutputStream() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
in = new YellOnCloseInputStream(in);
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
int count = IOUtils.copy(in, out);
assertTrue("Not all bytes were read", in.available() == 0);
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testCopy_inputStreamToOutputStream_nullIn() throws Exception {
OutputStream out = new ByteArrayOutputStream();
try {
IOUtils.copy((InputStream) null, out);
fail();
} catch (NullPointerException ex) {}
}
public void testCopy_inputStreamToOutputStream_nullOut() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
try {
IOUtils.copy(in, (OutputStream) null);
fail();
} catch (NullPointerException ex) {}
}
//-----------------------------------------------------------------------
public void testCopy_inputStreamToWriter() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
in = new YellOnCloseInputStream(in);
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
IOUtils.copy(in, writer);
out.off();
writer.flush();
assertTrue("Not all bytes were read", in.available() == 0);
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testCopy_inputStreamToWriter_nullIn() throws Exception {
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
try {
IOUtils.copy((InputStream) null, writer);
fail();
} catch (NullPointerException ex) {}
}
public void testCopy_inputStreamToWriter_nullOut() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
try {
IOUtils.copy(in, (Writer) null);
fail();
} catch (NullPointerException ex) {}
}
//-----------------------------------------------------------------------
public void testCopy_inputStreamToWriter_Encoding() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
in = new YellOnCloseInputStream(in);
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
IOUtils.copy(in, writer, "UTF8");
out.off();
writer.flush();
assertTrue("Not all bytes were read", in.available() == 0);
byte[] bytes = baout.toByteArray();
bytes = new String(bytes, "UTF8").getBytes("US-ASCII");
assertTrue("Content differs", Arrays.equals(inData, bytes));
}
public void testCopy_inputStreamToWriter_Encoding_nullIn() throws Exception {
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
try {
IOUtils.copy((InputStream) null, writer, "UTF8");
fail();
} catch (NullPointerException ex) {}
}
public void testCopy_inputStreamToWriter_Encoding_nullOut() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
try {
IOUtils.copy(in, (Writer) null, "UTF8");
fail();
} catch (NullPointerException ex) {}
}
public void testCopy_inputStreamToWriter_Encoding_nullEncoding() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
in = new YellOnCloseInputStream(in);
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
IOUtils.copy(in, writer, null);
out.off();
writer.flush();
assertTrue("Not all bytes were read", in.available() == 0);
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
//-----------------------------------------------------------------------
public void testCopy_readerToOutputStream() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
in = new YellOnCloseInputStream(in);
Reader reader = new InputStreamReader(in, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
IOUtils.copy(reader, out);
//Note: this method *does* flush. It is equivalent to:
// OutputStreamWriter _out = new OutputStreamWriter(fout);
// IOUtils.copy( fin, _out, 4096 ); // copy( Reader, Writer, int );
// _out.flush();
// out = fout;
// Note: rely on the method to flush
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testCopy_readerToOutputStream_nullIn() throws Exception {
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
try {
IOUtils.copy((Reader) null, out);
fail();
} catch (NullPointerException ex) {}
}
public void testCopy_readerToOutputStream_nullOut() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
in = new YellOnCloseInputStream(in);
Reader reader = new InputStreamReader(in, "US-ASCII");
try {
IOUtils.copy(reader, (OutputStream) null);
fail();
} catch (NullPointerException ex) {}
}
//-----------------------------------------------------------------------
public void testCopy_readerToOutputStream_Encoding() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
in = new YellOnCloseInputStream(in);
Reader reader = new InputStreamReader(in, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
IOUtils.copy(reader, out, "UTF16");
// note: this method *does* flush.
// note: we don't flush here; this IOUtils method does it for us
byte[] bytes = baout.toByteArray();
bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
assertTrue("Content differs", Arrays.equals(inData, bytes));
}
public void testCopy_readerToOutputStream_Encoding_nullIn() throws Exception {
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
try {
IOUtils.copy((Reader) null, out, "UTF16");
fail();
} catch (NullPointerException ex) {}
}
public void testCopy_readerToOutputStream_Encoding_nullOut() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
in = new YellOnCloseInputStream(in);
Reader reader = new InputStreamReader(in, "US-ASCII");
try {
IOUtils.copy(reader, (OutputStream) null, "UTF16");
fail();
} catch (NullPointerException ex) {}
}
public void testCopy_readerToOutputStream_Encoding_nullEncoding() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
in = new YellOnCloseInputStream(in);
Reader reader = new InputStreamReader(in, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
IOUtils.copy(reader, out, null);
// note: this method *does* flush.
// note: we don't flush here; this IOUtils method does it for us
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
//-----------------------------------------------------------------------
public void testCopy_readerToWriter() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
in = new YellOnCloseInputStream(in);
Reader reader = new InputStreamReader(in, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
int count = IOUtils.copy(reader, writer);
out.off();
writer.flush();
assertEquals("The number of characters returned by copy is wrong", inData.length, count);
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testCopy_readerToWriter_nullIn() throws Exception {
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
try {
IOUtils.copy((Reader) null, writer);
fail();
} catch (NullPointerException ex) {}
}
public void testCopy_readerToWriter_nullOut() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
in = new YellOnCloseInputStream(in);
Reader reader = new InputStreamReader(in, "US-ASCII");
try {
IOUtils.copy(reader, (Writer) null);
fail();
} catch (NullPointerException ex) {}
}
}
1.1 jakarta-commons/io/src/test/org/apache/commons/io/IOUtilsWriteTestCase.java
Index: IOUtilsWriteTestCase.java
===================================================================
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed 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;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Arrays;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.io.testtools.FileBasedTestCase;
import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream;
/**
* JUnit tests for IOUtils write methods.
*
* @author Jeff Turner
* @author Matthew Hawthorne
* @author Jeremias Maerki
* @author Stephen Colebourne
* @version $Id: IOUtilsWriteTestCase.java,v 1.1 2004/08/13 23:39:01 scolebourne Exp $
* @see IOUtils
*/
public class IOUtilsWriteTestCase extends FileBasedTestCase {
/*
* NOTE this is not particularly beautiful code. A better way to check for
* flush and close status would be to implement "trojan horse" wrapper
* implementations of the various stream classes, which set a flag when
* relevant methods are called. (JT)
*/
private static final int FILE_SIZE = 1024 * 4 + 1;
private byte[] inData = generateTestData(FILE_SIZE);
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(IOUtilsWriteTestCase.class);
}
public IOUtilsWriteTestCase(String testName) {
super(testName);
}
// ----------------------------------------------------------------
// Setup
// ----------------------------------------------------------------
public void setUp() throws Exception {
}
public void tearDown() throws Exception {
}
// ----------------------------------------------------------------
// Tests
// ----------------------------------------------------------------
//-----------------------------------------------------------------------
public void testWrite_byteArrayToOutputStream() throws Exception {
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
IOUtils.write(inData, out);
out.off();
out.flush();
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testWrite_byteArrayToOutputStream_nullData() throws Exception {
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
IOUtils.write((byte[]) null, out);
out.off();
out.flush();
assertEquals("Sizes differ", 0, baout.size());
}
public void testWrite_byteArrayToOutputStream_nullStream() throws Exception {
try {
IOUtils.write(inData, (OutputStream) null);
fail();
} catch (NullPointerException ex) {}
}
//-----------------------------------------------------------------------
public void testWrite_byteArrayToWriter() throws Exception {
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
IOUtils.write(inData, writer);
out.off();
writer.flush();
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testWrite_byteArrayToWriter_nullData() throws Exception {
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
IOUtils.write((byte[]) null, writer);
out.off();
writer.flush();
assertEquals("Sizes differ", 0, baout.size());
}
public void testWrite_byteArrayToWriter_nullWriter() throws Exception {
try {
IOUtils.write(inData, (Writer) null);
fail();
} catch (NullPointerException ex) {}
}
//-----------------------------------------------------------------------
public void testWrite_byteArrayToWriter_Encoding() throws Exception {
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
IOUtils.write(inData, writer, "UTF8");
out.off();
writer.flush();
byte[] bytes = baout.toByteArray();
bytes = new String(bytes, "UTF8").getBytes("US-ASCII");
assertTrue("Content differs", Arrays.equals(inData, bytes));
}
public void testWrite_byteArrayToWriter_Encoding_nullData() throws Exception {
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
IOUtils.write((byte[]) null, writer, "UTF8");
out.off();
writer.flush();
assertEquals("Sizes differ", 0, baout.size());
}
public void testWrite_byteArrayToWriter_Encoding_nullWriter() throws Exception {
try {
IOUtils.write(inData, (Writer) null, "UTF8");
fail();
} catch (NullPointerException ex) {}
}
public void testWrite_byteArrayToWriter_Encoding_nullEncoding() throws Exception {
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
IOUtils.write(inData, writer, null);
out.off();
writer.flush();
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
//-----------------------------------------------------------------------
public void testWrite_stringToOutputStream() throws Exception {
String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
IOUtils.write(str, out);
out.off();
out.flush();
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testWrite_stringToOutputStream_nullData() throws Exception {
String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
IOUtils.write((String) null, out);
out.off();
out.flush();
assertEquals("Sizes differ", 0, baout.size());
}
public void testWrite_stringToOutputStream_nullStream() throws Exception {
String str = new String(inData, "US-ASCII");
try {
IOUtils.write(str, (OutputStream) null);
fail();
} catch (NullPointerException ex) {}
}
//-----------------------------------------------------------------------
public void testWrite_stringToOutputStream_Encoding() throws Exception {
String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
IOUtils.write(str, out, "UTF16");
out.off();
out.flush();
byte[] bytes = baout.toByteArray();
bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
assertTrue("Content differs", Arrays.equals(inData, bytes));
}
public void testWrite_stringToOutputStream_Encoding_nullData() throws Exception {
String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
IOUtils.write((String) null, out);
out.off();
out.flush();
assertEquals("Sizes differ", 0, baout.size());
}
public void testWrite_stringToOutputStream_Encoding_nullStream() throws Exception {
String str = new String(inData, "US-ASCII");
try {
IOUtils.write(str, (OutputStream) null);
fail();
} catch (NullPointerException ex) {}
}
public void testWrite_stringToOutputStream_nullEncoding() throws Exception {
String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
IOUtils.write(str, out, null);
out.off();
out.flush();
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
//-----------------------------------------------------------------------
public void testWrite_stringToWriter() throws Exception {
String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
IOUtils.write(str, writer);
out.off();
writer.flush();
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testWrite_stringToWriter_Encoding_nullData() throws Exception {
String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
IOUtils.write((String) null, writer);
out.off();
writer.flush();
assertEquals("Sizes differ", 0, baout.size());
}
public void testWrite_stringToWriter_Encoding_nullStream() throws Exception {
String str = new String(inData, "US-ASCII");
try {
IOUtils.write(str, (Writer) null);
fail();
} catch (NullPointerException ex) {}
}
//-----------------------------------------------------------------------
public void testWrite_charArrayToOutputStream() throws Exception {
String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
IOUtils.write(str.toCharArray(), out);
out.off();
out.flush();
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testWrite_charArrayToOutputStream_nullData() throws Exception {
String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
IOUtils.write((char[]) null, out);
out.off();
out.flush();
assertEquals("Sizes differ", 0, baout.size());
}
public void testWrite_charArrayToOutputStream_nullStream() throws Exception {
String str = new String(inData, "US-ASCII");
try {
IOUtils.write(str.toCharArray(), (OutputStream) null);
fail();
} catch (NullPointerException ex) {}
}
//-----------------------------------------------------------------------
public void testWrite_charArrayToOutputStream_Encoding() throws Exception {
String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
IOUtils.write(str.toCharArray(), out, "UTF16");
out.off();
out.flush();
byte[] bytes = baout.toByteArray();
bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
assertTrue("Content differs", Arrays.equals(inData, bytes));
}
public void testWrite_charArrayToOutputStream_Encoding_nullData() throws Exception {
String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
IOUtils.write((char[]) null, out);
out.off();
out.flush();
assertEquals("Sizes differ", 0, baout.size());
}
public void testWrite_charArrayToOutputStream_Encoding_nullStream() throws Exception {
String str = new String(inData, "US-ASCII");
try {
IOUtils.write(str.toCharArray(), (OutputStream) null);
fail();
} catch (NullPointerException ex) {}
}
public void testWrite_charArrayToOutputStream_nullEncoding() throws Exception {
String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
IOUtils.write(str.toCharArray(), out, null);
out.off();
out.flush();
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
//-----------------------------------------------------------------------
public void testWrite_charArrayToWriter() throws Exception {
String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
IOUtils.write(str.toCharArray(), writer);
out.off();
writer.flush();
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testWrite_charArrayToWriter_Encoding_nullData() throws Exception {
String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
IOUtils.write((char[]) null, writer);
out.off();
writer.flush();
assertEquals("Sizes differ", 0, baout.size());
}
public void testWrite_charArrayToWriter_Encoding_nullStream() throws Exception {
String str = new String(inData, "US-ASCII");
try {
IOUtils.write(str.toCharArray(), (Writer) null);
fail();
} catch (NullPointerException ex) {}
}
}
1.19 +492 -29 jakarta-commons/io/src/java/org/apache/commons/io/IOUtils.java
Index: IOUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/IOUtils.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- IOUtils.java 31 Jul 2004 11:26:39 -0000 1.18
+++ IOUtils.java 13 Aug 2004 23:39:01 -0000 1.19
@@ -16,9 +16,12 @@
package org.apache.commons.io;
import java.io.BufferedInputStream;
+import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.OutputStream;
+import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
@@ -29,22 +32,46 @@
* General IO Stream manipulation.
* <p>
* This class provides static utility methods for input/output operations.
+ * <ul>
+ * <li>closeQuietly - these methods close a stream ignoring nulls and exceptions
+ * <li>toXxx - these methods read data from a stream
+ * <li>write - these methods write data to a stream
+ * <li>copy - these methods copy all the data from one stream to another
+ * <li>contentEquals - these methods compare the content of two streams
+ * </ul>
* <p>
- * The closeQuietly methods are expected to be used when an IOException
- * would be meaningless. This is usually when in a catch block for an
- * IOException.
+ * The byte-to-char methods and char-to-byte methods involve a conversion step.
+ * Two methods are provided in each case, one that uses the platform default encoding
+ * and the other which allows you to specify an encoding. You are encouraged to always
+ * specify an encoding because relying on the platform default can lead to unexpected
+ * results, for example when moving from development to production.
* <p>
- * The toString and toByteArray methods all rely on CopyUtils.copy
- * methods in the current implementation.
- *
- * <p>Origin of code: Apache Avalon (Excalibur)</p>
+ * All the methods in this class that read a stream are buffered internally.
+ * This means that there is no cause to use a <code>BufferedInputStream</code> or
+ * <code>BufferedReader</code>. The default buffer size of 4K has been show to be
+ * efficient in tests.
+ * <p>
+ * Wherever possible, the methods in this class do <em>not</em> flush or close the stream.
+ * This is to avoid making non-portable assumptions about the streams' origin and further use.
+ * Thus the caller is still responsible for closing streams after use.
+ * <p>
+ * Origin of code: Apache Avalon (Excalibur)
*
* @author Peter Donald
* @author Jeff Turner
+ * @author Matthew Hawthorne
* @author Stephen Colebourne
* @version CVS $Revision$ $Date$
*/
public class IOUtils {
+ // NOTE: This class is focussed on InputStream, OutputStream, Reader and Writer
+ // Each method should take at least one of these as a parameter.
+ // NOTE: This class should not depend on any other classes
+
+ /**
+ * The default buffer size to use.
+ */
+ private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
/**
* Instances should NOT be constructed in standard programming.
@@ -115,7 +142,7 @@
*
* @param input the OutputStream to close, may be null or already closed
*/
- public static void closeQuietly( OutputStream output ) {
+ public static void closeQuietly(OutputStream output) {
try {
if (output != null) {
output.close();
@@ -125,7 +152,7 @@
}
}
- // toByteArray
+ // read toByteArray
//-----------------------------------------------------------------------
/**
* Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
@@ -140,7 +167,7 @@
*/
public static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
- CopyUtils.copy(input, output);
+ copy(input, output);
return output.toByteArray();
}
@@ -158,7 +185,7 @@
*/
public static byte[] toByteArray(Reader input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
- CopyUtils.copy(input, output);
+ copy(input, output);
return output.toByteArray();
}
@@ -181,7 +208,7 @@
*/
public static byte[] toByteArray(Reader input, String encoding) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
- CopyUtils.copy(input, output, encoding);
+ copy(input, output, encoding);
return output.toByteArray();
}
@@ -201,7 +228,7 @@
return input.getBytes();
}
- // toString
+ // read toString
//-----------------------------------------------------------------------
/**
* Get the contents of an <code>InputStream</code> as a String
@@ -217,7 +244,7 @@
*/
public static String toString(InputStream input) throws IOException {
StringWriter sw = new StringWriter();
- CopyUtils.copy(input, sw);
+ copy(input, sw);
return sw.toString();
}
@@ -239,7 +266,7 @@
*/
public static String toString(InputStream input, String encoding) throws IOException {
StringWriter sw = new StringWriter();
- CopyUtils.copy(input, sw, encoding);
+ copy(input, sw, encoding);
return sw.toString();
}
@@ -256,7 +283,7 @@
*/
public static String toString(Reader input) throws IOException {
StringWriter sw = new StringWriter();
- CopyUtils.copy(input, sw);
+ copy(input, sw);
return sw.toString();
}
@@ -296,11 +323,413 @@
}
}
+ // write byte[]
+ //-----------------------------------------------------------------------
+ /**
+ * Writes bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
+ *
+ * @param data the byte array to write, do not modify during output, null ignored
+ * @param output the <code>OutputStream</code> to write to
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void write(byte[] data, OutputStream output) throws IOException {
+ if (data != null) {
+ output.write(data);
+ }
+ }
+
+ /**
+ * Writes bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
+ * using the default character encoding of the platform.
+ * <p>
+ * This method uses {@link String#String(byte[])}.
+ *
+ * @param data the byte array to write, do not modify during output, null ignored
+ * @param output the <code>Writer</code> to write to
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void write(byte[] data, Writer output) throws IOException {
+ if (data != null) {
+ output.write(new String(data));
+ }
+ }
+
+ /**
+ * Writes bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
+ * using the specified character encoding.
+ * <p>
+ * Character encoding names can be found at
+ * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+ * <p>
+ * This method uses {@link String#String(byte[], String)}.
+ *
+ * @param data the byte array to write, do not modify during output, null ignored
+ * @param output the <code>Writer</code> to write to
+ * @param encoding the encoding to use, null means platform default
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void write(byte[] data, Writer output, String encoding) throws IOException {
+ if (data != null) {
+ if (encoding == null) {
+ write(data, output);
+ } else {
+ output.write(new String(data, encoding));
+ }
+ }
+ }
+
+ // write char[]
+ //-----------------------------------------------------------------------
+ /**
+ * Writes chars from a <code>char[]</code> to a <code>Writer</code>
+ * using the default character encoding of the platform.
+ *
+ * @param data the char array to write, do not modify during output, null ignored
+ * @param output the <code>Writer</code> to write to
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void write(char[] data, Writer output) throws IOException {
+ if (data != null) {
+ output.write(data);
+ }
+ }
+
+ /**
+ * Writes chars from a <code>char[]</code> to bytes on an <code>OutputStream</code>.
+ * <p>
+ * This method uses {@link String#String(char[])} and {@link String#getBytes()}.
+ *
+ * @param data the char array to write, do not modify during output, null ignored
+ * @param output the <code>OutputStream</code> to write to
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void write(char[] data, OutputStream output) throws IOException {
+ if (data != null) {
+ output.write(new String(data).getBytes());
+ }
+ }
+
+ /**
+ * Writes chars from a <code>char[]</code> to bytes on an <code>OutputStream</code>
+ * using the specified character encoding.
+ * <p>
+ * Character encoding names can be found at
+ * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+ * <p>
+ * This method uses {@link String#String(char[])} and {@link String#getBytes(String)}
+ *
+ * @param data the char array to write, do not modify during output, null ignored
+ * @param output the <code>OutputStream</code> to write to
+ * @param encoding the encoding to use, null means platform default
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void write(char[] data, OutputStream output, String encoding) throws IOException {
+ if (data != null) {
+ if (encoding == null) {
+ write(data, output);
+ } else {
+ output.write(new String(data).getBytes(encoding));
+ }
+ }
+ }
+
+ // write String
+ //-----------------------------------------------------------------------
+ /**
+ * Writes chars from a <code>String</code> to a <code>Writer</code>.
+ *
+ * @param data the <code>String</code> to write, null ignored
+ * @param output the <code>Writer</code> to write to
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void write(String data, Writer output) throws IOException {
+ if (data != null) {
+ output.write(data);
+ }
+ }
+
+ /**
+ * Writes chars from a <code>String</code> to bytes on an <code>OutputStream</code>
+ * using the default character encoding of the platform.
+ * <p>
+ * This method uses {@link String#getBytes()}.
+ *
+ * @param data the <code>String</code> to write, null ignored
+ * @param output the <code>OutputStream</code> to write to
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void write(String data, OutputStream output) throws IOException {
+ if (data != null) {
+ output.write(data.getBytes());
+ }
+ }
+
+ /**
+ * Writes chars from a <code>String</code> to bytes on an <code>OutputStream</code>
+ * using the specified character encoding.
+ * <p>
+ * Character encoding names can be found at
+ * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+ * <p>
+ * This method uses {@link String#getBytes(String)}.
+ *
+ * @param data the <code>String</code> to write, null ignored
+ * @param output the <code>OutputStream</code> to write to
+ * @param encoding the encoding to use, null means platform default
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void write(String data, OutputStream output, String encoding) throws IOException {
+ if (data != null) {
+ if (encoding == null) {
+ write(data, output);
+ } else {
+ output.write(data.getBytes(encoding));
+ }
+ }
+ }
+
+ // write StringBuffer
+ //-----------------------------------------------------------------------
+ /**
+ * Writes chars from a <code>StringBuffer</code> to a <code>Writer</code>.
+ *
+ * @param data the <code>StringBuffer</code> to write, null ignored
+ * @param output the <code>Writer</code> to write to
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void write(StringBuffer data, Writer output) throws IOException {
+ if (data != null) {
+ output.write(data.toString());
+ }
+ }
+
+ /**
+ * Writes chars from a <code>StringBuffer</code> to bytes on an <code>OutputStream</code>
+ * using the default character encoding of the platform.
+ * <p>
+ * This method uses {@link StringBuffer#getBytes()}.
+ *
+ * @param data the <code>StringBuffer</code> to write, null ignored
+ * @param output the <code>OutputStream</code> to write to
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void write(StringBuffer data, OutputStream output) throws IOException {
+ if (data != null) {
+ output.write(data.toString().getBytes());
+ }
+ }
+
+ /**
+ * Writes chars from a <code>StringBuffer</code> to bytes on an <code>OutputStream</code>
+ * using the specified character encoding.
+ * <p>
+ * Character encoding names can be found at
+ * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+ * <p>
+ * This method uses {@link StringBuffer#getBytes(String)}.
+ *
+ * @param data the <code>StringBuffer</code> to write, null ignored
+ * @param output the <code>OutputStream</code> to write to
+ * @param encoding the encoding to use, null means platform default
+ * @throws NullPointerException if output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void write(StringBuffer data, OutputStream output, String encoding) throws IOException {
+ if (data != null) {
+ if (encoding == null) {
+ write(data, output);
+ } else {
+ output.write(data.toString().getBytes(encoding));
+ }
+ }
+ }
+
+ // copy from InputStream
+ //-----------------------------------------------------------------------
+ /**
+ * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedInputStream</code>.
+ *
+ * @param input the <code>InputStream</code> to read from
+ * @param output the <code>OutputStream</code> to write to
+ * @return the number of bytes copied
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static int copy(InputStream input, OutputStream output) throws IOException {
+ byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
+ int count = 0;
+ int n = 0;
+ while (-1 != (n = input.read(buffer))) {
+ output.write(buffer, 0, n);
+ count += n;
+ }
+ return count;
+ }
+
+ /**
+ * Copy bytes from an <code>InputStream</code> to chars on a <code>Writer</code>
+ * using the default character encoding of the platform.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedInputStream</code>.
+ * <p>
+ * This method uses {@link InputStreamReader}.
+ *
+ * @param input the <code>InputStream</code> to read from
+ * @param output the <code>Writer</code> to write to
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void copy(InputStream input, Writer output) throws IOException {
+ InputStreamReader in = new InputStreamReader(input);
+ copy(in, output);
+ }
+
+ /**
+ * Copy bytes from an <code>InputStream</code> to chars on a <code>Writer</code>
+ * using the specified character encoding.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedInputStream</code>.
+ * <p>
+ * Character encoding names can be found at
+ * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+ * <p>
+ * This method uses {@link InputStreamReader}.
+ *
+ * @param input the <code>InputStream</code> to read from
+ * @param output the <code>Writer</code> to write to
+ * @param encoding the encoding to use, null means platform default
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void copy(InputStream input, Writer output, String encoding) throws IOException {
+ if (encoding == null) {
+ copy(input, output);
+ } else {
+ InputStreamReader in = new InputStreamReader(input, encoding);
+ copy(in, output);
+ }
+ }
+
+ // copy from Reader
+ //-----------------------------------------------------------------------
+ /**
+ * Copy 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>.
+ *
+ * @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.1
+ */
+ public static int copy(Reader input, Writer output) throws IOException {
+ char[] buffer = new char[DEFAULT_BUFFER_SIZE];
+ int count = 0;
+ int n = 0;
+ while (-1 != (n = input.read(buffer))) {
+ output.write(buffer, 0, n);
+ count += n;
+ }
+ return count;
+ }
+
+ /**
+ * Copy chars from a <code>Reader</code> to bytes on an <code>OutputStream</code>
+ * using the default character encoding of the platform, and calling flush.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedReader</code>.
+ * <p>
+ * Due to the implementation of OutputStreamWriter, this method performs a flush.
+ * <p>
+ * This method uses {@link OutputStreamWriter}.
+ *
+ * @param input the <code>Reader</code> to read from
+ * @param output the <code>OutputStream</code> to write to
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void copy(Reader input, OutputStream output) throws IOException {
+ OutputStreamWriter out = new OutputStreamWriter(output);
+ copy(input, out);
+ // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
+ out.flush();
+ }
+
+ /**
+ * Copy chars from a <code>Reader</code> to bytes on an <code>OutputStream</code>
+ * using the specified character encoding, and calling flush.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedReader</code>.
+ * <p>
+ * Character encoding names can be found at
+ * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
+ * <p>
+ * Due to the implementation of OutputStreamWriter, this method performs a flush.
+ * <p>
+ * This method uses {@link OutputStreamWriter}.
+ *
+ * @param input the <code>Reader</code> to read from
+ * @param output the <code>OutputStream</code> to write to
+ * @param encoding the encoding to use, null means platform default
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static void copy(Reader input, OutputStream output, String encoding) throws IOException {
+ if (encoding == null) {
+ copy(input, output);
+ } else {
+ OutputStreamWriter out = new OutputStreamWriter(output, encoding);
+ copy(input, out);
+ // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
+ out.flush();
+ }
+ }
+
+ // content equals
//-----------------------------------------------------------------------
/**
* Compare the contents of two Streams to determine if they are equal or not.
* <p>
- * This method buffers the input internally using <code>BufferedInputStream</code>.
+ * This method buffers the input internally using <code>BufferedInputStream</code>
+ * if they are not already buffered.
*
* @param input1 the first stream
* @param input2 the second stream
@@ -309,24 +738,58 @@
* @throws IOException if an I/O error occurs
*/
public static boolean contentEquals(InputStream input1, InputStream input2) throws IOException {
- InputStream bufferedInput1 = new BufferedInputStream(input1);
- InputStream bufferedInput2 = new BufferedInputStream(input2);
-
- int ch = bufferedInput1.read();
+ if (input1 instanceof BufferedInputStream == false) {
+ input1 = new BufferedInputStream(input1);
+ }
+ if (input2 instanceof BufferedInputStream == false) {
+ input2 = new BufferedInputStream(input2);
+ }
+
+ int ch = input1.read();
while (-1 != ch) {
- int ch2 = bufferedInput2.read();
+ int ch2 = input2.read();
if (ch != ch2) {
return false;
}
- ch = bufferedInput1.read();
+ ch = input1.read();
}
+
+ int ch2 = input2.read();
+ return (ch2 == -1);
+ }
- int ch2 = bufferedInput2.read();
- if (-1 != ch2) {
- return false;
- } else {
- return true;
+ /**
+ * Compare the contents of two Readers to determine if they are equal or not.
+ * <p>
+ * This method buffers the input internally using <code>BufferedReader</code>
+ * if they are not already buffered.
+ *
+ * @param input1 the first reader
+ * @param input2 the second reader
+ * @return true if the content of the readers are equal or they both don't exist, false otherwise
+ * @throws NullPointerException if either input is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static boolean contentEquals(Reader input1, Reader input2) throws IOException {
+ if (input1 instanceof BufferedReader == false) {
+ input1 = new BufferedReader(input1);
+ }
+ if (input2 instanceof BufferedReader == false) {
+ input2 = new BufferedReader(input2);
+ }
+
+ int ch = input1.read();
+ while (-1 != ch) {
+ int ch2 = input2.read();
+ if (ch != ch2) {
+ return false;
+ }
+ ch = input1.read();
}
+
+ int ch2 = input2.read();
+ return (ch2 == -1);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org