Unverified 提交 3ae3bab1 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #965 from katzyn/DataUtils

Do not include mvstore.DataUtils in client jar and other changes
...@@ -8,7 +8,7 @@ package org.h2.compress; ...@@ -8,7 +8,7 @@ package org.h2.compress;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.DataUtils; import org.h2.util.Utils;
/** /**
* An input stream to read from an LZF stream. * An input stream to read from an LZF stream.
...@@ -31,7 +31,7 @@ public class LZFInputStream extends InputStream { ...@@ -31,7 +31,7 @@ public class LZFInputStream extends InputStream {
} }
private static byte[] ensureSize(byte[] buff, int len) { private static byte[] ensureSize(byte[] buff, int len) {
return buff == null || buff.length < len ? DataUtils.newBytes(len) : buff; return buff == null || buff.length < len ? Utils.newBytes(len) : buff;
} }
private void fillBuffer() throws IOException { private void fillBuffer() throws IOException {
......
...@@ -31,7 +31,6 @@ import org.h2.engine.Database; ...@@ -31,7 +31,6 @@ import org.h2.engine.Database;
import org.h2.engine.Mode; import org.h2.engine.Mode;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.schema.Schema; import org.h2.schema.Schema;
import org.h2.schema.Sequence; import org.h2.schema.Sequence;
import org.h2.security.BlockCipher; import org.h2.security.BlockCipher;
...@@ -1819,7 +1818,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -1819,7 +1818,7 @@ public class Function extends Expression implements FunctionCall {
private static byte[] getPaddedArrayCopy(byte[] data, int blockSize) { private static byte[] getPaddedArrayCopy(byte[] data, int blockSize) {
int size = MathUtils.roundUpInt(data.length, blockSize); int size = MathUtils.roundUpInt(data.length, blockSize);
return DataUtils.copyBytes(data, size); return Utils.copyBytes(data, size);
} }
private static byte[] decrypt(String algorithm, byte[] key, byte[] data) { private static byte[] decrypt(String algorithm, byte[] key, byte[] data) {
......
...@@ -228,7 +228,7 @@ public class Chunk { ...@@ -228,7 +228,7 @@ public class Chunk {
* @return the string * @return the string
*/ */
public String asString() { public String asString() {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder(240);
DataUtils.appendMap(buff, "chunk", id); DataUtils.appendMap(buff, "chunk", id);
DataUtils.appendMap(buff, "block", block); DataUtils.appendMap(buff, "block", block);
DataUtils.appendMap(buff, "len", len); DataUtils.appendMap(buff, "len", len);
...@@ -254,17 +254,17 @@ public class Chunk { ...@@ -254,17 +254,17 @@ public class Chunk {
} }
byte[] getFooterBytes() { byte[] getFooterBytes() {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder(FOOTER_LENGTH);
DataUtils.appendMap(buff, "chunk", id); DataUtils.appendMap(buff, "chunk", id);
DataUtils.appendMap(buff, "block", block); DataUtils.appendMap(buff, "block", block);
DataUtils.appendMap(buff, "version", version); DataUtils.appendMap(buff, "version", version);
byte[] bytes = buff.toString().getBytes(StandardCharsets.ISO_8859_1); byte[] bytes = buff.toString().getBytes(StandardCharsets.ISO_8859_1);
int checksum = DataUtils.getFletcher32(bytes, 0, bytes.length); int checksum = DataUtils.getFletcher32(bytes, 0, bytes.length);
DataUtils.appendMap(buff, "fletcher", checksum); DataUtils.appendMap(buff, "fletcher", checksum);
while (buff.length() < Chunk.FOOTER_LENGTH - 1) { while (buff.length() < FOOTER_LENGTH - 1) {
buff.append(' '); buff.append(' ');
} }
buff.append("\n"); buff.append('\n');
return buff.toString().getBytes(StandardCharsets.ISO_8859_1); return buff.toString().getBytes(StandardCharsets.ISO_8859_1);
} }
......
...@@ -12,9 +12,7 @@ import java.nio.ByteBuffer; ...@@ -12,9 +12,7 @@ import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -155,11 +153,6 @@ public final class DataUtils { ...@@ -155,11 +153,6 @@ public final class DataUtils {
*/ */
public static final int PAGE_LARGE = 2 * 1024 * 1024; public static final int PAGE_LARGE = 2 * 1024 * 1024;
/**
* An 0-size byte array.
*/
private static final byte[] EMPTY_BYTES = {};
/** /**
* Get the length of the variable size int. * Get the length of the variable size int.
* *
...@@ -568,16 +561,30 @@ public final class DataUtils { ...@@ -568,16 +561,30 @@ public final class DataUtils {
* @param map the map * @param map the map
* @return the string builder * @return the string builder
*/ */
public static StringBuilder appendMap(StringBuilder buff, public static StringBuilder appendMap(StringBuilder buff, HashMap<String, ?> map) {
HashMap<String, ?> map) { Object[] keys = map.keySet().toArray();
ArrayList<String> list = new ArrayList<>(map.keySet()); Arrays.sort(keys);
Collections.sort(list); for (Object k : keys) {
for (String k : list) { String key = (String) k;
appendMap(buff, k, map.get(k)); Object value = map.get(key);
if (value instanceof Long) {
appendMap(buff, key, (long) value);
} else if (value instanceof Integer) {
appendMap(buff, key, (int) value);
} else {
appendMap(buff, key, value.toString());
}
} }
return buff; return buff;
} }
private static StringBuilder appendMapKey(StringBuilder buff, String key) {
if (buff.length() > 0) {
buff.append(',');
}
return buff.append(key).append(':');
}
/** /**
* Append a key-value pair to the string builder. Keys may not contain a * Append a key-value pair to the string builder. Keys may not contain a
* colon. Values that contain a comma or a double quote are enclosed in * colon. Values that contain a comma or a double quote are enclosed in
...@@ -587,25 +594,14 @@ public final class DataUtils { ...@@ -587,25 +594,14 @@ public final class DataUtils {
* @param key the key * @param key the key
* @param value the value * @param value the value
*/ */
public static void appendMap(StringBuilder buff, String key, Object value) { public static void appendMap(StringBuilder buff, String key, String value) {
if (buff.length() > 0) { appendMapKey(buff, key);
buff.append(','); if (value.indexOf(',') < 0 && value.indexOf('\"') < 0) {
} buff.append(value);
buff.append(key).append(':');
String v;
if (value instanceof Long) {
v = Long.toHexString((Long) value);
} else if (value instanceof Integer) {
v = Integer.toHexString((Integer) value);
} else {
v = value.toString();
}
if (v.indexOf(',') < 0 && v.indexOf('\"') < 0) {
buff.append(v);
} else { } else {
buff.append('\"'); buff.append('\"');
for (int i = 0, size = v.length(); i < size; i++) { for (int i = 0, size = value.length(); i < size; i++) {
char c = v.charAt(i); char c = value.charAt(i);
if (c == '\"') { if (c == '\"') {
buff.append('\\'); buff.append('\\');
} }
...@@ -615,6 +611,30 @@ public final class DataUtils { ...@@ -615,6 +611,30 @@ public final class DataUtils {
} }
} }
/**
* Append a key-value pair to the string builder. Keys may not contain a
* colon.
*
* @param buff the target buffer
* @param key the key
* @param value the value
*/
public static void appendMap(StringBuilder buff, String key, long value) {
appendMapKey(buff, key).append(Long.toHexString(value));
}
/**
* Append a key-value pair to the string builder. Keys may not contain a
* colon.
*
* @param buff the target buffer
* @param key the key
* @param value the value
*/
public static void appendMap(StringBuilder buff, String key, int value) {
appendMapKey(buff, key).append(Integer.toHexString(value));
}
/** /**
* @param buff output buffer, should be empty * @param buff output buffer, should be empty
* @param s parsed string * @param s parsed string
...@@ -912,62 +932,6 @@ public final class DataUtils { ...@@ -912,62 +932,6 @@ public final class DataUtils {
return 0; return 0;
} }
/**
* Create an array of bytes with the given size. If this is not possible
* because not enough memory is available, an OutOfMemoryError with the
* requested size in the message is thrown.
* <p>
* This method should be used if the size of the array is user defined, or
* stored in a file, so wrong size data can be distinguished from regular
* out-of-memory.
* </p>
*
* @param len the number of bytes requested
* @return the byte array
* @throws OutOfMemoryError if the allocation was too large
*/
public static byte[] newBytes(int len) {
if (len == 0) {
return EMPTY_BYTES;
}
try {
return new byte[len];
} catch (OutOfMemoryError e) {
Error e2 = new OutOfMemoryError("Requested memory: " + len);
e2.initCause(e);
throw e2;
}
}
/**
* Creates a copy of array of bytes with the new size. If this is not possible
* because not enough memory is available, an OutOfMemoryError with the
* requested size in the message is thrown.
* <p>
* This method should be used if the size of the array is user defined, or
* stored in a file, so wrong size data can be distinguished from regular
* out-of-memory.
* </p>
*
* @param bytes source array
* @param len the number of bytes in the new array
* @return the byte array
* @throws OutOfMemoryError if the allocation was too large
* @see Arrays#copyOf(byte[], int)
*/
public static byte[] copyBytes(byte[] bytes, int len) {
if (len == 0) {
return EMPTY_BYTES;
}
try {
return Arrays.copyOf(bytes, len);
} catch (OutOfMemoryError e) {
Error e2 = new OutOfMemoryError("Requested memory: " + len);
e2.initCause(e);
throw e2;
}
}
/** /**
* Read a hex long value from a map. * Read a hex long value from a map.
* *
......
...@@ -801,7 +801,7 @@ public final class MVStore { ...@@ -801,7 +801,7 @@ public final class MVStore {
} }
private void writeStoreHeader() { private void writeStoreHeader() {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder(112);
if (lastChunk != null) { if (lastChunk != null) {
storeHeader.put("block", lastChunk.block); storeHeader.put("block", lastChunk.block);
storeHeader.put("chunk", lastChunk.id); storeHeader.put("chunk", lastChunk.id);
...@@ -811,7 +811,7 @@ public final class MVStore { ...@@ -811,7 +811,7 @@ public final class MVStore {
byte[] bytes = buff.toString().getBytes(StandardCharsets.ISO_8859_1); byte[] bytes = buff.toString().getBytes(StandardCharsets.ISO_8859_1);
int checksum = DataUtils.getFletcher32(bytes, 0, bytes.length); int checksum = DataUtils.getFletcher32(bytes, 0, bytes.length);
DataUtils.appendMap(buff, "fletcher", checksum); DataUtils.appendMap(buff, "fletcher", checksum);
buff.append("\n"); buff.append('\n');
bytes = buff.toString().getBytes(StandardCharsets.ISO_8859_1); bytes = buff.toString().getBytes(StandardCharsets.ISO_8859_1);
ByteBuffer header = ByteBuffer.allocate(2 * BLOCK_SIZE); ByteBuffer header = ByteBuffer.allocate(2 * BLOCK_SIZE);
header.put(bytes); header.put(bytes);
......
...@@ -26,6 +26,7 @@ import org.h2.mvstore.type.DataType; ...@@ -26,6 +26,7 @@ import org.h2.mvstore.type.DataType;
import org.h2.mvstore.type.StringDataType; import org.h2.mvstore.type.StringDataType;
import org.h2.store.fs.FilePath; import org.h2.store.fs.FilePath;
import org.h2.store.fs.FileUtils; import org.h2.store.fs.FileUtils;
import org.h2.util.Utils;
/** /**
* Utility methods used in combination with the MVStore. * Utility methods used in combination with the MVStore.
...@@ -226,7 +227,7 @@ public class MVStoreTool { ...@@ -226,7 +227,7 @@ public class MVStoreTool {
Compressor compressor = getCompressor(fast); Compressor compressor = getCompressor(fast);
int lenAdd = DataUtils.readVarInt(chunk); int lenAdd = DataUtils.readVarInt(chunk);
int compLen = pageSize + start - chunk.position(); int compLen = pageSize + start - chunk.position();
byte[] comp = DataUtils.newBytes(compLen); byte[] comp = Utils.newBytes(compLen);
chunk.get(comp); chunk.get(comp);
int l = compLen + lenAdd; int l = compLen + lenAdd;
data = ByteBuffer.allocate(l); data = ByteBuffer.allocate(l);
......
...@@ -9,6 +9,7 @@ import java.nio.ByteBuffer; ...@@ -9,6 +9,7 @@ import java.nio.ByteBuffer;
import java.util.HashSet; import java.util.HashSet;
import org.h2.compress.Compressor; import org.h2.compress.Compressor;
import org.h2.mvstore.type.DataType; import org.h2.mvstore.type.DataType;
import org.h2.util.Utils;
/** /**
* A page (a node or a leaf). * A page (a node or a leaf).
...@@ -702,7 +703,7 @@ public class Page { ...@@ -702,7 +703,7 @@ public class Page {
} }
int lenAdd = DataUtils.readVarInt(buff); int lenAdd = DataUtils.readVarInt(buff);
int compLen = pageLength + start - buff.position(); int compLen = pageLength + start - buff.position();
byte[] comp = DataUtils.newBytes(compLen); byte[] comp = Utils.newBytes(compLen);
buff.get(comp); buff.get(comp);
int l = compLen + lenAdd; int l = compLen + lenAdd;
buff = ByteBuffer.allocate(l); buff = ByteBuffer.allocate(l);
......
...@@ -23,6 +23,7 @@ import org.h2.result.SortOrder; ...@@ -23,6 +23,7 @@ import org.h2.result.SortOrder;
import org.h2.store.DataHandler; import org.h2.store.DataHandler;
import org.h2.tools.SimpleResultSet; import org.h2.tools.SimpleResultSet;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.Utils;
import org.h2.value.CompareMode; import org.h2.value.CompareMode;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueArray; import org.h2.value.ValueArray;
...@@ -489,7 +490,7 @@ public class ValueDataType implements DataType { ...@@ -489,7 +490,7 @@ public class ValueDataType implements DataType {
case Value.DECIMAL: { case Value.DECIMAL: {
int scale = readVarInt(buff); int scale = readVarInt(buff);
int len = readVarInt(buff); int len = readVarInt(buff);
byte[] buff2 = DataUtils.newBytes(len); byte[] buff2 = Utils.newBytes(len);
buff.get(buff2, 0, len); buff.get(buff2, 0, len);
BigInteger b = new BigInteger(buff2); BigInteger b = new BigInteger(buff2);
return ValueDecimal.get(new BigDecimal(b, scale)); return ValueDecimal.get(new BigDecimal(b, scale));
...@@ -514,13 +515,13 @@ public class ValueDataType implements DataType { ...@@ -514,13 +515,13 @@ public class ValueDataType implements DataType {
} }
case Value.BYTES: { case Value.BYTES: {
int len = readVarInt(buff); int len = readVarInt(buff);
byte[] b = DataUtils.newBytes(len); byte[] b = Utils.newBytes(len);
buff.get(b, 0, len); buff.get(b, 0, len);
return ValueBytes.getNoCopy(b); return ValueBytes.getNoCopy(b);
} }
case Value.JAVA_OBJECT: { case Value.JAVA_OBJECT: {
int len = readVarInt(buff); int len = readVarInt(buff);
byte[] b = DataUtils.newBytes(len); byte[] b = Utils.newBytes(len);
buff.get(b, 0, len); buff.get(b, 0, len);
return ValueJavaObject.getNoCopy(null, b, handler); return ValueJavaObject.getNoCopy(null, b, handler);
} }
...@@ -550,7 +551,7 @@ public class ValueDataType implements DataType { ...@@ -550,7 +551,7 @@ public class ValueDataType implements DataType {
case Value.CLOB: { case Value.CLOB: {
int smallLen = readVarInt(buff); int smallLen = readVarInt(buff);
if (smallLen >= 0) { if (smallLen >= 0) {
byte[] small = DataUtils.newBytes(smallLen); byte[] small = Utils.newBytes(smallLen);
buff.get(small, 0, smallLen); buff.get(small, 0, smallLen);
return ValueLobDb.createSmallLob(type, small); return ValueLobDb.createSmallLob(type, small);
} else if (smallLen == -3) { } else if (smallLen == -3) {
...@@ -597,7 +598,7 @@ public class ValueDataType implements DataType { ...@@ -597,7 +598,7 @@ public class ValueDataType implements DataType {
} }
case Value.GEOMETRY: { case Value.GEOMETRY: {
int len = readVarInt(buff); int len = readVarInt(buff);
byte[] b = DataUtils.newBytes(len); byte[] b = Utils.newBytes(len);
buff.get(b, 0, len); buff.get(b, 0, len);
return ValueGeometry.get(b); return ValueGeometry.get(b);
} }
...@@ -607,7 +608,7 @@ public class ValueDataType implements DataType { ...@@ -607,7 +608,7 @@ public class ValueDataType implements DataType {
if (JdbcUtils.customDataTypesHandler != null) { if (JdbcUtils.customDataTypesHandler != null) {
int customType = readVarInt(buff); int customType = readVarInt(buff);
int len = readVarInt(buff); int len = readVarInt(buff);
byte[] b = DataUtils.newBytes(len); byte[] b = Utils.newBytes(len);
buff.get(b, 0, len); buff.get(b, 0, len);
return JdbcUtils.customDataTypesHandler.convert( return JdbcUtils.customDataTypesHandler.convert(
ValueBytes.getNoCopy(b), customType); ValueBytes.getNoCopy(b), customType);
...@@ -622,7 +623,7 @@ public class ValueDataType implements DataType { ...@@ -622,7 +623,7 @@ public class ValueDataType implements DataType {
return ValueLong.get(type - LONG_0_7); return ValueLong.get(type - LONG_0_7);
} else if (type >= BYTES_0_31 && type < BYTES_0_31 + 32) { } else if (type >= BYTES_0_31 && type < BYTES_0_31 + 32) {
int len = type - BYTES_0_31; int len = type - BYTES_0_31;
byte[] b = DataUtils.newBytes(len); byte[] b = Utils.newBytes(len);
buff.get(b, 0, len); buff.get(b, 0, len);
return ValueBytes.getNoCopy(b); return ValueBytes.getNoCopy(b);
} else if (type >= STRING_0_31 && type < STRING_0_31 + 32) { } else if (type >= STRING_0_31 && type < STRING_0_31 + 32) {
......
...@@ -19,6 +19,7 @@ import java.util.HashMap; ...@@ -19,6 +19,7 @@ import java.util.HashMap;
import java.util.UUID; import java.util.UUID;
import org.h2.mvstore.DataUtils; import org.h2.mvstore.DataUtils;
import org.h2.mvstore.WriteBuffer; import org.h2.mvstore.WriteBuffer;
import org.h2.util.Utils;
/** /**
* A data type implementation for the most common data types, including * A data type implementation for the most common data types, including
...@@ -1000,7 +1001,7 @@ public class ObjectDataType implements DataType { ...@@ -1000,7 +1001,7 @@ public class ObjectDataType implements DataType {
return BigInteger.valueOf(DataUtils.readVarLong(buff)); return BigInteger.valueOf(DataUtils.readVarLong(buff));
} }
int len = DataUtils.readVarInt(buff); int len = DataUtils.readVarInt(buff);
byte[] bytes = DataUtils.newBytes(len); byte[] bytes = Utils.newBytes(len);
buff.get(bytes); buff.get(bytes);
return new BigInteger(bytes); return new BigInteger(bytes);
} }
...@@ -1077,7 +1078,7 @@ public class ObjectDataType implements DataType { ...@@ -1077,7 +1078,7 @@ public class ObjectDataType implements DataType {
} }
int scale = DataUtils.readVarInt(buff); int scale = DataUtils.readVarInt(buff);
int len = DataUtils.readVarInt(buff); int len = DataUtils.readVarInt(buff);
byte[] bytes = DataUtils.newBytes(len); byte[] bytes = Utils.newBytes(len);
buff.get(bytes); buff.get(bytes);
BigInteger b = new BigInteger(bytes); BigInteger b = new BigInteger(bytes);
return new BigDecimal(b, scale); return new BigDecimal(b, scale);
...@@ -1414,7 +1415,7 @@ public class ObjectDataType implements DataType { ...@@ -1414,7 +1415,7 @@ public class ObjectDataType implements DataType {
if (tag != TYPE_ARRAY) { if (tag != TYPE_ARRAY) {
byte[] data; byte[] data;
int len = tag - TAG_BYTE_ARRAY_0_15; int len = tag - TAG_BYTE_ARRAY_0_15;
data = DataUtils.newBytes(len); data = Utils.newBytes(len);
buff.get(data); buff.get(data);
return data; return data;
} }
...@@ -1545,7 +1546,7 @@ public class ObjectDataType implements DataType { ...@@ -1545,7 +1546,7 @@ public class ObjectDataType implements DataType {
@Override @Override
public Object read(ByteBuffer buff, int tag) { public Object read(ByteBuffer buff, int tag) {
int len = DataUtils.readVarInt(buff); int len = DataUtils.readVarInt(buff);
byte[] data = DataUtils.newBytes(len); byte[] data = Utils.newBytes(len);
int size = data.length * 2; int size = data.length * 2;
// adjust the average size // adjust the average size
// using an exponential moving average // using an exponential moving average
......
...@@ -40,7 +40,6 @@ import org.h2.jdbc.JdbcPreparedStatement; ...@@ -40,7 +40,6 @@ import org.h2.jdbc.JdbcPreparedStatement;
import org.h2.jdbc.JdbcResultSet; import org.h2.jdbc.JdbcResultSet;
import org.h2.jdbc.JdbcStatement; import org.h2.jdbc.JdbcStatement;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
...@@ -152,7 +151,7 @@ public class PgServerThread implements Runnable { ...@@ -152,7 +151,7 @@ public class PgServerThread implements Runnable {
} }
int len = dataInRaw.readInt(); int len = dataInRaw.readInt();
len -= 4; len -= 4;
byte[] data = DataUtils.newBytes(len); byte[] data = Utils.newBytes(len);
dataInRaw.readFully(data, 0, len); dataInRaw.readFully(data, 0, len);
dataIn = new DataInputStream(new ByteArrayInputStream(data, 0, len)); dataIn = new DataInputStream(new ByteArrayInputStream(data, 0, len));
switch (x) { switch (x) {
...@@ -639,7 +638,7 @@ public class PgServerThread implements Runnable { ...@@ -639,7 +638,7 @@ public class PgServerThread implements Runnable {
prep.setNull(col, Types.NULL); prep.setNull(col, Types.NULL);
} else if (text) { } else if (text) {
// plain text // plain text
byte[] data = DataUtils.newBytes(paramLen); byte[] data = Utils.newBytes(paramLen);
readFully(data); readFully(data);
String str = new String(data, getEncoding()); String str = new String(data, getEncoding());
switch (pgType) { switch (pgType) {
...@@ -687,13 +686,13 @@ public class PgServerThread implements Runnable { ...@@ -687,13 +686,13 @@ public class PgServerThread implements Runnable {
prep.setDouble(col, dataIn.readDouble()); prep.setDouble(col, dataIn.readDouble());
break; break;
case PgServer.PG_TYPE_BYTEA: case PgServer.PG_TYPE_BYTEA:
byte[] d1 = DataUtils.newBytes(paramLen); byte[] d1 = Utils.newBytes(paramLen);
readFully(d1); readFully(d1);
prep.setBytes(col, d1); prep.setBytes(col, d1);
break; break;
default: default:
server.trace("Binary format for type: "+pgType+" is unsupported"); server.trace("Binary format for type: "+pgType+" is unsupported");
byte[] d2 = DataUtils.newBytes(paramLen); byte[] d2 = Utils.newBytes(paramLen);
readFully(d2); readFully(d2);
prep.setString(col, new String(d2, getEncoding())); prep.setString(col, new String(d2, getEncoding()));
} }
......
...@@ -20,10 +20,10 @@ import java.util.StringTokenizer; ...@@ -20,10 +20,10 @@ import java.util.StringTokenizer;
import org.h2.engine.SysProperties; import org.h2.engine.SysProperties;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.NetUtils; import org.h2.util.NetUtils;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.util.Utils;
/** /**
* For each connection to a session, an object of this class is created. * For each connection to a session, an object of this class is created.
...@@ -317,7 +317,7 @@ class WebThread extends WebApp implements Runnable { ...@@ -317,7 +317,7 @@ class WebThread extends WebApp implements Runnable {
if (multipart) { if (multipart) {
// not supported // not supported
} else if (session != null && len > 0) { } else if (session != null && len > 0) {
byte[] bytes = DataUtils.newBytes(len); byte[] bytes = Utils.newBytes(len);
for (int pos = 0; pos < len;) { for (int pos = 0; pos < len;) {
pos += input.read(bytes, pos, len - pos); pos += input.read(bytes, pos, len - pos);
} }
......
...@@ -23,11 +23,11 @@ import org.h2.api.ErrorCode; ...@@ -23,11 +23,11 @@ import org.h2.api.ErrorCode;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.engine.SysProperties; import org.h2.engine.SysProperties;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.tools.SimpleResultSet; import org.h2.tools.SimpleResultSet;
import org.h2.util.Bits; import org.h2.util.Bits;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.Utils;
import org.h2.value.DataType; import org.h2.value.DataType;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueArray; import org.h2.value.ValueArray;
...@@ -745,7 +745,7 @@ public class Data { ...@@ -745,7 +745,7 @@ public class Data {
case Value.DECIMAL: { case Value.DECIMAL: {
int scale = readVarInt(); int scale = readVarInt();
int len = readVarInt(); int len = readVarInt();
byte[] buff = DataUtils.newBytes(len); byte[] buff = Utils.newBytes(len);
read(buff, 0, len); read(buff, 0, len);
BigInteger b = new BigInteger(buff); BigInteger b = new BigInteger(buff);
return ValueDecimal.get(new BigDecimal(b, scale)); return ValueDecimal.get(new BigDecimal(b, scale));
...@@ -783,19 +783,19 @@ public class Data { ...@@ -783,19 +783,19 @@ public class Data {
} }
case Value.BYTES: { case Value.BYTES: {
int len = readVarInt(); int len = readVarInt();
byte[] b = DataUtils.newBytes(len); byte[] b = Utils.newBytes(len);
read(b, 0, len); read(b, 0, len);
return ValueBytes.getNoCopy(b); return ValueBytes.getNoCopy(b);
} }
case Value.GEOMETRY: { case Value.GEOMETRY: {
int len = readVarInt(); int len = readVarInt();
byte[] b = DataUtils.newBytes(len); byte[] b = Utils.newBytes(len);
read(b, 0, len); read(b, 0, len);
return ValueGeometry.get(b); return ValueGeometry.get(b);
} }
case Value.JAVA_OBJECT: { case Value.JAVA_OBJECT: {
int len = readVarInt(); int len = readVarInt();
byte[] b = DataUtils.newBytes(len); byte[] b = Utils.newBytes(len);
read(b, 0, len); read(b, 0, len);
return ValueJavaObject.getNoCopy(null, b, handler); return ValueJavaObject.getNoCopy(null, b, handler);
} }
...@@ -825,7 +825,7 @@ public class Data { ...@@ -825,7 +825,7 @@ public class Data {
case Value.CLOB: { case Value.CLOB: {
int smallLen = readVarInt(); int smallLen = readVarInt();
if (smallLen >= 0) { if (smallLen >= 0) {
byte[] small = DataUtils.newBytes(smallLen); byte[] small = Utils.newBytes(smallLen);
read(small, 0, smallLen); read(small, 0, smallLen);
return ValueLobDb.createSmallLob(type, small); return ValueLobDb.createSmallLob(type, small);
} else if (smallLen == -3) { } else if (smallLen == -3) {
...@@ -889,7 +889,7 @@ public class Data { ...@@ -889,7 +889,7 @@ public class Data {
return ValueLong.get(type - LONG_0_7); return ValueLong.get(type - LONG_0_7);
} else if (type >= BYTES_0_31 && type < BYTES_0_31 + 32) { } else if (type >= BYTES_0_31 && type < BYTES_0_31 + 32) {
int len = type - BYTES_0_31; int len = type - BYTES_0_31;
byte[] b = DataUtils.newBytes(len); byte[] b = Utils.newBytes(len);
read(b, 0, len); read(b, 0, len);
return ValueBytes.getNoCopy(b); return ValueBytes.getNoCopy(b);
} else if (type >= STRING_0_31 && type < STRING_0_31 + 32) { } else if (type >= STRING_0_31 && type < STRING_0_31 + 32) {
...@@ -1316,7 +1316,7 @@ public class Data { ...@@ -1316,7 +1316,7 @@ public class Data {
private void expand(int plus) { private void expand(int plus) {
// must copy everything, because pos could be 0 and data may be // must copy everything, because pos could be 0 and data may be
// still required // still required
data = DataUtils.copyBytes(data, (data.length + plus) * 2); data = Utils.copyBytes(data, (data.length + plus) * 2);
} }
/** /**
......
...@@ -9,8 +9,8 @@ import java.io.IOException; ...@@ -9,8 +9,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.tools.CompressTool; import org.h2.tools.CompressTool;
import org.h2.util.Utils;
/** /**
* An input stream that is backed by a file store. * An input stream that is backed by a file store.
...@@ -117,7 +117,7 @@ public class FileStoreInputStream extends InputStream { ...@@ -117,7 +117,7 @@ public class FileStoreInputStream extends InputStream {
page.readInt(); page.readInt();
if (compress != null) { if (compress != null) {
int uncompressed = page.readInt(); int uncompressed = page.readInt();
byte[] buff = DataUtils.newBytes(remainingInBuffer); byte[] buff = Utils.newBytes(remainingInBuffer);
page.read(buff, 0, remainingInBuffer); page.read(buff, 0, remainingInBuffer);
page.reset(); page.reset();
page.checkCapacity(uncompressed); page.checkCapacity(uncompressed);
......
...@@ -15,7 +15,6 @@ import java.nio.channels.FileLock; ...@@ -15,7 +15,6 @@ import java.nio.channels.FileLock;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import org.h2.mvstore.DataUtils;
import org.h2.security.AES; import org.h2.security.AES;
import org.h2.security.BlockCipher; import org.h2.security.BlockCipher;
import org.h2.security.SHA256; import org.h2.security.SHA256;
...@@ -194,11 +193,11 @@ public class FilePathEncrypt extends FilePathWrapper { ...@@ -194,11 +193,11 @@ public class FilePathEncrypt extends FilePathWrapper {
byte[] header = Arrays.copyOf(HEADER, BLOCK_SIZE); byte[] header = Arrays.copyOf(HEADER, BLOCK_SIZE);
salt = MathUtils.secureRandomBytes(SALT_LENGTH); salt = MathUtils.secureRandomBytes(SALT_LENGTH);
System.arraycopy(salt, 0, header, SALT_POS, salt.length); System.arraycopy(salt, 0, header, SALT_POS, salt.length);
DataUtils.writeFully(base, 0, ByteBuffer.wrap(header)); writeFully(base, 0, ByteBuffer.wrap(header));
size = 0; size = 0;
} else { } else {
salt = new byte[SALT_LENGTH]; salt = new byte[SALT_LENGTH];
DataUtils.readFully(base, SALT_POS, ByteBuffer.wrap(salt)); readFully(base, SALT_POS, ByteBuffer.wrap(salt));
if ((size & BLOCK_SIZE_MASK) != 0) { if ((size & BLOCK_SIZE_MASK) != 0) {
size -= BLOCK_SIZE; size -= BLOCK_SIZE;
} }
...@@ -319,7 +318,7 @@ public class FilePathEncrypt extends FilePathWrapper { ...@@ -319,7 +318,7 @@ public class FilePathEncrypt extends FilePathWrapper {
int plus = (int) (size & BLOCK_SIZE_MASK); int plus = (int) (size & BLOCK_SIZE_MASK);
if (plus > 0) { if (plus > 0) {
temp = ByteBuffer.allocate(plus); temp = ByteBuffer.allocate(plus);
DataUtils.writeFully(base, p + HEADER_LENGTH + l, temp); writeFully(base, p + HEADER_LENGTH + l, temp);
} }
return len; return len;
} }
......
...@@ -25,9 +25,9 @@ import org.h2.compress.LZFInputStream; ...@@ -25,9 +25,9 @@ import org.h2.compress.LZFInputStream;
import org.h2.compress.LZFOutputStream; import org.h2.compress.LZFOutputStream;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.util.Bits; import org.h2.util.Bits;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.util.Utils;
/** /**
* A tool to losslessly compress data, and expand the compressed data again. * A tool to losslessly compress data, and expand the compressed data again.
...@@ -44,10 +44,10 @@ public class CompressTool { ...@@ -44,10 +44,10 @@ public class CompressTool {
private byte[] getBuffer(int min) { private byte[] getBuffer(int min) {
if (min > MAX_BUFFER_SIZE) { if (min > MAX_BUFFER_SIZE) {
return DataUtils.newBytes(min); return Utils.newBytes(min);
} }
if (cachedBuffer == null || cachedBuffer.length < min) { if (cachedBuffer == null || cachedBuffer.length < min) {
cachedBuffer = DataUtils.newBytes(min); cachedBuffer = Utils.newBytes(min);
} }
return cachedBuffer; return cachedBuffer;
} }
...@@ -79,7 +79,7 @@ public class CompressTool { ...@@ -79,7 +79,7 @@ public class CompressTool {
Compressor compress = getCompressor(algorithm); Compressor compress = getCompressor(algorithm);
byte[] buff = getBuffer((len < 100 ? len + 100 : len) * 2); byte[] buff = getBuffer((len < 100 ? len + 100 : len) * 2);
int newLen = compress(in, in.length, compress, buff); int newLen = compress(in, in.length, compress, buff);
return DataUtils.copyBytes(buff, newLen); return Utils.copyBytes(buff, newLen);
} }
private static int compress(byte[] in, int len, Compressor compress, private static int compress(byte[] in, int len, Compressor compress,
...@@ -108,7 +108,7 @@ public class CompressTool { ...@@ -108,7 +108,7 @@ public class CompressTool {
try { try {
int len = readVariableInt(in, 1); int len = readVariableInt(in, 1);
int start = 1 + getVariableIntLength(len); int start = 1 + getVariableIntLength(len);
byte[] buff = DataUtils.newBytes(len); byte[] buff = Utils.newBytes(len);
compress.expand(in, start, in.length - start, buff, 0, len); compress.expand(in, start, in.length - start, buff, 0, len);
return buff; return buff;
} catch (Exception e) { } catch (Exception e) {
......
...@@ -209,6 +209,62 @@ public class Utils { ...@@ -209,6 +209,62 @@ public class Utils {
return target; return target;
} }
/**
* Create an array of bytes with the given size. If this is not possible
* because not enough memory is available, an OutOfMemoryError with the
* requested size in the message is thrown.
* <p>
* This method should be used if the size of the array is user defined, or
* stored in a file, so wrong size data can be distinguished from regular
* out-of-memory.
* </p>
*
* @param len the number of bytes requested
* @return the byte array
* @throws OutOfMemoryError if the allocation was too large
*/
public static byte[] newBytes(int len) {
if (len == 0) {
return EMPTY_BYTES;
}
try {
return new byte[len];
} catch (OutOfMemoryError e) {
Error e2 = new OutOfMemoryError("Requested memory: " + len);
e2.initCause(e);
throw e2;
}
}
/**
* Creates a copy of array of bytes with the new size. If this is not possible
* because not enough memory is available, an OutOfMemoryError with the
* requested size in the message is thrown.
* <p>
* This method should be used if the size of the array is user defined, or
* stored in a file, so wrong size data can be distinguished from regular
* out-of-memory.
* </p>
*
* @param bytes source array
* @param len the number of bytes in the new array
* @return the byte array
* @throws OutOfMemoryError if the allocation was too large
* @see Arrays#copyOf(byte[], int)
*/
public static byte[] copyBytes(byte[] bytes, int len) {
if (len == 0) {
return EMPTY_BYTES;
}
try {
return Arrays.copyOf(bytes, len);
} catch (OutOfMemoryError e) {
Error e2 = new OutOfMemoryError("Requested memory: " + len);
e2.initCause(e);
throw e2;
}
}
/** /**
* Create a new byte array and copy all the data. If the size of the byte * Create a new byte array and copy all the data. If the size of the byte
* array is zero, the same array is returned. * array is zero, the same array is returned.
......
...@@ -22,7 +22,6 @@ import org.h2.api.ErrorCode; ...@@ -22,7 +22,6 @@ import org.h2.api.ErrorCode;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.engine.SessionInterface; import org.h2.engine.SessionInterface;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.security.SHA256; import org.h2.security.SHA256;
import org.h2.store.Data; import org.h2.store.Data;
import org.h2.store.DataReader; import org.h2.store.DataReader;
...@@ -284,7 +283,7 @@ public class Transfer { ...@@ -284,7 +283,7 @@ public class Transfer {
if (len == -1) { if (len == -1) {
return null; return null;
} }
byte[] b = DataUtils.newBytes(len); byte[] b = Utils.newBytes(len);
in.readFully(b); in.readFully(b);
return b; return b;
} }
......
...@@ -19,7 +19,6 @@ import org.h2.engine.Constants; ...@@ -19,7 +19,6 @@ import org.h2.engine.Constants;
import org.h2.engine.Mode; import org.h2.engine.Mode;
import org.h2.engine.SysProperties; import org.h2.engine.SysProperties;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.store.DataHandler; import org.h2.store.DataHandler;
import org.h2.store.FileStore; import org.h2.store.FileStore;
import org.h2.store.FileStoreInputStream; import org.h2.store.FileStoreInputStream;
...@@ -430,11 +429,11 @@ public class ValueLob extends Value { ...@@ -430,11 +429,11 @@ public class ValueLob extends Value {
buff = IOUtils.readBytesAndClose(in, -1); buff = IOUtils.readBytesAndClose(in, -1);
len = buff.length; len = buff.length;
} else { } else {
buff = DataUtils.newBytes(len); buff = Utils.newBytes(len);
len = IOUtils.readFully(in, buff, len); len = IOUtils.readFully(in, buff, len);
} }
if (len <= handler.getMaxLengthInplaceLob()) { if (len <= handler.getMaxLengthInplaceLob()) {
byte[] small = DataUtils.copyBytes(buff, len); byte[] small = Utils.copyBytes(buff, len);
return ValueLob.createSmallLob(Value.BLOB, small); return ValueLob.createSmallLob(Value.BLOB, small);
} }
ValueLob lob = new ValueLob(Value.BLOB, null); ValueLob lob = new ValueLob(Value.BLOB, null);
...@@ -796,7 +795,7 @@ public class ValueLob extends Value { ...@@ -796,7 +795,7 @@ public class ValueLob extends Value {
int tabId = tableId; int tabId = tableId;
if (type == Value.BLOB) { if (type == Value.BLOB) {
createFromStream( createFromStream(
DataUtils.newBytes(len), 0, getInputStream(), Long.MAX_VALUE, h); Utils.newBytes(len), 0, getInputStream(), Long.MAX_VALUE, h);
} else { } else {
createFromReader( createFromReader(
new char[len], 0, getReader(), Long.MAX_VALUE, h); new char[len], 0, getReader(), Long.MAX_VALUE, h);
......
...@@ -19,7 +19,6 @@ import org.h2.engine.Constants; ...@@ -19,7 +19,6 @@ import org.h2.engine.Constants;
import org.h2.engine.Mode; import org.h2.engine.Mode;
import org.h2.engine.SysProperties; import org.h2.engine.SysProperties;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.store.DataHandler; import org.h2.store.DataHandler;
import org.h2.store.FileStore; import org.h2.store.FileStore;
import org.h2.store.FileStoreInputStream; import org.h2.store.FileStoreInputStream;
...@@ -611,11 +610,11 @@ public class ValueLobDb extends Value implements Value.ValueClob, ...@@ -611,11 +610,11 @@ public class ValueLobDb extends Value implements Value.ValueClob,
buff = IOUtils.readBytesAndClose(in, -1); buff = IOUtils.readBytesAndClose(in, -1);
len = buff.length; len = buff.length;
} else { } else {
buff = DataUtils.newBytes(len); buff = Utils.newBytes(len);
len = IOUtils.readFully(in, buff, len); len = IOUtils.readFully(in, buff, len);
} }
if (len <= handler.getMaxLengthInplaceLob()) { if (len <= handler.getMaxLengthInplaceLob()) {
byte[] small = DataUtils.copyBytes(buff, len); byte[] small = Utils.copyBytes(buff, len);
return ValueLobDb.createSmallLob(Value.BLOB, small, small.length); return ValueLobDb.createSmallLob(Value.BLOB, small, small.length);
} }
ValueLobDb lob = new ValueLobDb(handler, buff, len, in, remaining); ValueLobDb lob = new ValueLobDb(handler, buff, len, in, remaining);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论