Bug 497548 - Make use of StandardCharsets
All the various places defining and using different UTF-8 charset name
constants is too error prone. Let's stick to StandardCharsets.UTF-8
which even saves the string lookup too.
Change-Id: Idcb356d0293cd2d0d0148de4e1f2e131584d09c4
Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
diff --git a/org.eclipse.help.base/src/org/eclipse/help/internal/browser/MozillaBrowserAdapter.java b/org.eclipse.help.base/src/org/eclipse/help/internal/browser/MozillaBrowserAdapter.java
index 92696ca..912c24f 100644
--- a/org.eclipse.help.base/src/org/eclipse/help/internal/browser/MozillaBrowserAdapter.java
+++ b/org.eclipse.help.base/src/org/eclipse/help/internal/browser/MozillaBrowserAdapter.java
@@ -11,6 +11,7 @@
package org.eclipse.help.internal.browser;
import java.io.*;
+import java.nio.charset.StandardCharsets;
import org.eclipse.core.runtime.*;
import org.eclipse.help.browser.*;
@@ -134,7 +135,7 @@
try {
outFile.getParentFile().mkdirs();
try (PrintWriter writer = new PrintWriter(
- new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF8")), //$NON-NLS-1$
+ new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), StandardCharsets.UTF_8)),
false)) {
writer.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"); //$NON-NLS-1$
writer.println("<html><head>"); //$NON-NLS-1$
diff --git a/org.eclipse.help.ui/src/org/eclipse/help/ui/internal/views/EngineDescriptorManager.java b/org.eclipse.help.ui/src/org/eclipse/help/ui/internal/views/EngineDescriptorManager.java
index aa7d207..45e195c 100644
--- a/org.eclipse.help.ui/src/org/eclipse/help/ui/internal/views/EngineDescriptorManager.java
+++ b/org.eclipse.help.ui/src/org/eclipse/help/ui/internal/views/EngineDescriptorManager.java
@@ -19,6 +19,7 @@
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Observable;
@@ -109,7 +110,7 @@
OutputStreamWriter osw = null;
try {
fos = new FileOutputStream(fileName);
- osw = new OutputStreamWriter(fos, "UTF8"); //$NON-NLS-1$
+ osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
PrintWriter writer = new PrintWriter(osw);
writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
writer.println("<engines>"); //$NON-NLS-1$
@@ -225,7 +226,7 @@
if (!file.exists()) return;
FileInputStream stream = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(
- stream, "utf-8"));//$NON-NLS-1$
+ stream, StandardCharsets.UTF_8));
load(reader);
reader.close();
}
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/ChildLinkInserter.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/ChildLinkInserter.java
index 6879936..aa50e95 100644
--- a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/ChildLinkInserter.java
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/ChildLinkInserter.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009, 2015 IBM Corporation and others.
+ * Copyright (c) 2009, 2016 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -14,6 +14,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
@@ -29,7 +30,6 @@
public class ChildLinkInserter {
- private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
private HttpServletRequest req;
private OutputStream out;
private static final String NO_CHILDREN = "no_child_topics"; //$NON-NLS-1$
@@ -77,7 +77,7 @@
if (encoding != null) {
out.write(linkString.getBytes(encoding));
} else {
- out.write(linkString.getBytes("UTF8")); //$NON-NLS-1$
+ out.write(linkString.getBytes(StandardCharsets.UTF_8));
}
} catch (UnsupportedEncodingException e) {
out.write(linkString.getBytes());
@@ -114,16 +114,16 @@
return buf.toString();
}
- public void addStyle() throws UnsupportedEncodingException, IOException {
+ public void addStyle() throws IOException {
ITopic[] subtopics = getSubtopics();
for (ITopic subtopic : subtopics) {
if (ScopeUtils.showInTree(subtopic, scope)) {
- out.write(HAS_CHILDREN.getBytes(UTF_8));
+ out.write(HAS_CHILDREN.getBytes(StandardCharsets.UTF_8));
return;
}
}
- out.write(NO_CHILDREN.getBytes(UTF_8));
+ out.write(NO_CHILDREN.getBytes(StandardCharsets.UTF_8));
}
}
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/EclipseConnector.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/EclipseConnector.java
index 049718f..cf59bb0 100644
--- a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/EclipseConnector.java
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/EclipseConnector.java
@@ -21,6 +21,7 @@
import java.lang.reflect.UndeclaredThrowableException;
import java.net.URL;
import java.net.URLConnection;
+import java.nio.charset.StandardCharsets;
import java.util.Locale;
import javax.servlet.ServletContext;
@@ -180,7 +181,7 @@
message.append("</pre>"); //$NON-NLS-1$
message.append(errorPageEnd);
- is = new ByteArrayInputStream(message.toString().getBytes("UTF8")); //$NON-NLS-1$
+ is = new ByteArrayInputStream(message.toString().getBytes(StandardCharsets.UTF_8));
}
OutputStream out = resp.getOutputStream();
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/PluginsRootResolvingStream.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/PluginsRootResolvingStream.java
index 7bababd..7a3fb3c 100644
--- a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/PluginsRootResolvingStream.java
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/PluginsRootResolvingStream.java
@@ -15,6 +15,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
@@ -38,7 +39,6 @@
private static final int MAY_BE_INCLUDE = 4;
private static final int IN_METATAG = 5;
private static final String PLUGINS_ROOT = "PLUGINS_ROOT/"; //$NON-NLS-1$
- private static final String UTF8 = "UTF8"; //$NON-NLS-1$
private static final String INSERT_CHILD_LINKS = "<!--INSERT_CHILD_LINKS-->"; //$NON-NLS-1$
private static final String INSERT_CHILD_LINK_STYLE = "<!--INSERT_CHILD_LINK_STYLE-->"; //$NON-NLS-1$
private final String[] keywords = { INSERT_CHILD_LINKS, INSERT_CHILD_LINK_STYLE };
@@ -200,12 +200,12 @@
}
private void flushPluginsRootCharacters() throws IOException {
- out.write(PLUGINS_ROOT.substring(0, charsMatched).getBytes(UTF8));
+ out.write(PLUGINS_ROOT.substring(0, charsMatched).getBytes(StandardCharsets.UTF_8));
}
private void flushKeywordCharacters() throws IOException {
String matchingCharacters = keywords[lastKeywordMatch].substring(0, charsMatched);
- out.write(matchingCharacters.getBytes(UTF8));
+ out.write(matchingCharacters.getBytes(StandardCharsets.UTF_8));
}
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/XMLGenerator.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/XMLGenerator.java
index 5c1cb21..5bb599d 100644
--- a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/XMLGenerator.java
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/XMLGenerator.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2007 IBM Corporation and others.
+ * Copyright (c) 2000, 2016 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -11,6 +11,7 @@
package org.eclipse.help.internal.webapp.servlet;
import java.io.*;
+import java.nio.charset.StandardCharsets;
import org.eclipse.help.internal.base.util.*;
import org.eclipse.help.internal.webapp.*;
@@ -49,7 +50,7 @@
this.outFile = outFile;
try {
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream(outFile), "UTF8")), //$NON-NLS-1$
+ new FileOutputStream(outFile), StandardCharsets.UTF_8)),
false /* no aotoFlush */
);
println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
diff --git a/org.eclipse.help/src/org/eclipse/help/internal/util/URLCoder.java b/org.eclipse.help/src/org/eclipse/help/internal/util/URLCoder.java
index 03a505e..7d4a4ad 100644
--- a/org.eclipse.help/src/org/eclipse/help/internal/util/URLCoder.java
+++ b/org.eclipse.help/src/org/eclipse/help/internal/util/URLCoder.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2015 IBM Corporation and others.
+ * Copyright (c) 2000, 2016 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -11,32 +11,20 @@
package org.eclipse.help.internal.util;
import java.io.ByteArrayOutputStream;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
public class URLCoder {
public static String encode(String s) {
- try {
- return urlEncode(s.getBytes("UTF8"), true); //$NON-NLS-1$
- } catch (UnsupportedEncodingException uee) {
- return null;
- }
+ return urlEncode(s.getBytes(StandardCharsets.UTF_8), true);
}
public static String compactEncode(String s) {
- try {
- return urlEncode(s.getBytes("UTF8"), false); //$NON-NLS-1$
- } catch (UnsupportedEncodingException uee) {
- return null;
- }
+ return urlEncode(s.getBytes(StandardCharsets.UTF_8), false);
}
public static String decode(String s) {
- try {
- return new String(urlDecode(s), "UTF8"); //$NON-NLS-1$
- } catch (UnsupportedEncodingException uee) {
- return null;
- }
+ return new String(urlDecode(s), StandardCharsets.UTF_8);
}
private static String urlEncode(byte[] data, boolean encodeAllCharacters) {
diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/RemoteTestUtils.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/RemoteTestUtils.java
index 288d85b..15de642 100644
--- a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/RemoteTestUtils.java
+++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/RemoteTestUtils.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009, 2011 IBM Corporation and others.
+ * Copyright (c) 2009, 2016 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -14,7 +14,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
@@ -54,8 +53,7 @@
return readFromURL(url);
}
- public static String readFromURL(URL url) throws IOException,
- UnsupportedEncodingException {
+ public static String readFromURL(URL url) throws IOException {
try (InputStream is = url.openStream();
InputStreamReader inputStreamReader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
StringBuffer buffer = new StringBuffer();
diff --git a/org.eclipse.ui.intro/src/org/eclipse/ui/internal/intro/impl/model/url/IntroURLParser.java b/org.eclipse.ui.intro/src/org/eclipse/ui/internal/intro/impl/model/url/IntroURLParser.java
index a6118f6..308d774 100644
--- a/org.eclipse.ui.intro/src/org/eclipse/ui/internal/intro/impl/model/url/IntroURLParser.java
+++ b/org.eclipse.ui.intro/src/org/eclipse/ui/internal/intro/impl/model/url/IntroURLParser.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2003, 2006 IBM Corporation and others.
+ * Copyright (c) 2003, 2016 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -11,9 +11,9 @@
package org.eclipse.ui.internal.intro.impl.model.url;
import java.io.ByteArrayOutputStream;
-import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
+import java.nio.charset.StandardCharsets;
import java.util.Properties;
import org.eclipse.ui.internal.intro.impl.util.Log;
@@ -198,32 +198,28 @@
/*
* Note: This was copied and adapted from org.eclipse.help.internal.util.URLCoder
*/
- private static String urlDecode(String encodedURL) {
+ private static String urlDecode(String encodedURL) {
int len = encodedURL.length();
ByteArrayOutputStream os = new ByteArrayOutputStream(len);
- try {
- for (int i = 0; i < len;) {
- switch (encodedURL.charAt(i)) {
- case '%':
- if (len >= i + 3) {
- os.write(Integer.parseInt(encodedURL.substring(i + 1, i + 3), 16));
- }
- i += 3;
- break;
- case '+': // exception from standard
- os.write(' ');
- i++;
- break;
- default:
- os.write(encodedURL.charAt(i++));
- break;
+ for (int i = 0; i < len;) {
+ switch (encodedURL.charAt(i)) {
+ case '%':
+ if (len >= i + 3) {
+ os.write(Integer.parseInt(encodedURL.substring(i + 1, i + 3), 16));
}
+ i += 3;
+ break;
+ case '+': // exception from standard
+ os.write(' ');
+ i++;
+ break;
+ default:
+ os.write(encodedURL.charAt(i++));
+ break;
}
- return new String(os.toByteArray(), "UTF8"); //$NON-NLS-1$
- } catch (UnsupportedEncodingException ex) {
- return null;
}
+ return new String(os.toByteArray(), StandardCharsets.UTF_8);
}
}