Skip to content

Commit f00d97a

Browse files
committed
2 parents c295a58 + bfd83b0 commit f00d97a

15 files changed

Lines changed: 244 additions & 25 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jdk:
2222
- oraclejdk8
2323

2424
script:
25-
- mvn test apache-rat:check javadoc:javadoc
25+
- mvn test apache-rat:check clirr:check checkstyle:check javadoc:javadoc
2626

2727
after_success:
2828
- mvn clean cobertura:cobertura coveralls:report

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ Apache Commons IO
4545

4646
[![Build Status](https://travis-ci.org/apache/commons-io.svg?branch=master)](https://travis-ci.org/apache/commons-io)
4747
[![Coverage Status](https://coveralls.io/repos/github/apache/commons-io/badge.svg?branch=master)](https://coveralls.io/github/apache/commons-io?branch=master)
48-
[![License](http://img.shields.io/:license-apache-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)
4948

5049
The Apache Commons IO library contains utility classes, stream implementations, file filters,
5150
file comparators, endian transformation classes, and much more.

checkstyle.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ limitations under the License.
3636
<module name="RedundantImport"/>
3737
<module name="UnusedImports"/>
3838
<module name="NeedBraces"/>
39-
<module name="RedundantThrows">
40-
<property name="allowUnchecked" value="true"/>
41-
<property name="allowSubclasses" value="true"/>
42-
</module>
4339
<module name="LineLength">
4440
<property name="max" value="160"/>
4541
</module>

pom.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<parent>
2020
<groupId>org.apache.commons</groupId>
2121
<artifactId>commons-parent</artifactId>
22-
<version>41</version>
22+
<version>42</version>
2323
</parent>
2424
<modelVersion>4.0.0</modelVersion>
2525
<groupId>commons-io</groupId>
@@ -259,6 +259,9 @@ file comparators, endian transformation classes, and much more.
259259
</commons.osgi.export>
260260

261261
<commons.scmPubCheckoutDirectory>site-content</commons.scmPubCheckoutDirectory>
262+
<!-- Override clirr version to be able to build the site on Java 8 -->
263+
<commons.clirr.version>2.8</commons.clirr.version>
264+
<checkstyle.plugin.version>2.17</checkstyle.plugin.version>
262265
</properties>
263266

264267
<build>
@@ -310,6 +313,15 @@ file comparators, endian transformation classes, and much more.
310313
<tarLongFileMode>gnu</tarLongFileMode>
311314
</configuration>
312315
</plugin>
316+
<plugin>
317+
<groupId>org.apache.maven.plugins</groupId>
318+
<artifactId>maven-checkstyle-plugin</artifactId>
319+
<version>${checkstyle.plugin.version}</version>
320+
<configuration>
321+
<configLocation>${basedir}/checkstyle.xml</configLocation>
322+
<enableRulesSummary>false</enableRulesSummary>
323+
</configuration>
324+
</plugin>
313325
<plugin>
314326
<groupId>org.apache.maven.plugins</groupId>
315327
<artifactId>maven-scm-publish-plugin</artifactId>
@@ -332,7 +344,7 @@ file comparators, endian transformation classes, and much more.
332344
<plugin>
333345
<groupId>org.apache.maven.plugins</groupId>
334346
<artifactId>maven-checkstyle-plugin</artifactId>
335-
<version>2.12.1</version>
347+
<version>${checkstyle.plugin.version}</version>
336348
<configuration>
337349
<configLocation>${basedir}/checkstyle.xml</configLocation>
338350
<enableRulesSummary>false</enableRulesSummary>

src/changes/changes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.6" date="2017-MM-DD" description="New features and bug fixes.">
50+
<action issue="IO-367" dev="pschumacher" type="add" due-to="James Sawle">
51+
Add convenience methods for copyToDirectory
52+
</action>
53+
<action issue="IO-442" dev="pschumacher" type="fix" due-to="Simon Robinson">
54+
Javadoc contradictory for FileFilterUtils.ageFileFilter(cutoff) and the filter it constructs: AgeFileFilter(cutoff)
55+
</action>
5056
<action issue="IO-534" dev="sebb" type="fix">
5157
FileUtilTestCase.testForceDeleteDir() should not delete testDirectory parent
5258
</action>

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,75 @@ public static void copyToFile(final InputStream source, final File destination)
15311531
}
15321532
}
15331533

1534+
/**
1535+
* Copies a file or directory to within another directory preserving the file dates.
1536+
* <p>
1537+
* This method copies the source file or directory, along all its contents, to a
1538+
* directory of the same name in the specified destination directory.
1539+
* <p>
1540+
* The destination directory is created if it does not exist.
1541+
* If the destination directory did exist, then this method merges
1542+
* the source with the destination, with the source taking precedence.
1543+
* <p>
1544+
* <strong>Note:</strong> This method tries to preserve the files' last
1545+
* modified date/times using {@link File#setLastModified(long)}, however
1546+
* it is not guaranteed that those operations will succeed.
1547+
* If the modification operation fails, no indication is provided.
1548+
*
1549+
* @param src an existing file or directory to copy, must not be {@code null}
1550+
* @param destDir the directory to place the copy in, must not be {@code null}
1551+
*
1552+
* @throws NullPointerException if source or destination is {@code null}
1553+
* @throws IOException if source or destination is invalid
1554+
* @throws IOException if an IO error occurs during copying
1555+
* @see #copyDirectoryToDirectory(File, File)
1556+
* @see #copyFileToDirectory(File, File)
1557+
* @since 2.6
1558+
*/
1559+
public static void copyToDirectory(final File src, final File destDir) throws IOException {
1560+
if (src == null) {
1561+
throw new NullPointerException("Source must not be null");
1562+
}
1563+
if (src.isFile()) {
1564+
copyFileToDirectory(src, destDir);
1565+
} else if (src.isDirectory()) {
1566+
copyDirectoryToDirectory(src, destDir);
1567+
} else {
1568+
throw new IOException("The source " + src + " does not exist");
1569+
}
1570+
}
1571+
1572+
/**
1573+
* Copies a files to a directory preserving each file's date.
1574+
* <p>
1575+
* This method copies the contents of the specified source files
1576+
* to a file of the same name in the specified destination directory.
1577+
* The destination directory is created if it does not exist.
1578+
* If the destination file exists, then this method will overwrite it.
1579+
* <p>
1580+
* <strong>Note:</strong> This method tries to preserve the file's last
1581+
* modified date/times using {@link File#setLastModified(long)}, however
1582+
* it is not guaranteed that the operation will succeed.
1583+
* If the modification operation fails, no indication is provided.
1584+
*
1585+
* @param srcs a existing files to copy, must not be {@code null}
1586+
* @param destDir the directory to place the copy in, must not be {@code null}
1587+
*
1588+
* @throws NullPointerException if source or destination is null
1589+
* @throws IOException if source or destination is invalid
1590+
* @throws IOException if an IO error occurs during copying
1591+
* @see #copyFileToDirectory(File, File)
1592+
* @since 2.6
1593+
*/
1594+
public static void copyToDirectory(final Iterable<File> srcs, final File destDir) throws IOException {
1595+
if (srcs == null) {
1596+
throw new NullPointerException("Sources must not be null");
1597+
}
1598+
for (File src : srcs) {
1599+
copyFileToDirectory(src, destDir);
1600+
}
1601+
}
1602+
15341603
//-----------------------------------------------------------------------
15351604
/**
15361605
* Deletes a directory recursively.

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -818,14 +818,14 @@ public static byte[] toByteArray(final InputStream input, final int size) throws
818818

819819
final byte[] data = new byte[size];
820820
int offset = 0;
821-
int readed;
821+
int read;
822822

823-
while (offset < size && (readed = input.read(data, offset, size - offset)) != EOF) {
824-
offset += readed;
823+
while (offset < size && (read = input.read(data, offset, size - offset)) != EOF) {
824+
offset += read;
825825
}
826826

827827
if (offset != size) {
828-
throw new IOException("Unexpected readed size. current: " + offset + ", excepted: " + size);
828+
throw new IOException("Unexpected read size. current: " + offset + ", expected: " + size);
829829
}
830830

831831
return data;

src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,8 @@ public static IOFileFilter asFileFilter(final FilenameFilter filter) {
518518

519519
//-----------------------------------------------------------------------
520520
/**
521-
* Returns a filter that returns true if the file was last modified after
522-
* the specified cutoff time.
521+
* Returns a filter that returns true if the file was last modified before
522+
* or at the specified cutoff time.
523523
*
524524
* @param cutoff the time threshold
525525
* @return an appropriately configured age file filter
@@ -544,8 +544,8 @@ public static IOFileFilter ageFileFilter(final long cutoff, final boolean accept
544544
}
545545

546546
/**
547-
* Returns a filter that returns true if the file was last modified after
548-
* the specified cutoff date.
547+
* Returns a filter that returns true if the file was last modified before
548+
* or at the specified cutoff date.
549549
*
550550
* @param cutoffDate the time threshold
551551
* @return an appropriately configured age file filter
@@ -570,8 +570,8 @@ public static IOFileFilter ageFileFilter(final Date cutoffDate, final boolean ac
570570
}
571571

572572
/**
573-
* Returns a filter that returns true if the file was last modified after
574-
* the specified reference file.
573+
* Returns a filter that returns true if the file was last modified before
574+
* or at the same time as the specified reference file.
575575
*
576576
* @param cutoffReference the file whose last modification
577577
* time is used as the threshold age of the files

src/main/java/org/apache/commons/io/input/MessageDigestCalculatingInputStream.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,16 @@
3131
*/
3232
public class MessageDigestCalculatingInputStream extends ObservableInputStream {
3333

34+
/**
35+
* Maintains the message digest.
36+
*/
3437
public static class MessageDigestMaintainingObserver extends Observer {
3538
private final MessageDigest md;
3639

40+
/**
41+
* Creates an MessageDigestMaintainingObserver for the given MessageDigest.
42+
* @param pMd the message digest to use
43+
*/
3744
public MessageDigestMaintainingObserver(MessageDigest pMd) {
3845
md = pMd;
3946
}
@@ -53,6 +60,8 @@ void data(byte[] pBuffer, int pOffset, int pLength) throws IOException {
5360

5461
/** Creates a new instance, which calculates a signature on the given stream,
5562
* using the given {@link MessageDigest}.
63+
* @param pStream the stream to calculate the message digest for
64+
* @param pDigest the message digest to use
5665
*/
5766
public MessageDigestCalculatingInputStream(InputStream pStream, MessageDigest pDigest) {
5867
super(pStream);
@@ -62,13 +71,18 @@ public MessageDigestCalculatingInputStream(InputStream pStream, MessageDigest pD
6271

6372
/** Creates a new instance, which calculates a signature on the given stream,
6473
* using a {@link MessageDigest} with the given algorithm.
74+
* @param pStream the stream to calculate the message digest for
75+
* @param pAlgorithm the name of the algorithm to use
76+
* @throws NoSuchAlgorithmException if no Provider supports a MessageDigestSpi implementation for the specified algorithm.
6577
*/
6678
public MessageDigestCalculatingInputStream(InputStream pStream, String pAlgorithm) throws NoSuchAlgorithmException {
6779
this(pStream, MessageDigest.getInstance(pAlgorithm));
6880
}
6981

7082
/** Creates a new instance, which calculates a signature on the given stream,
7183
* using a {@link MessageDigest} with the "MD5" algorithm.
84+
* @param pStream the stream to calculate the message digest for
85+
* @throws NoSuchAlgorithmException if no Provider supports a MessageDigestSpi implementation for the specified algorithm.
7286
*/
7387
public MessageDigestCalculatingInputStream(InputStream pStream) throws NoSuchAlgorithmException {
7488
this(pStream, MessageDigest.getInstance("MD5"));
@@ -80,6 +94,7 @@ public MessageDigestCalculatingInputStream(InputStream pStream) throws NoSuchAlg
8094
* This is probably not, what you expect. Make sure, that the complete data has been
8195
* read, if that is what you want. The easiest way to do so is by invoking
8296
* {@link #consume()}.
97+
* @return the message digest used
8398
*/
8499
public MessageDigest getMessageDigest() {
85100
return messageDigest;

src/main/java/org/apache/commons/io/input/ObservableInputStream.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.IOException;
2020
import java.io.InputStream;
21-
import java.security.MessageDigest;
2221
import java.util.ArrayList;
2322
import java.util.List;
2423

@@ -36,51 +35,77 @@
3635
* @see MessageDigestCalculatingInputStream
3736
*/
3837
public class ObservableInputStream extends ProxyInputStream {
38+
3939
public static abstract class Observer {
40+
4041
/** Called to indicate, that {@link InputStream#read()} has been invoked
4142
* on the {@link ObservableInputStream}, and will return a value.
4243
* @param pByte The value, which is being returned. This will never be -1 (EOF),
4344
* because, in that case, {@link #finished()} will be invoked instead.
45+
* @throws IOException if an i/o-error occurs
4446
*/
4547
void data(int pByte) throws IOException {}
48+
4649
/** Called to indicate, that {@link InputStream#read(byte[])}, or
4750
* {@link InputStream#read(byte[], int, int)} have been called, and are about to
4851
* invoke data.
4952
* @param pBuffer The byte array, which has been passed to the read call, and where
5053
* data has been stored.
5154
* @param pOffset The offset within the byte array, where data has been stored.
5255
* @param pLength The number of bytes, which have been stored in the byte array.
56+
* @throws IOException if an i/o-error occurs
5357
*/
5458
void data(byte[] pBuffer, int pOffset, int pLength) throws IOException {}
59+
5560
/** Called to indicate, that EOF has been seen on the underlying stream.
5661
* This method may be called multiple times, if the reader keeps invoking
5762
* either of the read methods, and they will consequently keep returning
5863
* EOF.
64+
* @throws IOException if an i/o-error occurs
5965
*/
6066
void finished() throws IOException {}
67+
6168
/** Called to indicate, that the {@link ObservableInputStream} has been closed.
69+
* @throws IOException if an i/o-error occurs
6270
*/
6371
void closed() throws IOException {}
72+
6473
/**
6574
* Called to indicate, that an error occurred on the underlying stream.
75+
* @throws IOException if an i/o-error occurs
6676
*/
6777
void error(IOException pException) throws IOException { throw pException; }
6878
}
6979

7080
private final List<Observer> observers = new ArrayList<>();
71-
81+
82+
/**
83+
* Creates a new ObservableInputStream for the given InputStream.
84+
* @param pProxy the input stream to proxy
85+
*/
7286
public ObservableInputStream(InputStream pProxy) {
7387
super(pProxy);
7488
}
7589

90+
/**
91+
* Adds an Observer.
92+
* @param pObserver the observer to add
93+
*/
7694
public void add(Observer pObserver) {
7795
observers.add(pObserver);
7896
}
7997

98+
/**
99+
* Removes an Observer.
100+
* @param pObserver the observer to remove
101+
*/
80102
public void remove(Observer pObserver) {
81103
observers.remove(pObserver);
82104
}
83105

106+
/**
107+
* Removes all Observers.
108+
*/
84109
public void removeAllObservers() {
85110
observers.clear();
86111
}
@@ -201,6 +226,9 @@ protected void noteClosed() throws IOException {
201226
}
202227
}
203228

229+
/** Gets all currently registered observers.
230+
* @return a list of the currently registered observers
231+
*/
204232
protected List<Observer> getObservers() {
205233
return observers;
206234
}

0 commit comments

Comments
 (0)