19
19
20
20
import java .io .BufferedReader ;
21
21
import java .io .File ;
22
- import java .io .FileReader ;
23
22
import java .io .IOException ;
24
23
import java .io .InputStream ;
25
24
import java .io .InputStreamReader ;
37
36
import org .apache .tools .ant .ProjectComponent ;
38
37
import org .apache .tools .ant .Task ;
39
38
import org .apache .tools .ant .filters .util .ChainReaderHelper ;
39
+ import org .apache .tools .ant .types .CharSet ;
40
40
import org .apache .tools .ant .types .FileList ;
41
41
import org .apache .tools .ant .types .FileSet ;
42
42
import org .apache .tools .ant .types .FilterChain ;
@@ -89,11 +89,11 @@ public class Concat extends Task implements ResourceCollection {
89
89
* sub element points to a file or contains text
90
90
*/
91
91
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 () ;
97
97
98
98
/**
99
99
* whether to filter the text in this element
@@ -117,7 +117,16 @@ private boolean getFiltering() {
117
117
* @param encoding the name of the charset used to encode
118
118
*/
119
119
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 ;
121
130
}
122
131
123
132
/**
@@ -132,20 +141,11 @@ public void setFile(File file) throws BuildException {
132
141
throw new BuildException ("File %s does not exist." , file );
133
142
}
134
143
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 ()))) {
144
146
value = FileUtils .safeReadFully (reader );
145
147
} catch (IOException ex ) {
146
148
throw new BuildException (ex );
147
- } finally {
148
- FileUtils .close (reader );
149
149
}
150
150
}
151
151
@@ -418,8 +418,7 @@ public InputStream getInputStream() {
418
418
rdr = new MultiReader <>(Arrays .asList (readers ).iterator (),
419
419
identityReaderFactory );
420
420
}
421
- return outputEncoding == null ? new ReaderInputStream (rdr )
422
- : new ReaderInputStream (rdr , outputEncoding );
421
+ return new ReaderInputStream (rdr , outputCharSet .getCharset ());
423
422
}
424
423
@ Override
425
424
public String getName () {
@@ -444,12 +443,18 @@ public String getName() {
444
443
private boolean append ;
445
444
446
445
/**
447
- * Stores the input file encoding .
446
+ * Stores the input file CharSet .
448
447
*/
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 ();
450
455
451
- /** Stores the output file encoding. */
452
- private String outputEncoding ;
456
+ /** Indicates if attribute is set explicitly */
457
+ private boolean hasOutputCharSet = false ;
453
458
454
459
/** Stores the binary attribute */
455
460
private boolean binary ;
@@ -489,15 +494,9 @@ public String getName() {
489
494
/** exposed resource name */
490
495
private String resourceName ;
491
496
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 ()));
501
500
502
501
private ReaderFactory <Reader > identityReaderFactory = o -> o ;
503
502
@@ -515,8 +514,10 @@ public void reset() {
515
514
append = false ;
516
515
forceOverwrite = true ;
517
516
dest = null ;
518
- encoding = null ;
519
- outputEncoding = null ;
517
+ charSet = CharSet .getDefault ();
518
+ hasCharSet = false ;
519
+ outputCharSet = CharSet .getDefault ();
520
+ hasOutputCharSet = false ;
520
521
fixLastLine = false ;
521
522
filterChains = null ;
522
523
footer = null ;
@@ -566,10 +567,7 @@ public void setAppend(boolean append) {
566
567
* outputencoding is set, the outputstream.
567
568
*/
568
569
public void setEncoding (String encoding ) {
569
- this .encoding = encoding ;
570
- if (outputEncoding == null ) {
571
- outputEncoding = encoding ;
572
- }
570
+ setCharSet (new CharSet (encoding ));
573
571
}
574
572
575
573
/**
@@ -578,7 +576,30 @@ public void setEncoding(String encoding) {
578
576
* @since Ant 1.6
579
577
*/
580
578
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 ;
582
603
}
583
604
584
605
/**
@@ -802,8 +823,8 @@ public void execute() {
802
823
ResourceUtils .copyResource (new ConcatResource (c ), dest == null
803
824
? new LogOutputResource (this , Project .MSG_WARN )
804
825
: 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 );
807
828
} catch (IOException e ) {
808
829
throw new BuildException ("error concatenating content to " + dest , e );
809
830
}
@@ -853,7 +874,7 @@ private void validate() {
853
874
throw new BuildException (
854
875
"Nested text is incompatible with binary concatenation" );
855
876
}
856
- if (encoding != null || outputEncoding != null ) {
877
+ if (hasCharSet || hasOutputCharSet ) {
857
878
throw new BuildException (
858
879
"Setting input or output encoding is incompatible with binary concatenation" );
859
880
}
0 commit comments