Skip to content

Commit

Permalink
Replace Guava Charsets with JDK StandardCharsets
Browse files Browse the repository at this point in the history
  • Loading branch information
joschi committed Dec 5, 2015
1 parent 3f49fc4 commit af1fbc4
Show file tree
Hide file tree
Showing 21 changed files with 87 additions and 71 deletions.
@@ -1,13 +1,14 @@
package io.dropwizard.assets;

import com.google.common.base.Charsets;
import io.dropwizard.Bundle;
import io.dropwizard.servlets.assets.AssetServlet;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;

import static com.google.common.base.Preconditions.checkArgument;

/**
Expand Down Expand Up @@ -122,6 +123,6 @@ public String getIndexFile() {
}

protected AssetServlet createServlet() {
return new AssetServlet(resourcePath, uriPath, indexFile, Charsets.UTF_8);
return new AssetServlet(resourcePath, uriPath, indexFile, StandardCharsets.UTF_8);
}
}
Expand Up @@ -3,7 +3,6 @@
import com.codahale.metrics.MetricRegistry;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.google.common.base.Optional;
import com.google.common.io.CharStreams;
import com.sun.net.httpserver.Headers;
Expand All @@ -25,6 +24,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.zip.GZIPInputStream;
Expand Down Expand Up @@ -154,7 +154,7 @@ public void handle(HttpExchange httpExchange) throws IOException {

httpExchange.getResponseHeaders().add(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
httpExchange.sendResponseHeaders(200, 0);
httpExchange.getResponseBody().write(JSON_TOKEN.getBytes(Charsets.UTF_8));
httpExchange.getResponseBody().write(JSON_TOKEN.getBytes(StandardCharsets.UTF_8));
httpExchange.getResponseBody().close();
} finally {
httpExchange.close();
Expand Down Expand Up @@ -196,7 +196,7 @@ private void postResponse(HttpExchange httpExchange) throws IOException {
httpExchange.getResponseHeaders().add(HttpHeaders.CONTENT_ENCODING, GZIP);
httpExchange.sendResponseHeaders(200, 0);
GZIPOutputStream gzipStream = new GZIPOutputStream(httpExchange.getResponseBody());
gzipStream.write(JSON_TOKEN.getBytes(Charsets.UTF_8));
gzipStream.write(JSON_TOKEN.getBytes(StandardCharsets.UTF_8));
gzipStream.close();
}

Expand All @@ -207,7 +207,7 @@ private void checkBody(HttpExchange httpExchange, boolean gzip) throws IOExcepti

InputStream requestBody = gzip ? new GZIPInputStream(httpExchange.getRequestBody()) :
httpExchange.getRequestBody();
String body = CharStreams.toString(new InputStreamReader(requestBody, Charsets.UTF_8));
String body = CharStreams.toString(new InputStreamReader(requestBody, StandardCharsets.UTF_8));
assertThat(JSON_MAPPER.readTree(body)).isEqualTo(JSON_MAPPER.createObjectNode()
.put("email", "john@doe.me")
.put("name", "John Doe"));
Expand All @@ -227,7 +227,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
httpExchange.getResponseBody().write(JSON_MAPPER.createObjectNode()
.put("email", "john@doe.me")
.put("name", "John Doe")
.toString().getBytes(Charsets.UTF_8));
.toString().getBytes(StandardCharsets.UTF_8));
} finally {
httpExchange.close();
}
Expand Down Expand Up @@ -265,7 +265,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
assertThat(httpExchange.getRequestHeaders().get(HttpHeaders.USER_AGENT))
.containsExactly("Custom user-agent");
httpExchange.sendResponseHeaders(200, 0);
httpExchange.getResponseBody().write("Hello World!".getBytes(Charsets.UTF_8));
httpExchange.getResponseBody().write("Hello World!".getBytes(StandardCharsets.UTF_8));
} finally {
httpExchange.close();
}
Expand Down Expand Up @@ -303,7 +303,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
try {
httpExchange.getResponseHeaders().add(HttpHeaders.CONTENT_TYPE, TEXT_PLAIN);
httpExchange.sendResponseHeaders(200, 0);
httpExchange.getResponseBody().write("Hello World!".getBytes(Charsets.UTF_8));
httpExchange.getResponseBody().write("Hello World!".getBytes(StandardCharsets.UTF_8));
} finally {
httpExchange.close();
}
Expand Down
@@ -1,11 +1,11 @@
package io.dropwizard.configuration;

import com.google.common.base.Charsets;
import com.google.common.io.ByteStreams;
import com.google.common.io.Resources;
import org.junit.Test;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -15,7 +15,7 @@ public class FileConfigurationSourceProviderTest {
@Test
public void readsFileContents() throws Exception {
try (InputStream input = provider.open(Resources.getResource("example.txt").getFile())) {
assertThat(new String(ByteStreams.toByteArray(input), Charsets.UTF_8).trim())
assertThat(new String(ByteStreams.toByteArray(input), StandardCharsets.UTF_8).trim())
.isEqualTo("whee");
}
}
Expand Down
@@ -1,11 +1,11 @@
package io.dropwizard.configuration;

import com.google.common.base.Charsets;
import com.google.common.io.ByteStreams;
import com.google.common.io.Resources;
import org.junit.Test;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -15,7 +15,7 @@ public class UrlConfigurationSourceProviderTest {
@Test
public void readsFileContents() throws Exception {
try (InputStream input = provider.open(Resources.getResource("example.txt").toString())) {
assertThat(new String(ByteStreams.toByteArray(input), Charsets.UTF_8).trim())
assertThat(new String(ByteStreams.toByteArray(input), StandardCharsets.UTF_8).trim())
.isEqualTo("whee");
}
}
Expand Down
13 changes: 9 additions & 4 deletions dropwizard-core/src/main/java/io/dropwizard/cli/Cli.java
@@ -1,17 +1,22 @@
package io.dropwizard.cli;

import com.google.common.base.Charsets;
import com.google.common.collect.Maps;
import io.dropwizard.configuration.ConfigurationException;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.util.JarLocation;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.*;
import net.sourceforge.argparse4j.inf.Argument;
import net.sourceforge.argparse4j.inf.ArgumentAction;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;
import java.util.SortedMap;
Expand Down Expand Up @@ -40,8 +45,8 @@ public class Cli {
* @param stdErr standard err
*/
public Cli(JarLocation location, Bootstrap<?> bootstrap, OutputStream stdOut, OutputStream stdErr) {
this.stdOut = new PrintWriter(new OutputStreamWriter(stdOut, Charsets.UTF_8), true);
this.stdErr = new PrintWriter(new OutputStreamWriter(stdErr, Charsets.UTF_8), true);
this.stdOut = new PrintWriter(new OutputStreamWriter(stdOut, StandardCharsets.UTF_8), true);
this.stdErr = new PrintWriter(new OutputStreamWriter(stdErr, StandardCharsets.UTF_8), true);
this.commands = Maps.newTreeMap();
this.parser = buildParser(location);
this.bootstrap = bootstrap;
Expand Down
Expand Up @@ -10,14 +10,13 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.io.Resources;
import io.dropwizard.jersey.errors.EarlyEofExceptionMapper;
import io.dropwizard.jersey.errors.LoggingExceptionMapper;
import io.dropwizard.jersey.filter.AllowedMethodsFilter;
import io.dropwizard.jersey.jackson.JsonProcessingExceptionMapper;
import io.dropwizard.jersey.jackson.JacksonMessageBodyProvider;
import io.dropwizard.jersey.jackson.JsonProcessingExceptionMapper;
import io.dropwizard.jersey.setup.JerseyEnvironment;
import io.dropwizard.jersey.validation.HibernateValidationFeature;
import io.dropwizard.jersey.validation.JerseyViolationExceptionMapper;
Expand All @@ -36,7 +35,6 @@
import org.eclipse.jetty.server.handler.ErrorHandler;
import org.eclipse.jetty.server.handler.RequestLogHandler;
import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.setuid.RLimit;
import org.eclipse.jetty.setuid.SetUIDListener;
import org.eclipse.jetty.util.BlockingArrayQueue;
Expand All @@ -53,6 +51,7 @@
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.EnumSet;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
Expand Down Expand Up @@ -589,7 +588,7 @@ protected Handler buildGzipHandler(Handler handler) {
protected void printBanner(String name) {
try {
final String banner = WINDOWS_NEWLINE.matcher(Resources.toString(Resources.getResource("banner.txt"),
Charsets.UTF_8))
StandardCharsets.UTF_8))
.replaceAll("\n")
.replace("\n", String.format("%n"));
LOGGER.info(String.format("Starting {}%n{}"), name, banner);
Expand Down
@@ -1,43 +1,43 @@
package com.example.helloworld.resources;

import com.google.common.base.Charsets;
import io.dropwizard.views.View;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.nio.charset.StandardCharsets;

@Path("/views")
public class ViewResource {
@GET
@Produces("text/html;charset=UTF-8")
@Path("/utf8.ftl")
public View freemarkerUTF8() {
return new View("/views/ftl/utf8.ftl", Charsets.UTF_8) {
return new View("/views/ftl/utf8.ftl", StandardCharsets.UTF_8) {
};
}

@GET
@Produces("text/html;charset=ISO-8859-1")
@Path("/iso88591.ftl")
public View freemarkerISO88591() {
return new View("/views/ftl/iso88591.ftl", Charsets.ISO_8859_1) {
return new View("/views/ftl/iso88591.ftl", StandardCharsets.ISO_8859_1) {
};
}

@GET
@Produces("text/html;charset=UTF-8")
@Path("/utf8.mustache")
public View mustacheUTF8() {
return new View("/views/mustache/utf8.mustache", Charsets.UTF_8) {
return new View("/views/mustache/utf8.mustache", StandardCharsets.UTF_8) {
};
}

@GET
@Produces("text/html;charset=ISO-8859-1")
@Path("/iso88591.mustache")
public View mustacheISO88591() {
return new View("/views/mustache/iso88591.mustache", Charsets.ISO_8859_1) {
return new View("/views/mustache/iso88591.mustache", StandardCharsets.ISO_8859_1) {
};
}
}
@@ -1,7 +1,6 @@
package io.dropwizard.jackson;

import com.fasterxml.jackson.databind.jsontype.impl.StdSubtypeResolver;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
Expand All @@ -12,6 +11,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.List;

Expand Down Expand Up @@ -58,7 +58,7 @@ protected List<Class<?>> discoverServices(Class<?> klass) {
while (resources.hasMoreElements()) {
final URL url = resources.nextElement();
try (InputStream input = url.openStream();
InputStreamReader streamReader = new InputStreamReader(input, Charsets.UTF_8);
InputStreamReader streamReader = new InputStreamReader(input, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(streamReader)) {
String line;
while ((line = reader.readLine()) != null) {
Expand Down
@@ -1,6 +1,5 @@
package io.dropwizard.jetty;

import com.google.common.base.Charsets;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import com.google.common.io.Resources;
Expand All @@ -20,8 +19,11 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.*;
import java.nio.charset.StandardCharsets;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.Inflater;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -120,9 +122,9 @@ public static class BannerServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding(Charsets.UTF_8.toString());
resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());
resp.setContentType(PLAIN_TEXT_UTF_8);
Resources.asCharSource(Resources.getResource("assets/banner.txt"), Charsets.UTF_8)
Resources.asCharSource(Resources.getResource("assets/banner.txt"), StandardCharsets.UTF_8)
.copyTo(resp.getWriter());
}

Expand All @@ -136,4 +138,4 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
resp.getWriter().write("Banner has been updated");
}
}
}
}
Expand Up @@ -5,7 +5,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.TextNode;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;
import com.google.common.io.Resources;
Expand All @@ -24,6 +23,7 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.charset.StandardCharsets;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -106,14 +106,14 @@ public void testConfigure() throws Exception {

config.stop();

assertThat(Files.readLines(defaultLog, Charsets.UTF_8)).containsOnly(
assertThat(Files.readLines(defaultLog, StandardCharsets.UTF_8)).containsOnly(
"INFO com.example.app: Application log",
"DEBUG com.example.newApp: New application debug log",
"INFO com.example.newApp: New application info log",
"DEBUG com.example.legacyApp: Legacy application debug log",
"INFO com.example.legacyApp: Legacy application info log");

assertThat(Files.readLines(newAppLog, Charsets.UTF_8)).containsOnly(
assertThat(Files.readLines(newAppLog, StandardCharsets.UTF_8)).containsOnly(
"DEBUG com.example.newApp: New application debug log",
"INFO com.example.newApp: New application info log",
"DEBUG com.example.notAdditive: Not additive application debug log",
Expand Down
@@ -1,6 +1,5 @@
package io.dropwizard.migrations;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import io.dropwizard.Configuration;
import io.dropwizard.db.DatabaseConfiguration;
Expand All @@ -10,6 +9,7 @@
import net.sourceforge.argparse4j.inf.Subparser;

import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class DbFastForwardCommand<T extends Configuration> extends AbstractLiquibaseCommand<T> {
Expand Down Expand Up @@ -49,13 +49,13 @@ public void run(Namespace namespace,
final String context = getContext(namespace);
if (namespace.getBoolean("all")) {
if (namespace.getBoolean("dry-run")) {
liquibase.changeLogSync(context, new OutputStreamWriter(System.out, Charsets.UTF_8));
liquibase.changeLogSync(context, new OutputStreamWriter(System.out, StandardCharsets.UTF_8));
} else {
liquibase.changeLogSync(context);
}
} else {
if (namespace.getBoolean("dry-run")) {
liquibase.markNextChangeSetRan(context, new OutputStreamWriter(System.out, Charsets.UTF_8));
liquibase.markNextChangeSetRan(context, new OutputStreamWriter(System.out, StandardCharsets.UTF_8));
} else {
liquibase.markNextChangeSetRan(context);
}
Expand Down

0 comments on commit af1fbc4

Please sign in to comment.