Skip to content

Commit 88ac298

Browse files
Initialize PrintStreams using UTF-8 charset,
call String.getBytes() with charset.
1 parent c013b25 commit 88ac298

10 files changed

Lines changed: 44 additions & 21 deletions

File tree

src/main/java/org/archive/extract/DumpingExtractorOutput.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.io.OutputStream;
55
import java.io.PrintStream;
6+
import java.io.UnsupportedEncodingException;
67
import java.util.logging.Logger;
78

89
import org.archive.resource.Resource;
@@ -12,13 +13,18 @@
1213
import com.google.common.io.ByteStreams;
1314
import com.google.common.io.CountingOutputStream;
1415

16+
import static java.nio.charset.StandardCharsets.UTF_8;
17+
1518
public class DumpingExtractorOutput implements ExtractorOutput {
1619
private static final Logger LOG =
1720
Logger.getLogger(DumpingExtractorOutput.class.getName());
1821

1922
private PrintStream out;
2023
public DumpingExtractorOutput(OutputStream out) {
21-
this.out = new PrintStream(out);
24+
try {
25+
this.out = new PrintStream(out, false, UTF_8.name());
26+
} catch (UnsupportedEncodingException e) {
27+
}
2228
}
2329

2430
public void output(Resource resource) throws IOException {

src/main/java/org/archive/extract/JSONViewExtractorOutput.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@
33
import java.io.IOException;
44
import java.io.OutputStream;
55
import java.io.PrintStream;
6+
import java.io.UnsupportedEncodingException;
67
import java.util.List;
78

89
import org.apache.commons.lang3.StringUtils;
910
import org.archive.format.json.JSONView;
1011
import org.archive.resource.Resource;
1112
import org.archive.util.StreamCopy;
1213

14+
import static java.nio.charset.StandardCharsets.UTF_8;
15+
1316
public class JSONViewExtractorOutput implements ExtractorOutput {
1417
private PrintStream out;
1518
private JSONView view;
1619
public JSONViewExtractorOutput(OutputStream out, String filterPath) {
1720
view = new JSONView(filterPath.split(","));
18-
this.out = new PrintStream(out);
21+
try {
22+
this.out = new PrintStream(out, false, UTF_8.name());
23+
} catch (UnsupportedEncodingException e) {
24+
}
1925
}
2026
public void output(Resource resource) throws IOException {
2127
StreamCopy.readToEOF(resource.getInputStream());

src/main/java/org/archive/format/gzip/zipnum/ZipNumCluster.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.io.BufferedReader;
1414
import java.io.FileReader;
1515
import java.io.IOException;
16+
import java.nio.charset.StandardCharsets;
1617
import java.text.ParseException;
1718
import java.text.SimpleDateFormat;
1819
import java.util.ArrayList;
@@ -35,6 +36,8 @@
3536
import org.archive.util.binsearch.impl.HTTPSeekableLineReader;
3637
import org.archive.util.iterator.CloseableIterator;
3738

39+
import static java.nio.charset.StandardCharsets.UTF_8;
40+
3841
public class ZipNumCluster extends ZipNumIndex {
3942

4043
final static Logger LOGGER = Logger.getLogger(ZipNumCluster.class.getName());

src/main/java/org/archive/io/arc/ARC2WCDX.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.archive.util.ArchiveUtils;
3333
import org.archive.util.SURT;
3434

35+
import static java.nio.charset.StandardCharsets.UTF_8;
36+
3537
/**
3638
* Create a 'Wide' CDX from an ARC. Takes one argument, the path to the ARC.
3739
* Writes .wcdx.gz in same directory.
@@ -61,7 +63,7 @@ public static Object[] createWcdx(ARCReader reader) {
6163
PrintStream writer = null;
6264
long count = 0;
6365
try {
64-
writer = new PrintStream(new GZIPOutputStream(new FileOutputStream(wcdxFile)));
66+
writer = new PrintStream(new GZIPOutputStream(new FileOutputStream(wcdxFile)), false, UTF_8.name());
6567

6668
// write header: legend + timestamp
6769
StringBuilder legend = new StringBuilder();

src/main/java/org/archive/io/warc/WARCWriter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545

4646
import static org.archive.format.warc.WARCConstants.*;
4747

48+
import static java.nio.charset.StandardCharsets.UTF_8;
49+
4850

4951
/**
5052
* WARC implementation.
@@ -357,12 +359,12 @@ public URI writeWarcinfoRecord(String filename, final String description)
357359
byte [] warcinfoBody = null;
358360
if (settings.getMetadata() == null) {
359361
// TODO: What to write into a warcinfo? What to associate?
360-
warcinfoBody = "TODO: Unimplemented".getBytes();
362+
warcinfoBody = "TODO: Unimplemented".getBytes(UTF_8);
361363
} else {
362364
ByteArrayOutputStream baos = new ByteArrayOutputStream();
363365
for (final Iterator<String> i = settings.getMetadata().iterator();
364366
i.hasNext();) {
365-
baos.write(i.next().toString().getBytes(UTF8Bytes.UTF8));
367+
baos.write(i.next().toString().getBytes(UTF_8));
366368
}
367369
warcinfoBody = baos.toByteArray();
368370
}

src/main/java/org/archive/url/URI.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import java.util.Hashtable;
4343
import java.util.Locale;
4444

45+
import static java.nio.charset.StandardCharsets.UTF_8;
46+
4547
/**
4648
* The interface for the URI(Uniform Resource Identifiers) version of RFC 2396.
4749
* This class has the purpose of supportting of parsing a URI reference to
@@ -1696,7 +1698,7 @@ private static byte[] getBytes(String original, String charset) {
16961698
try {
16971699
return original.getBytes(charset);
16981700
} catch (UnsupportedEncodingException e) {
1699-
return original.getBytes();
1701+
return original.getBytes(UTF_8);
17001702
}
17011703
}
17021704

src/main/java/org/archive/util/SURT.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.archive.url.URIException;
3333
import org.archive.url.UsableURIFactory;
3434

35+
import static java.nio.charset.StandardCharsets.UTF_8;
36+
3537
/**
3638
* Sort-friendly URI Reordering Transform.
3739
*
@@ -238,7 +240,7 @@ public static void main(String[] args) throws IOException {
238240
InputStream in = args.length > 0 ? new BufferedInputStream(
239241
new FileInputStream(args[0])) : System.in;
240242
PrintStream out = args.length > 1 ? new PrintStream(
241-
new BufferedOutputStream(new FileOutputStream(args[1])))
243+
new BufferedOutputStream(new FileOutputStream(args[1])), false, UTF_8.name())
242244
: System.out;
243245
BufferedReader br =
244246
new BufferedReader(new InputStreamReader(in));

src/main/java/org/archive/util/SurtPrefixSet.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.archive.util.iterator.LineReadingIterator;
3838
import org.archive.util.iterator.RegexLineIterator;
3939

40+
import static java.nio.charset.StandardCharsets.UTF_8;
41+
4042
/**
4143
* Specialized TreeSet for keeping a set of String prefixes.
4244
*
@@ -343,10 +345,10 @@ public static void main(String[] args) throws IOException {
343345
InputStream in = args.length > 0 ? new BufferedInputStream(
344346
new FileInputStream(args[0])) : System.in;
345347
PrintStream out = args.length > 1 ? new PrintStream(
346-
new BufferedOutputStream(new FileOutputStream(args[1])))
348+
new BufferedOutputStream(new FileOutputStream(args[1])), false, UTF_8.name())
347349
: System.out;
348350
BufferedReader br =
349-
new BufferedReader(new InputStreamReader(in));
351+
new BufferedReader(new InputStreamReader(in, UTF_8.name()));
350352
String line;
351353
while((line = br.readLine())!=null) {
352354
if(line.indexOf("#")>0) line=line.substring(0,line.indexOf("#"));

src/main/java/org/archive/util/TextUtils.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import com.google.common.cache.CacheLoader;
4141
import com.google.common.cache.LoadingCache;
4242

43+
import static java.nio.charset.StandardCharsets.UTF_8;
44+
4345
public class TextUtils {
4446
private static final String FIRSTWORD = "^([^\\s]*).*$";
4547

@@ -279,14 +281,11 @@ public static String exceptionToString(String message, Throwable e) {
279281
* @param s String to escape
280282
* @return URL-escaped string
281283
*/
282-
@SuppressWarnings("deprecation")
283284
public static String urlEscape(String s) {
284285
try {
285-
return URLEncoder.encode(s,"UTF8");
286+
return URLEncoder.encode(s, UTF_8.name());
286287
} catch (UnsupportedEncodingException e) {
287-
// should be impossible; all JVMs must support UTF8
288-
// but have a fallback just in case
289-
return URLEncoder.encode(s);
288+
return s;
290289
}
291290
}
292291

@@ -296,14 +295,11 @@ public static String urlEscape(String s) {
296295
* @param s String do unescape
297296
* @return URL-unescaped String
298297
*/
299-
@SuppressWarnings("deprecation")
300298
public static String urlUnescape(String s) {
301299
try {
302-
return URLDecoder.decode(s, "UTF8");
300+
return URLDecoder.decode(s, UTF_8.name());
303301
} catch (UnsupportedEncodingException e) {
304-
// should be impossible; all JVMs must support UTF8
305-
// but have a fallback just in case
306-
return URLDecoder.decode(s);
302+
return s;
307303
}
308304
}
309305
}

src/main/java/org/archive/util/binsearch/SortedTextFile.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.archive.util.GeneralURIStreamFactory;
1010
import org.archive.util.iterator.CloseableIterator;
1111

12+
import static java.nio.charset.StandardCharsets.UTF_8;
13+
1214
public class SortedTextFile {
1315

1416
public static class NumericComparator implements Comparator<String>
@@ -371,7 +373,7 @@ private long searchOffset(SeekableLineReader slr,
371373
String prev = null;
372374
while(true) {
373375
if (line != null) {
374-
offset += line.getBytes().length + 1;
376+
offset += line.getBytes(UTF_8).length + 1;
375377
}
376378
line = slr.readLine();
377379
if(line == null) break;
@@ -380,7 +382,7 @@ private long searchOffset(SeekableLineReader slr,
380382
}
381383

382384
if (lessThan && prev != null) {
383-
offset -= prev.getBytes().length + 1;
385+
offset -= prev.getBytes(UTF_8).length + 1;
384386
}
385387

386388
return offset;

0 commit comments

Comments
 (0)