Hi,
The attached patch adds copy methods for byte arrays. This was quite an
obvious omission, given that Strings (char arrays) are already
supported. I needed it to do stuff like:
URL url = new URL("http://jakarta.apache.org");
InputStream in = url.openStream();
byte[] bArr = IOUtil.toByteArray( in );
in.close();
Copy methods now exist from (InputStream|Reader|String|byte[]) to
(OutputStream|Writer|String|byte[]).
IOUtilsTestlet.java is also updated to test the new methods, *however*,
it will fail if run with "ant test"; that is the subject of the second
patch, which fixes build.xml to exclude
../jakarta-avalon/tools/lib/avalon-excalibur.jar.
--Jeff
Index: src/java/org/apache/avalon/excalibur/io/IOUtil.java
===================================================================
RCS file:
/home/cvspublic/jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/io/IOUtil.java,v
retrieving revision 1.1
diff -u -r1.1 IOUtil.java
--- src/java/org/apache/avalon/excalibur/io/IOUtil.java 2001/07/19 07:33:01
1.1
+++ src/java/org/apache/avalon/excalibur/io/IOUtil.java 2001/07/26 12:34:02
@@ -20,18 +20,27 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
/**
* General IO Stream manipulation.
* <p>
* This class provides static utility methods for input/output operations,
particularly buffered
- * copying between sources (<code>InputStream</code> and <code>Reader</code>,
<code>String</code>)
- * and destinations (<code>OutputStream</code>, <code>Writer</code> and
<code>String</code>).
+ * copying between sources (<code>InputStream</code>, <code>Reader</code>,
<code>String</code> and
+ * <code>byte[]</code>) and destinations (<code>OutputStream</code>,
<code>Writer</code>,
+ * <code>String</code> and <code>byte[]</code>).
* </p>
*
* <p>Unless otherwise noted, these <code>copy</code> methods do <em>not</em>
flush or close the
* streams. Often, doing so would require making non-portable assumptions
about the streams' origin
- * and further use.</p>
+ * and further use. This means that both streams' <code>close()</code> methods
must be called after
+ * copying. if one omits this step, then the stream resources (sockets, file
descriptors) are
+ * released when the associated Stream is garbage-collected. It is not a good
idea to rely on this
+ * mechanism. For a good overview of the distinction between "memory
management" and "resource
+ * management", see <a
href="http://www.unixreview.com/articles/1998/9804/9804ja/ja.htm">this
+ * UnixReview
+ * article</a></p>
*
* <p>For each <code>copy</code> method, a variant is provided that allows the
caller to specify the
* buffer size (the default is 4k). As the buffer size can have a fairly large
impact on speed, this
@@ -63,6 +72,38 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Jeff Turner</a>
*/
+
+/*
+ * Behold, intrepid explorers; a map of this class:
+ *
+ * method Input Output Depency
+ * ------ ----- ------ -------
+ * 1 copy InputStream OutputStream (primitive)
+ * 2 copy Reader Writer (primitive)
+ *
+ * 3 copy InputStream Writer 2
+ * 4 toString InputStream String 3
+ * 5 toByteArray InputStream byte[] 1
+ *
+ * 6 copy Reader OutputStream 2
+ * 7 toString Reader String 2
+ * 8 toByteArray Reader byte[] 6
+ *
+ * 9 copy String OutputStream 2
+ * 10 copy String Writer (trivial)
+ * 11 toByteArray String byte[] 9
+ *
+ * 12 copy byte[] Writer 3
+ * 13 toString byte[] String 12
+ * 14 copy byte[] OutputStream (trivial)
+ *
+ *
+ * Note that only the first two methods shuffle bytes; the rest use these two,
or (if possible) copy
+ * using native Java copy methods. As there are method variants to specify
buffer size and encoding,
+ * each row may correspond to up to 4 methods.
+ *
+ */
+
public final class IOUtil
{
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
@@ -115,35 +156,35 @@
*/
public static void copy( final InputStream input, final OutputStream
output )
throws IOException
- {
- copy( input, output, DEFAULT_BUFFER_SIZE );
- }
+ {
+ copy( input, output, DEFAULT_BUFFER_SIZE );
+ }
/**
* Copy bytes from an <code>InputStream</code> to an
<code>OutputStream</code>.
* @param bufferSize Size of internal buffer to use.
*/
public static void copy( final InputStream input,
- final OutputStream output,
- final int bufferSize )
+ final OutputStream output,
+ final int bufferSize )
throws IOException
- {
- final byte[] buffer = new byte[ bufferSize ];
- int n = 0;
- while( -1 != (n = input.read( buffer )) )
{
- output.write( buffer, 0, n );
+ final byte[] buffer = new byte[ bufferSize ];
+ int n = 0;
+ while( -1 != (n = input.read( buffer )) )
+ {
+ output.write( buffer, 0, n );
+ }
}
- }
/**
* Copy chars from a <code>Reader</code> to a <code>Writer</code>.
*/
public static void copy( final Reader input, final Writer output )
throws IOException
- {
- copy( input, output, DEFAULT_BUFFER_SIZE );
- }
+ {
+ copy( input, output, DEFAULT_BUFFER_SIZE );
+ }
/**
* Copy chars from a <code>Reader</code> to a <code>Writer</code>.
@@ -151,20 +192,21 @@
*/
public static void copy( final Reader input, final Writer output, final
int bufferSize )
throws IOException
- {
- final char[] buffer = new char[ bufferSize ];
- int n = 0;
- while( -1 != (n = input.read( buffer )) )
{
- output.write( buffer, 0, n );
+ final char[] buffer = new char[ bufferSize ];
+ int n = 0;
+ while( -1 != (n = input.read( buffer )) )
+ {
+ output.write( buffer, 0, n );
+ }
}
- }
///////////////////////////////////////////////////////////////
// Derived copy methods
// InputStream -> *
///////////////////////////////////////////////////////////////
+
///////////////////////////////////////////////////////////////
// InputStream -> Writer
@@ -175,9 +217,9 @@
*/
public static void copy( final InputStream input, final Writer output )
throws IOException
- {
- copy( input, output, DEFAULT_BUFFER_SIZE );
- }
+ {
+ copy( input, output, DEFAULT_BUFFER_SIZE );
+ }
/**
* Copy and convert bytes from an <code>InputStream</code> to chars on a
@@ -187,10 +229,10 @@
*/
public static void copy( final InputStream input, final Writer output,
final int bufferSize )
throws IOException
- {
- final InputStreamReader in = new InputStreamReader( input );
- copy( in, output, bufferSize );
- }
+ {
+ final InputStreamReader in = new InputStreamReader( input );
+ copy( in, output, bufferSize );
+ }
/**
* Copy and convert bytes from an <code>InputStream</code> to chars on a
@@ -201,10 +243,10 @@
*/
public static void copy( final InputStream input, final Writer output,
final String encoding )
throws IOException
- {
- final InputStreamReader in = new InputStreamReader( input, encoding );
- copy( in, output );
- }
+ {
+ final InputStreamReader in = new InputStreamReader( input,
encoding );
+ copy( in, output );
+ }
/**
* Copy and convert bytes from an <code>InputStream</code> to chars on a
@@ -215,14 +257,15 @@
* @param bufferSize Size of internal buffer to use.
*/
public static void copy( final InputStream input,
- final Writer output,
- final String encoding,
- final int bufferSize )
+ final Writer output,
+ final String encoding,
+ final int bufferSize )
throws IOException
- {
- final InputStreamReader in = new InputStreamReader( input, encoding );
- copy( in, output, bufferSize );
- }
+ {
+ final InputStreamReader in = new InputStreamReader( input,
encoding );
+ copy( in, output, bufferSize );
+ }
+
///////////////////////////////////////////////////////////////
// InputStream -> String
@@ -233,9 +276,9 @@
*/
public static String toString( final InputStream input )
throws IOException
- {
- return toString( input, DEFAULT_BUFFER_SIZE );
- }
+ {
+ return toString( input, DEFAULT_BUFFER_SIZE );
+ }
/**
* Get the contents of an <code>InputStream</code> as a String.
@@ -244,11 +287,11 @@
*/
public static String toString( final InputStream input, final int
bufferSize )
throws IOException
- {
- final StringWriter sw = new StringWriter();
- copy( input, sw, bufferSize );
- return sw.toString();
- }
+ {
+ final StringWriter sw = new StringWriter();
+ copy( input, sw, bufferSize );
+ return sw.toString();
+ }
/**
* Get the contents of an <code>InputStream</code> as a String.
@@ -258,9 +301,9 @@
*/
public static String toString( final InputStream input, final String
encoding )
throws IOException
- {
- return toString( input, encoding, DEFAULT_BUFFER_SIZE );
- }
+ {
+ return toString( input, encoding, DEFAULT_BUFFER_SIZE );
+ }
/**
* Get the contents of an <code>InputStream</code> as a String.
@@ -270,45 +313,40 @@
* @param bufferSize Size of internal buffer to use.
*/
public static String toString( final InputStream input,
- final String encoding,
- final int bufferSize )
+ final String encoding,
+ final int bufferSize )
throws IOException
- {
- final StringWriter sw = new StringWriter();
- copy( input, sw, encoding, bufferSize );
- return sw.toString();
- }
-
+ {
+ final StringWriter sw = new StringWriter();
+ copy( input, sw, encoding, bufferSize );
+ return sw.toString();
+ }
///////////////////////////////////////////////////////////////
- // String -> OutputStream
+ // InputStream -> byte[]
/**
- * Serialize chars from a <code>String</code> to bytes on an
<code>OutputStream</code>, and
- * flush the <code>OutputStream</code>.
+ * Get the contents of an <code>InputStream</code> as a
<code>byte[]</code>.
*/
- public static void copy( final String input, final OutputStream output )
+ public static byte[] toByteArray( final InputStream input )
throws IOException
- {
- copy( input, output, DEFAULT_BUFFER_SIZE );
- }
+ {
+ return toByteArray( input, DEFAULT_BUFFER_SIZE );
+ }
/**
- * Serialize chars from a <code>String</code> to bytes on an
<code>OutputStream</code>, and
- * flush the <code>OutputStream</code>.
+ * Get the contents of an <code>InputStream</code> as a
<code>byte[]</code>.
* @param bufferSize Size of internal buffer to use.
*/
- public static void copy( final String input, final OutputStream output,
final int bufferSize )
+ public static byte[] toByteArray( final InputStream input, final int
bufferSize )
throws IOException
- {
- final StringReader in = new StringReader( input );
- final OutputStreamWriter out = new OutputStreamWriter( output );
- copy( in, out, bufferSize );
- // NOTE: Unless anyone is planning on rewriting OutputStreamWriter, we
have to flush
- // here.
- out.flush();
- }
+ {
+ final ByteArrayOutputStream output = new ByteArrayOutputStream();
+ copy( input, output, bufferSize );
+ return output.toByteArray();
+ }
+
///////////////////////////////////////////////////////////////
// Derived copy methods
// Reader -> *
@@ -322,9 +360,9 @@
*/
public static void copy( final Reader input, final OutputStream output )
throws IOException
- {
- copy( input, output, DEFAULT_BUFFER_SIZE );
- }
+ {
+ copy( input, output, DEFAULT_BUFFER_SIZE );
+ }
/**
* Serialize chars from a <code>Reader</code> to bytes on an
<code>OutputStream</code>, and
@@ -333,13 +371,13 @@
*/
public static void copy( final Reader input, final OutputStream output,
final int bufferSize )
throws IOException
- {
- final OutputStreamWriter out = new OutputStreamWriter(output);
- copy( input, out, bufferSize );
- // NOTE: Unless anyone is planning on rewriting OutputStreamWriter, we
have to flush
- // here.
- out.flush();
- }
+ {
+ final OutputStreamWriter out = new OutputStreamWriter(output);
+ copy( input, out, bufferSize );
+ // NOTE: Unless anyone is planning on rewriting
OutputStreamWriter, we have to flush
+ // here.
+ out.flush();
+ }
///////////////////////////////////////////////////////////////
// Reader -> String
@@ -348,9 +386,9 @@
*/
public static String toString( final Reader input )
throws IOException
- {
- return toString( input, DEFAULT_BUFFER_SIZE );
- }
+ {
+ return toString( input, DEFAULT_BUFFER_SIZE );
+ }
/**
* Get the contents of a <code>Reader</code> as a String.
@@ -358,14 +396,75 @@
*/
public static String toString( final Reader input, final int bufferSize )
throws IOException
- {
- final StringWriter sw = new StringWriter();
- copy( input, sw, bufferSize );
- return sw.toString();
- }
+ {
+ final StringWriter sw = new StringWriter();
+ copy( input, sw, bufferSize );
+ return sw.toString();
+ }
///////////////////////////////////////////////////////////////
+ // Reader -> byte[]
+ /**
+ * Get the contents of a <code>Reader</code> as a <code>byte[]</code>.
+ */
+ public static byte[] toByteArray( final Reader input )
+ throws IOException
+ {
+ return toByteArray( input, DEFAULT_BUFFER_SIZE );
+ }
+
+ /**
+ * Get the contents of a <code>Reader</code> as a <code>byte[]</code>.
+ * @param bufferSize Size of internal buffer to use.
+ */
+ public static byte[] toByteArray( final Reader input, final int bufferSize
)
+ throws IOException
+ {
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ copy( input, output, bufferSize );
+ return output.toByteArray();
+ }
+
+
+ ///////////////////////////////////////////////////////////////
+ // Derived copy methods
+ // String -> *
+ ///////////////////////////////////////////////////////////////
+
+
+ ///////////////////////////////////////////////////////////////
+ // String -> OutputStream
+
+ /**
+ * Serialize chars from a <code>String</code> to bytes on an
<code>OutputStream</code>, and
+ * flush the <code>OutputStream</code>.
+ */
+ public static void copy( final String input, final OutputStream output )
+ throws IOException
+ {
+ copy( input, output, DEFAULT_BUFFER_SIZE );
+ }
+
+ /**
+ * Serialize chars from a <code>String</code> to bytes on an
<code>OutputStream</code>, and
+ * flush the <code>OutputStream</code>.
+ * @param bufferSize Size of internal buffer to use.
+ */
+ public static void copy( final String input, final OutputStream output,
final int bufferSize )
+ throws IOException
+ {
+ final StringReader in = new StringReader( input );
+ final OutputStreamWriter out = new OutputStreamWriter( output );
+ copy( in, out, bufferSize );
+ // NOTE: Unless anyone is planning on rewriting
OutputStreamWriter, we have to flush
+ // here.
+ out.flush();
+ }
+
+
+
+ ///////////////////////////////////////////////////////////////
// String -> Writer
/**
@@ -373,9 +472,9 @@
*/
public static void copy( final String input, final Writer output )
throws IOException
- {
- output.write( input );
- }
+ {
+ output.write( input );
+ }
/**
* Copy bytes from an <code>InputStream</code> to an
@@ -390,10 +489,183 @@
*/
public static void bufferedCopy( final InputStream input, final
OutputStream output )
throws IOException
- {
- final BufferedInputStream in = new BufferedInputStream( input );
- final BufferedOutputStream out = new BufferedOutputStream( output );
- copy( in, out );
- out.flush();
- }
+ {
+ final BufferedInputStream in = new BufferedInputStream( input );
+ final BufferedOutputStream out = new BufferedOutputStream( output
);
+ copy( in, out );
+ out.flush();
+ }
+
+
+ ///////////////////////////////////////////////////////////////
+ // String -> byte[]
+ /**
+ * Get the contents of a <code>String</code> as a <code>byte[]</code>.
+ */
+ public static byte[] toByteArray( final String input )
+ throws IOException
+ {
+ return toByteArray( input, DEFAULT_BUFFER_SIZE );
+ }
+
+ /**
+ * Get the contents of a <code>String</code> as a <code>byte[]</code>.
+ * @param bufferSize Size of internal buffer to use.
+ */
+ public static byte[] toByteArray( final String input, final int bufferSize
)
+ throws IOException
+ {
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ copy( input, output, bufferSize );
+ return output.toByteArray();
+ }
+
+
+
+ ///////////////////////////////////////////////////////////////
+ // Derived copy methods
+ // byte[] -> *
+ ///////////////////////////////////////////////////////////////
+
+
+ ///////////////////////////////////////////////////////////////
+ // byte[] -> Writer
+
+ /**
+ * Copy and convert bytes from a <code>byte[]</code> to chars on a
+ * <code>Writer</code>.
+ * The platform's default encoding is used for the byte-to-char conversion.
+ */
+ public static void copy( final byte[] input, final Writer output )
+ throws IOException
+ {
+ copy( input, output, DEFAULT_BUFFER_SIZE );
+ }
+
+ /**
+ * Copy and convert bytes from a <code>byte[]</code> to chars on a
+ * <code>Writer</code>.
+ * The platform's default encoding is used for the byte-to-char conversion.
+ * @param bufferSize Size of internal buffer to use.
+ */
+ public static void copy( final byte[] input, final Writer output, final
int bufferSize )
+ throws IOException
+ {
+ final ByteArrayInputStream in = new ByteArrayInputStream( input );
+ copy( in, output, bufferSize );
+ }
+
+ /**
+ * Copy and convert bytes from a <code>byte[]</code> to chars on a
+ * <code>Writer</code>, using the specified encoding.
+ * @param encoding The name of a supported character encoding. See the
+ * <a href="http://www.iana.org/assignments/character-sets">IANA
+ * Charset Registry</a> for a list of valid encoding types.
+ */
+ public static void copy( final byte[] input, final Writer output, final
String encoding )
+ throws IOException
+ {
+ final ByteArrayInputStream in = new ByteArrayInputStream( input );
+ copy( in, output, encoding );
+ }
+
+ /**
+ * Copy and convert bytes from a <code>byte[]</code> to chars on a
+ * <code>Writer</code>, using the specified encoding.
+ * @param encoding The name of a supported character encoding. See the
+ * <a href="http://www.iana.org/assignments/character-sets">IANA
+ * Charset Registry</a> for a list of valid encoding types.
+ * @param bufferSize Size of internal buffer to use.
+ */
+ public static void copy( final byte[] input,
+ final Writer output,
+ final String encoding,
+ final int bufferSize )
+ throws IOException
+ {
+ final ByteArrayInputStream in = new ByteArrayInputStream( input );
+ copy( in, output, encoding, bufferSize );
+ }
+
+
+ ///////////////////////////////////////////////////////////////
+ // byte[] -> String
+
+ /**
+ * Get the contents of a <code>byte[]</code> as a String.
+ * The platform's default encoding is used for the byte-to-char conversion.
+ */
+ public static String toString( final byte[] input )
+ throws IOException
+ {
+ return toString( input, DEFAULT_BUFFER_SIZE );
+ }
+
+ /**
+ * Get the contents of a <code>byte[]</code> as a String.
+ * The platform's default encoding is used for the byte-to-char conversion.
+ * @param bufferSize Size of internal buffer to use.
+ */
+ public static String toString( final byte[] input, final int bufferSize )
+ throws IOException
+ {
+ final StringWriter sw = new StringWriter();
+ copy( input, sw, bufferSize );
+ return sw.toString();
+ }
+
+ /**
+ * Get the contents of a <code>byte[]</code> as a String.
+ * @param encoding The name of a supported character encoding. See the
+ * <a href="http://www.iana.org/assignments/character-sets">IANA
+ * Charset Registry</a> for a list of valid encoding types.
+ */
+ public static String toString( final byte[] input, final String encoding )
+ throws IOException
+ {
+ return toString( input, encoding, DEFAULT_BUFFER_SIZE );
+ }
+
+ /**
+ * Get the contents of a <code>byte[]</code> as a String.
+ * @param encoding The name of a supported character encoding. See the
+ * <a href="http://www.iana.org/assignments/character-sets">IANA
+ * Charset Registry</a> for a list of valid encoding types.
+ * @param bufferSize Size of internal buffer to use.
+ */
+ public static String toString( final byte[] input,
+ final String encoding,
+ final int bufferSize )
+ throws IOException
+ {
+ final StringWriter sw = new StringWriter();
+ copy( input, sw, encoding, bufferSize );
+ return sw.toString();
+ }
+
+
+ ///////////////////////////////////////////////////////////////
+ // byte[] -> OutputStream
+
+ /**
+ * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
+ */
+ public static void copy( final byte[] input, final OutputStream output )
+ throws IOException
+ {
+ copy( input, output, DEFAULT_BUFFER_SIZE );
+ }
+
+ /**
+ * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
+ * @param bufferSize Size of internal buffer to use.
+ */
+ public static void copy( final byte[] input,
+ final OutputStream output,
+ final int bufferSize )
+ throws IOException
+ {
+ output.write(input);
+ }
+
}
Index: src/test/org/apache/avalon/excalibur/io/test/IOUtilTestlet.java
===================================================================
RCS file:
/home/cvspublic/jakarta-avalon-excalibur/src/test/org/apache/avalon/excalibur/io/test/IOUtilTestlet.java,v
retrieving revision 1.1
diff -u -r1.1 IOUtilTestlet.java
--- src/test/org/apache/avalon/excalibur/io/test/IOUtilTestlet.java
2001/07/19 07:36:03 1.1
+++ src/test/org/apache/avalon/excalibur/io/test/IOUtilTestlet.java
2001/07/26 12:34:04
@@ -22,11 +22,20 @@
* <li>The output stream must not have been closed (a byte/char is written
to test this, and
* subsequent size checked)</li>
* </ul>
+ * Due to interdependencies in IOUtils and IOUtilsTestlet, one bug may cause
+ * multiple tests to fail.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jeff Turner</a>
*/
+
+/*
+ * 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)
+ */
public final class IOUtilTestlet
- extends AbstractTestlet
+extends AbstractTestlet
{
private final int FILE_SIZE = 1024 * 4 + 1;
@@ -35,241 +44,340 @@
public IOUtilTestlet()
throws IOException
- {
- m_testDirectory = (new File( "test/io/" )).getAbsoluteFile();
- if( !m_testDirectory.exists() )
{
- m_testDirectory.mkdirs();
- }
+ m_testDirectory = (new File( "test/io/" )).getAbsoluteFile();
+ if( !m_testDirectory.exists() )
+ {
+ m_testDirectory.mkdirs();
+ }
- m_testFile = new File( m_testDirectory, "file2-test.txt" );
+ m_testFile = new File( m_testDirectory, "file2-test.txt" );
- createFile( m_testFile, FILE_SIZE );
- }
+ createFile( m_testFile, FILE_SIZE );
+ }
private void createFile( final File file, final long size )
throws IOException
- {
- final BufferedOutputStream output =
- new BufferedOutputStream( new FileOutputStream( file ) );
-
- for( int i = 0; i < size; i++ )
{
- output.write( (byte)(i % 255) ); // nice varied byte pattern
+ final BufferedOutputStream output =
+ new BufferedOutputStream( new FileOutputStream( file ) );
+
+ for( int i = 0; i < size; i++ )
+ {
+ output.write( (byte)(i % 255) ); // nice varied byte pattern
+ }
+
+ output.close();
}
- output.close();
- }
+ /** Assert that the contents of two byte arrays are the same. */
+ private void assertEqualContent( final byte[] b0, final byte[] b1 )
+ throws IOException
+ {
+ assert( "Content not equal according to
java.util.Arrays#equals()", Arrays.equals( b0, b1 ) );
+ }
/** Assert that the content of two files is the same. */
private void assertEqualContent( final File f0, final File f1 )
throws IOException
- {
- final FileInputStream is0 = new FileInputStream( f0 );
- final FileInputStream is1 = new FileInputStream( f1 );
- final byte[] buf0 = new byte[ FILE_SIZE ];
- final byte[] buf1 = new byte[ FILE_SIZE ];
- int n0 = 0;
- int n1 = 0;
-
- while( -1 != n0 )
- {
- n0 = is0.read( buf0 );
- n1 = is1.read( buf1 );
- assert( "The files " + f0 + " and " + f1 +
- " have differing number of bytes available (" + n0 +
- " vs " + n1 + ")", (n0 == n1) );
+ {
+ final FileInputStream is0 = new FileInputStream( f0 );
+ final FileInputStream is1 = new FileInputStream( f1 );
+ final byte[] buf0 = new byte[ FILE_SIZE ];
+ final byte[] buf1 = new byte[ FILE_SIZE ];
+ int n0 = 0;
+ int n1 = 0;
+
+ while( -1 != n0 )
+ {
+ n0 = is0.read( buf0 );
+ n1 = is1.read( buf1 );
+ assert( "The files " + f0 + " and " + f1 +
+ " have differing number of bytes available (" + n0 +
+ " vs " + n1 + ")", (n0 == n1) );
+
+ assert( "The files " + f0 + " and " + f1 +
+ " have different content", Arrays.equals( buf0, buf1 )
);
+ }
+ }
- assert( "The files " + f0 + " and " + f1 +
- " have different content", Arrays.equals( buf0, buf1 ) );
+ /** Assert that the content of a file is equal to that in a byte[]. */
+ private void assertEqualContent( final byte[] b0, final File file )
+ throws IOException
+ {
+ final FileInputStream is = new FileInputStream( file );
+ byte[] b1 = new byte[b0.length];
+ int numRead = is.read(b1);
+ assert( "Different number of bytes", numRead == b0.length &&
is.available() == 0 );
+ for (
+ int i=0;
+ i<numRead;
+ assert( "Byte "+i+" differs ("+b0[i]+" != "+b1[i]+")",
b0[i] == b1[i] ), i++
+ );
}
- }
public void testInputStreamToOutputStream()
throws Exception
- {
- final File destination = newFile();
- final FileInputStream fin = new FileInputStream( m_testFile );
- final FileOutputStream fout = new FileOutputStream( destination );
-
- IOUtil.copy( fin, fout );
- assert( "Not all bytes were read", fin.available() == 0 );
- fout.flush();
-
- checkFile( destination );
- checkWrite( fout );
- fout.close();
- deleteFile( destination );
- }
+ {
+ final File destination = newFile();
+ final FileInputStream fin = new FileInputStream( m_testFile );
+ final FileOutputStream fout = new FileOutputStream( destination );
+
+ IOUtil.copy( fin, fout );
+ assert( "Not all bytes were read", fin.available() == 0 );
+ fout.flush();
+
+ checkFile( destination );
+ checkWrite( fout );
+ fout.close();
+ deleteFile( destination );
+ }
public void testInputStreamToWriter()
throws Exception
- {
- final File destination = newFile();
- final FileInputStream fin = new FileInputStream( m_testFile );
- final FileWriter fout = new FileWriter( destination );
-
- IOUtil.copy( fin, fout );
-
- assert( "Not all bytes were read", fin.available() == 0 );
- fout.flush();
-
- checkFile( destination );
- checkWrite( fout );
- fout.close();
- deleteFile( destination );
- }
+ {
+
+ final File destination = newFile();
+ final FileInputStream fin = new FileInputStream( m_testFile );
+ final FileWriter fout = new FileWriter( destination );
+
+ IOUtil.copy( fin, fout );
+
+ assert( "Not all bytes were read", fin.available() == 0 );
+ fout.flush();
+
+ checkFile( destination );
+ checkWrite( fout );
+ fout.close();
+ deleteFile( destination );
+ }
public void testInputStreamToString()
throws Exception
- {
- final FileInputStream fin = new FileInputStream( m_testFile );
- final String out = IOUtil.toString( fin );
- assertNotNull( out );
- assert( "Not all bytes were read", fin.available() == 0 );
- assert( "Wrong output size: out.length()=" + out.length() +
- "!=" + FILE_SIZE, out.length() == FILE_SIZE );
- }
+ {
+ final FileInputStream fin = new FileInputStream( m_testFile );
+ final String out = IOUtil.toString( fin );
+ assertNotNull( out );
+ assert( "Not all bytes were read", fin.available() == 0 );
+ assert( "Wrong output size: out.length()=" + out.length() +
+ "!=" + FILE_SIZE, out.length() == FILE_SIZE );
+ }
///////////////////////////////////////////////////////////
public void testReaderToOutputStream()
throws Exception
- {
- final File destination = newFile();
- final FileReader fin = new FileReader( m_testFile );
- final FileOutputStream fout = new FileOutputStream( destination );
- IOUtil.copy( fin, fout );
- //Note: this method *does* flush. It is equivalent to:
- // final OutputStreamWriter _out = new OutputStreamWriter(fout);
- // IOUtil.copy( fin, _out, 4096 ); // copy( Reader, Writer, int );
- // _out.flush();
- // out = fout;
-
- // Note: rely on the method to flush
- checkFile( destination );
- checkWrite( fout );
- fout.close();
- deleteFile( destination );
- }
+ {
+ final File destination = newFile();
+ final FileReader fin = new FileReader( m_testFile );
+ final FileOutputStream fout = new FileOutputStream( destination );
+ IOUtil.copy( fin, fout );
+ //Note: this method *does* flush. It is equivalent to:
+ // final OutputStreamWriter _out = new OutputStreamWriter(fout);
+ // IOUtil.copy( fin, _out, 4096 ); // copy( Reader, Writer, int );
+ // _out.flush();
+ // out = fout;
+
+ // Note: rely on the method to flush
+ checkFile( destination );
+ checkWrite( fout );
+ fout.close();
+ deleteFile( destination );
+ }
public void testReaderToWriter()
throws Exception
- {
- final File destination = newFile();
- final FileReader fin = new FileReader( m_testFile );
- final FileWriter fout = new FileWriter( destination );
- IOUtil.copy( fin, fout );
-
- fout.flush();
- checkFile( destination );
- checkWrite( fout );
- fout.close();
- fin.close();
- deleteFile( destination );
- }
+ {
+ final File destination = newFile();
+ final FileReader fin = new FileReader( m_testFile );
+ final FileWriter fout = new FileWriter( destination );
+ IOUtil.copy( fin, fout );
+
+ fout.flush();
+ checkFile( destination );
+ checkWrite( fout );
+ fout.close();
+ fin.close();
+ deleteFile( destination );
+ }
public void testReaderToString()
throws Exception
- {
- final FileReader fin = new FileReader( m_testFile );
- final String out = IOUtil.toString( fin );
- assertNotNull( out );
- assert( "Wrong output size: out.length()=" +
- out.length() + "!=" + FILE_SIZE,
- out.length() == FILE_SIZE );
- }
+ {
+ final FileReader fin = new FileReader( m_testFile );
+ final String out = IOUtil.toString( fin );
+ assertNotNull( out );
+ assert( "Wrong output size: out.length()=" +
+ out.length() + "!=" + FILE_SIZE,
+ out.length() == FILE_SIZE );
+ }
public void testStringToOutputStream()
throws Exception
- {
- final File destination = newFile();
- final FileReader fin = new FileReader( m_testFile );
- // Create our String. Rely on testReaderToString() to make sure this
is valid.
- final String str = IOUtil.toString( fin );
- final FileOutputStream fout = new FileOutputStream( destination );
- IOUtil.copy( str, fout );
- //Note: this method *does* flush. It is equivalent to:
- // final OutputStreamWriter _out = new OutputStreamWriter(fout);
- // IOUtil.copy( str, _out, 4096 ); // copy( Reader, Writer, int );
- // _out.flush();
- // out = fout;
- // note: we don't flush here; this IOUtils method does it for us
-
- checkFile( destination );
- checkWrite( fout );
- fout.close();
- deleteFile( destination );
- }
+ {
+ final File destination = newFile();
+ final FileReader fin = new FileReader( m_testFile );
+ // Create our String. Rely on testReaderToString() to make sure
this is valid.
+ final String str = IOUtil.toString( fin );
+ final FileOutputStream fout = new FileOutputStream( destination );
+ IOUtil.copy( str, fout );
+ //Note: this method *does* flush. It is equivalent to:
+ // final OutputStreamWriter _out = new OutputStreamWriter(fout);
+ // IOUtil.copy( str, _out, 4096 ); // copy( Reader, Writer, int );
+ // _out.flush();
+ // out = fout;
+ // note: we don't flush here; this IOUtils method does it for us
+
+ checkFile( destination );
+ checkWrite( fout );
+ fout.close();
+ deleteFile( destination );
+ }
public void testStringToWriter()
throws Exception
- {
- final File destination = newFile();
- FileReader fin = new FileReader( m_testFile );
- // Create our String. Rely on testReaderToString() to make sure this
is valid.
- final String str = IOUtil.toString( fin );
- final FileWriter fout = new FileWriter( destination );
- IOUtil.copy( str, fout );
- fout.flush();
-
- checkFile( destination );
- checkWrite( fout );
- fout.close();
+ {
+ final File destination = newFile();
+ FileReader fin = new FileReader( m_testFile );
+ // Create our String. Rely on testReaderToString() to make sure
this is valid.
+ final String str = IOUtil.toString( fin );
+ final FileWriter fout = new FileWriter( destination );
+ IOUtil.copy( str, fout );
+ fout.flush();
+
+ checkFile( destination );
+ checkWrite( fout );
+ fout.close();
- deleteFile( destination );
- }
+ deleteFile( destination );
+ }
- private File newFile()
+
+ public void testInputStreamToByteArray()
+ throws Exception
+ {
+ final FileInputStream fin = new FileInputStream( m_testFile );
+ final byte[] out = IOUtil.toByteArray( fin );
+ assertNotNull( out );
+ assert( "Not all bytes were read", fin.available() == 0 );
+ assert( "Wrong output size: out.length=" + out.length +
+ "!=" + FILE_SIZE, out.length == FILE_SIZE );
+ assertEqualContent( out, m_testFile );
+ }
+
+ public void testStringToByteArray()
throws Exception
- {
- final File destination = new File( m_testDirectory, "copy.txt" );
- assert( "Test output data file shouldn't previously exist",
- !destination.exists() );
+ {
+ final FileReader fin = new FileReader( m_testFile );
- return destination;
- }
+ // Create our String. Rely on testReaderToString() to make sure
this is valid.
+ final String str = IOUtil.toString( fin );
- private void checkFile( final File file )
+ final byte[] out = IOUtil.toByteArray( str );
+ assertEqualContent( str.getBytes(), out );
+ }
+
+ public void testByteArrayToWriter()
throws Exception
- {
- assert( "Check existence of output file", file.exists() );
- assertEqualContent( m_testFile, file );
- }
+ {
+ final File destination = newFile();
+ final FileWriter fout = new FileWriter( destination );
- private void checkWrite( final OutputStream output )
+ // Create our byte[]. Rely on testInputStreamToByteArray() to make
sure this is valid.
+ final byte[] in = IOUtil.toByteArray( new FileInputStream(
m_testFile ) );
+ IOUtil.copy( in, fout );
+ fout.flush();
+ checkFile( destination );
+ checkWrite( fout );
+ fout.close();
+ deleteFile( destination );
+ }
+
+
+ public void testByteArrayToString()
throws Exception
- {
- try
{
- new PrintStream( output ).write( 0 );
+ final byte[] in = IOUtil.toByteArray( new FileInputStream(
m_testFile ) );
+ // Create our byte[]. Rely on testInputStreamToByteArray() to make
sure this is valid.
+ String str = IOUtil.toString( in );
+ assertEqualContent( in, str.getBytes() );
}
- catch( final Throwable t )
+
+
+ public void testByteArrayToOutputStream()
+ throws Exception
{
- throw new TestFailedException( "The copy() method closed the
stream " +
- "when it shouldn't have. " +
t.getMessage() );
+ final File destination = newFile();
+ final FileOutputStream fout = new FileOutputStream( destination );
+
+ // Create our byte[]. Rely on testInputStreamToByteArray() to make
sure this is valid.
+ final byte[] in = IOUtil.toByteArray( new FileInputStream(
m_testFile ) );
+
+ IOUtil.copy( in, fout );
+
+ fout.flush();
+
+ checkFile( destination );
+ checkWrite( fout );
+ fout.close();
+ deleteFile( destination );
}
- }
- private void checkWrite( final Writer output )
+
+ //////////////////////////////////////////////////////
+ // xxxxxxxxx
+
+
+ private File newFile()
+ throws Exception
+ {
+ final File destination = new File( m_testDirectory, "copy.txt" );
+ assert( "Test output data file shouldn't previously exist",
+ !destination.exists() );
+
+ return destination;
+ }
+
+ private void checkFile( final File file )
+ throws Exception
+ {
+ assert( "Check existence of output file", file.exists() );
+ assertEqualContent( m_testFile, file );
+ }
+
+ private void checkWrite( final OutputStream output )
throws Exception
- {
- try
{
- new PrintWriter( output ).write( 'a' );
+ try
+ {
+ new PrintStream( output ).write( 0 );
+ }
+ catch( final Throwable t )
+ {
+ throw new TestFailedException( "The copy() method closed the
stream " +
+ "when it shouldn't have. " + t.getMessage() );
+ }
}
- catch( final Throwable t )
+
+ private void checkWrite( final Writer output )
+ throws Exception
{
- throw new TestFailedException( "The copy() method closed the
stream " +
- "when it shouldn't have. " +
t.getMessage() );
+ try
+ {
+ new PrintWriter( output ).write( 'a' );
+ }
+ catch( final Throwable t )
+ {
+ throw new TestFailedException( "The copy() method closed the
stream " +
+ "when it shouldn't have. " + t.getMessage() );
+ }
}
- }
private void deleteFile( final File file )
throws Exception
- {
- assert( "Wrong output size: file.length()=" +
- file.length() + "!=" + FILE_SIZE + 1,
- file.length() == FILE_SIZE + 1 );
- file.delete();
- }
+ {
+ assert( "Wrong output size: file.length()=" +
+ file.length() + "!=" + FILE_SIZE + 1,
+ file.length() == FILE_SIZE + 1 );
+ file.delete();
+ }
}
Index: build.xml
===================================================================
RCS file: /home/cvspublic/jakarta-avalon-excalibur/build.xml,v
retrieving revision 1.9
diff -u -r1.9 build.xml
--- build.xml 2001/07/27 07:19:01 1.9
+++ build.xml 2001/07/27 12:11:44
@@ -116,6 +116,7 @@
<include name="*.jar" />
<exclude name="testlet.jar"/>
<exclude name="xerces.jar"/>
+ <exclude name="avalon-excalibur*.jar"/>
</fileset>
</path>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]