You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ma...@apache.org on 2004/10/24 06:20:07 UTC
cvs commit: jakarta-commons/io/src/java/org/apache/commons/io CopyUtils.java EndianUtils.java FileCleaner.java FileUtils.java FilenameUtils.java HexDump.java IOUtils.java
martinc 2004/10/23 21:20:07
Modified: io/src/java/org/apache/commons/io CopyUtils.java
EndianUtils.java FileCleaner.java FileUtils.java
FilenameUtils.java HexDump.java IOUtils.java
Log:
Made a start on getting us Checkstyle clean. Fixed a few hundred gripes,
but there are still many, many more.
Revision Changes Path
1.10 +53 -41 jakarta-commons/io/src/java/org/apache/commons/io/CopyUtils.java
Index: CopyUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/CopyUtils.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- CopyUtils.java 13 Aug 2004 23:51:41 -0000 1.9
+++ CopyUtils.java 24 Oct 2004 04:20:06 -0000 1.10
@@ -1,12 +1,12 @@
/*
* 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.
@@ -26,24 +26,25 @@
import java.io.Writer;
/**
- * <p>
- * This class provides static utility methods for buffered
- * 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. 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
+ * <p>This class provides static utility methods for buffered
+ * 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. 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 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
+ * <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>
*
@@ -51,24 +52,30 @@
* 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
- * <code>Buffered*</code> streams. For example, don't do the
- * following:</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 <code>Buffered*</code> streams. For
+ * example, don't do the following:</p>
*
- * <code>copy( new BufferedInputStream( in ), new BufferedOutputStream( out ) );</code>
+ * <code>copy( new BufferedInputStream( in ),
+ * new BufferedOutputStream( out ) );</code>
*
* <p>The rationale is as follows:</p>
*
- * <p>Imagine that an InputStream's read() is a very expensive operation, which would usually suggest
- * wrapping in a BufferedInputStream. The BufferedInputStream works by issuing infrequent
- * {@link java.io.InputStream#read(byte[] b, int off, int len)} requests on the underlying InputStream, to
- * fill an internal buffer, from which further <code>read</code> requests can inexpensively get
- * their data (until the buffer runs out).</p>
- * <p>However, the <code>copy</code> methods do the same thing, keeping an internal buffer,
- * populated by {@link InputStream#read(byte[] b, int off, int len)} requests. Having two buffers
- * (or three if the destination stream is also buffered) is pointless, and the unnecessary buffer
- * management hurts performance slightly (about 3%, according to some simple experiments).</p>
+ * <p>Imagine that an InputStream's read() is a very expensive operation, which
+ * would usually suggest wrapping in a BufferedInputStream. The
+ * BufferedInputStream works by issuing infrequent
+ * {@link java.io.InputStream#read(byte[] b, int off, int len)} requests on the
+ * underlying InputStream, to fill an internal buffer, from which further
+ * <code>read</code> requests can inexpensively get their data (until the buffer
+ * runs out).</p>
+ *
+ * <p>However, the <code>copy</code> methods do the same thing, keeping an
+ * internal buffer, populated by
+ * {@link InputStream#read(byte[] b, int off, int len)} requests. Having two
+ * buffers (or three if the destination stream is also buffered) is pointless,
+ * and the unnecessary buffer management hurts performance slightly (about 3%,
+ * according to some simple experiments).</p>
*
* <p>Behold, intrepid explorers; a map of this class:</p>
* <pre>
@@ -101,7 +108,8 @@
* @version $Id$
* @deprecated Use IOUtils. Will be removed in 2.0.
* Methods renamed to IOUtils.write() or IOUtils.copy().
- * Null handling behaviour changed in IOUtils (null data does not throw NullPointerException).
+ * Null handling behaviour changed in IOUtils (null data does not
+ * throw NullPointerException).
*/
public class CopyUtils {
@@ -113,7 +121,7 @@
/**
* Instances should NOT be constructed in standard programming.
*/
- public CopyUtils() {}
+ public CopyUtils() { }
// ----------------------------------------------------------------
// byte[] -> OutputStream
@@ -174,7 +182,8 @@
// ----------------------------------------------------------------
/**
- * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
+ * 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
* @return the number of bytes copied
@@ -264,7 +273,7 @@
// ----------------------------------------------------------------
/**
- * Serialize chars from a <code>Reader</code> to bytes on an
+ * 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
@@ -276,7 +285,8 @@
throws IOException {
OutputStreamWriter out = new OutputStreamWriter(output);
copy(input, out);
- // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
+ // XXX Unless anyone is planning on rewriting OutputStreamWriter, we
+ // have to flush here.
out.flush();
}
@@ -285,7 +295,8 @@
// ----------------------------------------------------------------
/**
- * Serialize chars from a <code>String</code> to bytes on an <code>OutputStream</code>, and
+ * 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
@@ -298,7 +309,8 @@
StringReader in = new StringReader(input);
OutputStreamWriter out = new OutputStreamWriter(output);
copy(in, out);
- // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
+ // XXX Unless anyone is planning on rewriting OutputStreamWriter, we
+ // have to flush here.
out.flush();
}
1.15 +42 -61 jakarta-commons/io/src/java/org/apache/commons/io/EndianUtils.java
Index: EndianUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/EndianUtils.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- EndianUtils.java 13 Jun 2004 04:45:25 -0000 1.14
+++ EndianUtils.java 24 Oct 2004 04:20:06 -0000 1.15
@@ -1,12 +1,12 @@
/*
* 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.
@@ -28,8 +28,7 @@
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
* @version CVS $Revision$ $Date$
*/
-public class EndianUtils
-{
+public class EndianUtils {
/**
* Instances should NOT be constructed in standard programming.
@@ -43,9 +42,8 @@
* @param value value to convert
* @return the converted value
*/
- public static short swapShort( short value )
- {
- return (short)( ( ( ( value >> 0 ) & 0xff ) << 8 ) +
+ public static short swapShort(short value) {
+ return (short) ( ( ( ( value >> 0 ) & 0xff ) << 8 ) +
( ( ( value >> 8 ) & 0xff ) << 0 ) );
}
@@ -54,8 +52,7 @@
* @param value value to convert
* @return the converted value
*/
- public static int swapInteger( int value )
- {
+ public static int swapInteger(int value) {
return
( ( ( value >> 0 ) & 0xff ) << 24 ) +
( ( ( value >> 8 ) & 0xff ) << 16 ) +
@@ -68,8 +65,7 @@
* @param value value to convert
* @return the converted value
*/
- public static long swapLong( long value )
- {
+ public static long swapLong(long value) {
return
( ( ( value >> 0 ) & 0xff ) << 56 ) +
( ( ( value >> 8 ) & 0xff ) << 48 ) +
@@ -86,8 +82,7 @@
* @param value value to convert
* @return the converted value
*/
- public static float swapFloat( float value )
- {
+ public static float swapFloat(float value) {
return Float.intBitsToFloat( swapInteger( Float.floatToIntBits( value ) ) );
}
@@ -96,8 +91,7 @@
* @param value value to convert
* @return the converted value
*/
- public static double swapDouble( double value )
- {
+ public static double swapDouble(double value) {
return Double.longBitsToDouble( swapLong( Double.doubleToLongBits( value ) ) );
}
@@ -110,8 +104,7 @@
* @param offset starting offset in the byte array
* @param value value to write
*/
- public static void writeSwappedShort( byte[] data, int offset, short value )
- {
+ public static void writeSwappedShort(byte[] data, int offset, short value) {
data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
}
@@ -123,22 +116,20 @@
* @param offset starting offset in the byte array
* @return the value read
*/
- public static short readSwappedShort( byte[] data, int offset )
- {
+ public static short readSwappedShort(byte[] data, int offset) {
return (short)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
( ( data[ offset + 1 ] & 0xff ) << 8 ) );
}
/**
- * Reads an unsigned short (16-bit) value from a byte array at a given
- * offset. The value is converted to the opposed endian system while
+ * Reads an unsigned short (16-bit) value from a byte array at a given
+ * offset. The value is converted to the opposed endian system while
* reading.
* @param data source byte array
* @param offset starting offset in the byte array
* @return the value read
*/
- public static int readSwappedUnsignedShort( byte[] data, int offset )
- {
+ public static int readSwappedUnsignedShort(byte[] data, int offset) {
return (int)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
( ( data[ offset + 1 ] & 0xff ) << 8 ) );
}
@@ -150,8 +141,7 @@
* @param offset starting offset in the byte array
* @param value value to write
*/
- public static void writeSwappedInteger( byte[] data, int offset, int value )
- {
+ public static void writeSwappedInteger(byte[] data, int offset, int value) {
data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
@@ -165,8 +155,7 @@
* @param offset starting offset in the byte array
* @return the value read
*/
- public static int readSwappedInteger( byte[] data, int offset )
- {
+ public static int readSwappedInteger(byte[] data, int offset) {
return (int)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
( ( data[ offset + 1 ] & 0xff ) << 8 ) +
( ( data[ offset + 2 ] & 0xff ) << 16 ) +
@@ -174,15 +163,14 @@
}
/**
- * Reads an unsigned integer (32-bit) value from a byte array at a given
- * offset. The value is converted to the opposed endian system while
+ * Reads an unsigned integer (32-bit) value from a byte array at a given
+ * offset. The value is converted to the opposed endian system while
* reading.
* @param data source byte array
* @param offset starting offset in the byte array
* @return the value read
*/
- public static long readSwappedUnsignedInteger( byte[] data, int offset )
- {
+ public static long readSwappedUnsignedInteger(byte[] data, int offset) {
return (long)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
( ( data[ offset + 1 ] & 0xff ) << 8 ) +
( ( data[ offset + 2 ] & 0xff ) << 16 ) +
@@ -196,8 +184,7 @@
* @param offset starting offset in the byte array
* @param value value to write
*/
- public static void writeSwappedLong( byte[] data, int offset, long value )
- {
+ public static void writeSwappedLong(byte[] data, int offset, long value) {
data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
@@ -215,9 +202,8 @@
* @param offset starting offset in the byte array
* @return the value read
*/
- public static long readSwappedLong( byte[] data, int offset )
- {
- long low = (long)(
+ public static long readSwappedLong(byte[] data, int offset) {
+ long low = (long)(
( ( data[ offset + 0 ] & 0xff ) << 0 ) +
( ( data[ offset + 1 ] & 0xff ) << 8 ) +
( ( data[ offset + 2 ] & 0xff ) << 16 ) +
@@ -237,8 +223,7 @@
* @param offset starting offset in the byte array
* @param value value to write
*/
- public static void writeSwappedFloat( byte[] data, int offset, float value )
- {
+ public static void writeSwappedFloat(byte[] data, int offset, float value) {
writeSwappedInteger( data, offset, Float.floatToIntBits( value ) );
}
@@ -249,8 +234,7 @@
* @param offset starting offset in the byte array
* @return the value read
*/
- public static float readSwappedFloat( byte[] data, int offset )
- {
+ public static float readSwappedFloat(byte[] data, int offset) {
return Float.intBitsToFloat( readSwappedInteger( data, offset ) );
}
@@ -261,8 +245,7 @@
* @param offset starting offset in the byte array
* @param value value to write
*/
- public static void writeSwappedDouble( byte[] data, int offset, double value )
- {
+ public static void writeSwappedDouble(byte[] data, int offset, double value) {
writeSwappedLong( data, offset, Double.doubleToLongBits( value ) );
}
@@ -273,8 +256,7 @@
* @param offset starting offset in the byte array
* @return the value read
*/
- public static double readSwappedDouble( byte[] data, int offset )
- {
+ public static double readSwappedDouble(byte[] data, int offset) {
return Double.longBitsToDouble( readSwappedLong( data, offset ) );
}
@@ -285,7 +267,7 @@
* @param value value to write
* @throws IOException in case of an I/O problem
*/
- public static void writeSwappedShort( OutputStream output, short value )
+ public static void writeSwappedShort(OutputStream output, short value)
throws IOException
{
output.write( (byte)( ( value >> 0 ) & 0xff ) );
@@ -299,7 +281,7 @@
* @return the value just read
* @throws IOException in case of an I/O problem
*/
- public static short readSwappedShort( InputStream input )
+ public static short readSwappedShort(InputStream input)
throws IOException
{
return (short)( ( ( read( input ) & 0xff ) << 0 ) +
@@ -313,7 +295,7 @@
* @return the value just read
* @throws IOException in case of an I/O problem
*/
- public static int readSwappedUnsignedShort( InputStream input )
+ public static int readSwappedUnsignedShort(InputStream input)
throws IOException
{
int value1 = read( input );
@@ -330,7 +312,7 @@
* @param value value to write
* @throws IOException in case of an I/O problem
*/
- public static void writeSwappedInteger( OutputStream output, int value )
+ public static void writeSwappedInteger(OutputStream output, int value)
throws IOException
{
output.write( (byte)( ( value >> 0 ) & 0xff ) );
@@ -346,7 +328,7 @@
* @return the value just read
* @throws IOException in case of an I/O problem
*/
- public static int readSwappedInteger( InputStream input )
+ public static int readSwappedInteger(InputStream input)
throws IOException
{
int value1 = read( input );
@@ -367,7 +349,7 @@
* @return the value just read
* @throws IOException in case of an I/O problem
*/
- public static long readSwappedUnsignedInteger( InputStream input )
+ public static long readSwappedUnsignedInteger(InputStream input)
throws IOException
{
int value1 = read( input );
@@ -388,7 +370,7 @@
* @param value value to write
* @throws IOException in case of an I/O problem
*/
- public static void writeSwappedLong( OutputStream output, long value )
+ public static void writeSwappedLong(OutputStream output, long value)
throws IOException
{
output.write( (byte)( ( value >> 0 ) & 0xff ) );
@@ -408,7 +390,7 @@
* @return the value just read
* @throws IOException in case of an I/O problem
*/
- public static long readSwappedLong( InputStream input )
+ public static long readSwappedLong(InputStream input)
throws IOException
{
byte[] bytes = new byte[8];
@@ -423,7 +405,7 @@
* @param value value to write
* @throws IOException in case of an I/O problem
*/
- public static void writeSwappedFloat( OutputStream output, float value )
+ public static void writeSwappedFloat(OutputStream output, float value)
throws IOException
{
writeSwappedInteger( output, Float.floatToIntBits( value ) );
@@ -436,7 +418,7 @@
* @return the value just read
* @throws IOException in case of an I/O problem
*/
- public static float readSwappedFloat( InputStream input )
+ public static float readSwappedFloat(InputStream input)
throws IOException
{
return Float.intBitsToFloat( readSwappedInteger( input ) );
@@ -449,7 +431,7 @@
* @param value value to write
* @throws IOException in case of an I/O problem
*/
- public static void writeSwappedDouble( OutputStream output, double value )
+ public static void writeSwappedDouble(OutputStream output, double value)
throws IOException
{
writeSwappedLong( output, Double.doubleToLongBits( value ) );
@@ -462,19 +444,18 @@
* @return the value just read
* @throws IOException in case of an I/O problem
*/
- public static double readSwappedDouble( InputStream input )
+ public static double readSwappedDouble(InputStream input)
throws IOException
{
return Double.longBitsToDouble( readSwappedLong( input ) );
}
- private static int read( InputStream input )
+ private static int read(InputStream input)
throws IOException
{
int value = input.read();
- if( -1 == value )
- {
+ if( -1 == value ) {
throw new EOFException( "Unexpected EOF reached" );
}
1.2 +9 -9 jakarta-commons/io/src/java/org/apache/commons/io/FileCleaner.java
Index: FileCleaner.java
===================================================================
RCS file: /home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/FileCleaner.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FileCleaner.java 18 Mar 2004 06:04:14 -0000 1.1
+++ FileCleaner.java 24 Oct 2004 04:20:06 -0000 1.2
@@ -57,7 +57,7 @@
try {
// Wait for a tracker to remove.
tracker = (Tracker) q.remove();
- } catch(Exception _) {
+ } catch (Exception e) {
continue;
}
@@ -124,10 +124,10 @@
*
* @param file The file to be tracked.
* @param marker The marker object used to track the file.
- * @param q The queue on to which the tracker will be pushed.
+ * @param queue The queue on to which the tracker will be pushed.
*/
- public Tracker(File file, Object marker, ReferenceQueue q) {
- this(file.getPath(), marker, q);
+ public Tracker(File file, Object marker, ReferenceQueue queue) {
+ this(file.getPath(), marker, queue);
}
/**
@@ -135,10 +135,10 @@
*
* @param path The full path to the file to be tracked.
* @param marker The marker object used to track the file.
- * @param q The queue on to which the tracker will be pushed.
+ * @param queue The queue on to which the tracker will be pushed.
*/
- public Tracker(String path, Object marker, ReferenceQueue q) {
- super(marker, q);
+ public Tracker(String path, Object marker, ReferenceQueue queue) {
+ super(marker, queue);
this.path = path;
}
@@ -152,4 +152,4 @@
return new File(path).delete();
}
}
-}
\ No newline at end of file
+}
1.38 +119 -88 jakarta-commons/io/src/java/org/apache/commons/io/FileUtils.java
Index: FileUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/FileUtils.java,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -r1.37 -r1.38
--- FileUtils.java 13 Aug 2004 23:51:41 -0000 1.37
+++ FileUtils.java 24 Oct 2004 04:20:06 -0000 1.38
@@ -1,12 +1,12 @@
/*
* 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.
@@ -35,7 +35,7 @@
import org.apache.commons.io.filefilter.TrueFileFilter;
/**
- * This class provides basic facilities for manipulating files.
+ * This class provides basic facilities for manipulating files.
*
* <h3>File-related methods</h3>
* <p>
@@ -43,8 +43,8 @@
* {@link #copyFileToDirectory File to a directory},
* copy a {@link #copyFile File to another File},
* copy a {@link #copyURLToFile URL's contents to a File},
- * as well as methods to {@link #deleteDirectory(File) delete} and {@link #cleanDirectory(File)
- * clean} a directory.
+ * as well as methods to {@link #deleteDirectory(File) delete} and
+ * {@link #cleanDirectory(File) clean} a directory.
* </p>
*
* Common {@link java.io.File} manipulation routines.
@@ -87,7 +87,7 @@
* The number of bytes in a gigabyte.
*/
public static final long ONE_GB = ONE_KB * ONE_MB;
-
+
/**
* An empty array of type <code>File</code>.
*/
@@ -134,9 +134,17 @@
}
- private static void innerListFiles(Collection files, File directory, IOFileFilter filter) {
- File[] found = directory.listFiles((FileFilter)filter);
- if (found != null) {
+ /**
+ * <p>Finds files within a given directory (and optionally its
+ * subdirectories). All files found are filtered by an IOFileFilter.</p>
+ * @param files the collection of files found.
+ * @param directory the directory to search in.
+ * @param filter the filter to apply to files and directories.
+ */
+ private static void innerListFiles(Collection files, File directory,
+ IOFileFilter filter) {
+ File[] found = directory.listFiles((FileFilter) filter);
+ if (found != null) {
for (int i = 0; i < found.length; i++) {
if (found[i].isDirectory()) {
innerListFiles(files, found[i], filter);
@@ -156,17 +164,17 @@
* @return an array of java.io.File
*/
public static File[] convertFileCollectionToFileArray(Collection files) {
- return (File[])files.toArray(new File[files.size()]);
+ return (File[]) files.toArray(new File[files.size()]);
}
/**
- * <p>Finds files within a given directory (and optionally its
+ * <p>Finds files within a given directory (and optionally its
* subdirectories). All files found are filtered by an IOFileFilter.
* </p>
- * <p>If your search should recurse into subdirectories you can pass in
- * an IOFileFilter for directories. You don't need to bind a
- * DirectoryFileFilter (via logical AND) to this filter. This method does
+ * <p>If your search should recurse into subdirectories you can pass in
+ * an IOFileFilter for directories. You don't need to bind a
+ * DirectoryFileFilter (via logical AND) to this filter. This method does
* that for you.
* </p>
* <p>An example: If you want to search through all directories called
@@ -175,28 +183,30 @@
* <p>Another common usage of this method is find files in a directory
* tree but ignoring the directories generated CVS. You can simply pass
* in <code>FileFilterUtils.makeCVSAware(null)</code>.
- * </p>
+ * </p>
* @param directory the directory to search in
* @param fileFilter filter to apply when finding files.
- * @param dirFilter optional filter to apply when finding subdirectories.
+ * @param dirFilter optional filter to apply when finding subdirectories.
* If this parameter is null, subdirectories will not be included in the
* search. Use TrueFileFilter.INSTANCE to match all directories.
* @return an collection of java.io.File with the matching files
* @see org.apache.commons.io.filefilter.FileFilterUtils
* @see org.apache.commons.io.filefilter.NameFileFilter
*/
- public static Collection listFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
+ public static Collection listFiles(File directory, IOFileFilter fileFilter,
+ IOFileFilter dirFilter) {
if (!directory.isDirectory()) {
- throw new IllegalArgumentException("Parameter 'directory' is not a directory");
+ throw new IllegalArgumentException(
+ "Parameter 'directory' is not a directory");
}
if (fileFilter == null) {
throw new NullPointerException("Parameter 'fileFilter' is null");
}
-
+
//Setup effective file filter
- IOFileFilter effFileFilter = FileFilterUtils.andFileFilter(fileFilter,
+ IOFileFilter effFileFilter = FileFilterUtils.andFileFilter(fileFilter,
FileFilterUtils.notFileFilter(DirectoryFileFilter.INSTANCE));
-
+
//Setup effective directory filter
IOFileFilter effDirFilter;
if (dirFilter == null) {
@@ -205,14 +215,14 @@
effDirFilter = FileFilterUtils.andFileFilter(dirFilter,
DirectoryFileFilter.INSTANCE);
}
-
+
//Find files
Collection files = new java.util.LinkedList();
- innerListFiles(files, directory,
+ innerListFiles(files, directory,
FileFilterUtils.orFileFilter(effFileFilter, effDirFilter));
return files;
}
-
+
/**
* Converts an array of file extensions to suffixes for use
@@ -231,14 +241,15 @@
/**
* Finds files within a given directory (and optionally its subdirectories)
- * which match an array of extensions.
+ * which match an array of extensions.
* @param directory the directory to search in
* @param extensions an array of extensions, ex. {"java","xml"}. If this
* parameter is null, all files are returned.
* @param recursive If true all subdirectories are searched, too.
* @return an collection of java.io.File with the matching files
*/
- public static Collection listFiles(File directory, String[] extensions, boolean recursive) {
+ public static Collection listFiles(File directory, String[] extensions,
+ boolean recursive) {
IOFileFilter filter;
if (extensions == null) {
filter = TrueFileFilter.INSTANCE;
@@ -246,18 +257,20 @@
String[] suffixes = toSuffixes(extensions);
filter = new SuffixFileFilter(suffixes);
}
- return listFiles(directory, filter,
+ return listFiles(directory, filter,
(recursive ? TrueFileFilter.INSTANCE : FalseFileFilter.INSTANCE));
}
/**
- * <p>Compare the contents of two files to determine if they are equal or not.</p>
+ * <p>Compare the contents of two files to determine if they are equal or
+ * not.</p>
* <p>Code origin: Avalon</p>
*
* @param file1 the first file
* @param file2 the second file
- * @return true if the content of the files are equal or they both don't exist, false otherwise
+ * @return true if the content of the files are equal or they both don't
+ * exist, false otherwise
* @throws IOException in case of an I/O error
*/
public static boolean contentEquals(File file1, File file2)
@@ -293,13 +306,13 @@
//-----------------------------------------------------------------------
/**
* Convert from a <code>URL</code> to a <code>File</code>.
- *
+ *
* @param url the file URL to convert
* @return the equivalent <code>File</code> object, or <code>null</code>
* if the URL's protocol is not <code>file</code>
*/
public static File toFile(URL url) {
- if (url.getProtocol().equals("file") == false) {
+ if (!url.getProtocol().equals("file")) {
return null;
} else {
String filename =
@@ -313,12 +326,12 @@
* <p>
* Returns an array of the same size as the input.
* If the input is null, an empty array is returned.
- * If the input contains null, the output array contains null at the same index.
- *
+ * If the input contains null, the output array contains null at the same
+ * index.
+ *
* @param urls the file URLs to convert, null returns empty array
* @return a non-null array of Files matching the input, with a null item
* if there was a null at that index in the input array
- * @throws IllegalArgumentException if the URL could not be converted to a File
*/
public static File[] toFiles(URL[] urls) {
if (urls == null || urls.length == 0) {
@@ -329,7 +342,8 @@
URL url = urls[i];
if (url != null) {
if (url.getProtocol().equals("file") == false) {
- throw new IllegalArgumentException("URL could not be converted to a File: " + url);
+ throw new IllegalArgumentException(
+ "URL could not be converted to a File: " + url);
}
files[i] = toFile(url);
}
@@ -358,8 +372,9 @@
//-----------------------------------------------------------------------
/**
- * Copy file from source to destination. If <code>destinationDirectory</code> does not exist, it
- * (and any parent directories) will be created. If a file <code>source</code> in
+ * Copy file from source to destination. If
+ * <code>destinationDirectory</code> does not exist, it (and any parent
+ * directories) will be created. If a file <code>source</code> in
* <code>destinationDirectory</code> exists, it will be overwritten.
* The copy will have the same file date as the original.
*
@@ -367,9 +382,11 @@
* @param destinationDirectory A directory to copy <code>source</code> into.
*
* @throws FileNotFoundException if <code>source</code> isn't a normal file.
- * @throws IllegalArgumentException if <code>destinationDirectory</code> isn't a directory.
+ * @throws IllegalArgumentException if <code>destinationDirectory</code>
+ * isn't a directory.
* @throws IOException if <code>source</code> does not exist, the file in
- * <code>destinationDirectory</code> cannot be written to, or an IO error occurs during copying.
+ * <code>destinationDirectory</code> cannot be written to, or an IO error
+ * occurs during copying.
*/
public static void copyFileToDirectory(
File source,
@@ -377,25 +394,28 @@
throws IOException {
if (destinationDirectory.exists()
&& !destinationDirectory.isDirectory()) {
- throw new IllegalArgumentException("Destination is not a directory");
+ throw new IllegalArgumentException(
+ "Destination is not a directory");
}
- copyFile(source, new File(destinationDirectory, source.getName()), true);
+ copyFile(source,
+ new File(destinationDirectory, source.getName()), true);
}
/**
- * Copy file from source to destination. The directories up to
- * <code>destination</code> will be created if they don't already exist.
+ * Copy file from source to destination. The directories up to
+ * <code>destination</code> will be created if they don't already exist.
* <code>destination</code> will be overwritten if it already exists.
* The copy will have the same file date as the original.
*
- * @param source An existing non-directory <code>File</code> to copy
+ * @param source An existing non-directory <code>File</code> to copy
* bytes from.
- * @param destination A non-directory <code>File</code> to write bytes to
+ * @param destination A non-directory <code>File</code> to write bytes to
* (possibly overwriting).
*
- * @throws IOException if <code>source</code> does not exist, <code>destination</code> cannot be
- * written to, or an IO error occurs during copying.
+ * @throws IOException if <code>source</code> does not exist,
+ * <code>destination</code> cannot be written to, or an IO error occurs
+ * during copying.
*
* @throws FileNotFoundException if <code>destination</code> is a directory
* (use {@link #copyFileToDirectory}).
@@ -404,28 +424,30 @@
throws IOException {
copyFile(source, destination, true);
}
-
-
+
+
/**
- * Copy file from source to destination. The directories up to
- * <code>destination</code> will be created if they don't already exist.
+ * Copy file from source to destination. The directories up to
+ * <code>destination</code> will be created if they don't already exist.
* <code>destination</code> will be overwritten if it already exists.
*
- * @param source An existing non-directory <code>File</code> to copy
+ * @param source An existing non-directory <code>File</code> to copy
* bytes from.
- * @param destination A non-directory <code>File</code> to write bytes to
+ * @param destination A non-directory <code>File</code> to write bytes to
* (possibly overwriting).
* @param preserveFileDate True if the file date of the copy should be the
* same as the original.
*
- * @throws IOException if <code>source</code> does not exist, <code>destination</code> cannot be
- * written to, or an IO error occurs during copying.
+ * @throws IOException if <code>source</code> does not exist,
+ * <code>destination</code> cannot be written to, or an IO error occurs
+ * during copying.
*
* @throws FileNotFoundException if <code>destination</code> is a directory
* (use {@link #copyFileToDirectory}).
*/
- public static void copyFile(File source, File destination, boolean preserveFileDate)
- throws IOException {
+ public static void copyFile(File source, File destination,
+ boolean preserveFileDate)
+ throws IOException {
//check source exists
if (!source.exists()) {
String message = "File " + source + " does not exist";
@@ -445,7 +467,7 @@
throw new IOException(message);
}
- //makes sure it is not the same file
+ //makes sure it is not the same file
if (source.getCanonicalPath().equals(destination.getCanonicalPath())) {
String message =
"Unable to write file " + source + " on itself.";
@@ -472,21 +494,22 @@
+ destination;
throw new IOException(message);
}
-
+
if (preserveFileDate) {
//file copy should preserve file date
- destination.setLastModified(source.lastModified());
+ destination.setLastModified(source.lastModified());
}
}
/**
- * Copies bytes from the URL <code>source</code> to a file <code>destination</code>.
- * The directories up to <code>destination</code> will be created if they don't already exist.
- * <code>destination</code> will be overwritten if it already exists.
+ * Copies bytes from the URL <code>source</code> to a file
+ * <code>destination</code>. The directories up to <code>destination</code>
+ * will be created if they don't already exist. <code>destination</code>
+ * will be overwritten if it already exists.
*
* @param source A <code>URL</code> to copy bytes from.
- * @param destination A non-directory <code>File</code> to write bytes to (possibly
- * overwriting).
+ * @param destination A non-directory <code>File</code> to write bytes to
+ * (possibly overwriting).
*
* @throws IOException if
* <ul>
@@ -598,7 +621,9 @@
}
try {
Thread.sleep(100);
- } catch (InterruptedException ignore) {} catch (Exception ex) {
+ } catch (InterruptedException ignore) {
+ ;
+ } catch (Exception ex) {
break;
}
}
@@ -667,7 +692,7 @@
* @throws UnsupportedEncodingException if the encoding is not supported
* by the VM
*/
- public static void writeStringToFile(File file,
+ public static void writeStringToFile(File file,
String data, String encoding) throws IOException {
OutputStream out = new FileOutputStream(file);
try {
@@ -705,7 +730,7 @@
* </p>
* <ul>
* <li>A directory to be deleted does not have to be empty.</li>
- * <li>You get exceptions when a file or directory cannot be deleted.
+ * <li>You get exceptions when a file or directory cannot be deleted.
* (java.io.File methods returns a boolean)</li>
* </ul>
* @param file file or directory to delete.
@@ -807,7 +832,7 @@
throw new IOException(message);
}
} else {
- if (false == directory.mkdirs()) {
+ if (!directory.mkdirs()) {
String message =
"Unable to create directory " + directory;
throw new IOException(message);
@@ -847,33 +872,37 @@
return size;
}
-
+
/**
- * Tests if the specified <code>File</code> is newer than the reference
+ * Tests if the specified <code>File</code> is newer than the reference
* <code>File</code>.
*
- * @param file the <code>File</code> of which the modification date must be compared
- * @param reference the <code>File</code> of which the modification date is used
+ * @param file the <code>File</code> of which the modification date must
+ * be compared.
+ * @param reference the <code>File</code> of which the modification date
+ * is used.
* like reference
- * @return true if the <code>File</code> exists and has been modified more recently
- * than the reference <code>File</code>.
+ * @return true if the <code>File</code> exists and has been modified more
+ * recently than the reference <code>File</code>.
*/
public static boolean isFileNewer(File file, File reference) {
if (reference == null) {
throw new IllegalArgumentException("No specified reference file");
}
if (!reference.exists()) {
- throw new IllegalArgumentException("The reference file '" + file + "' doesn't exist");
+ throw new IllegalArgumentException(
+ "The reference file '" + file + "' doesn't exist");
}
-
+
return isFileNewer(file, reference.lastModified());
}
-
+
/**
- * Tests if the specified <code>File</code> is newer than the specified
- * <code>Date</code>
+ * Tests if the specified <code>File</code> is newer than the specified
+ * <code>Date</code>.
*
- * @param file the <code>File</code> of which the modification date must be compared
+ * @param file the <code>File</code> of which the modification date must be
+ * compared.
* @param date the date reference
* @return true if the <code>File</code> exists and has been modified after
* the given <code>Date</code>.
@@ -884,13 +913,15 @@
}
return isFileNewer(file, date.getTime());
}
-
+
/**
- * Tests if the specified <code>File</code> is newer than the specified
+ * Tests if the specified <code>File</code> is newer than the specified
* time reference.
*
- * @param file the <code>File</code> of which the modification date must be compared.
- * @param timeMillis the time reference measured in milliseconds since the epoch
+ * @param file the <code>File</code> of which the modification date must
+ * be compared.
+ * @param timeMillis the time reference measured in milliseconds since the
+ * epoch
* (00:00:00 GMT, January 1, 1970)
* @return true if the <code>File</code> exists and has been modified after
* the given time reference.
@@ -902,7 +933,7 @@
if (!file.exists()) {
return false;
}
-
+
return file.lastModified() > timeMillis;
}
1.12 +58 -42 jakarta-commons/io/src/java/org/apache/commons/io/FilenameUtils.java
Index: FilenameUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/FilenameUtils.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- FilenameUtils.java 13 Jun 2004 05:17:11 -0000 1.11
+++ FilenameUtils.java 24 Oct 2004 04:20:07 -0000 1.12
@@ -1,12 +1,12 @@
/*
* 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.
@@ -16,25 +16,27 @@
package org.apache.commons.io;
import java.io.File;
-import java.io.FileNotFoundException;
import java.io.IOException;
/**
- * Common {@link java.io.File} manipulation routines through
+ * Common {@link java.io.File} manipulation routines through
* use of a filename/path.
*
* <h3>Path-related methods</h3>
*
- * <p>Methods exist to retrieve the components of a typical file path. For example
- * <code>/www/hosted/mysite/index.html</code>, can be broken into:
+ * <p>Methods exist to retrieve the components of a typical file path. For
+ * example <code>/www/hosted/mysite/index.html</code>, can be broken into:
* <ul>
- * <li><code>/www/hosted/mysite/</code> -- retrievable through {@link #getPath}</li>
+ * <li><code>/www/hosted/mysite/</code> -- retrievable through
+ * {@link #getPath}</li>
* <li><code>index.html</code> -- retrievable through {@link #removePath}</li>
- * <li><code>/www/hosted/mysite/index</code> -- retrievable through {@link #removeExtension}</li>
+ * <li><code>/www/hosted/mysite/index</code> -- retrievable through
+ * {@link #removeExtension}</li>
* <li><code>html</code> -- retrievable through {@link #getExtension}</li>
* </ul>
- * There are also methods to {@link #catPath concatenate two paths}, {@link #resolveFile resolve a
- * path relative to a File} and {@link #normalize} a path.
+ * There are also methods to {@link #catPath concatenate two paths},
+ * {@link #resolveFile resolve a path relative to a File} and
+ * {@link #normalize} a path.
* </p>
*
* <h3>Origin of code</h3>
@@ -109,10 +111,10 @@
* @param filename the filename
* @return the filename minus extension
*/
- public static String removeExtension( final String filename) {
+ public static String removeExtension(final String filename) {
String ext = getExtension(filename);
int index = ext.length();
- if(index > 0) {
+ if (index > 0) {
// include the . in the count
index++;
}
@@ -140,7 +142,7 @@
String shortFilename = filename;
int lastDirSeparator = filename.lastIndexOf(File.separatorChar);
- if(lastDirSeparator > 0){
+ if (lastDirSeparator > 0) {
shortFilename = filename.substring(lastDirSeparator + 1);
}
@@ -154,7 +156,8 @@
}
/**
- * Remove path from filename. Equivalent to the unix command <code>basename</code>
+ * Remove path from filename. Equivalent to the unix command
+ * <code>basename</code>.
* ie.
* <pre>
* a/b/c.txt --> c.txt
@@ -164,7 +167,7 @@
* @param filepath the filepath
* @return the filename minus path
*/
- public static String removePath( String filepath) {
+ public static String removePath(String filepath) {
return removePath(filepath, File.separatorChar);
}
@@ -193,7 +196,8 @@
}
/**
- * Get path from filename. Roughly equivalent to the unix command <code>dirname</code>.
+ * Get path from filename. Roughly equivalent to the unix command
+ * <code>dirname</code>.
* ie.
* <pre>
* a/b/c.txt --> a/b
@@ -203,7 +207,7 @@
* @param filepath the filepath
* @return the filename minus path
*/
- public static String getPath( String filepath) {
+ public static String getPath(String filepath) {
return getPath(filepath, File.separatorChar);
}
@@ -234,8 +238,8 @@
/**
* Normalize a path.
- * Eliminates "/../" and "/./" in a string. Returns <code>null</code> if the ..'s went past the
- * root.
+ * Eliminates "/../" and "/./" in a string. Returns <code>null</code> if
+ * the ..'s went past the root.
* Eg:
* <pre>
* /foo// --> /foo/
@@ -250,13 +254,14 @@
* @param path the path to normalize
* @return the normalized String, or <code>null</code> if too many ..'s.
*/
- public static String normalize( String path) {
+ public static String normalize(String path) {
String normalized = path;
// Resolve occurrences of "//" in the normalized path
while (true) {
int index = normalized.indexOf("//");
- if (index < 0)
+ if (index < 0) {
break;
+ }
normalized =
normalized.substring(0, index)
+ normalized.substring(index + 1);
@@ -265,8 +270,9 @@
// Resolve occurrences of "/./" in the normalized path
while (true) {
int index = normalized.indexOf("/./");
- if (index < 0)
+ if (index < 0) {
break;
+ }
normalized =
normalized.substring(0, index)
+ normalized.substring(index + 2);
@@ -275,10 +281,12 @@
// Resolve occurrences of "/../" in the normalized path
while (true) {
int index = normalized.indexOf("/../");
- if (index < 0)
+ if (index < 0) {
break;
- if (index == 0)
+ }
+ if (index == 0) {
return null; // Trying to go outside our context
+ }
int index2 = normalized.lastIndexOf('/', index - 1);
normalized =
normalized.substring(0, index2)
@@ -291,7 +299,7 @@
/**
* Will concatenate 2 paths. Paths with <code>..</code> will be
- * properly handled. The path separator between the 2 paths is the
+ * properly handled. The path separator between the 2 paths is the
* system default path separator.
*
* <p>Eg. on UNIX,<br />
@@ -311,7 +319,7 @@
* @param path path the second path to attach to the first
* @return The concatenated paths, or null if error occurs
*/
- public static String catPath( String lookupPath, String path) {
+ public static String catPath(String lookupPath, String path) {
// Cut off the last slash and everything beyond
int index = indexOfLastPathSeparator(lookupPath);
String lookup = lookupPath.substring(0, index);
@@ -330,13 +338,15 @@
pth = pth.substring(3);
}
- return new StringBuffer(lookup).append(File.separator).append(pth).toString();
+ return new StringBuffer(lookup).
+ append(File.separator).append(pth).toString();
}
-
+
/**
- * Return the index of the last 'path separator' character. The 'path separator'
- * character is '/' for UNIX systems and '\' for Microsoft Windows systems.
- *
+ * Return the index of the last 'path separator' character. The 'path
+ * separator' character is '/' for UNIX systems and '\' for Microsoft
+ * Windows systems.
+ *
* @param path The path to find the last path separator in
* @return The index of the last 'path separator' character, or -1 if there
* is no such character.
@@ -348,16 +358,17 @@
}
/**
- * Resolve a file <code>filename</code> to it's canonical form. If <code>filename</code> is
- * relative (doesn't start with <code>/</code>), it will be resolved relative to
- * <code>baseFile</code>, otherwise it is treated as a normal root-relative path.
+ * Resolve a file <code>filename</code> to it's canonical form. If
+ * <code>filename</code> is relative (doesn't start with <code>/</code>),
+ * it will be resolved relative to <code>baseFile</code>, otherwise it is
+ * treated as a normal root-relative path.
*
- * @param baseFile Where to resolve <code>filename</code> from, if <code>filename</code> is
- * relative.
+ * @param baseFile Where to resolve <code>filename</code> from, if
+ * <code>filename</code> is relative.
* @param filename Absolute or relative file path to resolve.
* @return The canonical <code>File</code> of <code>filename</code>.
*/
- public static File resolveFile( File baseFile, String filename) {
+ public static File resolveFile(File baseFile, String filename) {
String filenm = filename;
if ('/' != File.separatorChar) {
filenm = filename.replace('/', File.separatorChar);
@@ -373,11 +384,14 @@
try {
file = file.getCanonicalFile();
- } catch ( IOException ioe) {}
+ } catch (IOException ioe) {
+ // ignore
+ }
return file;
}
- // FIXME: I'm almost certain this // removal is unnecessary, as getAbsoluteFile() strips
+ // FIXME: I'm almost certain this // removal is unnecessary, as
+ // getAbsoluteFile() strips
// them. However, I'm not sure about this UNC stuff. (JT)
char[] chars = filename.toCharArray();
StringBuffer sb = new StringBuffer();
@@ -408,7 +422,9 @@
try {
file = file.getCanonicalFile();
- } catch ( IOException ioe) {}
+ } catch ( IOException ioe) {
+ ;
+ }
return file;
}
1.9 +9 -9 jakarta-commons/io/src/java/org/apache/commons/io/HexDump.java
Index: HexDump.java
===================================================================
RCS file: /home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/HexDump.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- HexDump.java 23 Feb 2004 04:35:59 -0000 1.8
+++ HexDump.java 24 Oct 2004 04:20:07 -0000 1.9
@@ -1,12 +1,12 @@
/*
* Copyright 2002,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.
@@ -37,7 +37,7 @@
public HexDump() { }
/**
- * dump an array of bytes to an OutputStream
+ * Dump an array of bytes to an OutputStream.
*
* @param data the byte array to be dumped
* @param offset its offset, whatever that might mean
@@ -103,12 +103,12 @@
System.getProperty("line.separator");
private static final StringBuffer _lbuffer = new StringBuffer(8);
private static final StringBuffer _cbuffer = new StringBuffer(2);
- private static final char _hexcodes[] =
+ private static final char[] _hexcodes =
{
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
- 'E', 'F'
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ 'A', 'B', 'C', 'D', 'E', 'F'
};
- private static final int _shifts[] =
+ private static final int[] _shifts =
{
28, 24, 20, 16, 12, 8, 4, 0
};
1.22 +130 -86 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.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- IOUtils.java 24 Oct 2004 02:09:40 -0000 1.21
+++ IOUtils.java 24 Oct 2004 04:20:07 -0000 1.22
@@ -42,19 +42,21 @@
* </ul>
* <p>
* 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.
+ * 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>
* 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.
+ * 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.
+ * 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)
*
@@ -66,8 +68,8 @@
* @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 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
/**
@@ -208,7 +210,8 @@
* @throws IOException if an I/O error occurs
* @since 1.1
*/
- public static byte[] toByteArray(Reader input, String encoding) throws IOException {
+ public static byte[] toByteArray(Reader input, String encoding)
+ throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
copy(input, output, encoding);
return output.toByteArray();
@@ -266,7 +269,8 @@
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs
*/
- public static char[] toCharArray(InputStream is, String encoding) throws IOException {
+ public static char[] toCharArray(InputStream is, String encoding)
+ throws IOException {
CharArrayWriter output = new CharArrayWriter();
copy(is, output, encoding);
return output.toCharArray();
@@ -325,7 +329,8 @@
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs
*/
- public static String toString(InputStream input, String encoding) throws IOException {
+ public static String toString(InputStream input, String encoding)
+ throws IOException {
StringWriter sw = new StringWriter();
copy(input, sw, encoding);
return sw.toString();
@@ -376,7 +381,8 @@
* @throws IOException if an I/O error occurs (never occurs)
* @deprecated Use {@link String#String(byte[],String)}
*/
- public static String toString(byte[] input, String encoding) throws IOException {
+ public static String toString(byte[] input, String encoding)
+ throws IOException {
if (encoding == null) {
return new String(input);
} else {
@@ -389,13 +395,15 @@
/**
* 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 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 {
+ public static void write(byte[] data, OutputStream output)
+ throws IOException {
if (data != null) {
output.write(data);
}
@@ -407,7 +415,8 @@
* <p>
* This method uses {@link String#String(byte[])}.
*
- * @param data the byte array to write, do not modify during output, null ignored
+ * @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
@@ -428,14 +437,16 @@
* <p>
* This method uses {@link String#String(byte[], String)}.
*
- * @param data the byte array to write, do not modify during output, null ignored
+ * @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 {
+ public static void write(byte[] data, Writer output, String encoding)
+ throws IOException {
if (data != null) {
if (encoding == null) {
write(data, output);
@@ -451,7 +462,8 @@
* 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 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
@@ -464,39 +476,46 @@
}
/**
- * Writes chars from a <code>char[]</code> to bytes on an <code>OutputStream</code>.
+ * 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()}.
+ * 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 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 {
+ 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.
+ * 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)}
+ * 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 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 {
+ public static void write(char[] data, OutputStream output, String encoding)
+ throws IOException {
if (data != null) {
if (encoding == null) {
write(data, output);
@@ -524,8 +543,9 @@
}
/**
- * Writes chars from a <code>String</code> to bytes on an <code>OutputStream</code>
- * using the default character encoding of the platform.
+ * 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()}.
*
@@ -535,15 +555,16 @@
* @throws IOException if an I/O error occurs
* @since 1.1
*/
- public static void write(String data, OutputStream output) throws IOException {
+ 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.
+ * 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>.
@@ -557,7 +578,8 @@
* @throws IOException if an I/O error occurs
* @since 1.1
*/
- public static void write(String data, OutputStream output, String encoding) throws IOException {
+ public static void write(String data, OutputStream output, String encoding)
+ throws IOException {
if (data != null) {
if (encoding == null) {
write(data, output);
@@ -578,15 +600,17 @@
* @throws IOException if an I/O error occurs
* @since 1.1
*/
- public static void write(StringBuffer data, Writer output) throws IOException {
+ 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.
+ * 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 String#getBytes()}.
*
@@ -596,15 +620,16 @@
* @throws IOException if an I/O error occurs
* @since 1.1
*/
- public static void write(StringBuffer data, OutputStream output) throws IOException {
+ 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.
+ * 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>.
@@ -618,7 +643,8 @@
* @throws IOException if an I/O error occurs
* @since 1.1
*/
- public static void write(StringBuffer data, OutputStream output, String encoding) throws IOException {
+ public static void write(StringBuffer data, OutputStream output,
+ String encoding) throws IOException {
if (data != null) {
if (encoding == null) {
write(data, output);
@@ -631,7 +657,8 @@
// copy from InputStream
//-----------------------------------------------------------------------
/**
- * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
+ * 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>.
@@ -643,7 +670,8 @@
* @throws IOException if an I/O error occurs
* @since 1.1
*/
- public static int copy(InputStream input, OutputStream output) throws IOException {
+ public static int copy(InputStream input, OutputStream output)
+ throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int count = 0;
int n = 0;
@@ -655,28 +683,29 @@
}
/**
- * Copy bytes from an <code>InputStream</code> to chars on a <code>Writer</code>
- * using the default character encoding of the platform.
+ * 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 {
+ 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.
+ * 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>.
@@ -685,7 +714,7 @@
* <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
@@ -693,7 +722,8 @@
* @throws IOException if an I/O error occurs
* @since 1.1
*/
- public static void copy(InputStream input, Writer output, String encoding) throws IOException {
+ public static void copy(InputStream input, Writer output, String encoding)
+ throws IOException {
if (encoding == null) {
copy(input, output);
} else {
@@ -709,7 +739,7 @@
* <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
@@ -729,32 +759,37 @@
}
/**
- * 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.
+ * 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.
+ * 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 {
+ 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.
+ // 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.
+ * 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>.
@@ -762,10 +797,11 @@
* 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.
+ * 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
@@ -773,13 +809,15 @@
* @throws IOException if an I/O error occurs
* @since 1.1
*/
- public static void copy(Reader input, OutputStream output, String encoding) throws IOException {
+ 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.
+ // XXX Unless anyone is planning on rewriting OutputStreamWriter,
+ // we have to flush here.
out.flush();
}
}
@@ -787,25 +825,28 @@
// content equals
//-----------------------------------------------------------------------
/**
- * Compare the contents of two Streams to determine if they are equal or not.
+ * 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>
- * if they are not already buffered.
+ * 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
- * @return true if the content of the streams are equal or they both don't exist, false otherwise
+ * @return true if the content of the streams 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
*/
- public static boolean contentEquals(InputStream input1, InputStream input2) throws IOException {
- if (input1 instanceof BufferedInputStream == false) {
+ public static boolean contentEquals(InputStream input1, InputStream input2)
+ throws IOException {
+ if (!(input1 instanceof BufferedInputStream)) {
input1 = new BufferedInputStream(input1);
}
- if (input2 instanceof BufferedInputStream == false) {
+ if (!(input2 instanceof BufferedInputStream)) {
input2 = new BufferedInputStream(input2);
}
-
+
int ch = input1.read();
while (-1 != ch) {
int ch2 = input2.read();
@@ -814,32 +855,35 @@
}
ch = input1.read();
}
-
+
int ch2 = input2.read();
return (ch2 == -1);
}
/**
- * Compare the contents of two Readers to determine if they are equal or not.
+ * 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.
+ * 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
+ * @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) {
+ public static boolean contentEquals(Reader input1, Reader input2)
+ throws IOException {
+ if (!(input1 instanceof BufferedReader)) {
input1 = new BufferedReader(input1);
}
- if (input2 instanceof BufferedReader == false) {
+ if (!(input2 instanceof BufferedReader)) {
input2 = new BufferedReader(input2);
}
-
+
int ch = input1.read();
while (-1 != ch) {
int ch2 = input2.read();
@@ -848,7 +892,7 @@
}
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