Skip to content

Commit

Permalink
Avoid some charset lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
scop committed Feb 13, 2014
1 parent 73e2355 commit e154ad7
Show file tree
Hide file tree
Showing 42 changed files with 143 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
Expand Down Expand Up @@ -119,7 +120,7 @@ public boolean runExample() throws Exception

private static void sendFrame(Socket socket, String data) throws Exception
{
byte[] bytes = data.getBytes("UTF-8");
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
OutputStream outputStream = socket.getOutputStream();
for (int i = 0; i < bytes.length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
Expand Down Expand Up @@ -126,7 +127,7 @@ public boolean runExample() throws Exception

private static void sendFrame(Socket socket, String data) throws Exception
{
byte[] bytes = data.getBytes("UTF-8");
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
OutputStream outputStream = socket.getOutputStream();
for (int i = 0; i < bytes.length; i++)
{
Expand All @@ -144,7 +145,7 @@ private static String receiveFrame(Socket socket) throws Exception
byte[] data = new byte[size];
System.arraycopy(buffer, 0, data, 0, size);

String frame = new String(data, "UTF-8");
String frame = new String(data, StandardCharsets.UTF_8);
return frame;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.hornetq.jms.example;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;

Expand Down Expand Up @@ -39,7 +40,6 @@
import org.hornetq.common.example.DummyXid;
import org.hornetq.common.example.HornetQExample;
import org.hornetq.utils.UUIDGenerator;
import io.netty.util.CharsetUtil;

/**
* A simple JMS example showing how to administer un-finished transactions.
Expand Down Expand Up @@ -103,7 +103,7 @@ public boolean runExample() throws Exception

// Step 12. create a transaction
Xid xid1 =
new DummyXid("xa-example1".getBytes(CharsetUtil.ISO_8859_1), 1, UUIDGenerator.getInstance()
new DummyXid("xa-example1".getBytes(StandardCharsets.ISO_8859_1), 1, UUIDGenerator.getInstance()
.generateStringUUID()
.getBytes());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.hornetq.jms.example;

import java.nio.charset.StandardCharsets;

import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
Expand All @@ -27,7 +29,6 @@
import org.hornetq.common.example.DummyXid;
import org.hornetq.common.example.HornetQExample;
import org.hornetq.utils.UUIDGenerator;
import io.netty.util.CharsetUtil;

/**
* A simple JMS example showing the usage of XA support in JMS.
Expand Down Expand Up @@ -86,7 +87,7 @@ public boolean runExample() throws Exception

// Step 12. create a transaction
Xid xid1 =
new DummyXid("xa-example1".getBytes(CharsetUtil.US_ASCII), 1, UUIDGenerator.getInstance()
new DummyXid("xa-example1".getBytes(StandardCharsets.US_ASCII), 1, UUIDGenerator.getInstance()
.generateStringUUID()
.getBytes());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.hornetq.jms.example;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

import javax.jms.JMSException;
Expand All @@ -32,7 +33,6 @@
import org.hornetq.common.example.DummyXid;
import org.hornetq.common.example.HornetQExample;
import org.hornetq.utils.UUIDGenerator;
import io.netty.util.CharsetUtil;

/**
* A simple JMS example showing the usage of XA support in JMS.
Expand Down Expand Up @@ -94,7 +94,7 @@ public boolean runExample() throws Exception

// Step 12. create a transaction
Xid xid1 =
new DummyXid("xa-example1".getBytes(CharsetUtil.UTF_8), 1, UUIDGenerator.getInstance()
new DummyXid("xa-example1".getBytes(StandardCharsets.UTF_8), 1, UUIDGenerator.getInstance()
.generateStringUUID()
.getBytes());

Expand Down
42 changes: 8 additions & 34 deletions hornetq-commons/src/main/java/org/hornetq/utils/Base64.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
package org.hornetq.utils;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* <p>Encodes and decodes to and from Base64 notation.</p>
* <p>Homepage: <a href="http://iharder.net/base64">http://iharder.net/base64</a>.</p>
Expand Down Expand Up @@ -162,7 +165,7 @@ public class Base64
/**
* Preferred encoding.
*/
private static final String PREFERRED_ENCODING = "UTF-8";
private static final Charset PREFERRED_ENCODING = StandardCharsets.UTF_8;

// I think I end up not using the BAD_ENCODING indicator.
// private final static byte BAD_ENCODING = -9; // Indicates error in encoding
Expand Down Expand Up @@ -1121,14 +1124,7 @@ public static String encodeObject(final java.io.Serializable serializableObject,
} // end finally

// Return value according to relevant encoding.
try
{
return new String(baos.toByteArray(), Base64.PREFERRED_ENCODING);
} // end try
catch (java.io.UnsupportedEncodingException uue)
{
return new String(baos.toByteArray());
} // end catch
return new String(baos.toByteArray(), Base64.PREFERRED_ENCODING);

} // end encode

Expand Down Expand Up @@ -1257,14 +1253,7 @@ public static String encodeBytes(final byte[] source, final int off, final int l
} // end finally

// Return value according to relevant encoding.
try
{
return new String(baos.toByteArray(), Base64.PREFERRED_ENCODING);
} // end try
catch (java.io.UnsupportedEncodingException uue)
{
return new String(baos.toByteArray());
} // end catch
return new String(baos.toByteArray(), Base64.PREFERRED_ENCODING);
} // end if: compress

// Else, don't compress. Better not to use streams at all then.
Expand Down Expand Up @@ -1301,14 +1290,7 @@ public static String encodeBytes(final byte[] source, final int off, final int l
} // end if: some padding needed

// Return value according to relevant encoding.
try
{
return new String(outBuff, 0, e, Base64.PREFERRED_ENCODING);
} // end try
catch (java.io.UnsupportedEncodingException uue)
{
return new String(outBuff, 0, e);
} // end catch
return new String(outBuff, 0, e, Base64.PREFERRED_ENCODING);

} // end else: don't compress

Expand Down Expand Up @@ -1492,15 +1474,7 @@ public static byte[] decode(final String s)
*/
public static byte[] decode(final String s, final int options)
{
byte[] bytes;
try
{
bytes = s.getBytes(Base64.PREFERRED_ENCODING);
} // end try
catch (java.io.UnsupportedEncodingException uee)
{
bytes = s.getBytes();
} // end catch
byte[] bytes = s.getBytes(Base64.PREFERRED_ENCODING);
// </change>

// Decode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -89,7 +90,6 @@
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.ssl.SslHandler;
import io.netty.util.AttributeKey;
import io.netty.util.CharsetUtil;
import io.netty.util.ResourceLeakDetector;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GlobalEventExecutor;
Expand Down Expand Up @@ -1163,7 +1163,7 @@ private static String base64(byte[] data)
{
ByteBuf encodedData = Unpooled.wrappedBuffer(data);
ByteBuf encoded = Base64.encode(encodedData);
String encodedString = encoded.toString(CharsetUtil.UTF_8);
String encodedString = encoded.toString(StandardCharsets.UTF_8);
encoded.release();
return encodedString;
}
Expand Down Expand Up @@ -1198,7 +1198,7 @@ public static String createExpectedResponse(final String magicNumber, final Stri
final String concat = secretKey + magicNumber;
final MessageDigest digest = MessageDigest.getInstance("SHA1");

digest.update(concat.getBytes("UTF-8"));
digest.update(concat.getBytes(StandardCharsets.UTF_8));
final byte[] bytes = digest.digest();
return encodeBytes(bytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicLong;
import java.util.zip.Deflater;
Expand All @@ -39,7 +40,7 @@ public class CompressionUtilTest extends Assert
public void testDeflaterReader() throws Exception
{
String inputString = "blahblahblah??blahblahblahblahblah??blablahblah??blablahblah??bla";
byte[] input = inputString.getBytes("UTF-8");
byte[] input = inputString.getBytes(StandardCharsets.UTF_8);

ByteArrayInputStream inputStream = new ByteArrayInputStream(input);

Expand Down Expand Up @@ -77,7 +78,7 @@ public void testDeflaterReader() throws Exception
public void testDeflaterReader2() throws Exception
{
String inputString = "blahblahblah??blahblahblahblahblah??blablahblah??blablahblah??bla";
byte[] input = inputString.getBytes("UTF-8");
byte[] input = inputString.getBytes(StandardCharsets.UTF_8);

ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
AtomicLong counter = new AtomicLong(0);
Expand Down Expand Up @@ -119,7 +120,7 @@ public void testDeflaterReader2() throws Exception
public void testInflaterReader() throws Exception
{
String inputString = "blahblahblah??blahblahblahblahblah??blablahblah??blablahblah??bla";
byte[] input = inputString.getBytes("UTF-8");
byte[] input = inputString.getBytes(StandardCharsets.UTF_8);
byte[] output = new byte[30];
Deflater compresser = new Deflater();
compresser.setInput(input);
Expand Down Expand Up @@ -158,7 +159,7 @@ public void testInflaterReader() throws Exception
public void testInflaterWriter() throws Exception
{
String inputString = "blahblahblah??blahblahblahblahblah??blablahblah??blablahblah??bla";
byte[] input = inputString.getBytes("UTF-8");
byte[] input = inputString.getBytes(StandardCharsets.UTF_8);
byte[] output = new byte[30];
Deflater compresser = new Deflater();
compresser.setInput(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.hornetq.core.protocol.proton;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.EnumSet;

import io.netty.channel.ChannelPipeline;
Expand Down Expand Up @@ -110,7 +111,7 @@ public void addChannelHandlers(ChannelPipeline pipeliner)
@Override
public boolean isProtocol(byte[] array)
{
String startFrame = new String(array);
String startFrame = new String(array, StandardCharsets.US_ASCII);
return startFrame.startsWith("AMQP");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package org.hornetq.core.protocol.stomp;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -90,13 +90,7 @@ public StompFrame getFrame()

if (body != null)
{
try
{
frame.setByteBody(body.getBytes("UTF-8"));
}
catch (UnsupportedEncodingException e)
{
}
frame.setByteBody(body.getBytes(StandardCharsets.UTF_8));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,9 @@ public interface HornetQStompProtocolMessageBundle
@Message(id = 339035, value = "Must specify the subscription's id or the destination you are unsubscribing from", format = Message.Format.MESSAGE_FORMAT)
HornetQStompException needIDorDestination();

@Message(id = 339036, value = "Encoding error.", format = Message.Format.MESSAGE_FORMAT)
HornetQStompException encodingError();

@Message(id = 339037, value = "Must specify the subscription's id", format = Message.Format.MESSAGE_FORMAT)
HornetQStompException needSubscriptionID();

@Message(id = 339038, value = "Encoding error", format = Message.Format.MESSAGE_FORMAT)
HornetQStompException encodingErrorWithCause(@Cause Exception e);

@Message(id = 339039, value = "No id header in ACK/NACK frame.", format = Message.Format.MESSAGE_FORMAT)
HornetQStompException noIDInAck();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package org.hornetq.core.protocol.stomp;

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


public class SimpleBytes
Expand All @@ -28,13 +28,13 @@ public SimpleBytes(int initCapacity)
index = 0;
}

public String getString() throws UnsupportedEncodingException
public String getString()
{
if (index == 0) return "";
byte[] realData = new byte[index];
System.arraycopy(contents, 0, realData, 0, realData.length);

return new String(realData, "UTF-8");
return new String(realData, StandardCharsets.UTF_8);
}

public void reset()
Expand Down
Loading

0 comments on commit e154ad7

Please sign in to comment.