Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Kurtakov2016-07-07 20:22:29 +0000
committerAlexander Kurtakov2016-07-07 20:22:29 +0000
commit72a315fe74d17991b23773f7f60fc597efc15bd2 (patch)
treef487bcec32e3c5b1caa21878fbad4e030511f288
parent19f86cc43702bc82e3e79202feb740e2dea678dd (diff)
downloadeclipse.platform.swt-72a315fe74d17991b23773f7f60fc597efc15bd2.tar.gz
eclipse.platform.swt-72a315fe74d17991b23773f7f60fc597efc15bd2.tar.xz
eclipse.platform.swt-72a315fe74d17991b23773f7f60fc597efc15bd2.zip
Bug 497525 - Move from charsets string constants to StandardCharsets
More readable code as there is no need to catch UnsupportedEncodingException and it's also faster as there is no need for the JVM to lookup the charset based on the string constant passed. Change-Id: I33140d381e42268d51160bfa6906df84533f82d4 Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT Mozilla/common/org/eclipse/swt/browser/Mozilla.java20
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT WebKit/cocoa/org/eclipse/swt/browser/WebKit.java15
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java67
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT WebKit/win32/org/eclipse/swt/browser/WebFrameLoadDelegate.java9
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT WebKit/win32/org/eclipse/swt/browser/WebKit.java51
5 files changed, 30 insertions, 132 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Mozilla/common/org/eclipse/swt/browser/Mozilla.java b/bundles/org.eclipse.swt/Eclipse SWT Mozilla/common/org/eclipse/swt/browser/Mozilla.java
index 82940c6607..c4bc3f75dc 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT Mozilla/common/org/eclipse/swt/browser/Mozilla.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT Mozilla/common/org/eclipse/swt/browser/Mozilla.java
@@ -14,6 +14,7 @@ package org.eclipse.swt.browser;
import java.io.*;
import java.lang.reflect.*;
+import java.nio.charset.*;
import java.util.*;
import java.util.List;
@@ -450,11 +451,7 @@ class Mozilla extends WebBrowser {
if (pathBytes_NSFree == null) {
String mozillaPath = getMozillaPath ();
mozillaPath += MozillaDelegate.getLibraryName (mozillaPath) + '\0';
- try {
- pathBytes_NSFree = mozillaPath.getBytes ("UTF-8"); //$NON-NLS-1$
- } catch (UnsupportedEncodingException e) {
- pathBytes_NSFree = mozillaPath.getBytes ();
- }
+ pathBytes_NSFree = mozillaPath.getBytes (StandardCharsets.UTF_8); //$NON-NLS-1$
}
if (!XPCOM.NS_Free (pathBytes_NSFree, cookieString)) {
C.free (cookieString);
@@ -1753,11 +1750,7 @@ static byte[] getJSLibPathBytes () {
File file = new File (getMozillaPath (), names[i]);
if (file.exists ()) {
String pathString = file.getAbsolutePath () + '\0';
- try {
- jsLibPathBytes = pathString.getBytes ("UTF-8"); //$NON-NLS-1$
- } catch (UnsupportedEncodingException e) {
- jsLibPathBytes = pathString.getBytes ();
- }
+ jsLibPathBytes = pathString.getBytes (StandardCharsets.UTF_8); //$NON-NLS-1$
break;
}
}
@@ -3357,12 +3350,7 @@ public boolean setText (String html, boolean trusted) {
if (browser != browser.getDisplay ().getFocusControl ()) Deactivate ();
/* convert the String containing HTML to an array of bytes with UTF-8 data */
- byte[] data = null;
- try {
- data = html.getBytes ("UTF-8"); //$NON-NLS-1$
- } catch (UnsupportedEncodingException e) {
- return false;
- }
+ byte[] data = html.getBytes (StandardCharsets.UTF_8); //$NON-NLS-1$
/*
* This could be the first content that is set into the browser, so
diff --git a/bundles/org.eclipse.swt/Eclipse SWT WebKit/cocoa/org/eclipse/swt/browser/WebKit.java b/bundles/org.eclipse.swt/Eclipse SWT WebKit/cocoa/org/eclipse/swt/browser/WebKit.java
index 2faa2e5b2c..b18e08b136 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT WebKit/cocoa/org/eclipse/swt/browser/WebKit.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT WebKit/cocoa/org/eclipse/swt/browser/WebKit.java
@@ -10,7 +10,7 @@
*******************************************************************************/
package org.eclipse.swt.browser;
-import java.io.*;
+import java.nio.charset.*;
import java.util.*;
import org.eclipse.swt.*;
@@ -483,19 +483,10 @@ public boolean execute (String script) {
WebFrame frame = webView.mainFrame();
long /*int*/ context = frame.globalContext();
- byte[] bytes = null;
- try {
- bytes = (script + '\0').getBytes("UTF-8"); //$NON-NLS-1$
- } catch (UnsupportedEncodingException e) {
- bytes = (script + '\0').getBytes();
- }
+ byte[] bytes = (script + '\0').getBytes(StandardCharsets.UTF_8); //$NON-NLS-1$
long /*int*/ scriptString = OS.JSStringCreateWithUTF8CString(bytes);
- try {
- bytes = (getUrl() + '\0').getBytes("UTF-8"); //$NON-NLS-1$
- } catch (UnsupportedEncodingException e) {
- bytes = (getUrl() + '\0').getBytes();
- }
+ bytes = (getUrl() + '\0').getBytes(StandardCharsets.UTF_8); //$NON-NLS-1$
long /*int*/ urlString = OS.JSStringCreateWithUTF8CString(bytes);
long /*int*/ result = OS.JSEvaluateScript(context, scriptString, 0, urlString, 0, null);
diff --git a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java
index 1d0cb8957b..192ba8a82d 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java
@@ -14,6 +14,7 @@ package org.eclipse.swt.browser;
import java.io.*;
import java.net.*;
+import java.nio.charset.*;
import java.util.*;
import org.eclipse.swt.*;
@@ -39,7 +40,6 @@ class WebKit extends WebBrowser {
static Map<LONG, LONG> WindowMappings = new HashMap<> ();
static final String ABOUT_BLANK = "about:blank"; //$NON-NLS-1$
- static final String CHARSET_UTF8 = "UTF-8"; //$NON-NLS-1$
static final String CLASSNAME_EXTERNAL = "External"; //$NON-NLS-1$
static final String FUNCTIONNAME_CALLJAVA = "callJava"; //$NON-NLS-1$
static final String HEADER_CONTENTTYPE = "content-type"; //$NON-NLS-1$
@@ -292,12 +292,7 @@ static long /*int*/ JSObjectCallAsFunctionProc (long /*int*/ ctx, long /*int*/ f
}
static long /*int*/ JSObjectGetPropertyProc (long /*int*/ ctx, long /*int*/ object, long /*int*/ propertyName, long /*int*/ exception) {
- byte[] bytes = null;
- try {
- bytes = (FUNCTIONNAME_CALLJAVA + '\0').getBytes (CHARSET_UTF8); //$NON-NLS-1$
- } catch (UnsupportedEncodingException e) {
- bytes = Converter.wcsToMbcs (null, FUNCTIONNAME_CALLJAVA, true);
- }
+ byte[] bytes = (FUNCTIONNAME_CALLJAVA + '\0').getBytes (StandardCharsets.UTF_8); //$NON-NLS-1$
long /*int*/ name = WebKitGTK.JSStringCreateWithUTF8CString (bytes);
long /*int*/ function = WebKitGTK.JSObjectMakeFunctionWithCallback (ctx, name, JSObjectCallAsFunctionProc.getAddress ());
WebKitGTK.JSStringRelease (name);
@@ -305,12 +300,7 @@ static long /*int*/ JSObjectGetPropertyProc (long /*int*/ ctx, long /*int*/ obje
}
static long /*int*/ JSObjectHasPropertyProc (long /*int*/ ctx, long /*int*/ object, long /*int*/ propertyName) {
- byte[] bytes = null;
- try {
- bytes = (FUNCTIONNAME_CALLJAVA + '\0').getBytes (CHARSET_UTF8); //$NON-NLS-1$
- } catch (UnsupportedEncodingException e) {
- bytes = Converter.wcsToMbcs (null, FUNCTIONNAME_CALLJAVA, true);
- }
+ byte[] bytes = (FUNCTIONNAME_CALLJAVA + '\0').getBytes (StandardCharsets.UTF_8); //$NON-NLS-1$
return WebKitGTK.JSStringIsEqualToUTF8CString (propertyName, bytes);
}
@@ -924,19 +914,10 @@ boolean close (boolean showPrompters) {
@Override
public boolean execute (String script) {
- byte[] bytes = null;
- try {
- bytes = (script + '\0').getBytes (CHARSET_UTF8); //$NON-NLS-1$
- } catch (UnsupportedEncodingException e) {
- bytes = Converter.wcsToMbcs (null, script, true);
- }
+ byte[] bytes = (script + '\0').getBytes (StandardCharsets.UTF_8); //$NON-NLS-1$
long /*int*/ scriptString = WebKitGTK.JSStringCreateWithUTF8CString (bytes);
- try {
- bytes = (getUrl () + '\0').getBytes (CHARSET_UTF8); //$NON-NLS-1$
- } catch (UnsupportedEncodingException e) {
- bytes = Converter.wcsToMbcs (null, getUrl (), true);
- }
+ bytes = (getUrl () + '\0').getBytes (StandardCharsets.UTF_8); //$NON-NLS-1$
long /*int*/ result = 0;
if (WEBKIT2){
@@ -1473,7 +1454,7 @@ long /*int*/ handleLoadFinished (long /*int*/ uri, boolean top) {
if (url.startsWith(ABOUT_BLANK)) {
loadingText = true;
byte[] mimeType = Converter.wcsToMbcs (null, "text/html", true); //$NON-NLS-1$
- byte[] encoding = Converter.wcsToMbcs (null, CHARSET_UTF8, true); //$NON-NLS-1$
+ byte[] encoding = Converter.wcsToMbcs (null, StandardCharsets.UTF_8.displayName(), true); //$NON-NLS-1$
byte[] uriBytes;
if (untrustedText) {
uriBytes = Converter.wcsToMbcs (null, ABOUT_BLANK, true);
@@ -1656,12 +1637,7 @@ public void refresh () {
@Override
public boolean setText (String html, boolean trusted) {
/* convert the String containing HTML to an array of bytes with UTF-8 data */
- byte[] bytes = null;
- try {
- bytes = (html + '\0').getBytes (CHARSET_UTF8); //$NON-NLS-1$
- } catch (UnsupportedEncodingException e) {
- bytes = Converter.wcsToMbcs (null, html, true);
- }
+ byte[] bytes = (html + '\0').getBytes (StandardCharsets.UTF_8); //$NON-NLS-1$
/*
* If this.htmlBytes is not null then the about:blank page is already being loaded,
@@ -2298,12 +2274,7 @@ long /*int*/ webkit_web_view_ready (long /*int*/ web_view) {
long /*int*/ webkit_window_object_cleared (long /*int*/ web_view, long /*int*/ frame, long /*int*/ context, long /*int*/ window_object) {
long /*int*/ globalObject = WebKitGTK.JSContextGetGlobalObject (context);
long /*int*/ externalObject = WebKitGTK.JSObjectMake (context, ExternalClass, webViewData);
- byte[] bytes = null;
- try {
- bytes = (OBJECTNAME_EXTERNAL + '\0').getBytes (CHARSET_UTF8);
- } catch (UnsupportedEncodingException e) {
- bytes = Converter.wcsToMbcs (null, OBJECTNAME_EXTERNAL, true);
- }
+ byte[] bytes = (OBJECTNAME_EXTERNAL + '\0').getBytes (StandardCharsets.UTF_8);
long /*int*/ name = WebKitGTK.JSStringCreateWithUTF8CString (bytes);
WebKitGTK.JSObjectSetProperty (context, globalObject, name, externalObject, 0, null);
WebKitGTK.JSStringRelease (name);
@@ -2366,12 +2337,7 @@ long /*int*/ convertToJS (long /*int*/ ctx, Object value) {
return WebKitGTK.JSValueMakeUndefined (ctx);
}
if (value instanceof String) {
- byte[] bytes = null;
- try {
- bytes = ((String)value + '\0').getBytes (CHARSET_UTF8); //$NON-NLS-1$
- } catch (UnsupportedEncodingException e) {
- bytes = Converter.wcsToMbcs (null, (String)value, true);
- }
+ byte[] bytes = ((String)value + '\0').getBytes (StandardCharsets.UTF_8); //$NON-NLS-1$
long /*int*/ stringRef = WebKitGTK.JSStringCreateWithUTF8CString (bytes);
long /*int*/ result = WebKitGTK.JSValueMakeString (ctx, stringRef);
WebKitGTK.JSStringRelease (stringRef);
@@ -2416,23 +2382,14 @@ Object convertToJava (long /*int*/ ctx, long /*int*/ value) {
byte[] bytes = new byte[(int)/*64*/length];
length = WebKitGTK.JSStringGetUTF8CString (string, bytes, length);
WebKitGTK.JSStringRelease (string);
- try {
- /* length-1 is needed below to exclude the terminator character */
- return new String (bytes, 0, (int)/*64*/length - 1, CHARSET_UTF8);
- } catch (UnsupportedEncodingException e) {
- return new String (Converter.mbcsToWcs (null, bytes));
- }
+ /* length-1 is needed below to exclude the terminator character */
+ return new String (bytes, 0, (int)/*64*/length - 1, StandardCharsets.UTF_8);
}
case WebKitGTK.kJSTypeNull:
// FALL THROUGH
case WebKitGTK.kJSTypeUndefined: return null;
case WebKitGTK.kJSTypeObject: {
- byte[] bytes = null;
- try {
- bytes = (PROPERTY_LENGTH + '\0').getBytes (CHARSET_UTF8); //$NON-NLS-1$
- } catch (UnsupportedEncodingException e) {
- bytes = Converter.wcsToMbcs (null, PROPERTY_LENGTH, true);
- }
+ byte[] bytes = (PROPERTY_LENGTH + '\0').getBytes (StandardCharsets.UTF_8); //$NON-NLS-1$
long /*int*/ propertyName = WebKitGTK.JSStringCreateWithUTF8CString (bytes);
long /*int*/ valuePtr = WebKitGTK.JSObjectGetProperty (ctx, value, propertyName, null);
WebKitGTK.JSStringRelease (propertyName);
diff --git a/bundles/org.eclipse.swt/Eclipse SWT WebKit/win32/org/eclipse/swt/browser/WebFrameLoadDelegate.java b/bundles/org.eclipse.swt/Eclipse SWT WebKit/win32/org/eclipse/swt/browser/WebFrameLoadDelegate.java
index 9ba6e2ee66..d94e6fa5e2 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT WebKit/win32/org/eclipse/swt/browser/WebFrameLoadDelegate.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT WebKit/win32/org/eclipse/swt/browser/WebFrameLoadDelegate.java
@@ -11,8 +11,8 @@
package org.eclipse.swt.browser;
-import java.io.*;
import java.net.*;
+import java.nio.charset.*;
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
@@ -202,12 +202,7 @@ int didClearWindowObject (long /*int*/ webView, long /*int*/ context, long /*int
long /*int*/ globalObject = WebKit_win32.JSContextGetGlobalObject (context);
long /*int*/ privateData = ((WebKit)browser.webBrowser).webViewData;
long /*int*/ externalObject = WebKit_win32.JSObjectMake (context, WebKit.ExternalClass, privateData);
- byte[] bytes = null;
- try {
- bytes = (OBJECTNAME_EXTERNAL + '\0').getBytes (WebKit.CHARSET_UTF8);
- } catch (UnsupportedEncodingException e) {
- bytes = (OBJECTNAME_EXTERNAL + '\0').getBytes ();
- }
+ byte[] bytes = (OBJECTNAME_EXTERNAL + '\0').getBytes (StandardCharsets.UTF_8);
long /*int*/ name = WebKit_win32.JSStringCreateWithUTF8CString (bytes);
WebKit_win32.JSObjectSetProperty (context, globalObject, name, externalObject, 0, null);
WebKit_win32.JSStringRelease (name);
diff --git a/bundles/org.eclipse.swt/Eclipse SWT WebKit/win32/org/eclipse/swt/browser/WebKit.java b/bundles/org.eclipse.swt/Eclipse SWT WebKit/win32/org/eclipse/swt/browser/WebKit.java
index f098d8e460..b0e0b9c272 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT WebKit/win32/org/eclipse/swt/browser/WebKit.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT WebKit/win32/org/eclipse/swt/browser/WebKit.java
@@ -13,6 +13,7 @@ package org.eclipse.swt.browser;
import java.io.*;
import java.net.*;
+import java.nio.charset.*;
import java.util.*;
import org.eclipse.swt.*;
@@ -52,7 +53,6 @@ class WebKit extends WebBrowser {
static Callback JSObjectCallAsFunctionProc;
static final int MAX_PROGRESS = 100;
static final String ABOUT_BLANK = "about:blank"; //$NON-NLS-1$
- static final String CHARSET_UTF8 = "UTF-8"; //$NON-NLS-1$
static final String CLASSNAME_EXTERNAL = "External"; //$NON-NLS-1$
static final String EMPTY_STRING = ""; //$NON-NLS-1$
static final String FUNCTIONNAME_CALLJAVA = "callJava"; //$NON-NLS-1$
@@ -325,12 +325,7 @@ static long /*int*/ JSObjectCallAsFunctionProc (long /*int*/ ctx, long /*int*/ f
}
static long /*int*/ JSObjectGetPropertyProc (long /*int*/ ctx, long /*int*/ object, long /*int*/ propertyName, long /*int*/ exception) {
- byte[] bytes = null;
- try {
- bytes = (FUNCTIONNAME_CALLJAVA + '\0').getBytes (CHARSET_UTF8);
- } catch (UnsupportedEncodingException e) {
- bytes = (FUNCTIONNAME_CALLJAVA + '\0').getBytes ();
- }
+ byte[] bytes = (FUNCTIONNAME_CALLJAVA + '\0').getBytes (StandardCharsets.UTF_8);
long /*int*/ name = WebKit_win32.JSStringCreateWithUTF8CString (bytes);
long /*int*/ addr = WebKit_win32.JSObjectCallAsFunctionProc_CALLBACK (WebKit.JSObjectCallAsFunctionProc.getAddress ());
long /*int*/ function = WebKit_win32.JSObjectMakeFunctionWithCallback (ctx, name, addr);
@@ -339,12 +334,7 @@ static long /*int*/ JSObjectGetPropertyProc (long /*int*/ ctx, long /*int*/ obje
}
static long /*int*/ JSObjectHasPropertyProc (long /*int*/ ctx, long /*int*/ object, long /*int*/ propertyName) {
- byte[] bytes = null;
- try {
- bytes = (FUNCTIONNAME_CALLJAVA + '\0').getBytes (CHARSET_UTF8);
- } catch (UnsupportedEncodingException e) {
- bytes = (FUNCTIONNAME_CALLJAVA + '\0').getBytes ();
- }
+ byte[] bytes = (FUNCTIONNAME_CALLJAVA + '\0').getBytes (StandardCharsets.UTF_8);
return WebKit_win32.JSStringIsEqualToUTF8CString (propertyName, bytes);
}
@@ -466,23 +456,14 @@ Object convertToJava (long /*int*/ ctx, long /*int*/ value) {
byte[] bytes = new byte[(int)/*64*/length];
length = WebKit_win32.JSStringGetUTF8CString (string, bytes, length);
WebKit_win32.JSStringRelease (string);
- try {
- /* length-1 is needed below to exclude the terminator character */
- return new String (bytes, 0, (int)/*64*/length - 1, CHARSET_UTF8);
- } catch (UnsupportedEncodingException e) {
- return new String (bytes);
- }
+ /* length-1 is needed below to exclude the terminator character */
+ return new String (bytes, 0, (int)/*64*/length - 1, StandardCharsets.UTF_8);
}
case WebKit_win32.kJSTypeNull:
// FALL THROUGH
case WebKit_win32.kJSTypeUndefined: return null;
case WebKit_win32.kJSTypeObject: {
- byte[] bytes = null;
- try {
- bytes = (PROPERTY_LENGTH + '\0').getBytes (CHARSET_UTF8);
- } catch (UnsupportedEncodingException e) {
- bytes = (PROPERTY_LENGTH + '\0').getBytes ();
- }
+ byte[] bytes = (PROPERTY_LENGTH + '\0').getBytes (StandardCharsets.UTF_8);
long /*int*/ propertyName = WebKit_win32.JSStringCreateWithUTF8CString (bytes);
long /*int*/ valuePtr = WebKit_win32.JSObjectGetProperty (ctx, value, propertyName, null);
WebKit_win32.JSStringRelease (propertyName);
@@ -509,12 +490,7 @@ long /*int*/ convertToJS (long /*int*/ ctx, Object value) {
return WebKit_win32.JSValueMakeNull (ctx);
}
if (value instanceof String) {
- byte[] bytes = null;
- try {
- bytes = ((String)value + '\0').getBytes (CHARSET_UTF8);
- } catch (UnsupportedEncodingException e) {
- bytes = ((String)value + '\0').getBytes ();
- }
+ byte[] bytes = ((String)value + '\0').getBytes (StandardCharsets.UTF_8);
long /*int*/ stringRef = WebKit_win32.JSStringCreateWithUTF8CString (bytes);
long /*int*/ result = WebKit_win32.JSValueMakeString (ctx, stringRef);
WebKit_win32.JSStringRelease (stringRef);
@@ -701,19 +677,10 @@ public boolean execute (String script) {
if (context == 0) {
return false;
}
- byte[] bytes = null;
- try {
- bytes = (script + '\0').getBytes ("UTF-8"); //$NON-NLS-1$
- } catch (UnsupportedEncodingException e) {
- bytes = (script + '\0').getBytes ();
- }
+ byte[] bytes = (script + '\0').getBytes (StandardCharsets.UTF_8); //$NON-NLS-1$
long /*int*/ scriptString = WebKit_win32.JSStringCreateWithUTF8CString (bytes);
if (scriptString == 0) return false;
- try {
- bytes = (getUrl () + '\0').getBytes ("UTF-8"); //$NON-NLS-1$
- } catch (UnsupportedEncodingException e) {
- bytes = (getUrl () + '\0').getBytes ();
- }
+ bytes = (getUrl () + '\0').getBytes (StandardCharsets.UTF_8); //$NON-NLS-1$
long /*int*/ urlString = WebKit_win32.JSStringCreateWithUTF8CString (bytes);
if (urlString == 0) {
WebKit_win32.JSStringRelease (scriptString);

Back to the top