Skip to content

Commit

Permalink
Use StandardCharsets.UTF_8 where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
anistor authored and tristantarrant committed Sep 13, 2018
1 parent b074b92 commit 5f54196
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.infinispan.cli;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
Expand All @@ -19,7 +21,7 @@ public void testStandardInput() throws Exception {
InputStream in = System.in;
PrintStream out = System.out;
try {
bais = new ByteArrayInputStream("version;\n".getBytes("UTF-8"));
bais = new ByteArrayInputStream("version;\n".getBytes(UTF_8));
baos = new ByteArrayOutputStream();
System.setIn(bais);
System.setOut(new PrintStream(baos));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static java.nio.charset.StandardCharsets.UTF_8;

import org.infinispan.cli.interpreter.Interpreter;
import org.infinispan.cli.interpreter.logging.Log;
import org.infinispan.commons.configuration.ClassWhiteList;
import org.infinispan.util.logging.LogFactory;
Expand All @@ -16,7 +15,7 @@
*/
@MetaInfServices(org.infinispan.cli.interpreter.codec.Codec.class)
public class MemcachedCodec extends AbstractCodec {
private static final Log log = LogFactory.getLog(Interpreter.class, Log.class);
private static final Log log = LogFactory.getLog(MemcachedCodec.class, Log.class);

public MemcachedCodec() {
try {
Expand Down Expand Up @@ -72,7 +71,5 @@ public Object decodeValue(Object value) {
} else {
return null;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.function.Predicate;
import java.util.stream.Stream;

Expand Down Expand Up @@ -178,7 +179,7 @@ public interface HotRodConstants {
*/
@Deprecated
byte CLIENT_INTELLIGENCE_HASH_DISTRIBUTION_AWARE = 0x03;
Charset HOTROD_STRING_CHARSET = Charset.forName("UTF-8");
Charset HOTROD_STRING_CHARSET = StandardCharsets.UTF_8;

byte[] DEFAULT_CACHE_NAME_BYTES = new byte[]{};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.infinispan.client.hotrod.stress;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.net.SocketAddress;

import org.infinispan.client.hotrod.RemoteCache;
Expand Down Expand Up @@ -42,7 +44,7 @@ public void testConsistentHashPerf() throws Exception {
byte[][] keys = new byte[NUM_KEYS][];

for (int i = 0; i < NUM_KEYS; i++) {
keys[i] = String.valueOf(i).getBytes("UTF-8");
keys[i] = String.valueOf(i).getBytes(UTF_8);
}

SocketAddress aServer = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.infinispan.commons.dataconversion;

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

import org.infinispan.commons.logging.Log;
import org.infinispan.commons.logging.LogFactory;
Expand All @@ -14,19 +14,20 @@ public class UTF8Encoder implements Encoder {

private static final Log log = LogFactory.getLog(UTF8Encoder.class);

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

public static final UTF8Encoder INSTANCE = new UTF8Encoder();

@Override
public Object toStorage(Object content) {
if (content instanceof String) return String.class.cast(content).getBytes(CHARSET_UTF8);
if (content instanceof String) {
return String.class.cast(content).getBytes(UTF_8);
}

throw log.unsupportedContent(content);
}

@Override
public Object fromStorage(Object stored) {
return new String((byte[]) stored, CHARSET_UTF8);
return new String((byte[]) stored, UTF_8);
}

@Override
Expand All @@ -43,5 +44,4 @@ public MediaType getStorageFormat() {
public short id() {
return EncoderIds.UTF8;
}

}
3 changes: 2 additions & 1 deletion commons/src/main/java/org/infinispan/commons/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.FileLock;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.text.NumberFormat;
Expand Down Expand Up @@ -424,7 +425,7 @@ public static byte[] readStream(InputStream is) throws IOException {
*/
public static String read(InputStream is) throws IOException {
try {
final Reader reader = new InputStreamReader(is, "UTF-8");
final Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
StringWriter writer = new StringWriter();
char[] buf = new char[1024];
int len;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.infinispan.commons.hash;

import java.io.ObjectInput;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Set;

import org.infinispan.commons.marshall.Ids;
Expand Down Expand Up @@ -36,8 +36,6 @@ public static MurmurHash3Old getInstance() {
private MurmurHash3Old() {
}

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

static class State {
long h1;
long h2;
Expand Down Expand Up @@ -391,7 +389,7 @@ public int hash(Object o) {
else if (o instanceof long[])
return hash((long[]) o);
else if (o instanceof String)
return hash(((String) o).getBytes(UTF8));
return hash(((String) o).getBytes(StandardCharsets.UTF_8));
else
return hash(o.hashCode());
}
Expand All @@ -415,7 +413,7 @@ public static class Externalizer extends NoStateExternalizer<MurmurHash3Old> {
@Override
@SuppressWarnings("unchecked")
public Set<Class<? extends MurmurHash3Old>> getTypeClasses() {
return Util.<Class<? extends MurmurHash3Old>>asSet(MurmurHash3Old.class);
return Util.asSet(MurmurHash3Old.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static org.infinispan.profiling.testinternals.Generator.getRandomByteArray;
import static org.infinispan.profiling.testinternals.Generator.getRandomString;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -225,8 +225,6 @@ private String padDouble(double d) {

class SuperFastHash implements Hash {

public static final Charset CHARSET_UTF8 = Charset.forName("UTF-8");

@Override
public int hash(int hashcode) {
byte[] b = new byte[4];
Expand All @@ -242,7 +240,7 @@ public int hash(Object o) {
if (o instanceof byte[])
return hash((byte[]) o);
else if (o instanceof String)
return hash(((String) o).getBytes(CHARSET_UTF8));
return hash(((String) o).getBytes(StandardCharsets.UTF_8));
else
return hash(o.hashCode());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void testStringKeysAndByteArrayValue() throws Exception {
public void testIntegerKeysAndByteArrayValue() throws Exception {
String integerKeyType = "application/x-java-object; type=java.lang.Integer";
byte[] value = {12};
byte[] otherValue = "random".getBytes("UTF-8");
byte[] otherValue = "random".getBytes(UTF_8);

// Write <Integer, byte[]> via Hot Rod (the HR client is configured with the default marshaller)
defaultMarshalledRemoteCache.put(10, value);
Expand Down Expand Up @@ -360,7 +360,7 @@ private String getEndpoint(String cache) {

private String asString(Object content) throws Exception {
if (content instanceof byte[]) {
return new String((byte[]) content, "UTF-8");
return new String((byte[]) content, UTF_8);
}
return content.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
Expand Down Expand Up @@ -360,7 +361,7 @@ public Flowable<K> publishKeys(Predicate<? super K> filter) {
Channel ch = bootstrap.connect(configuration.host(), configuration.port()).awaitUninterruptibly().channel().pipeline().addLast(
new HttpObjectAggregator(maxContentLength), handler).channel();
ch.writeAndFlush(get).sync().channel().closeFuture().sync();
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteBufInputStream((handler.getResponse()).content()), "UTF-8"));
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteBufInputStream((handler.getResponse()).content()), StandardCharsets.UTF_8));
// Response can't be null now
return new KeyValuePair<>(handler.getResponse(), reader);
} catch (Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.Serializable;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import org.apache.http.HttpResponse;
Expand Down Expand Up @@ -430,7 +431,7 @@ public void testNonExistentCache() throws Exception {
@Test
public void testByteArrayStorage() throws Exception {
final String KEY_Z = "z";
byte[] data = "data".getBytes("UTF-8");
byte[] data = "data".getBytes(StandardCharsets.UTF_8);

ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bout);
Expand Down

0 comments on commit 5f54196

Please sign in to comment.