提交 f7b26686 authored 作者: Thomas Mueller's avatar Thomas Mueller

Move convertStringToBytes to StringUtils.

上级 39baefe5
...@@ -18,7 +18,9 @@ Change Log ...@@ -18,7 +18,9 @@ Change Log
<h1>Change Log</h1> <h1>Change Log</h1>
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul><li>Memory mapped files: There was a bug in version 1.2.139 so that memory mapped files <ul><li>Primary key violations threw a strange exception message when using a single column INT or BIGINT key.
</li><li>EXPLAIN ANALYZE now also lists the number of pages read from the file.
</li><li>Memory mapped files: There was a bug in version 1.2.139 so that memory mapped files
could only be used together with split, for example: split:nioMapped: - the problem is now solved. could only be used together with split, for example: split:nioMapped: - the problem is now solved.
It is still a good idea to use split:nioMapped: to work around the 2 GB limitation of memory mapped files. It is still a good idea to use split:nioMapped: to work around the 2 GB limitation of memory mapped files.
</li><li>Memory mapped files: the system property h2.nioCleanerHack is now disabled by default </li><li>Memory mapped files: the system property h2.nioCleanerHack is now disabled by default
......
...@@ -127,7 +127,6 @@ import org.h2.table.Table; ...@@ -127,7 +127,6 @@ import org.h2.table.Table;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.table.TableView; import org.h2.table.TableView;
import org.h2.table.TableFilter.TableFilterVisitor; import org.h2.table.TableFilter.TableFilterVisitor;
import org.h2.util.Utils;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.New; import org.h2.util.New;
import org.h2.util.StatementBuilder; import org.h2.util.StatementBuilder;
...@@ -2321,7 +2320,7 @@ public class Parser { ...@@ -2321,7 +2320,7 @@ public class Parser {
read(); read();
if (equalsToken("X", name) && currentTokenType == VALUE && currentValue.getType() == Value.STRING) { if (equalsToken("X", name) && currentTokenType == VALUE && currentValue.getType() == Value.STRING) {
read(); read();
byte[] buffer = Utils.convertStringToBytes(currentValue.getString()); byte[] buffer = StringUtils.convertStringToBytes(currentValue.getString());
r = ValueExpression.get(ValueBytes.getNoCopy(buffer)); r = ValueExpression.get(ValueBytes.getNoCopy(buffer));
} else if (readIf(".")) { } else if (readIf(".")) {
r = readTermObjectDot(name); r = readTermObjectDot(name);
......
...@@ -13,7 +13,7 @@ import org.h2.engine.User; ...@@ -13,7 +13,7 @@ import org.h2.engine.User;
import org.h2.expression.Expression; import org.h2.expression.Expression;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.security.SHA256; import org.h2.security.SHA256;
import org.h2.util.Utils; import org.h2.util.StringUtils;
/** /**
* This class represents the statements * This class represents the statements
...@@ -83,7 +83,7 @@ public class AlterUser extends DefineCommand { ...@@ -83,7 +83,7 @@ public class AlterUser extends DefineCommand {
} }
private byte[] getByteArray(Expression e) { private byte[] getByteArray(Expression e) {
return Utils.convertStringToBytes(e.optimize(session).getValue(session).getString()); return StringUtils.convertStringToBytes(e.optimize(session).getValue(session).getString());
} }
public int update() { public int update() {
......
...@@ -13,7 +13,7 @@ import org.h2.engine.User; ...@@ -13,7 +13,7 @@ import org.h2.engine.User;
import org.h2.expression.Expression; import org.h2.expression.Expression;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.security.SHA256; import org.h2.security.SHA256;
import org.h2.util.Utils; import org.h2.util.StringUtils;
/** /**
* This class represents the statement * This class represents the statement
...@@ -50,7 +50,7 @@ public class CreateUser extends DefineCommand { ...@@ -50,7 +50,7 @@ public class CreateUser extends DefineCommand {
} }
private byte[] getByteArray(Expression e) { private byte[] getByteArray(Expression e) {
return Utils.convertStringToBytes(e.optimize(session).getValue(session).getString()); return StringUtils.convertStringToBytes(e.optimize(session).getValue(session).getString());
} }
public int update() { public int update() {
......
...@@ -356,7 +356,7 @@ public class ScriptCommand extends ScriptBase { ...@@ -356,7 +356,7 @@ public class ScriptCommand extends ScriptBase {
if (len <= 0) { if (len <= 0) {
break; break;
} }
buff.append(Utils.convertBytesToString(bytes, len)).append("')"); buff.append(StringUtils.convertBytesToString(bytes, len)).append("')");
String sql = buff.toString(); String sql = buff.toString();
add(sql, true); add(sql, true);
} }
......
...@@ -366,7 +366,7 @@ public class SessionRemote extends SessionWithState implements SessionFactory, D ...@@ -366,7 +366,7 @@ public class SessionRemote extends SessionWithState implements SessionFactory, D
// ignore // ignore
} }
if (clientVersion >= Constants.TCP_PROTOCOL_VERSION) { if (clientVersion >= Constants.TCP_PROTOCOL_VERSION) {
sessionId = Utils.convertBytesToString(MathUtils.secureRandomBytes(32)); sessionId = StringUtils.convertBytesToString(MathUtils.secureRandomBytes(32));
synchronized (this) { synchronized (this) {
for (Transfer transfer : transferList) { for (Transfer transfer : transferList) {
try { try {
......
...@@ -159,9 +159,9 @@ public class User extends RightOwner { ...@@ -159,9 +159,9 @@ public class User extends RightOwner {
} }
if (password) { if (password) {
buff.append(" SALT '"). buff.append(" SALT '").
append(Utils.convertBytesToString(salt)). append(StringUtils.convertBytesToString(salt)).
append("' HASH '"). append("' HASH '").
append(Utils.convertBytesToString(passwordHash)). append(StringUtils.convertBytesToString(passwordHash)).
append('\''); append('\'');
} else { } else {
buff.append(" PASSWORD ''"); buff.append(" PASSWORD ''");
......
...@@ -34,7 +34,6 @@ import org.h2.expression.ValueExpression; ...@@ -34,7 +34,6 @@ import org.h2.expression.ValueExpression;
import org.h2.jdbc.JdbcConnection; import org.h2.jdbc.JdbcConnection;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.tools.SimpleResultSet; import org.h2.tools.SimpleResultSet;
import org.h2.util.Utils;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.New; import org.h2.util.New;
...@@ -461,7 +460,7 @@ public class FullText { ...@@ -461,7 +460,7 @@ public class FullText {
case Types.VARBINARY: case Types.VARBINARY:
case Types.LONGVARBINARY: case Types.LONGVARBINARY:
case Types.BINARY: case Types.BINARY:
return "'" + Utils.convertBytesToString((byte[]) data) + "'"; return "'" + StringUtils.convertBytesToString((byte[]) data) + "'";
case Types.CLOB: case Types.CLOB:
case Types.JAVA_OBJECT: case Types.JAVA_OBJECT:
case Types.OTHER: case Types.OTHER:
......
...@@ -22,7 +22,6 @@ import javax.transaction.xa.Xid; ...@@ -22,7 +22,6 @@ import javax.transaction.xa.Xid;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties; import org.h2.constant.SysProperties;
import org.h2.jdbc.JdbcConnection; import org.h2.jdbc.JdbcConnection;
import org.h2.util.Utils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.New; import org.h2.util.New;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
...@@ -436,9 +435,9 @@ implements XAConnection, XAResource ...@@ -436,9 +435,9 @@ implements XAConnection, XAResource
buff.append("\"f:"). buff.append("\"f:").
append(xid.getFormatId()). append(xid.getFormatId()).
append(",bq:"). append(",bq:").
append(Utils.convertBytesToString(xid.getBranchQualifier())). append(StringUtils.convertBytesToString(xid.getBranchQualifier())).
append(",gx:"). append(",gx:").
append(Utils.convertBytesToString(xid.getGlobalTransactionId())). append(StringUtils.convertBytesToString(xid.getGlobalTransactionId())).
append(",c:"). append(",c:").
append(xid.getClass().getName()). append(xid.getClass().getName()).
append("\""); append("\"");
......
...@@ -11,7 +11,7 @@ import javax.transaction.xa.Xid; ...@@ -11,7 +11,7 @@ import javax.transaction.xa.Xid;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.message.TraceObject; import org.h2.message.TraceObject;
import org.h2.util.Utils; import org.h2.util.StringUtils;
/** /**
* An object of this class represents a transaction id. * An object of this class represents a transaction id.
...@@ -37,8 +37,8 @@ implements Xid ...@@ -37,8 +37,8 @@ implements Xid
throw DbException.get(ErrorCode.WRONG_XID_FORMAT_1, tid); throw DbException.get(ErrorCode.WRONG_XID_FORMAT_1, tid);
} }
formatId = Integer.parseInt(tokenizer.nextToken()); formatId = Integer.parseInt(tokenizer.nextToken());
branchQualifier = Utils.convertStringToBytes(tokenizer.nextToken()); branchQualifier = StringUtils.convertStringToBytes(tokenizer.nextToken());
globalTransactionId = Utils.convertStringToBytes(tokenizer.nextToken()); globalTransactionId = StringUtils.convertStringToBytes(tokenizer.nextToken());
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw DbException.get(ErrorCode.WRONG_XID_FORMAT_1, tid); throw DbException.get(ErrorCode.WRONG_XID_FORMAT_1, tid);
} }
...@@ -52,9 +52,9 @@ implements Xid ...@@ -52,9 +52,9 @@ implements Xid
buff.append('_'). buff.append('_').
append(formatId). append(formatId).
append('_'). append('_').
append(Utils.convertBytesToString(branchQualifier)). append(StringUtils.convertBytesToString(branchQualifier)).
append('_'). append('_').
append(Utils.convertBytesToString(globalTransactionId)); append(StringUtils.convertBytesToString(globalTransactionId));
return buff.toString(); return buff.toString();
} }
......
...@@ -14,7 +14,6 @@ import java.util.ArrayList; ...@@ -14,7 +14,6 @@ import java.util.ArrayList;
import java.util.Map; import java.util.Map;
import org.h2.constant.SysProperties; import org.h2.constant.SysProperties;
import org.h2.expression.ParameterInterface; import org.h2.expression.ParameterInterface;
import org.h2.util.Utils;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.StatementBuilder; import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
...@@ -326,7 +325,7 @@ public class TraceObject { ...@@ -326,7 +325,7 @@ public class TraceObject {
if (x == null) { if (x == null) {
return "null"; return "null";
} }
return "org.h2.util.Utils.convertStringToBytes(\"" + Utils.convertBytesToString(x) + "\")"; return "org.h2.util.Utils.convertStringToBytes(\"" + StringUtils.convertBytesToString(x) + "\")";
} }
/** /**
......
...@@ -31,6 +31,7 @@ import org.h2.constant.ErrorCode; ...@@ -31,6 +31,7 @@ import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties; import org.h2.constant.SysProperties;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.StringUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
/** /**
...@@ -169,13 +170,13 @@ public class CipherFactory { ...@@ -169,13 +170,13 @@ public class CipherFactory {
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); KeyFactory keyFactory = KeyFactory.getInstance("RSA");
store.load(null, password.toCharArray()); store.load(null, password.toCharArray());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec( PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
Utils.convertStringToBytes("30820277020100300d06092a864886f70d0101010500048202613082025d02010002818100dc0a13c602b7141110eade2f051b54777b060d0f74e6a110f9cce81159f271ebc88d8e8aa1f743b505fc2e7dfe38d33b8d3f64d1b363d1af4d877833897954cbaec2fa384c22a415498cf306bb07ac09b76b001cd68bf77ea0a628f5101959cf2993a9c23dbee79b19305977f8715ae78d023471194cc900b231eecb0aaea98d02030100010281810099aa4ff4d0a09a5af0bd953cb10c4d08c3d98df565664ac5582e494314d5c3c92dddedd5d316a32a206be4ec084616fe57be15e27cad111aa3c21fa79e32258c6ca8430afc69eddd52d3b751b37da6b6860910b94653192c0db1d02abcfd6ce14c01f238eec7c20bd3bb750940004bacba2880349a9494d10e139ecb2355d101024100ffdc3defd9c05a2d377ef6019fa62b3fbd5b0020a04cc8533bca730e1f6fcf5dfceea1b044fbe17d9eababfbc7d955edad6bc60f9be826ad2c22ba77d19a9f65024100dc28d43fdbbc93852cc3567093157702bc16f156f709fb7db0d9eec028f41fd0edcd17224c866e66be1744141fb724a10fd741c8a96afdd9141b36d67fff6309024077b1cddbde0f69604bdcfe33263fb36ddf24aa3b9922327915b890f8a36648295d0139ecdf68c245652c4489c6257b58744fbdd961834a4cab201801a3b1e52d024100b17142e8991d1b350a0802624759d48ae2b8071a158ff91fabeb6a8f7c328e762143dc726b8529f42b1fab6220d1c676fdc27ba5d44e847c72c52064afd351a902407c6e23fe35bcfcd1a662aa82a2aa725fcece311644d5b6e3894853fd4ce9fe78218c957b1ff03fc9e5ef8ffeb6bd58235f6a215c97d354fdace7e781e4a63e8b")); StringUtils.convertStringToBytes("30820277020100300d06092a864886f70d0101010500048202613082025d02010002818100dc0a13c602b7141110eade2f051b54777b060d0f74e6a110f9cce81159f271ebc88d8e8aa1f743b505fc2e7dfe38d33b8d3f64d1b363d1af4d877833897954cbaec2fa384c22a415498cf306bb07ac09b76b001cd68bf77ea0a628f5101959cf2993a9c23dbee79b19305977f8715ae78d023471194cc900b231eecb0aaea98d02030100010281810099aa4ff4d0a09a5af0bd953cb10c4d08c3d98df565664ac5582e494314d5c3c92dddedd5d316a32a206be4ec084616fe57be15e27cad111aa3c21fa79e32258c6ca8430afc69eddd52d3b751b37da6b6860910b94653192c0db1d02abcfd6ce14c01f238eec7c20bd3bb750940004bacba2880349a9494d10e139ecb2355d101024100ffdc3defd9c05a2d377ef6019fa62b3fbd5b0020a04cc8533bca730e1f6fcf5dfceea1b044fbe17d9eababfbc7d955edad6bc60f9be826ad2c22ba77d19a9f65024100dc28d43fdbbc93852cc3567093157702bc16f156f709fb7db0d9eec028f41fd0edcd17224c866e66be1744141fb724a10fd741c8a96afdd9141b36d67fff6309024077b1cddbde0f69604bdcfe33263fb36ddf24aa3b9922327915b890f8a36648295d0139ecdf68c245652c4489c6257b58744fbdd961834a4cab201801a3b1e52d024100b17142e8991d1b350a0802624759d48ae2b8071a158ff91fabeb6a8f7c328e762143dc726b8529f42b1fab6220d1c676fdc27ba5d44e847c72c52064afd351a902407c6e23fe35bcfcd1a662aa82a2aa725fcece311644d5b6e3894853fd4ce9fe78218c957b1ff03fc9e5ef8ffeb6bd58235f6a215c97d354fdace7e781e4a63e8b"));
PrivateKey privateKey = keyFactory.generatePrivate(keySpec); PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
Certificate[] certs = { CertificateFactory Certificate[] certs = { CertificateFactory
.getInstance("X.509") .getInstance("X.509")
.generateCertificate( .generateCertificate(
new ByteArrayInputStream( new ByteArrayInputStream(
Utils.convertStringToBytes("3082018b3081f502044295ce6b300d06092a864886f70d0101040500300d310b3009060355040313024832301e170d3035303532363133323630335a170d3337303933303036353734375a300d310b300906035504031302483230819f300d06092a864886f70d010101050003818d0030818902818100dc0a13c602b7141110eade2f051b54777b060d0f74e6a110f9cce81159f271ebc88d8e8aa1f743b505fc2e7dfe38d33b8d3f64d1b363d1af4d877833897954cbaec2fa384c22a415498cf306bb07ac09b76b001cd68bf77ea0a628f5101959cf2993a9c23dbee79b19305977f8715ae78d023471194cc900b231eecb0aaea98d0203010001300d06092a864886f70d01010405000381810083f4401a279453701bef9a7681a5b8b24f153f7d18c7c892133d97bd5f13736be7505290a445a7d5ceb75522403e5097515cd966ded6351ff60d5193de34cd36e5cb04d380398e66286f99923fd92296645fd4ada45844d194dfd815e6cd57f385c117be982809028bba1116c85740b3d27a55b1a0948bf291ddba44bed337b9"))), }; StringUtils.convertStringToBytes("3082018b3081f502044295ce6b300d06092a864886f70d0101040500300d310b3009060355040313024832301e170d3035303532363133323630335a170d3337303933303036353734375a300d310b300906035504031302483230819f300d06092a864886f70d010101050003818d0030818902818100dc0a13c602b7141110eade2f051b54777b060d0f74e6a110f9cce81159f271ebc88d8e8aa1f743b505fc2e7dfe38d33b8d3f64d1b363d1af4d877833897954cbaec2fa384c22a415498cf306bb07ac09b76b001cd68bf77ea0a628f5101959cf2993a9c23dbee79b19305977f8715ae78d023471194cc900b231eecb0aaea98d0203010001300d06092a864886f70d01010405000381810083f4401a279453701bef9a7681a5b8b24f153f7d18c7c892133d97bd5f13736be7505290a445a7d5ceb75522403e5097515cd966ded6351ff60d5193de34cd36e5cb04d380398e66286f99923fd92296645fd4ada45844d194dfd815e6cd57f385c117be982809028bba1116c85740b3d27a55b1a0948bf291ddba44bed337b9"))), };
store.setKeyEntry("h2", privateKey, password.toCharArray(), certs); store.setKeyEntry("h2", privateKey, password.toCharArray(), certs);
// --- generated code end --- // --- generated code end ---
return store; return store;
......
...@@ -31,6 +31,7 @@ import org.h2.engine.Constants; ...@@ -31,6 +31,7 @@ import org.h2.engine.Constants;
import org.h2.message.TraceSystem; import org.h2.message.TraceSystem;
import org.h2.server.Service; import org.h2.server.Service;
import org.h2.server.ShutdownHandler; import org.h2.server.ShutdownHandler;
import org.h2.util.StringUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
...@@ -157,7 +158,7 @@ public class WebServer implements Service { ...@@ -157,7 +158,7 @@ public class WebServer implements Service {
private String generateSessionId() { private String generateSessionId() {
byte[] buff = MathUtils.secureRandomBytes(16); byte[] buff = MathUtils.secureRandomBytes(16);
return Utils.convertBytesToString(buff); return StringUtils.convertBytesToString(buff);
} }
/** /**
......
...@@ -22,7 +22,7 @@ import org.h2.message.DbException; ...@@ -22,7 +22,7 @@ import org.h2.message.DbException;
import org.h2.message.Trace; import org.h2.message.Trace;
import org.h2.message.TraceSystem; import org.h2.message.TraceSystem;
import org.h2.store.fs.FileSystem; import org.h2.store.fs.FileSystem;
import org.h2.util.Utils; import org.h2.util.StringUtils;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.NetUtils; import org.h2.util.NetUtils;
import org.h2.util.SortedProperties; import org.h2.util.SortedProperties;
...@@ -288,7 +288,7 @@ public class FileLock implements Runnable { ...@@ -288,7 +288,7 @@ public class FileLock implements Runnable {
private void setUniqueId() { private void setUniqueId() {
byte[] bytes = MathUtils.secureRandomBytes(RANDOM_BYTES); byte[] bytes = MathUtils.secureRandomBytes(RANDOM_BYTES);
String random = Utils.convertBytesToString(bytes); String random = StringUtils.convertBytesToString(bytes);
uniqueId = Long.toHexString(System.currentTimeMillis()) + random; uniqueId = Long.toHexString(System.currentTimeMillis()) + random;
properties.setProperty("id", uniqueId); properties.setProperty("id", uniqueId);
} }
......
...@@ -12,7 +12,7 @@ import java.io.OutputStream; ...@@ -12,7 +12,7 @@ import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.New; import org.h2.util.New;
import org.h2.util.Utils; import org.h2.util.StringUtils;
/** /**
* The file system is a storage abstraction. * The file system is a storage abstraction.
...@@ -308,7 +308,7 @@ public abstract class FileSystem { ...@@ -308,7 +308,7 @@ public abstract class FileSystem {
if (newRandom || tempRandom == null) { if (newRandom || tempRandom == null) {
byte[] prefix = new byte[8]; byte[] prefix = new byte[8];
MathUtils.randomBytes(prefix); MathUtils.randomBytes(prefix);
tempRandom = Utils.convertBytesToString(prefix) + "."; tempRandom = StringUtils.convertBytesToString(prefix) + ".";
} }
return tempRandom + tempSequence++; return tempRandom + tempSequence++;
} }
......
...@@ -51,6 +51,7 @@ import org.h2.util.MathUtils; ...@@ -51,6 +51,7 @@ import org.h2.util.MathUtils;
import org.h2.util.New; import org.h2.util.New;
import org.h2.util.SmallLRUCache; import org.h2.util.SmallLRUCache;
import org.h2.util.StatementBuilder; import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils;
import org.h2.util.TempFileDeleter; import org.h2.util.TempFileDeleter;
import org.h2.util.Tool; import org.h2.util.Tool;
import org.h2.util.Utils; import org.h2.util.Utils;
...@@ -1027,9 +1028,9 @@ public class Recover extends Tool implements DataHandler { ...@@ -1027,9 +1028,9 @@ public class Recover extends Tool implements DataHandler {
byte[] passwordHash = sha.getHashWithSalt(userPasswordHash, salt); byte[] passwordHash = sha.getHashWithSalt(userPasswordHash, salt);
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
buff.append("SALT '"). buff.append("SALT '").
append(Utils.convertBytesToString(salt)). append(StringUtils.convertBytesToString(salt)).
append("' HASH '"). append("' HASH '").
append(Utils.convertBytesToString(passwordHash)). append(StringUtils.convertBytesToString(passwordHash)).
append('\''); append('\'');
byte[] replacement = buff.toString().getBytes(); byte[] replacement = buff.toString().getBytes();
System.arraycopy(replacement, 0, s.getBytes(), saltIndex, replacement.length); System.arraycopy(replacement, 0, s.getBytes(), saltIndex, replacement.length);
......
...@@ -21,6 +21,7 @@ import org.h2.message.DbException; ...@@ -21,6 +21,7 @@ import org.h2.message.DbException;
public class StringUtils { public class StringUtils {
private static SoftReference<String[]> softCache = new SoftReference<String[]>(null); private static SoftReference<String[]> softCache = new SoftReference<String[]>(null);
private static final char[] HEX = "0123456789abcdef".toCharArray();
private StringUtils() { private StringUtils() {
// utility class // utility class
...@@ -932,4 +933,64 @@ public class StringUtils { ...@@ -932,4 +933,64 @@ public class StringUtils {
softCache = new SoftReference<String[]>(null); softCache = new SoftReference<String[]>(null);
} }
/**
* Convert a hex encoded string to a byte array.
*
* @param s the hex encoded string
* @return the byte array
*/
public static byte[] convertStringToBytes(String s) {
int len = s.length();
if (len % 2 != 0) {
throw DbException.get(ErrorCode.HEX_STRING_ODD_1, s);
}
len /= 2;
byte[] buff = new byte[len];
for (int i = 0; i < len; i++) {
buff[i] = (byte) ((getHexDigit(s, i + i) << 4) | getHexDigit(s, i + i + 1));
}
return buff;
}
private static int getHexDigit(String s, int i) {
char c = s.charAt(i);
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 0xa;
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 0xa;
} else {
throw DbException.get(ErrorCode.HEX_STRING_WRONG_1, s);
}
}
/**
* Convert a byte array to a hex encoded string.
*
* @param value the byte array
* @return the hex encoded string
*/
public static String convertBytesToString(byte[] value) {
return convertBytesToString(value, value.length);
}
/**
* Convert a byte array to a hex encoded string.
*
* @param value the byte array
* @param len the number of bytes to encode
* @return the hex encoded string
*/
public static String convertBytesToString(byte[] value, int len) {
char[] buff = new char[len + len];
char[] hex = HEX;
for (int i = 0; i < len; i++) {
int c = value[i] & 0xff;
buff[i + i] = hex[c >> 4];
buff[i + i + 1] = hex[c & 0xf];
}
return new String(buff);
}
} }
...@@ -46,8 +46,6 @@ public class Utils { ...@@ -46,8 +46,6 @@ public class Utils {
private static final int GC_DELAY = 50; private static final int GC_DELAY = 50;
private static final int MAX_GC = 8; private static final int MAX_GC = 8;
private static final char[] HEX = "0123456789abcdef".toCharArray();
private static long lastGC; private static long lastGC;
private static final boolean ALLOW_ALL_CLASSES; private static final boolean ALLOW_ALL_CLASSES;
...@@ -137,38 +135,6 @@ public class Utils { ...@@ -137,38 +135,6 @@ public class Utils {
return -1; return -1;
} }
/**
* Convert a hex encoded string to a byte array.
*
* @param s the hex encoded string
* @return the byte array
*/
public static byte[] convertStringToBytes(String s) {
int len = s.length();
if (len % 2 != 0) {
throw DbException.get(ErrorCode.HEX_STRING_ODD_1, s);
}
len /= 2;
byte[] buff = new byte[len];
for (int i = 0; i < len; i++) {
buff[i] = (byte) ((getHexDigit(s, i + i) << 4) | getHexDigit(s, i + i + 1));
}
return buff;
}
private static int getHexDigit(String s, int i) {
char c = s.charAt(i);
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 0xa;
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 0xa;
} else {
throw DbException.get(ErrorCode.HEX_STRING_WRONG_1, s);
}
}
/** /**
* Calculate the hash code of the given byte array. * Calculate the hash code of the given byte array.
* *
...@@ -195,34 +161,6 @@ public class Utils { ...@@ -195,34 +161,6 @@ public class Utils {
return h; return h;
} }
/**
* Convert a byte array to a hex encoded string.
*
* @param value the byte array
* @return the hex encoded string
*/
public static String convertBytesToString(byte[] value) {
return convertBytesToString(value, value.length);
}
/**
* Convert a byte array to a hex encoded string.
*
* @param value the byte array
* @param len the number of bytes to encode
* @return the hex encoded string
*/
public static String convertBytesToString(byte[] value, int len) {
char[] buff = new char[len + len];
char[] hex = HEX;
for (int i = 0; i < len; i++) {
int c = value[i] & 0xff;
buff[i + i] = hex[c >> 4];
buff[i + i + 1] = hex[c & 0xf];
}
return new String(buff);
}
/** /**
* Compare two byte arrays. This method will always loop over all bytes and * Compare two byte arrays. This method will always loop over all bytes and
* doesn't use conditional operations in the loop to make sure an attacker * doesn't use conditional operations in the loop to make sure an attacker
......
...@@ -25,7 +25,6 @@ import org.h2.store.LobStorage; ...@@ -25,7 +25,6 @@ import org.h2.store.LobStorage;
import org.h2.tools.SimpleResultSet; import org.h2.tools.SimpleResultSet;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.util.Utils;
/** /**
* This is the base class for all value classes. * This is the base class for all value classes.
...@@ -751,9 +750,9 @@ public abstract class Value { ...@@ -751,9 +750,9 @@ public abstract class Value {
case TIMESTAMP: case TIMESTAMP:
return ValueTimestamp.getNoCopy(ValueTimestamp.parseTimestamp(s.trim())); return ValueTimestamp.getNoCopy(ValueTimestamp.parseTimestamp(s.trim()));
case BYTES: case BYTES:
return ValueBytes.getNoCopy(Utils.convertStringToBytes(s.trim())); return ValueBytes.getNoCopy(StringUtils.convertStringToBytes(s.trim()));
case JAVA_OBJECT: case JAVA_OBJECT:
return ValueJavaObject.getNoCopy(Utils.convertStringToBytes(s.trim())); return ValueJavaObject.getNoCopy(StringUtils.convertStringToBytes(s.trim()));
case STRING: case STRING:
return ValueString.get(s); return ValueString.get(s);
case STRING_IGNORECASE: case STRING_IGNORECASE:
...@@ -767,7 +766,7 @@ public abstract class Value { ...@@ -767,7 +766,7 @@ public abstract class Value {
case CLOB: case CLOB:
return LobStorage.createSmallLob(CLOB, StringUtils.utf8Encode(s)); return LobStorage.createSmallLob(CLOB, StringUtils.utf8Encode(s));
case BLOB: case BLOB:
return LobStorage.createSmallLob(BLOB, Utils.convertStringToBytes(s.trim())); return LobStorage.createSmallLob(BLOB, StringUtils.convertStringToBytes(s.trim()));
case ARRAY: case ARRAY:
return ValueArray.get(new Value[]{ValueString.get(s)}); return ValueArray.get(new Value[]{ValueString.get(s)});
case RESULT_SET: { case RESULT_SET: {
......
...@@ -10,6 +10,7 @@ import java.sql.PreparedStatement; ...@@ -10,6 +10,7 @@ import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import org.h2.constant.SysProperties; import org.h2.constant.SysProperties;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.StringUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
/** /**
...@@ -82,7 +83,7 @@ public class ValueBytes extends Value { ...@@ -82,7 +83,7 @@ public class ValueBytes extends Value {
} }
public String getString() { public String getString() {
return Utils.convertBytesToString(value); return StringUtils.convertBytesToString(value);
} }
public long getPrecision() { public long getPrecision() {
......
...@@ -538,7 +538,7 @@ public class ValueLob extends Value { ...@@ -538,7 +538,7 @@ public class ValueLob extends Value {
} else { } else {
buff = IOUtils.readBytesAndClose(getInputStream(), len); buff = IOUtils.readBytesAndClose(getInputStream(), len);
} }
return Utils.convertBytesToString(buff); return StringUtils.convertBytesToString(buff);
} catch (IOException e) { } catch (IOException e) {
throw DbException.convertIOException(e, fileName); throw DbException.convertIOException(e, fileName);
} }
...@@ -632,7 +632,7 @@ public class ValueLob extends Value { ...@@ -632,7 +632,7 @@ public class ValueLob extends Value {
return StringUtils.quoteStringSQL(s); return StringUtils.quoteStringSQL(s);
} }
byte[] buff = getBytes(); byte[] buff = getBytes();
s = Utils.convertBytesToString(buff); s = StringUtils.convertBytesToString(buff);
return "X'" + s + "'"; return "X'" + s + "'";
} }
......
...@@ -193,7 +193,7 @@ public class ValueLobDb extends Value implements Value.ValueClob, Value.ValueBlo ...@@ -193,7 +193,7 @@ public class ValueLobDb extends Value implements Value.ValueClob, Value.ValueBlo
} else { } else {
buff = IOUtils.readBytesAndClose(getInputStream(), len); buff = IOUtils.readBytesAndClose(getInputStream(), len);
} }
return Utils.convertBytesToString(buff); return StringUtils.convertBytesToString(buff);
} catch (IOException e) { } catch (IOException e) {
throw DbException.convertIOException(e, toString()); throw DbException.convertIOException(e, toString());
} }
...@@ -293,7 +293,7 @@ public class ValueLobDb extends Value implements Value.ValueClob, Value.ValueBlo ...@@ -293,7 +293,7 @@ public class ValueLobDb extends Value implements Value.ValueClob, Value.ValueBlo
return StringUtils.quoteStringSQL(s); return StringUtils.quoteStringSQL(s);
} }
byte[] buff = getBytes(); byte[] buff = getBytes();
s = Utils.convertBytesToString(buff); s = StringUtils.convertBytesToString(buff);
return "X'" + s + "'"; return "X'" + s + "'";
} }
......
...@@ -64,7 +64,7 @@ public class ValueUuid extends Value { ...@@ -64,7 +64,7 @@ public class ValueUuid extends Value {
*/ */
public static ValueUuid get(byte[] binary) { public static ValueUuid get(byte[] binary) {
if (binary.length < 32) { if (binary.length < 32) {
return get(Utils.convertBytesToString(binary)); return get(StringUtils.convertBytesToString(binary));
} }
long high = Utils.readLong(binary, 0); long high = Utils.readLong(binary, 0);
long low = Utils.readLong(binary, 16); long low = Utils.readLong(binary, 16);
......
...@@ -27,7 +27,6 @@ import org.h2.tools.Csv; ...@@ -27,7 +27,6 @@ import org.h2.tools.Csv;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.New; import org.h2.util.New;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.util.Utils;
/** /**
* CSVREAD and CSVWRITE tests. * CSVREAD and CSVWRITE tests.
...@@ -67,7 +66,7 @@ public class TestCsv extends TestBase { ...@@ -67,7 +66,7 @@ public class TestCsv extends TestBase {
private void testPseudoBom() throws Exception { private void testPseudoBom() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
// UTF-8 "BOM" / marker // UTF-8 "BOM" / marker
out.write(Utils.convertStringToBytes("ef" + "bb" + "bf")); out.write(StringUtils.convertStringToBytes("ef" + "bb" + "bf"));
out.write("\"ID\", \"NAME\"\n1, Hello".getBytes("UTF-8")); out.write("\"ID\", \"NAME\"\n1, Hello".getBytes("UTF-8"));
byte[] buff = out.toByteArray(); byte[] buff = out.toByteArray();
Reader r = new InputStreamReader(new ByteArrayInputStream(buff), "UTF-8"); Reader r = new InputStreamReader(new ByteArrayInputStream(buff), "UTF-8");
......
...@@ -10,6 +10,7 @@ import org.h2.security.BlockCipher; ...@@ -10,6 +10,7 @@ import org.h2.security.BlockCipher;
import org.h2.security.CipherFactory; import org.h2.security.CipherFactory;
import org.h2.security.SHA256; import org.h2.security.SHA256;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.StringUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
/** /**
...@@ -42,7 +43,7 @@ public class TestSecurity extends TestBase { ...@@ -42,7 +43,7 @@ public class TestSecurity extends TestBase {
if (data.length > 0) { if (data.length > 0) {
assertEquals(0, data[0]); assertEquals(0, data[0]);
} }
return Utils.convertBytesToString(result); return StringUtils.convertBytesToString(result);
} }
private void testOneSHA(SHA256 sha) { private void testOneSHA(SHA256 sha) {
...@@ -69,7 +70,7 @@ public class TestSecurity extends TestBase { ...@@ -69,7 +70,7 @@ public class TestSecurity extends TestBase {
private void checkSHA256(String message, String expected) { private void checkSHA256(String message, String expected) {
SHA256 sha = new SHA256(); SHA256 sha = new SHA256();
String hash = Utils.convertBytesToString(sha.getHash(message.getBytes(), true)).toUpperCase(); String hash = StringUtils.convertBytesToString(sha.getHash(message.getBytes(), true)).toUpperCase();
assertEquals(expected, hash); assertEquals(expected, hash);
} }
...@@ -84,7 +85,7 @@ public class TestSecurity extends TestBase { ...@@ -84,7 +85,7 @@ public class TestSecurity extends TestBase {
private void testAES() { private void testAES() {
BlockCipher test = CipherFactory.getBlockCipher("AES"); BlockCipher test = CipherFactory.getBlockCipher("AES");
test.setKey(Utils.convertStringToBytes("000102030405060708090A0B0C0D0E0F")); test.setKey(StringUtils.convertStringToBytes("000102030405060708090A0B0C0D0E0F"));
byte[] in = new byte[128]; byte[] in = new byte[128];
byte[] enc = new byte[128]; byte[] enc = new byte[128];
......
...@@ -15,7 +15,6 @@ import org.h2.message.DbException; ...@@ -15,7 +15,6 @@ import org.h2.message.DbException;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.util.Utils;
/** /**
* Tests string utility methods. * Tests string utility methods.
...@@ -41,18 +40,18 @@ public class TestStringUtils extends TestBase { ...@@ -41,18 +40,18 @@ public class TestStringUtils extends TestBase {
} }
private void testHex() { private void testHex() {
assertEquals("face", Utils.convertBytesToString(new byte[] { (byte) 0xfa, (byte) 0xce })); assertEquals("face", StringUtils.convertBytesToString(new byte[] { (byte) 0xfa, (byte) 0xce }));
assertEquals(new byte[] { (byte) 0xfa, (byte) 0xce }, Utils.convertStringToBytes("face")); assertEquals(new byte[] { (byte) 0xfa, (byte) 0xce }, StringUtils.convertStringToBytes("face"));
assertEquals(new byte[] { (byte) 0xfa, (byte) 0xce }, Utils.convertStringToBytes("fAcE")); assertEquals(new byte[] { (byte) 0xfa, (byte) 0xce }, StringUtils.convertStringToBytes("fAcE"));
assertEquals(new byte[] { (byte) 0xfa, (byte) 0xce }, Utils.convertStringToBytes("FaCe")); assertEquals(new byte[] { (byte) 0xfa, (byte) 0xce }, StringUtils.convertStringToBytes("FaCe"));
try { try {
Utils.convertStringToBytes("120"); StringUtils.convertStringToBytes("120");
fail(); fail();
} catch (DbException e) { } catch (DbException e) {
assertKnownException(DbException.toSQLException(e)); assertKnownException(DbException.toSQLException(e));
} }
try { try {
Utils.convertStringToBytes("fast"); StringUtils.convertStringToBytes("fast");
fail(); fail();
} catch (DbException e) { } catch (DbException e) {
assertKnownException(DbException.toSQLException(e)); assertKnownException(DbException.toSQLException(e));
......
...@@ -16,7 +16,7 @@ import java.security.cert.CertificateEncodingException; ...@@ -16,7 +16,7 @@ import java.security.cert.CertificateEncodingException;
import java.util.Enumeration; import java.util.Enumeration;
import org.h2.security.CipherFactory; import org.h2.security.CipherFactory;
import org.h2.util.Utils; import org.h2.util.StringUtils;
/** /**
* Tool to generate source code for the SecureSocketFactory. First, create a * Tool to generate source code for the SecureSocketFactory. First, create a
...@@ -57,14 +57,14 @@ public class SecureKeyStoreBuilder { ...@@ -57,14 +57,14 @@ public class SecureKeyStoreBuilder {
System.out.println("KeyFactory keyFactory = KeyFactory.getInstance(\"" + key.getAlgorithm() + "\");"); System.out.println("KeyFactory keyFactory = KeyFactory.getInstance(\"" + key.getAlgorithm() + "\");");
System.out.println("store.load(null, password.toCharArray());"); System.out.println("store.load(null, password.toCharArray());");
String pkFormat = key.getFormat(); String pkFormat = key.getFormat();
String encoded = Utils.convertBytesToString(key.getEncoded()); String encoded = StringUtils.convertBytesToString(key.getEncoded());
System.out.println(pkFormat + "EncodedKeySpec keySpec = new " + pkFormat + "EncodedKeySpec(getBytes(\"" System.out.println(pkFormat + "EncodedKeySpec keySpec = new " + pkFormat + "EncodedKeySpec(getBytes(\""
+ encoded + "\"));"); + encoded + "\"));");
System.out.println("PrivateKey privateKey = keyFactory.generatePrivate(keySpec);"); System.out.println("PrivateKey privateKey = keyFactory.generatePrivate(keySpec);");
System.out.println("Certificate[] certs = {"); System.out.println("Certificate[] certs = {");
for (Certificate cert : store.getCertificateChain(alias)) { for (Certificate cert : store.getCertificateChain(alias)) {
System.out.println(" CertificateFactory.getInstance(\""+cert.getType()+"\")."); System.out.println(" CertificateFactory.getInstance(\""+cert.getType()+"\").");
String enc = Utils.convertBytesToString(cert.getEncoded()); String enc = StringUtils.convertBytesToString(cert.getEncoded());
System.out.println(" generateCertificate(new ByteArrayInputStream(getBytes(\""+enc+"\"))),"); System.out.println(" generateCertificate(new ByteArrayInputStream(getBytes(\""+enc+"\"))),");
// PublicKey pubKey = cert.getPublicKey(); // PublicKey pubKey = cert.getPublicKey();
// System.out.println(" pubKey algorithm="+pubKey.getAlgorithm()); // System.out.println(" pubKey algorithm="+pubKey.getAlgorithm());
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论