* it was)
*/
public byte[] encrypt(String data) throws SSLException {
- try {
- return encrypt(data.getBytes("UTF8"));
- } catch (UnsupportedEncodingException e) {
- // UTF-8 is required in all confirm JVMs
- e.printStackTrace();
- return null;
- }
+ return encrypt(StringUtils.getBytes(data));
}
/**
* it was)
*/
public String encrypt64(String data) throws SSLException {
- try {
- return encrypt64(data.getBytes("UTF8"));
- } catch (UnsupportedEncodingException e) {
- // UTF-8 is required in all confirm JVMs
- e.printStackTrace();
- return null;
- }
+ return encrypt64(StringUtils.getBytes(data));
}
/**
throws IOException {
FileOutputStream out = new FileOutputStream(file);
try {
- out.write(content.getBytes("UTF-8"));
+ out.write(StringUtils.getBytes(content));
} finally {
out.close();
}
static public String getMd5Hash(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
- md.update(input.getBytes("UTF-8"));
+ md.update(getBytes(input));
byte byteData[] = md.digest();
StringBuffer hexString = new StringBuffer();
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
return input;
- } catch (UnsupportedEncodingException e) {
- return input;
}
}
*/
public static String zip64s(String data) throws IOException {
try {
- return zip64(data.getBytes("UTF-8"));
+ return zip64(getBytes(data));
} catch (UnsupportedEncodingException e) {
// All conforming JVM are required to support UTF-8
e.printStackTrace();
*/
public static byte[] unzip64(String data) throws IOException {
InputStream in = new Base64InputStream(new ByteArrayInputStream(
- data.getBytes("UTF-8")), false);
+ getBytes(data)), false);
try {
in = new GZIPInputStream(in);
return IOUtils.toByteArray(in);
* in case of I/O errors
*/
public static String base64(String data) throws IOException {
- return base64(data.getBytes("UTF-8"));
+ return base64(getBytes(data));
}
/**
*/
public static byte[] unbase64(String data) throws IOException {
Base64InputStream in = new Base64InputStream(new ByteArrayInputStream(
- data.getBytes("UTF-8")), false);
+ getBytes(data)), false);
try {
return IOUtils.toByteArray(in);
} finally {
return count;
}
+ /**
+ * Return the bytes array representation of the given {@link String} in
+ * UTF-8.
+ *
+ * @param str
+ * the {@link String} to transform into bytes
+ * @return the content in bytes
+ */
+ static public byte[] getBytes(String str) {
+ try {
+ return str.getBytes("UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ // All conforming JVM must support UTF-8
+ e.printStackTrace();
+ return null;
+ }
+ }
+
/**
* The "remove accents" pattern.
*
import java.io.InputStream;
import java.io.NotSerializableException;
import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
val = ((URL) value).toString();
}
- out.write(val.getBytes("UTF-8"));
+ out.write(StringUtils.getBytes(val));
}
@Override
if (customTypes.containsKey(type)) {
// TODO: we should start with a stream
InputStream streamEncodedValue = new ByteArrayInputStream(
- encodedValue.getBytes("UTF-8"));
+ StringUtils.getBytes(encodedValue));
try {
return customTypes.get(type).decode(streamEncodedValue);
} finally {
* in case of I/O error
*/
static void write(OutputStream out, Object data) throws IOException {
- try {
- out.write(data.toString().getBytes("UTF-8"));
- } catch (UnsupportedEncodingException e) {
- // A conforming JVM is required to support UTF-8
- e.printStackTrace();
- }
+ out.write(StringUtils.getBytes(data.toString()));
}
/**
import be.nikiroo.utils.CryptUtils;
import be.nikiroo.utils.IOUtils;
+import be.nikiroo.utils.StringUtils;
import be.nikiroo.utils.serial.Exporter;
import be.nikiroo.utils.serial.Importer;
import be.nikiroo.utils.streams.BufferedOutputStream;
try {
if (asString) {
- sub.write(data.toString().getBytes("UTF-8"));
+ sub.write(StringUtils.getBytes(data.toString()));
} else {
new Exporter(sub).append(data);
}
}
InputStream stream = new ByteArrayInputStream(
- data.getBytes("UTF-8"));
+ StringUtils.getBytes(data));
try {
Object obj = new Importer().read(stream).getValue();
if (obj == null) {
import java.io.UnsupportedEncodingException;
+import be.nikiroo.utils.StringUtils;
+
/**
* Utilities for encoding and decoding the Base64 representation of
* binary data. See RFCs <a
* incorrect padding
*/
public static byte[] decode(String str, int flags) {
- try{
- return decode(str.getBytes("UTF-8"), flags);
- } catch (UnsupportedEncodingException e) {
- // All conforming JVM are expected to support UTF-8
- return null;
- }
+ return decode(StringUtils.getBytes(str), flags);
}
/**
import java.io.InputStream;
import java.util.Arrays;
+import be.nikiroo.utils.StringUtils;
+
/**
* A simple {@link InputStream} that is buffered with a bytes array.
* <p>
* greater than the internal buffer
*/
public boolean startsWiths(String search) throws IOException {
- return startsWith(search.getBytes("UTF-8"));
+ return startsWith(StringUtils.getBytes(search));
}
/**
import java.io.IOException;
import java.io.InputStream;
+import be.nikiroo.utils.StringUtils;
+
/**
* This {@link InputStream} will change some of its content by replacing it with
* something else.
* the {@link String} to replace with
*/
public ReplaceInputStream(InputStream in, String from, String to) {
- this(in, StreamUtils.bytes(from), StreamUtils.bytes(to));
+ this(in, StringUtils.getBytes(from), StringUtils.getBytes(to));
}
/**
* the values to replace with
*/
public ReplaceInputStream(InputStream in, String[] froms, String[] tos) {
- this(in, StreamUtils.bytes(froms), StreamUtils.bytes(tos));
+ this(in, StreamUtils.getBytes(froms), StreamUtils.getBytes(tos));
}
/**
import java.io.IOException;
import java.io.OutputStream;
+import be.nikiroo.utils.StringUtils;
+
/**
* This {@link OutputStream} will change some of its content by replacing it
* with something else.
* the {@link String} to replace with
*/
public ReplaceOutputStream(OutputStream out, String from, String to) {
- this(out, StreamUtils.bytes(from), StreamUtils.bytes(to));
+ this(out, StringUtils.getBytes(from), StringUtils.getBytes(to));
}
/**
* the values to replace with
*/
public ReplaceOutputStream(OutputStream out, String[] froms, String[] tos) {
- this(out, StreamUtils.bytes(froms), StreamUtils.bytes(tos));
+ this(out, StreamUtils.getBytes(froms), StreamUtils.getBytes(tos));
}
/**
package be.nikiroo.utils.streams;
-import java.io.UnsupportedEncodingException;
+import be.nikiroo.utils.StringUtils;
/**
* Some non-public utilities used in the stream classes.
return same;
}
- /**
- * Return the bytes array representation of the given {@link String} in
- * UTF-8.
- *
- * @param str
- * the {@link String} to transform into bytes
- * @return the content in bytes
- */
- static public byte[] bytes(String str) {
- try {
- return str.getBytes("UTF-8");
- } catch (UnsupportedEncodingException e) {
- // All conforming JVM must support UTF-8
- e.printStackTrace();
- return null;
- }
- }
-
/**
* Return the bytes array representation of the given {@link String} in
* UTF-8.
* the {@link String}s to transform into bytes
* @return the content in bytes
*/
- static public byte[][] bytes(String[] strs) {
- try {
- byte[][] bytes = new byte[strs.length][];
- for (int i = 0; i < strs.length; i++) {
- bytes[i] = strs[i].getBytes("UTF-8");
- }
- return bytes;
- } catch (UnsupportedEncodingException e) {
- // All conforming JVM must support UTF-8
- e.printStackTrace();
- return null;
+ static public byte[][] getBytes(String[] strs) {
+ byte[][] bytes = new byte[strs.length][];
+ for (int i = 0; i < strs.length; i++) {
+ bytes[i] = StringUtils.getBytes(strs[i]);
}
+
+ return bytes;
}
}