Skip to content

Commit

Permalink
Use StandardCharsets instead of "utf-8"
Browse files Browse the repository at this point in the history
  • Loading branch information
ato committed Aug 30, 2024
1 parent b41efd0 commit 6480a3e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 32 deletions.
23 changes: 8 additions & 15 deletions src/outbackcdx/Web.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.nio.charset.StandardCharsets.UTF_8;
import static outbackcdx.Json.JSON_MAPPER;
import static outbackcdx.Web.Status.*;

Expand Down Expand Up @@ -49,7 +49,7 @@ static class Response {

public Response(int status, String mime, String body) {
this.status = status;
byte[] bodyBytes = body.getBytes(StandardCharsets.UTF_8);
byte[] bodyBytes = body.getBytes(UTF_8);
this.bodyLength = bodyBytes.length;
this.bodyWriter = out -> out.write(bodyBytes);
if (mime != null) addHeader("Content-Type", mime);
Expand Down Expand Up @@ -141,11 +141,8 @@ private void parseQueryString(String query) {
if (pair.isEmpty()) continue;
String[] parts = pair.split("=", 2);
if (parts.length != 2) continue;
try {
params.add(URLDecoder.decode(parts[0], "UTF-8"),
URLDecoder.decode(parts[1], "UTF-8"));
} catch (UnsupportedEncodingException ignored) {
}
params.add(URLDecoder.decode(parts[0], UTF_8),
URLDecoder.decode(parts[1], UTF_8));
}
}

Expand Down Expand Up @@ -280,7 +277,7 @@ private static String slurp(InputStream stream) throws IOException {
for (int n = stream.read(buffer); n >= 0; n = stream.read(buffer)) {
baos.write(buffer, 0, n);
}
return baos.toString("utf-8");
return baos.toString(UTF_8);
}

static Response jsonResponse(Object data) throws JsonProcessingException {
Expand Down Expand Up @@ -454,13 +451,9 @@ default String rebuildUrl() {
for (String value: params().getAll(key)) {
buf.append(first ? '?' : '&');
first = false;
try {
buf.append(URLEncoder.encode(key, "UTF-8"));
buf.append('=');
buf.append(URLEncoder.encode(value, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e); // not possible
}
buf.append(URLEncoder.encode(key, UTF_8));
buf.append('=');
buf.append(URLEncoder.encode(value, UTF_8));
}
}
}
Expand Down
25 changes: 11 additions & 14 deletions src/outbackcdx/XmlQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java.io.BufferedOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
Expand All @@ -13,6 +12,8 @@
import java.util.NoSuchElementException;
import java.util.logging.Logger;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Implements the Wayback RemoteCollection XmlQuery interface.
*/
Expand Down Expand Up @@ -63,20 +64,16 @@ public XmlQuery(Web.Request request, Index index, Iterable<FilterPlugin> filterP
}

private static Map<String,String> decodeQueryString(String q) {
try {
Map<String,String> m = new HashMap<>();
for (String entry : q.split(" ")) {
String[] fields = entry.split(":", 2);
// we use URLDecoder rather than URLCanonicalize here to match Wayback's encoding behaviour
// (spaces encoded as + rather than %20)
String key = URLDecoder.decode(fields[0], "UTF-8");
String value = URLDecoder.decode(fields[1], "UTF-8");
m.put(key, value);
}
return m;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
Map<String,String> m = new HashMap<>();
for (String entry : q.split(" ")) {
String[] fields = entry.split(":", 2);
// we use URLDecoder rather than URLCanonicalize here to match Wayback's encoding behaviour
// (spaces encoded as + rather than %20)
String key = URLDecoder.decode(fields[0], UTF_8);
String value = URLDecoder.decode(fields[1], UTF_8);
m.put(key, value);
}
return m;
}

private static void writeElement(XMLStreamWriter out, String name, Object value) throws XMLStreamException {
Expand Down
3 changes: 2 additions & 1 deletion test/outbackcdx/ReplicationFeaturesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.*;
import java.util.Collections;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static outbackcdx.Web.Method.*;
Expand Down Expand Up @@ -133,7 +134,7 @@ private String slurp(Web.Response response) throws IOException {
if (streamer != null) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
streamer.stream(out);
return out.toString("UTF-8");
return out.toString(UTF_8);
}
return "";
}
Expand Down
3 changes: 1 addition & 2 deletions test/outbackcdx/WebappTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import outbackcdx.UrlCanonicalizer.ConfigurationException;
import outbackcdx.auth.Permit;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -381,7 +380,7 @@ private String slurp(Web.Response response) throws IOException {
if (streamer != null) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
streamer.stream(out);
return out.toString("UTF-8");
return out.toString(UTF_8);
}
return "";
}
Expand Down

0 comments on commit 6480a3e

Please sign in to comment.