You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by je...@apache.org on 2003/12/30 17:35:11 UTC
cvs commit: jakarta-commons-sandbox/io/src/java/org/apache/commons/io CopyUtils.java IOUtils.java
jeremias 2003/12/30 08:35:11
Modified: io/src/java/org/apache/commons/io CopyUtils.java
IOUtils.java
Log:
Removed methods that take a bufferSize parameter.
Discussion:
http://www.mail-archive.com/commons-dev@jakarta.apache.org/msg19703.html
http://nagoya.apache.org/eyebrowse/BrowseList?listName=commons-dev@jakarta.apache.org&by=thread&from=572862
Revision Changes Path
1.4 +15 -158 jakarta-commons-sandbox/io/src/java/org/apache/commons/io/CopyUtils.java
Index: CopyUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/io/src/java/org/apache/commons/io/CopyUtils.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- CopyUtils.java 30 Dec 2003 06:52:49 -0000 1.3
+++ CopyUtils.java 30 Dec 2003 16:35:11 -0000 1.4
@@ -94,23 +94,7 @@
* @throws IOException In case of an I/O problem
*/
public static void copy(byte[] input, OutputStream output)
- throws IOException {
- copy(input, output, DEFAULT_BUFFER_SIZE);
- }
-
- /**
- * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
- * @param input the byte array to read from
- * @param output the <code>OutputStream</code> to write to
- * @param bufferSize Size of internal buffer to use.
- * @throws IOException In case of an I/O problem
- */
- public static void copy(
- byte[] input,
- OutputStream output,
- int bufferSize)
- throws IOException {
- // TODO Is bufferSize param needed?
+ throws IOException {
output.write(input);
}
@@ -127,28 +111,12 @@
* @throws IOException In case of an I/O problem
*/
public static void copy(byte[] input, 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 input the byte array to read from
- * @param output the <code>Writer</code> to write to
- * @param bufferSize Size of internal buffer to use.
- * @throws IOException In case of an I/O problem
- */
- public static void copy(
- byte[] input,
- Writer output,
- int bufferSize)
- throws IOException {
+ throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(input);
- copy(in, output, bufferSize);
+ copy(in, output);
}
+
/**
* Copy and convert bytes from a <code>byte[]</code> to chars on a
* <code>Writer</code>, using the specified encoding.
@@ -168,26 +136,6 @@
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 input the byte array to read from
- * @param output the <code>Writer</code> to write to
- * @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.
- * @throws IOException In case of an I/O problem
- */
- public static void copy(
- byte[] input,
- Writer output,
- String encoding,
- int bufferSize)
- throws IOException {
- ByteArrayInputStream in = new ByteArrayInputStream(input);
- copy(in, output, encoding, bufferSize);
- }
// ----------------------------------------------------------------
// Core copy methods
@@ -200,25 +148,11 @@
* @return the number of bytes copied
* @throws IOException In case of an I/O problem
*/
- public static int copy(InputStream input, OutputStream output)
- throws IOException {
- return copy(input, output, DEFAULT_BUFFER_SIZE);
- }
-
- /**
- * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
- * @param input the <code>InputStream</code> to read from
- * @param output the <code>OutputStream</code> to write to
- * @param bufferSize Size of internal buffer to use.
- * @return the number of bytes copied
- * @throws IOException In case of an I/O problem
- */
public static int copy(
InputStream input,
- OutputStream output,
- int bufferSize)
+ OutputStream output)
throws IOException {
- byte[] buffer = new byte[bufferSize];
+ byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
@@ -239,25 +173,11 @@
* @return the number of characters copied
* @throws IOException In case of an I/O problem
*/
- public static int copy(Reader input, Writer output)
- throws IOException {
- return copy(input, output, DEFAULT_BUFFER_SIZE);
- }
-
- /**
- * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
- * @param input the <code>Reader</code> to read from
- * @param output the <code>Writer</code> to write to
- * @param bufferSize Size of internal buffer to use.
- * @return the number of characters copied
- * @throws IOException In case of an I/O problem
- */
public static int copy(
Reader input,
- Writer output,
- int bufferSize)
+ Writer output)
throws IOException {
- char[] buffer = new char[bufferSize];
+ char[] buffer = new char[DEFAULT_BUFFER_SIZE];
int count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
@@ -279,27 +199,12 @@
* @param output the <code>Writer</code> to write to
* @throws IOException In case of an I/O problem
*/
- public static void copy(InputStream input, Writer output)
- throws IOException {
- copy(input, output, DEFAULT_BUFFER_SIZE);
- }
-
- /**
- * Copy and convert bytes from an <code>InputStream</code> to chars on a
- * <code>Writer</code>.
- * The platform's default encoding is used for the byte-to-char conversion.
- * @param input the <code>InputStream</code> to read from
- * @param output the <code>Writer</code> to write to
- * @param bufferSize Size of internal buffer to use.
- * @throws IOException In case of an I/O problem
- */
public static void copy(
InputStream input,
- Writer output,
- int bufferSize)
+ Writer output)
throws IOException {
InputStreamReader in = new InputStreamReader(input);
- copy(in, output, bufferSize);
+ copy(in, output);
}
/**
@@ -321,26 +226,6 @@
copy(in, output);
}
- /**
- * Copy and convert bytes from an <code>InputStream</code> to chars on a
- * <code>Writer</code>, using the specified encoding.
- * @param input the <code>InputStream</code> to read from
- * @param output the <code>Writer</code> to write to
- * @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.
- * @throws IOException In case of an I/O problem
- */
- public static void copy(
- InputStream input,
- Writer output,
- String encoding,
- int bufferSize)
- throws IOException {
- InputStreamReader in = new InputStreamReader(input, encoding);
- copy(in, output, bufferSize);
- }
// ----------------------------------------------------------------
// Reader -> OutputStream
@@ -353,26 +238,12 @@
* @param output the <code>OutputStream</code> to write to
* @throws IOException In case of an I/O problem
*/
- public static void copy(Reader input, OutputStream output)
- throws IOException {
- copy(input, output, DEFAULT_BUFFER_SIZE);
- }
-
- /**
- * Serialize chars from a <code>Reader</code> to bytes on an
- * <code>OutputStream</code>, and flush the <code>OutputStream</code>.
- * @param input the <code>Reader</code> to read from
- * @param output the <code>OutputStream</code> to write to
- * @param bufferSize Size of internal buffer to use.
- * @throws IOException In case of an I/O problem
- */
public static void copy(
Reader input,
- OutputStream output,
- int bufferSize)
+ OutputStream output)
throws IOException {
OutputStreamWriter out = new OutputStreamWriter(output);
- copy(input, out, bufferSize);
+ copy(input, out);
// XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
out.flush();
}
@@ -388,27 +259,13 @@
* @param output the <code>OutputStream</code> to write to
* @throws IOException In case of an I/O problem
*/
- public static void copy(String input, 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 input the <code>String</code> to read from
- * @param output the <code>OutputStream</code> to write to
- * @param bufferSize Size of internal buffer to use.
- * @throws IOException In case of an I/O problem
- */
public static void copy(
String input,
- OutputStream output,
- int bufferSize)
+ OutputStream output)
throws IOException {
StringReader in = new StringReader(input);
OutputStreamWriter out = new OutputStreamWriter(output);
- copy(in, out, bufferSize);
+ copy(in, out);
// XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
out.flush();
}
1.12 +21 -134 jakarta-commons-sandbox/io/src/java/org/apache/commons/io/IOUtils.java
Index: IOUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/io/src/java/org/apache/commons/io/IOUtils.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- IOUtils.java 30 Dec 2003 15:24:23 -0000 1.11
+++ IOUtils.java 30 Dec 2003 16:35:11 -0000 1.12
@@ -81,13 +81,14 @@
* 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
- * may be worth tweaking. Often "large buffer -> faster" does not hold, even for large data
- * transfers.</p>
- *
- * <p>For byte-to-char methods, a <code>copy</code> variant allows the encoding to be selected
- * (otherwise the platform default is used).</p>
+ * <p>For byte-to-char methods, a <code>copy</code> variant allows the encoding
+ * to be selected (otherwise the platform default is used). We would like to
+ * encourage you to always specify the encoding because relying on the platform
+ * default can lead to unexpected results.</p>
+ *
+ * <p>We don't provide special variants for the <code>copy</code> methods that
+ * let you specify the buffer size because in modern VMs the impact on speed
+ * seems to be minimal. We're using a default buffer size of 4 KB.</p>
*
* <p>The <code>copy</code> methods use an internal buffer when copying. It is therefore advisable
* <em>not</em> to deliberately wrap the stream arguments to the <code>copy</code> methods in
@@ -134,8 +135,8 @@
*
* <p>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.</p>
+ * method variants to specify the encoding, each row may
+ * correspond to up to 2 methods.</p>
*
* <p>Origin of code: Apache Avalon (Excalibur)</p>
*
@@ -248,22 +249,8 @@
public static String toString( InputStream input )
throws IOException
{
- return toString( input, DEFAULT_BUFFER_SIZE );
- }
-
- /**
- * Get the contents of an <code>InputStream</code> as a String.
- * The platform's default encoding is used for the byte-to-char conversion.
- * @param input the <code>InputStream</code> to read from
- * @param bufferSize Size of internal buffer to use.
- * @return the requested <code>String</code>
- * @throws IOException In case of an I/O problem
- */
- public static String toString( InputStream input, int bufferSize )
- throws IOException
- {
StringWriter sw = new StringWriter();
- CopyUtils.copy( input, sw, bufferSize );
+ CopyUtils.copy( input, sw );
return sw.toString();
}
@@ -271,34 +258,17 @@
* Get the contents of an <code>InputStream</code> as a String.
* @param input the <code>InputStream</code> to read from
* @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.
- * @return the requested <code>String</code>
- * @throws IOException In case of an I/O problem
- */
- public static String toString( InputStream input, String encoding )
- throws IOException
- {
- return toString( input, encoding, DEFAULT_BUFFER_SIZE );
- }
-
- /**
- * Get the contents of an <code>InputStream</code> as a String.
- * @param input the <code>InputStream</code> to read from
- * @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.
* @return the requested <code>String</code>
* @throws IOException In case of an I/O problem
*/
public static String toString( InputStream input,
- String encoding,
- int bufferSize )
+ String encoding )
throws IOException
{
StringWriter sw = new StringWriter();
- CopyUtils.copy( input, sw, encoding, bufferSize );
+ CopyUtils.copy( input, sw, encoding );
return sw.toString();
}
@@ -314,21 +284,8 @@
public static byte[] toByteArray( InputStream input )
throws IOException
{
- return toByteArray( input, DEFAULT_BUFFER_SIZE );
- }
-
- /**
- * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
- * @param input the <code>InputStream</code> to read from
- * @param bufferSize Size of internal buffer to use.
- * @return the requested byte array
- * @throws IOException In case of an I/O problem
- */
- public static byte[] toByteArray( InputStream input, int bufferSize )
- throws IOException
- {
ByteArrayOutputStream output = new ByteArrayOutputStream();
- CopyUtils.copy( input, output, bufferSize );
+ CopyUtils.copy( input, output );
return output.toByteArray();
}
@@ -349,21 +306,8 @@
public static String toString( Reader input )
throws IOException
{
- return toString( input, DEFAULT_BUFFER_SIZE );
- }
-
- /**
- * Get the contents of a <code>Reader</code> as a String.
- * @param input the <code>Reader</code> to read from
- * @param bufferSize Size of internal buffer to use.
- * @return the requested <code>String</code>
- * @throws IOException In case of an I/O problem
- */
- public static String toString( Reader input, int bufferSize )
- throws IOException
- {
StringWriter sw = new StringWriter();
- CopyUtils.copy( input, sw, bufferSize );
+ CopyUtils.copy( input, sw );
return sw.toString();
}
@@ -379,21 +323,8 @@
public static byte[] toByteArray( Reader input )
throws IOException
{
- return toByteArray( input, DEFAULT_BUFFER_SIZE );
- }
-
- /**
- * Get the contents of a <code>Reader</code> as a <code>byte[]</code>.
- * @param input the <code>Reader</code> to read from
- * @param bufferSize Size of internal buffer to use.
- * @return the requested byte array
- * @throws IOException In case of an I/O problem
- */
- public static byte[] toByteArray( Reader input, int bufferSize )
- throws IOException
- {
ByteArrayOutputStream output = new ByteArrayOutputStream();
- CopyUtils.copy( input, output, bufferSize );
+ CopyUtils.copy( input, output );
return output.toByteArray();
}
@@ -415,26 +346,12 @@
public static byte[] toByteArray( String input )
throws IOException
{
- return toByteArray( input, DEFAULT_BUFFER_SIZE );
- }
-
- /**
- * Get the contents of a <code>String</code> as a <code>byte[]</code>.
- * @param input the <code>String</code> to convert
- * @param bufferSize Size of internal buffer to use.
- * @return the requested byte array
- * @throws IOException In case of an I/O problem
- */
- public static byte[] toByteArray( String input, int bufferSize )
- throws IOException
- {
ByteArrayOutputStream output = new ByteArrayOutputStream();
- CopyUtils.copy( input, output, bufferSize );
+ CopyUtils.copy( input, output );
return output.toByteArray();
}
-
///////////////////////////////////////////////////////////////
// Derived copy methods
// byte[] -> *
@@ -453,39 +370,11 @@
public static String toString( 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 input the byte array to read from
- * @param bufferSize Size of internal buffer to use.
- * @return the requested <code>String</code>
- * @throws IOException In case of an I/O problem
- */
- public static String toString( byte[] input, int bufferSize )
- throws IOException
- {
StringWriter sw = new StringWriter();
- CopyUtils.copy( input, sw, bufferSize );
+ CopyUtils.copy( input, sw );
return sw.toString();
}
- /**
- * Get the contents of a <code>byte[]</code> as a String.
- * @param input the byte array to read from
- * @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.
- * @return the requested <code>String</code>
- * @throws IOException In case of an I/O problem
- */
- public static String toString( byte[] input, String encoding )
- throws IOException
- {
- return toString( input, encoding, DEFAULT_BUFFER_SIZE );
- }
/**
* Get the contents of a <code>byte[]</code> as a String.
@@ -493,17 +382,15 @@
* @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.
* @return the requested <code>String</code>
* @throws IOException In case of an I/O problem
*/
public static String toString( byte[] input,
- String encoding,
- int bufferSize )
+ String encoding )
throws IOException
{
StringWriter sw = new StringWriter();
- CopyUtils.copy( input, sw, encoding, bufferSize );
+ CopyUtils.copy( input, sw, encoding );
return sw.toString();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org