Skip to content

Commit

Permalink
Avoid some charset lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
scop authored and bstansberry committed Feb 14, 2014
1 parent 02ba779 commit 7253bd9
Show file tree
Hide file tree
Showing 50 changed files with 133 additions and 161 deletions.
5 changes: 2 additions & 3 deletions cli/src/main/java/org/jboss/as/cli/impl/CLIVaultReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

package org.jboss.as.cli.impl;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
Expand All @@ -38,7 +38,6 @@
class CLIVaultReader {

private static final Pattern VAULT_PATTERN = Pattern.compile("VAULT::.*::.*::.*");
private static final Charset CHARSET = Charset.forName("UTF-8");

private volatile SecurityVault vault;

Expand Down Expand Up @@ -70,7 +69,7 @@ private char[] getValue(String vaultString) throws SecurityVaultException {
byte[] sharedKey = null;
if (tokens.length > 2) {
// only in case of conversion of old vault implementation
sharedKey = tokens[3].getBytes(CHARSET);
sharedKey = tokens[3].getBytes(StandardCharsets.UTF_8);
}
return vault.retrieve(tokens[1], tokens[2], sharedKey);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import io.undertow.server.HttpServerExchange;
import io.undertow.util.HeaderMap;
Expand Down Expand Up @@ -75,7 +76,7 @@ private static byte[] getResponseBytes(final ModelNode modelNode, final Operatio
return baos.toByteArray();
} else {
String json = modelNode.toJSONString(!operationParameter.isPretty());
return json.getBytes(Common.UTF_8);
return json.getBytes(StandardCharsets.UTF_8);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
import io.undertow.util.HeaderMap;
import io.undertow.util.StatusCodes;

import static java.nio.charset.StandardCharsets.UTF_8;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
Expand All @@ -65,8 +65,6 @@ public class LogoutHandler implements HttpHandler {
private static final String DIGEST = "DIGEST";
private static final String MECHANISM = "mechanism";

private static final Charset UTF_8 = Charset.forName("UTF-8");

private final DigestAuthenticationMechanism digestMechanism;
private final DigestAuthenticationMechanism fakeRealmdigestMechanism;
private final BasicAuthenticationMechanism basicMechanism;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package org.jboss.as.domain.http.server.security;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.jboss.as.domain.http.server.HttpServerLogger.ROOT_LOGGER;
import static org.jboss.as.domain.http.server.HttpServerMessages.MESSAGES;
import static org.jboss.as.domain.management.RealmConfigurationConstants.DIGEST_PLAIN_TEXT;
Expand All @@ -34,7 +35,6 @@

import java.io.IOException;
import java.net.InetAddress;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
Expand Down Expand Up @@ -63,7 +63,6 @@
*/
public class RealmIdentityManager implements IdentityManager {

private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final ThreadLocal<ThreadLocalStore> requestSpecific = new ThreadLocal<ThreadLocalStore>();

static void setRequestSpecific(final AuthMechanism mechanism, final InetAddress clientAddress) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -147,7 +147,7 @@ protected void loadAsRequired() throws IOException {
protected void load() throws IOException {
ROOT_LOGGER.debugf("Reloading properties file '%s'", propertiesFile.getAbsolutePath());
Properties props = new Properties();
InputStreamReader is = new InputStreamReader(new FileInputStream(propertiesFile), Charset.forName("UTF-8"));
InputStreamReader is = new InputStreamReader(new FileInputStream(propertiesFile), StandardCharsets.UTF_8);
try {
props.load(is);
} finally {
Expand All @@ -169,7 +169,7 @@ public synchronized void persistProperties() throws IOException {
// Shouldn't be so bad - it's a small file
List<String> content = readFile(propertiesFile);

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(propertiesFile), "UTF8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(propertiesFile), StandardCharsets.UTF_8));

try {
for (String line : content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

import static java.lang.System.getProperty;
Expand Down Expand Up @@ -60,7 +61,7 @@ private File createPropertyFile(String filename, String mode) throws IOException
File propertyUserFile = new File(domainDir, filename);
propertyUserFile.createNewFile();
propertyUserFile.deleteOnExit();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(propertyUserFile),"UTF8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(propertyUserFile), StandardCharsets.UTF_8));
try {
Properties domainPropeties = new Properties();
domainPropeties.setProperty(USER_NAME,"mypassword");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -115,14 +115,14 @@ protected void checkHandlerRuntimeFailureMetrics(ModelNode handler, int maxFailu
Assert.assertEquals(disabled, handler.get(AuditLogHandlerResourceDefinition.DISABLED_DUE_TO_FAILURE.getName()).asBoolean());
}

protected String stripSyslogHeader(byte[] bytes) throws UnsupportedEncodingException {
String s = new String(bytes, "utf-8");
protected String stripSyslogHeader(byte[] bytes) {
String s = new String(bytes, StandardCharsets.UTF_8);
int i = s.indexOf(" - - ");
return s.substring(i + 6);
}

protected ModelNode getSyslogRecord(byte[] bytes) throws UnsupportedEncodingException {
String msg = new String(bytes, "utf-8");
protected ModelNode getSyslogRecord(byte[] bytes) {
String msg = new String(bytes, StandardCharsets.UTF_8);
return getSyslogRecord(msg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.rmi.RemoteException;
import java.security.AccessController;
import java.security.Principal;
Expand Down Expand Up @@ -267,7 +268,7 @@ public OutputStream _invoke(final String opName, final InputStream in, final Res
if ( incomingName != null && incomingName.length > 0) {
//we have an identity token, which is a trust based mechanism
if (incomingName.length > 0) {
String name = new String(incomingName, "UTF-8");
String name = new String(incomingName, StandardCharsets.UTF_8);
int domainIndex = name.indexOf('@');
if (domainIndex > 0)
name = name.substring(0, domainIndex);
Expand All @@ -282,13 +283,13 @@ public OutputStream _invoke(final String opName, final InputStream in, final Res
final byte[] username = sasCurrent.get_incoming_username();
final byte[] incomingPassword = sasCurrent.get_incoming_password();
if(username.length > 0) {
String name = new String(username, "UTF-8");
String name = new String(username, StandardCharsets.UTF_8);
int domainIndex = name.indexOf('@');
if (domainIndex > 0) {
name = name.substring(0, domainIndex);
}
principal = new SimplePrincipal(name);
credential = new String(incomingPassword, "UTF-8").toCharArray();
credential = new String(incomingPassword, StandardCharsets.UTF_8).toCharArray();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package org.jboss.as.host.controller;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.HOST;
import static org.jboss.as.host.controller.HostControllerLogger.ROOT_LOGGER;
import static org.jboss.as.host.controller.HostControllerMessages.MESSAGES;
Expand All @@ -35,7 +36,6 @@
import javax.security.sasl.RealmCallback;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Collection;
Expand Down Expand Up @@ -82,8 +82,6 @@
*/
public class ServerInventoryImpl implements ServerInventory {

private static final Charset UTF_8 = Charset.forName("UTF-8");

/** The managed servers. */
private final ConcurrentMap<String, ManagedServer> servers = new ConcurrentHashMap<String, ManagedServer>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
Expand Down Expand Up @@ -318,7 +319,7 @@ else if(LOCATION_EU.equals(location)) {
HttpURLConnection request=makeRequest("PUT", bucket, "", null, headers);
if(body != null) {
request.setDoOutput(true);
request.getOutputStream().write(body.getBytes("UTF-8"));
request.getOutputStream().write(body.getBytes(StandardCharsets.UTF_8));
}
return new Response(request);
}
Expand Down
14 changes: 3 additions & 11 deletions jacorb/src/main/java/org/jboss/as/jacorb/csiv2/CSIv2Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
package org.jboss.as.jacorb.csiv2;

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

import org.ietf.jgss.GSSException;
import org.ietf.jgss.Oid;
Expand Down Expand Up @@ -817,17 +817,9 @@ public static void toString(CompoundSecMech securityMech, StringBuilder builder)
builder.append("AS_ContextSec[");

builder.append("client_authentication_mech: ");
try {
builder.append(new String(asMech.client_authentication_mech, "UTF-8"));
} catch (UnsupportedEncodingException e) {
builder.append(e.getMessage());
}
builder.append(new String(asMech.client_authentication_mech, StandardCharsets.UTF_8));
builder.append(", target_name: ");
try {
builder.append(new String(asMech.target_name, "UTF-8"));
} catch (UnsupportedEncodingException e) {
builder.append(e.getMessage());
}
builder.append(new String(asMech.target_name, StandardCharsets.UTF_8));
builder.append(", target_requires: ");
builder.append(asMech.target_requires);
builder.append(", target_supports: ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package org.jboss.as.jacorb.csiv2;

import java.nio.charset.StandardCharsets;
import java.security.Principal;

import org.jacorb.orb.MinorCodes;
Expand Down Expand Up @@ -138,7 +139,7 @@ public void send_request(ClientRequestInfo ri) {
if (name.indexOf('@') < 0) {
name += "@default"; // hardcoded (REVISIT!)
}
byte[] principalName = name.getBytes("UTF-8");
byte[] principalName = name.getBytes(StandardCharsets.UTF_8);

// encode the principal name as mandated by RFC2743.
byte[] encodedName = CSIv2Util.encodeGssExportedName(principalName);
Expand Down Expand Up @@ -170,12 +171,12 @@ public void send_request(ClientRequestInfo ri) {
if (name.indexOf('@') < 0) {
byte[] decodedTargetName =
CSIv2Util.decodeGssExportedName(encodedTargetName);
String targetName = new String(decodedTargetName, "UTF-8");
String targetName = new String(decodedTargetName, StandardCharsets.UTF_8);
name += "@" + targetName; // "@default"
}
byte[] username = name.getBytes("UTF-8");
byte[] username = name.getBytes(StandardCharsets.UTF_8);
// I don't know why there is not a better way to go from char[] -> byte[].
byte[] password = serverPassword.getBytes("UTF-8");
byte[] password = serverPassword.getBytes(StandardCharsets.UTF_8);

// create authentication token
InitialContextToken authenticationToken = new InitialContextToken(username, password, encodedTargetName);
Expand All @@ -198,8 +199,6 @@ public void send_request(ClientRequestInfo ri) {
ServiceContext sc = new ServiceContext(sasContextId, codec.encode_value(any));
ri.add_request_service_context(sc, true /*replace existing context*/);
}
} catch (java.io.UnsupportedEncodingException e) {
throw JacORBMessages.MESSAGES.unexpectedException(e);
} catch (InvalidTypeForEncoding e) {
throw JacORBMessages.MESSAGES.unexpectedException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package org.jboss.as.jacorb.csiv2;

import java.nio.charset.StandardCharsets;
import java.security.Principal;

import org.jacorb.orb.MinorCodes;
Expand Down Expand Up @@ -112,22 +113,22 @@ public void send_request(ClientRequestInfo ri) {
String name = p.getName();
if (name.indexOf('@') < 0) {
byte[] decodedTargetName = CSIv2Util.decodeGssExportedName(encodedTargetName);
String targetName = new String(decodedTargetName, "UTF-8");
String targetName = new String(decodedTargetName, StandardCharsets.UTF_8);
name += "@" + targetName; // "@default"
}
byte[] username = name.getBytes("UTF-8");
byte[] username = name.getBytes(StandardCharsets.UTF_8);
// I don't know why there is not a better way to go from char[] -> byte[].

Object credential = SecurityActions.getCredential();
byte[] password = {};
if (credential instanceof char[]) {
String tmp = new String((char[]) credential);
password = tmp.getBytes("UTF-8");
password = tmp.getBytes(StandardCharsets.UTF_8);
} else if (credential instanceof byte[])
password = (byte[]) credential;
else if (credential != null) {
String tmp = credential.toString();
password = tmp.getBytes("UTF-8");
password = tmp.getBytes(StandardCharsets.UTF_8);
}

// create authentication token.
Expand All @@ -152,8 +153,6 @@ else if (credential != null) {
ri.add_request_service_context(sc, true /*replace existing context*/);
}
}
} catch (java.io.UnsupportedEncodingException e) {
throw JacORBMessages.MESSAGES.unexpectedException(e);
} catch (org.omg.IOP.CodecPackage.InvalidTypeForEncoding e) {
throw JacORBMessages.MESSAGES.unexpectedException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Hashtable;
import java.util.Vector;

Expand Down Expand Up @@ -402,7 +403,7 @@ private String getStringifiedIor(String url) throws NamingException {
URL u = new URL(url);
in = u.openStream();
if (in != null) {
BufferedReader bufin = new BufferedReader(new InputStreamReader(in, "8859_1"));
BufferedReader bufin = new BufferedReader(new InputStreamReader(in, StandardCharsets.ISO_8859_1));
String str;
while ((str = bufin.readLine()) != null) {
if (str.startsWith("IOR:")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
package org.jboss.as.jacorb.rmi.ir;

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

import org.jboss.as.jacorb.JacORBMessages;
import org.omg.CORBA.ContainedPackage.Description;
Expand Down Expand Up @@ -131,10 +131,6 @@ public void move(Container new_container,
* "repository_name:".
*/
protected byte[] getObjectId() {
try {
return (getRepository().getObjectIdPrefix() + id).getBytes("UTF-8");
} catch (UnsupportedEncodingException ex) {
throw JacORBMessages.MESSAGES.unsupportedUTF8Encoding();
}
return (getRepository().getObjectIdPrefix() + id).getBytes(StandardCharsets.UTF_8);
}
}

0 comments on commit 7253bd9

Please sign in to comment.