Skip to content

Commit 5d982c5

Browse files
committed
Make explicit where the default platform encoding is being used
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1468885 13f79535-47bb-0310-9956-ffa450edef68
1 parent 493a38a commit 5d982c5

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.io.RandomAccessFile;
2222
import java.io.Serializable;
23+
import java.nio.charset.Charset;
2324
import java.util.Arrays;
2425

2526
import org.apache.commons.io.IOUtils;
@@ -168,7 +169,7 @@ public MagicNumberFileFilter(final String magicNumber, final long offset) {
168169
throw new IllegalArgumentException("The offset cannot be negative");
169170
}
170171

171-
this.magicNumbers = magicNumber.getBytes(); // uses the platform default charset
172+
this.magicNumbers = magicNumber.getBytes(Charset.defaultCharset()); // explicitly uses the platform default charset
172173
this.byteOffset = offset;
173174
}
174175

@@ -267,7 +268,7 @@ public boolean accept(final File file) {
267268
public String toString() {
268269
final StringBuilder builder = new StringBuilder(super.toString());
269270
builder.append("(");
270-
builder.append(new String(magicNumbers));// TODO perhaps use hex if value is not printable
271+
builder.append(new String(magicNumbers, Charset.defaultCharset()));// TODO perhaps use hex if value is not printable
271272
builder.append(",");
272273
builder.append(this.byteOffset);
273274
builder.append(")");

src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.OutputStream;
2323
import java.io.SequenceInputStream;
2424
import java.io.UnsupportedEncodingException;
25+
import java.nio.charset.Charset;
2526
import java.util.ArrayList;
2627
import java.util.Collections;
2728
import java.util.List;
@@ -336,13 +337,15 @@ public synchronized byte[] toByteArray() {
336337
}
337338

338339
/**
339-
* Gets the curent contents of this byte stream as a string.
340+
* Gets the curent contents of this byte stream as a string
341+
* using the platform default charset.
340342
* @return the contents of the byte array as a String
341343
* @see java.io.ByteArrayOutputStream#toString()
342344
*/
343345
@Override
344346
public String toString() {
345-
return new String(toByteArray());
347+
// make explicit the use of the default charset
348+
return new String(toByteArray(), Charset.defaultCharset());
346349
}
347350

348351
/**

0 commit comments

Comments
 (0)