IOUtils is no longer used, using java 1.7 StandardCharsets makes EncodeUtil also obsolete (#1410)

This commit is contained in:
Roland Praml
2018-06-10 22:30:47 +12:00
committed by Rob Bygrave
parent f031bb72b6
commit 8fc6abd480
6 changed files with 12 additions and 227 deletions
@@ -4,7 +4,6 @@ import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.text.TextException;
import io.ebean.text.json.EJson;
import io.ebeaninternal.json.ModifyAwareOwner;
import io.ebeaninternal.util.EncodeUtil;
import io.ebeanservice.docstore.api.mapping.DocPropertyType;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
@@ -15,6 +14,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;
@@ -104,7 +104,7 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase<Map> {
b.setNull(Types.BLOB);
} else {
String rawJson = formatValue(value);
b.setBytes(EncodeUtil.utf8ToBytes(rawJson));
b.setBytes(rawJson.getBytes(StandardCharsets.UTF_8));
}
}
}
@@ -1,7 +1,6 @@
package io.ebeaninternal.server.type;
import io.ebean.text.TextException;
import io.ebeaninternal.util.EncodeUtil;
import io.ebeanservice.docstore.api.mapping.DocPropertyType;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
@@ -14,6 +13,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.sql.Types;
@@ -82,7 +82,7 @@ public abstract class ScalarTypeJsonNode extends ScalarTypeBase<JsonNode> {
dataBind.setNull(Types.BLOB);
} else {
String rawJson = formatValue(value);
dataBind.setBlob(EncodeUtil.utf8ToBytes(rawJson));
dataBind.setBlob(rawJson.getBytes(StandardCharsets.UTF_8));
}
}
}
@@ -6,7 +6,7 @@ import io.ebean.config.Encryptor;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
/**
* Simple AES based encryption and decryption.
@@ -32,15 +32,6 @@ public class SimpleAesEncryptor implements Encryptor {
return key;
}
private byte[] getKeyBytes(String skey) {
try {
return skey.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
private IvParameterSpec getIvParameterSpec(String initialVector) {
return new IvParameterSpec(initialVector.getBytes());
}
@@ -56,7 +47,7 @@ public class SimpleAesEncryptor implements Encryptor {
try {
byte[] keyBytes = getKeyBytes(key);
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
IvParameterSpec iv = getIvParameterSpec(key);
SecretKeySpec sks = new SecretKeySpec(keyBytes, "AES");
@@ -81,7 +72,7 @@ public class SimpleAesEncryptor implements Encryptor {
String key = paddKey(encryptKey);
try {
byte[] keyBytes = getKeyBytes(key);
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
IvParameterSpec iv = getIvParameterSpec(key);
SecretKeySpec sks = new SecretKeySpec(keyBytes, "AES");
@@ -103,12 +94,7 @@ public class SimpleAesEncryptor implements Encryptor {
}
byte[] bytes = decrypt(data, key);
try {
return new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return new String(bytes, StandardCharsets.UTF_8);
}
@Override
@@ -117,13 +103,8 @@ public class SimpleAesEncryptor implements Encryptor {
if (valueFormatValue == null) {
return null;
}
try {
byte[] d = valueFormatValue.getBytes("UTF-8");
return encrypt(d, key);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
byte[] d = valueFormatValue.getBytes(StandardCharsets.UTF_8);
return encrypt(d, key);
}
}
@@ -1,5 +1,6 @@
package io.ebeaninternal.server.util;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
public class Md5 {
@@ -11,7 +12,7 @@ public class Md5 {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest(content.getBytes("UTF-8"));
byte[] digest = md.digest(content.getBytes(StandardCharsets.UTF_8));
return digestToHex(digest);
} catch (Exception e) {
throw new RuntimeException("MD5 hashing failed", e);
@@ -1,98 +0,0 @@
package io.ebeaninternal.util;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
* Utilities for encoding and decoding strings.
*/
public final class EncodeUtil {
private EncodeUtil() {
/* no instances */
}
/**
* URL-encodes the specified UTF-8 string.
*/
public static String urlEncode(String string) {
if (string == null) return null;
try {
return URLEncoder.encode(string, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Support for UTF-8 is mandated by the Java spec", e);
}
}
/**
* URL-decodes the specified string as a UTF-8 string.
*/
public static String urlDecode(String string) {
if (string == null) return null;
try {
return URLDecoder.decode(string, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Support for UTF-8 is mandated by the Java spec", e);
}
}
/**
* Returns the bytes corresponding to the specified ASCII string.
*/
public static byte[] asciiToBytes(String string) {
if (string == null) return null;
try {
return string.getBytes("US-ASCII");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Support for US-ASCII is mandated by the Java spec", e);
}
}
/**
* Returns the ASCII string corresponding to the specified bytes.
*/
public static String bytesToAscii(byte[] data) {
try {
return new String(data, "US-ASCII");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Support for US-ASCII is mandated by the Java spec", e);
}
}
/**
* Returns the bytes corresponding to the specified UTF-8 string.
*/
public static byte[] utf8ToBytes(String string) {
if (string == null) return null;
try {
return string.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Support for UTF-8 is mandated by the Java spec", e);
}
}
/**
* Returns the UTF-8 string corresponding to the specified bytes.
*/
public static String bytesToUtf8(byte[] data) {
try {
return new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Support for UTF-8 is mandated by the Java spec", e);
}
}
/**
* Returns the UTF-8 string corresponding to the specified bytes.
*/
public static String decodeBytes(byte[] data, String encoding) {
try {
return new String(data, encoding);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Error decoding bytes with " + encoding, e);
}
}
}
@@ -1,99 +0,0 @@
package io.ebeaninternal.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
/**
* Utilities for IO.
*/
public class IOUtils {
/**
* Reads the entire contents of the specified input stream and returns them
* as a byte array.
*/
public static byte[] read(InputStream in) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
pump(in, buffer);
return buffer.toByteArray();
}
/**
* Reads the entire contents of the specified input stream and returns them
* as an ASCII string.
*/
public static String readAscii(InputStream in) throws IOException {
return EncodeUtil.bytesToAscii(read(in));
}
/**
* Reads the entire contents of the specified input stream and returns them
* as UTF-8 string.
*/
public static String readUtf8(InputStream in) throws IOException {
return EncodeUtil.bytesToUtf8(read(in));
}
/**
* Reads the entire contents of the specified input stream and returns them
* as a string using the encoding supplied.
*/
public static String readEncoded(InputStream in, String encoding) throws IOException {
return EncodeUtil.decodeBytes(read(in), encoding);
}
/**
* Read the entire contents from the reader returning as a String.
*/
public static String read(Reader reader) throws IOException {
StringBuilder sb = new StringBuilder();
try {
char[] buffer = new char[4096];
for (; ; ) {
int len = reader.read(buffer);
if (len < 0) {
break;
}
sb.append(buffer, 0, len);
}
} finally {
reader.close();
}
return sb.toString();
}
/**
* Reads data from the specified input stream and copies it to the specified
* output stream, until the input stream is at EOF. Both streams are then
* closed.
*
* @throws IOException if the input or output stream is <code>null</code>
*/
public static void pump(InputStream in, OutputStream out) throws IOException {
if (in == null) throw new IOException("Input stream is null");
if (out == null) throw new IOException("Output stream is null");
try {
try {
byte[] buffer = new byte[4096];
for (; ; ) {
int bytes = in.read(buffer);
if (bytes < 0) {
break;
}
out.write(buffer, 0, bytes);
}
} finally {
in.close();
}
} finally {
out.close();
}
}
}