Skip to content

Commit a8d564f

Browse files
author
Gary Gregory
committed
Add and use XmlStreamReader(Path).
Remove leading underscores in test method names. Remove useless parentheses. Use try-with-resources. Use longer lines in some places.
1 parent a71e129 commit a8d564f

3 files changed

Lines changed: 186 additions & 163 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ The <action> type attribute can be add,update,fix,remove.
6666
<action dev="ggregory" type="update" due-to="trncate">
6767
Add test to make sure the setter of AndFileFilter works correctly #244.
6868
</action>
69+
<action dev="ggregory" type="update" due-to="Gary Gregory">
70+
Add XmlStreamReader(Path).
71+
</action>
6972
<!-- UPDATE -->
7073
<action dev="ggregory" type="update" due-to="Dependabot">
7174
Bump mockito-inline from 3.11.0 to 3.11.2 #247.

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.net.URL;
2929
import java.net.URLConnection;
3030
import java.nio.file.Files;
31+
import java.nio.file.Path;
3132
import java.text.MessageFormat;
3233
import java.util.Locale;
3334
import java.util.Objects;
@@ -275,9 +276,8 @@ static boolean isTextXml(final String mime) {
275276
* @param file File to create a Reader from.
276277
* @throws IOException thrown if there is a problem reading the file.
277278
*/
278-
@SuppressWarnings("resource") // FileInputStream is managed through another reader in this instance.
279279
public XmlStreamReader(final File file) throws IOException {
280-
this(Files.newInputStream(Objects.requireNonNull(file, "file").toPath()));
280+
this(Objects.requireNonNull(file, "file").toPath());
281281
}
282282

283283
/**
@@ -387,7 +387,6 @@ public XmlStreamReader(final InputStream inputStream, final String httpContentTy
387387
this(inputStream, httpContentType, true);
388388
}
389389

390-
391390
/**
392391
* Creates a Reader using an InputStream and the associated content-type
393392
* header. This constructor is lenient regarding the encoding detection.
@@ -426,6 +425,7 @@ public XmlStreamReader(final InputStream inputStream, final String httpContentTy
426425
this(inputStream, httpContentType, lenient, null);
427426
}
428427

428+
429429
/**
430430
* Creates a Reader using an InputStream and the associated content-type
431431
* header. This constructor is lenient regarding the encoding detection.
@@ -471,6 +471,24 @@ public XmlStreamReader(final InputStream inputStream, final String httpContentTy
471471
this.reader = new InputStreamReader(pis, encoding);
472472
}
473473

474+
/**
475+
* Creates a Reader for a File.
476+
* <p>
477+
* It looks for the UTF-8 BOM first, if none sniffs the XML prolog charset,
478+
* if this is also missing defaults to UTF-8.
479+
* <p>
480+
* It does a lenient charset encoding detection, check the constructor with
481+
* the lenient parameter for details.
482+
*
483+
* @param file File to create a Reader from.
484+
* @throws IOException thrown if there is a problem reading the file.
485+
* @since 2.11.0
486+
*/
487+
@SuppressWarnings("resource") // InputStream is managed through another reader in this instance.
488+
public XmlStreamReader(final Path file) throws IOException {
489+
this(Files.newInputStream(Objects.requireNonNull(file, "file")));
490+
}
491+
474492
/**
475493
* Creates a Reader using the InputStream of a URL.
476494
* <p>

0 commit comments

Comments
 (0)