I grow tired of having a commons-io dependency (and all the crap that comes
along with it just to have essentially two methods: copy(InputStream,OutputStream) and toString(InputStream) We'll just duplicate those ten lines of code and save a dozen of our libraries some trouble. This is a precursor to a possible Velocity jihad because Velocity is pissing me off by not working because of some bullshit classloader tomfoolery that is the sort of thing that makes Java programmers want to jump butt first off the building. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2586 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -3,10 +3,7 @@
|
||||
<project name="library-dependencies">
|
||||
<fileset dir="${libs.dir}" id="samskivert.libs">
|
||||
<include name="ant.jar"/>
|
||||
<include name="commons-collections.jar"/>
|
||||
<include name="commons-digester.jar"/>
|
||||
<include name="commons-io.jar"/>
|
||||
<include name="commons-logging.jar"/>
|
||||
<include name="ehcache.jar"/>
|
||||
<include name="junit-3.7.jar"/>
|
||||
<include name="junit4.jar"/>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.";
|
||||
}
|
||||
@@ -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 ()
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user