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

Add StringBuilder-based variants of convertBytesToHex()

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