提交 140e8a8d authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add StringBuilder-based variants of convertBytesToHex()

上级 390fe075
...@@ -471,7 +471,7 @@ public class ScriptCommand extends ScriptBase { ...@@ -471,7 +471,7 @@ public class ScriptCommand extends ScriptBase {
if (len <= 0) { if (len <= 0) {
break; break;
} }
buff.append(StringUtils.convertBytesToHex(bytes, len)).append("')"); StringUtils.convertBytesToHex(buff, bytes, len).append("')");
String sql = buff.toString(); String sql = buff.toString();
add(sql, true); add(sql, true);
} }
......
...@@ -163,10 +163,10 @@ public class User extends RightOwner { ...@@ -163,10 +163,10 @@ public class User extends RightOwner {
StringUtils.quoteStringSQL(buff, comment); StringUtils.quoteStringSQL(buff, comment);
} }
if (password) { if (password) {
buff.append(" SALT '"). buff.append(" SALT '");
append(StringUtils.convertBytesToHex(salt)). StringUtils.convertBytesToHex(buff, salt).
append("' HASH '"). append("' HASH '");
append(StringUtils.convertBytesToHex(passwordHash)). StringUtils.convertBytesToHex(buff, passwordHash).
append('\''); append('\'');
} else { } else {
buff.append(" PASSWORD ''"); buff.append(" PASSWORD ''");
......
...@@ -514,7 +514,10 @@ public class FullText { ...@@ -514,7 +514,10 @@ public class FullText {
if (data instanceof UUID) { if (data instanceof UUID) {
return "'" + data.toString() + "'"; return "'" + data.toString() + "'";
} }
return "'" + StringUtils.convertBytesToHex((byte[]) data) + "'"; byte[] bytes = (byte[]) data;
StringBuilder builder = new StringBuilder(bytes.length * 2 + 2).append('\'');
StringUtils.convertBytesToHex(builder, bytes).append('\'');
return builder.toString();
case Types.CLOB: case Types.CLOB:
case Types.JAVA_OBJECT: case Types.JAVA_OBJECT:
case Types.OTHER: case Types.OTHER:
......
...@@ -44,8 +44,15 @@ public class JdbcXid extends TraceObject implements Xid { ...@@ -44,8 +44,15 @@ public class JdbcXid extends TraceObject implements Xid {
* INTERNAL * INTERNAL
*/ */
public static String toString(Xid xid) { public static String toString(Xid xid) {
return PREFIX + '_' + xid.getFormatId() + '_' + StringUtils.convertBytesToHex(xid.getBranchQualifier()) + '_' StringBuilder builder = new StringBuilder()
+ StringUtils.convertBytesToHex(xid.getGlobalTransactionId()); .append(PREFIX)
.append('_')
.append(xid.getFormatId())
.append('_');
StringUtils.convertBytesToHex(builder, xid.getBranchQualifier())
.append('_');
StringUtils.convertBytesToHex(builder, xid.getGlobalTransactionId());
return builder.toString();
} }
/** /**
......
...@@ -312,8 +312,9 @@ public class TraceObject { ...@@ -312,8 +312,9 @@ public class TraceObject {
if (x == null) { if (x == null) {
return "null"; return "null";
} }
return "org.h2.util.StringUtils.convertHexToBytes(\"" + StringBuilder builder = new StringBuilder(x.length * 2 + 45)
StringUtils.convertBytesToHex(x) + "\")"; .append("org.h2.util.StringUtils.convertHexToBytes(\"");
return StringUtils.convertBytesToHex(builder, x).append("\")").toString();
} }
/** /**
......
...@@ -732,10 +732,11 @@ public class Recover extends Tool implements DataHandler { ...@@ -732,10 +732,11 @@ public class Recover extends Tool implements DataHandler {
try { try {
for (int seq = 0;; seq++) { for (int seq = 0;; seq++) {
int l = IOUtils.readFully(in, block, block.length); int l = IOUtils.readFully(in, block, block.length);
String x = StringUtils.convertBytesToHex(block, l);
if (l > 0) { if (l > 0) {
writer.println("INSERT INTO INFORMATION_SCHEMA.LOB_BLOCKS " + writer.print("INSERT INTO INFORMATION_SCHEMA.LOB_BLOCKS " +
"VALUES(" + lobId + ", " + seq + ", '" + x + "');"); "VALUES(" + lobId + ", " + seq + ", '");
writer.print(StringUtils.convertBytesToHex(block, l));
writer.println("');");
} }
if (l != len) { if (l != len) {
break; break;
...@@ -1446,12 +1447,12 @@ public class Recover extends Tool implements DataHandler { ...@@ -1446,12 +1447,12 @@ public class Recover extends Tool implements DataHandler {
byte[] salt = MathUtils.secureRandomBytes(Constants.SALT_LEN); byte[] salt = MathUtils.secureRandomBytes(Constants.SALT_LEN);
byte[] passwordHash = SHA256.getHashWithSalt( byte[] passwordHash = SHA256.getHashWithSalt(
userPasswordHash, salt); userPasswordHash, salt);
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder()
buff.append("SALT '"). .append("SALT '");
append(StringUtils.convertBytesToHex(salt)). StringUtils.convertBytesToHex(buff, salt)
append("' HASH '"). .append("' HASH '");
append(StringUtils.convertBytesToHex(passwordHash)). StringUtils.convertBytesToHex(buff, passwordHash)
append('\''); .append('\'');
byte[] replacement = buff.toString().getBytes(); byte[] replacement = buff.toString().getBytes();
System.arraycopy(replacement, 0, s.getBytes(), System.arraycopy(replacement, 0, s.getBytes(),
saltIndex, replacement.length); saltIndex, replacement.length);
......
...@@ -1057,6 +1057,34 @@ public class StringUtils { ...@@ -1057,6 +1057,34 @@ public class StringUtils {
return new String(buff); return new String(buff);
} }
/**
* Convert a byte array to a hex encoded string and appends it to a specified string builder.
*
* @param builder string builder to append to
* @param value the byte array
* @return the hex encoded string
*/
public static StringBuilder convertBytesToHex(StringBuilder builder, byte[] value) {
return convertBytesToHex(builder, value, value.length);
}
/**
* Convert a byte array to a hex encoded string and appends it to a specified string builder.
*
* @param builder string builder to append to
* @param value the byte array
* @param len the number of bytes to encode
* @return the hex encoded string
*/
public static StringBuilder convertBytesToHex(StringBuilder builder, byte[] value, int len) {
char[] hex = HEX;
for (int i = 0; i < len; i++) {
int c = value[i] & 0xff;
builder.append(hex[c >>> 4]).append(hex[c & 0xf]);
}
return builder;
}
/** /**
* Check if this string is a decimal number. * Check if this string is a decimal number.
* *
......
...@@ -80,7 +80,8 @@ public class ValueBytes extends Value { ...@@ -80,7 +80,8 @@ public class ValueBytes extends Value {
@Override @Override
public StringBuilder getSQL(StringBuilder builder) { public StringBuilder getSQL(StringBuilder builder) {
return builder.append("X'").append(StringUtils.convertBytesToHex(getBytesNoCopy())).append('\''); builder.append("X'");
return StringUtils.convertBytesToHex(builder, getBytesNoCopy()).append('\'');
} }
@Override @Override
......
...@@ -270,7 +270,8 @@ public class ValueGeometry extends Value { ...@@ -270,7 +270,8 @@ public class ValueGeometry extends Value {
@Override @Override
public StringBuilder getSQL(StringBuilder builder) { public StringBuilder getSQL(StringBuilder builder) {
// Using bytes is faster than converting to EWKT. // Using bytes is faster than converting to EWKT.
return builder.append("X'").append(StringUtils.convertBytesToHex(getBytesNoCopy())).append("'::Geometry"); builder.append("X'");
return StringUtils.convertBytesToHex(builder, getBytesNoCopy()).append("'::Geometry");
} }
@Override @Override
......
...@@ -574,7 +574,8 @@ public class ValueLob extends Value { ...@@ -574,7 +574,8 @@ public class ValueLob extends Value {
if (valueType == Value.CLOB) { if (valueType == Value.CLOB) {
StringUtils.quoteStringSQL(builder, getString()); StringUtils.quoteStringSQL(builder, getString());
} else { } else {
builder.append("X'").append(StringUtils.convertBytesToHex(getBytes())).append('\''); builder.append("X'");
StringUtils.convertBytesToHex(builder, getBytes()).append('\'');
} }
return builder; return builder;
} }
......
...@@ -465,7 +465,8 @@ public class ValueLobDb extends Value { ...@@ -465,7 +465,8 @@ public class ValueLobDb extends Value {
if (valueType == Value.CLOB) { if (valueType == Value.CLOB) {
StringUtils.quoteStringSQL(builder, getString()); StringUtils.quoteStringSQL(builder, getString());
} else { } else {
builder.append("X'").append(StringUtils.convertBytesToHex(getBytes())).append('\''); builder.append("X'");
StringUtils.convertBytesToHex(builder, getBytes()).append('\'');
} }
return builder; return builder;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论