diff --git a/etc/libs-incl.xml b/etc/libs-incl.xml index 02993325..ae7e7844 100644 --- a/etc/libs-incl.xml +++ b/etc/libs-incl.xml @@ -3,10 +3,7 @@ - - - diff --git a/src/java/com/samskivert/io/StreamUtil.java b/src/java/com/samskivert/io/StreamUtil.java index 3b0f1306..4db8f2c6 100644 --- a/src/java/com/samskivert/io/StreamUtil.java +++ b/src/java/com/samskivert/io/StreamUtil.java @@ -22,9 +22,11 @@ package com.samskivert.io; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Writer; import java.io.Reader; +import java.nio.charset.Charset; import static com.samskivert.Log.log; @@ -88,4 +90,31 @@ public class StreamUtil } } } + + /** + * Reads the contents of the supplied stream into a string using the supplied {@link Charset}. + */ + public static String toString (InputStream stream, String charset) + throws IOException + { + InputStreamReader reader = new InputStreamReader(stream, charset); + char[] inbuf = new char[4096]; + StringBuffer outbuf = new StringBuffer(); + for (int read = 0; (read = reader.read(inbuf)) > 0; ) { + outbuf.append(inbuf, 0, read); + } + return outbuf.toString(); + } + + /** + * Copies the contents of the supplied input stream to the supplied output stream. + */ + public static void copy (InputStream in, OutputStream out) + throws IOException + { + byte[] buffer = new byte[4096]; + for (int read = 0; (read = in.read(buffer)) > 0; ) { + out.write(buffer, 0, read); + } + } } diff --git a/src/java/com/samskivert/io/tests/StreamUtilTest.java b/src/java/com/samskivert/io/tests/StreamUtilTest.java new file mode 100644 index 00000000..74c357d8 --- /dev/null +++ b/src/java/com/samskivert/io/tests/StreamUtilTest.java @@ -0,0 +1,76 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001-2008 Michael Bayne +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.io.tests; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.nio.charset.Charset; + +import org.junit.*; +import static org.junit.Assert.*; + +import com.samskivert.io.StreamUtil; + +/** + * Tests the {@link StreamUtil} class. + */ +public class StreamUtilTest +{ + @Test public void testCopy () + throws Exception + { + assertTrue(PHRASE.equals(copyString(PHRASE))); + String bulk = getBigText(); + assertTrue(bulk.equals(copyString(bulk))); + } + + @Test public void testToString () + throws Exception + { + byte[] data = PHRASE.getBytes("UTF-8"); + assertTrue(PHRASE.equals(StreamUtil.toString(new ByteArrayInputStream(data), "UTF-8"))); + String text = getBigText(); + data = text.getBytes("UTF-8"); + assertTrue(text.equals(StreamUtil.toString(new ByteArrayInputStream(data), "UTF-8"))); + } + + protected String getBigText () + { + StringBuffer buf = new StringBuffer(); + for (int ii = 0; ii < 10000; ii++) { + buf.append(PHRASE).append("\n"); + } + return buf.toString(); + } + + protected String copyString (String text) + throws Exception + { + Charset charset = Charset.forName("UTF-8"); + ByteArrayInputStream bin = new ByteArrayInputStream(text.getBytes(charset)); + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + StreamUtil.copy(bin, bout); + return bout.toString("UTF-8"); + } + + protected static final String PHRASE = + "Now is the time for all good men\nto come to the aid of their government."; +} diff --git a/src/java/com/samskivert/servlet/tests/SiteResourceLoaderTest.java b/src/java/com/samskivert/servlet/tests/SiteResourceLoaderTest.java index f6e51580..13e2dd0c 100644 --- a/src/java/com/samskivert/servlet/tests/SiteResourceLoaderTest.java +++ b/src/java/com/samskivert/servlet/tests/SiteResourceLoaderTest.java @@ -31,8 +31,7 @@ import javax.servlet.http.HttpServletRequest; import junit.framework.Test; import junit.framework.TestCase; -import org.apache.commons.io.IOUtils; - +import com.samskivert.io.StreamUtil; import com.samskivert.servlet.Site; import com.samskivert.servlet.SiteIdentifier; import com.samskivert.servlet.SiteResourceLoader; @@ -67,8 +66,7 @@ public class SiteResourceLoaderTest extends TestCase } } - protected void testResourceLoader ( - int siteId, SiteResourceLoader loader, String compareFile) + protected void testResourceLoader (int siteId, SiteResourceLoader loader, String compareFile) throws IOException { StringBuffer gen = new StringBuffer(); @@ -82,17 +80,16 @@ public class SiteResourceLoaderTest extends TestCase if (cin == null) { throw new IOException("Unable to load " + compareFile); } - cmp.append(IOUtils.toString(cin)); + cmp.append(StreamUtil.toString(cin, "UTF-8")); // Log.info("Loaded resources [cmp=" + compareFile + "]: " + gen); // now make sure the strings match - assertEquals("Testing " + compareFile, - cmp.toString(), gen.toString()); + assertEquals("Testing " + compareFile, cmp.toString(), gen.toString()); } - protected void appendResource (int siteId, StringBuffer buffer, - SiteResourceLoader loader, String path) + protected void appendResource ( + int siteId, StringBuffer buffer, SiteResourceLoader loader, String path) throws IOException { InputStream rin = null; @@ -111,7 +108,7 @@ public class SiteResourceLoaderTest extends TestCase rpath = TestUtil.getResourcePath(rpath); rin = new FileInputStream(rpath); } - buffer.append(IOUtils.toString(rin)); + buffer.append(StreamUtil.toString(rin, "UTF-8")); } public static Test suite () diff --git a/src/java/com/samskivert/util/FileUtil.java b/src/java/com/samskivert/util/FileUtil.java index 2dfc89f0..bb0f4c68 100644 --- a/src/java/com/samskivert/util/FileUtil.java +++ b/src/java/com/samskivert/util/FileUtil.java @@ -29,8 +29,6 @@ import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; -import org.apache.commons.io.IOUtils; - import com.samskivert.io.StreamUtil; import static com.samskivert.Log.log; @@ -108,7 +106,7 @@ public class FileUtil try { fout = new BufferedOutputStream(new FileOutputStream(efile)); jin = jar.getInputStream(entry); - IOUtils.copy(jin, fout); + StreamUtil.copy(jin, fout); } catch (Exception e) { log.warning("Failure unpacking", "jar", jar, "entry", efile, "error", e); failure = true;