1919
2020import java .io .BufferedReader ;
2121import java .io .File ;
22- import java .io .FileReader ;
2322import java .io .IOException ;
2423import java .io .InputStream ;
2524import java .io .InputStreamReader ;
3736import org .apache .tools .ant .ProjectComponent ;
3837import org .apache .tools .ant .Task ;
3938import org .apache .tools .ant .filters .util .ChainReaderHelper ;
39+ import org .apache .tools .ant .types .CharSet ;
4040import org .apache .tools .ant .types .FileList ;
4141import org .apache .tools .ant .types .FileSet ;
4242import org .apache .tools .ant .types .FilterChain ;
@@ -89,11 +89,11 @@ public class Concat extends Task implements ResourceCollection {
8989 * sub element points to a file or contains text
9090 */
9191 public static class TextElement extends ProjectComponent {
92- private String value = "" ;
93- private boolean trimLeading = false ;
94- private boolean trim = false ;
95- private boolean filtering = true ;
96- private String encoding = null ;
92+ private String value = "" ;
93+ private boolean trimLeading = false ;
94+ private boolean trim = false ;
95+ private boolean filtering = true ;
96+ private CharSet charSet = CharSet . getDefault () ;
9797
9898 /**
9999 * whether to filter the text in this element
@@ -117,7 +117,16 @@ private boolean getFiltering() {
117117 * @param encoding the name of the charset used to encode
118118 */
119119 public void setEncoding (String encoding ) {
120- this .encoding = encoding ;
120+ this .charSet = new CharSet (encoding );
121+ }
122+
123+ /**
124+ * The CharSet of the text element
125+ *
126+ * @param charSet the name of the charset used to encode
127+ */
128+ public void setEncoding (CharSet charSet ) {
129+ this .charSet = charSet ;
121130 }
122131
123132 /**
@@ -132,20 +141,11 @@ public void setFile(File file) throws BuildException {
132141 throw new BuildException ("File %s does not exist." , file );
133142 }
134143
135- BufferedReader reader = null ;
136- try {
137- if (this .encoding == null ) {
138- reader = new BufferedReader (new FileReader (file ));
139- } else {
140- reader = new BufferedReader (
141- new InputStreamReader (Files .newInputStream (file .toPath ()),
142- this .encoding ));
143- }
144+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (
145+ Files .newInputStream (file .toPath ()), charSet .getCharset ()))) {
144146 value = FileUtils .safeReadFully (reader );
145147 } catch (IOException ex ) {
146148 throw new BuildException (ex );
147- } finally {
148- FileUtils .close (reader );
149149 }
150150 }
151151
@@ -418,8 +418,7 @@ public InputStream getInputStream() {
418418 rdr = new MultiReader <>(Arrays .asList (readers ).iterator (),
419419 identityReaderFactory );
420420 }
421- return outputEncoding == null ? new ReaderInputStream (rdr )
422- : new ReaderInputStream (rdr , outputEncoding );
421+ return new ReaderInputStream (rdr , outputCharSet .getCharset ());
423422 }
424423 @ Override
425424 public String getName () {
@@ -444,12 +443,18 @@ public String getName() {
444443 private boolean append ;
445444
446445 /**
447- * Stores the input file encoding .
446+ * Stores the input file CharSet .
448447 */
449- private String encoding ;
448+ private CharSet charSet = CharSet .getDefault ();
449+
450+ /** Indicates if attribute is set explicitly */
451+ private boolean hasCharSet = false ;
452+
453+ /** Stores the output file CharSet. */
454+ private CharSet outputCharSet = CharSet .getDefault ();
450455
451- /** Stores the output file encoding. */
452- private String outputEncoding ;
456+ /** Indicates if attribute is set explicitly */
457+ private boolean hasOutputCharSet = false ;
453458
454459 /** Stores the binary attribute */
455460 private boolean binary ;
@@ -489,15 +494,9 @@ public String getName() {
489494 /** exposed resource name */
490495 private String resourceName ;
491496
492- private ReaderFactory <Resource > resourceReaderFactory = new ReaderFactory <Resource >() {
493- @ Override
494- public Reader getReader (Resource o ) throws IOException {
495- InputStream is = o .getInputStream ();
496- return new BufferedReader (encoding == null
497- ? new InputStreamReader (is )
498- : new InputStreamReader (is , encoding ));
499- }
500- };
497+ private ReaderFactory <Resource > resourceReaderFactory =
498+ o -> new BufferedReader (new InputStreamReader (o .getInputStream (),
499+ charSet .getCharset ()));
501500
502501 private ReaderFactory <Reader > identityReaderFactory = o -> o ;
503502
@@ -515,8 +514,10 @@ public void reset() {
515514 append = false ;
516515 forceOverwrite = true ;
517516 dest = null ;
518- encoding = null ;
519- outputEncoding = null ;
517+ charSet = CharSet .getDefault ();
518+ hasCharSet = false ;
519+ outputCharSet = CharSet .getDefault ();
520+ hasOutputCharSet = false ;
520521 fixLastLine = false ;
521522 filterChains = null ;
522523 footer = null ;
@@ -566,10 +567,7 @@ public void setAppend(boolean append) {
566567 * outputencoding is set, the outputstream.
567568 */
568569 public void setEncoding (String encoding ) {
569- this .encoding = encoding ;
570- if (outputEncoding == null ) {
571- outputEncoding = encoding ;
572- }
570+ setCharSet (new CharSet (encoding ));
573571 }
574572
575573 /**
@@ -578,7 +576,30 @@ public void setEncoding(String encoding) {
578576 * @since Ant 1.6
579577 */
580578 public void setOutputEncoding (String outputEncoding ) {
581- this .outputEncoding = outputEncoding ;
579+ setOutputCharSet (new CharSet (outputEncoding ));
580+ }
581+
582+
583+ /**
584+ * Sets the charset
585+ * @param charSet the charset of the input stream and unless
586+ * outputcharset is set, the outputstream.
587+ */
588+ public void setCharSet (CharSet charSet ) {
589+ this .charSet = charSet ;
590+ hasCharSet = true ;
591+ if (!hasOutputCharSet ) {
592+ outputCharSet = charSet ;
593+ }
594+ }
595+
596+ /**
597+ * Sets the charset for outputting
598+ * @param outputCharSet the charset for the output file
599+ */
600+ public void setOutputCharSet (CharSet outputCharSet ) {
601+ this .outputCharSet = outputCharSet ;
602+ hasOutputCharSet = true ;
582603 }
583604
584605 /**
@@ -802,8 +823,8 @@ public void execute() {
802823 ResourceUtils .copyResource (new ConcatResource (c ), dest == null
803824 ? new LogOutputResource (this , Project .MSG_WARN )
804825 : dest ,
805- null , null , true , false , append , null ,
806- null , getProject (), force );
826+ null , null , true , false , append , CharSet . getDefault () ,
827+ CharSet . getDefault () , getProject (), force );
807828 } catch (IOException e ) {
808829 throw new BuildException ("error concatenating content to " + dest , e );
809830 }
@@ -853,7 +874,7 @@ private void validate() {
853874 throw new BuildException (
854875 "Nested text is incompatible with binary concatenation" );
855876 }
856- if (encoding != null || outputEncoding != null ) {
877+ if (hasCharSet || hasOutputCharSet ) {
857878 throw new BuildException (
858879 "Setting input or output encoding is incompatible with binary concatenation" );
859880 }
0 commit comments