提交 5e8bd060 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Move newBytes() and copyBytes() to Utils

上级 dda78956
......@@ -8,7 +8,7 @@ package org.h2.compress;
import java.io.IOException;
import java.io.InputStream;
import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.util.Utils;
/**
* An input stream to read from an LZF stream.
......@@ -31,7 +31,7 @@ public class LZFInputStream extends InputStream {
}
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 {
......
......@@ -31,7 +31,6 @@ import org.h2.engine.Database;
import org.h2.engine.Mode;
import org.h2.engine.Session;
import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.schema.Schema;
import org.h2.schema.Sequence;
import org.h2.security.BlockCipher;
......@@ -1819,7 +1818,7 @@ public class Function extends Expression implements FunctionCall {
private static byte[] getPaddedArrayCopy(byte[] data, int 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) {
......
......@@ -153,11 +153,6 @@ public final class DataUtils {
*/
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.
*
......@@ -937,62 +932,6 @@ public final class DataUtils {
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.
*
......
......@@ -26,6 +26,7 @@ import org.h2.mvstore.type.DataType;
import org.h2.mvstore.type.StringDataType;
import org.h2.store.fs.FilePath;
import org.h2.store.fs.FileUtils;
import org.h2.util.Utils;
/**
* Utility methods used in combination with the MVStore.
......@@ -226,7 +227,7 @@ public class MVStoreTool {
Compressor compressor = getCompressor(fast);
int lenAdd = DataUtils.readVarInt(chunk);
int compLen = pageSize + start - chunk.position();
byte[] comp = DataUtils.newBytes(compLen);
byte[] comp = Utils.newBytes(compLen);
chunk.get(comp);
int l = compLen + lenAdd;
data = ByteBuffer.allocate(l);
......
......@@ -9,6 +9,7 @@ import java.nio.ByteBuffer;
import java.util.HashSet;
import org.h2.compress.Compressor;
import org.h2.mvstore.type.DataType;
import org.h2.util.Utils;
/**
* A page (a node or a leaf).
......@@ -702,7 +703,7 @@ public class Page {
}
int lenAdd = DataUtils.readVarInt(buff);
int compLen = pageLength + start - buff.position();
byte[] comp = DataUtils.newBytes(compLen);
byte[] comp = Utils.newBytes(compLen);
buff.get(comp);
int l = compLen + lenAdd;
buff = ByteBuffer.allocate(l);
......
......@@ -23,6 +23,7 @@ import org.h2.result.SortOrder;
import org.h2.store.DataHandler;
import org.h2.tools.SimpleResultSet;
import org.h2.util.JdbcUtils;
import org.h2.util.Utils;
import org.h2.value.CompareMode;
import org.h2.value.Value;
import org.h2.value.ValueArray;
......@@ -489,7 +490,7 @@ public class ValueDataType implements DataType {
case Value.DECIMAL: {
int scale = readVarInt(buff);
int len = readVarInt(buff);
byte[] buff2 = DataUtils.newBytes(len);
byte[] buff2 = Utils.newBytes(len);
buff.get(buff2, 0, len);
BigInteger b = new BigInteger(buff2);
return ValueDecimal.get(new BigDecimal(b, scale));
......@@ -514,13 +515,13 @@ public class ValueDataType implements DataType {
}
case Value.BYTES: {
int len = readVarInt(buff);
byte[] b = DataUtils.newBytes(len);
byte[] b = Utils.newBytes(len);
buff.get(b, 0, len);
return ValueBytes.getNoCopy(b);
}
case Value.JAVA_OBJECT: {
int len = readVarInt(buff);
byte[] b = DataUtils.newBytes(len);
byte[] b = Utils.newBytes(len);
buff.get(b, 0, len);
return ValueJavaObject.getNoCopy(null, b, handler);
}
......@@ -550,7 +551,7 @@ public class ValueDataType implements DataType {
case Value.CLOB: {
int smallLen = readVarInt(buff);
if (smallLen >= 0) {
byte[] small = DataUtils.newBytes(smallLen);
byte[] small = Utils.newBytes(smallLen);
buff.get(small, 0, smallLen);
return ValueLobDb.createSmallLob(type, small);
} else if (smallLen == -3) {
......@@ -597,7 +598,7 @@ public class ValueDataType implements DataType {
}
case Value.GEOMETRY: {
int len = readVarInt(buff);
byte[] b = DataUtils.newBytes(len);
byte[] b = Utils.newBytes(len);
buff.get(b, 0, len);
return ValueGeometry.get(b);
}
......@@ -607,7 +608,7 @@ public class ValueDataType implements DataType {
if (JdbcUtils.customDataTypesHandler != null) {
int customType = readVarInt(buff);
int len = readVarInt(buff);
byte[] b = DataUtils.newBytes(len);
byte[] b = Utils.newBytes(len);
buff.get(b, 0, len);
return JdbcUtils.customDataTypesHandler.convert(
ValueBytes.getNoCopy(b), customType);
......@@ -622,7 +623,7 @@ public class ValueDataType implements DataType {
return ValueLong.get(type - LONG_0_7);
} else if (type >= BYTES_0_31 && type < BYTES_0_31 + 32) {
int len = type - BYTES_0_31;
byte[] b = DataUtils.newBytes(len);
byte[] b = Utils.newBytes(len);
buff.get(b, 0, len);
return ValueBytes.getNoCopy(b);
} else if (type >= STRING_0_31 && type < STRING_0_31 + 32) {
......
......@@ -19,6 +19,7 @@ import java.util.HashMap;
import java.util.UUID;
import org.h2.mvstore.DataUtils;
import org.h2.mvstore.WriteBuffer;
import org.h2.util.Utils;
/**
* A data type implementation for the most common data types, including
......@@ -1000,7 +1001,7 @@ public class ObjectDataType implements DataType {
return BigInteger.valueOf(DataUtils.readVarLong(buff));
}
int len = DataUtils.readVarInt(buff);
byte[] bytes = DataUtils.newBytes(len);
byte[] bytes = Utils.newBytes(len);
buff.get(bytes);
return new BigInteger(bytes);
}
......@@ -1077,7 +1078,7 @@ public class ObjectDataType implements DataType {
}
int scale = DataUtils.readVarInt(buff);
int len = DataUtils.readVarInt(buff);
byte[] bytes = DataUtils.newBytes(len);
byte[] bytes = Utils.newBytes(len);
buff.get(bytes);
BigInteger b = new BigInteger(bytes);
return new BigDecimal(b, scale);
......@@ -1414,7 +1415,7 @@ public class ObjectDataType implements DataType {
if (tag != TYPE_ARRAY) {
byte[] data;
int len = tag - TAG_BYTE_ARRAY_0_15;
data = DataUtils.newBytes(len);
data = Utils.newBytes(len);
buff.get(data);
return data;
}
......@@ -1545,7 +1546,7 @@ public class ObjectDataType implements DataType {
@Override
public Object read(ByteBuffer buff, int tag) {
int len = DataUtils.readVarInt(buff);
byte[] data = DataUtils.newBytes(len);
byte[] data = Utils.newBytes(len);
int size = data.length * 2;
// adjust the average size
// using an exponential moving average
......
......@@ -40,7 +40,6 @@ import org.h2.jdbc.JdbcPreparedStatement;
import org.h2.jdbc.JdbcResultSet;
import org.h2.jdbc.JdbcStatement;
import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.util.DateTimeUtils;
import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils;
......@@ -152,7 +151,7 @@ public class PgServerThread implements Runnable {
}
int len = dataInRaw.readInt();
len -= 4;
byte[] data = DataUtils.newBytes(len);
byte[] data = Utils.newBytes(len);
dataInRaw.readFully(data, 0, len);
dataIn = new DataInputStream(new ByteArrayInputStream(data, 0, len));
switch (x) {
......@@ -639,7 +638,7 @@ public class PgServerThread implements Runnable {
prep.setNull(col, Types.NULL);
} else if (text) {
// plain text
byte[] data = DataUtils.newBytes(paramLen);
byte[] data = Utils.newBytes(paramLen);
readFully(data);
String str = new String(data, getEncoding());
switch (pgType) {
......@@ -687,13 +686,13 @@ public class PgServerThread implements Runnable {
prep.setDouble(col, dataIn.readDouble());
break;
case PgServer.PG_TYPE_BYTEA:
byte[] d1 = DataUtils.newBytes(paramLen);
byte[] d1 = Utils.newBytes(paramLen);
readFully(d1);
prep.setBytes(col, d1);
break;
default:
server.trace("Binary format for type: "+pgType+" is unsupported");
byte[] d2 = DataUtils.newBytes(paramLen);
byte[] d2 = Utils.newBytes(paramLen);
readFully(d2);
prep.setString(col, new String(d2, getEncoding()));
}
......
......@@ -20,10 +20,10 @@ import java.util.StringTokenizer;
import org.h2.engine.SysProperties;
import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.util.IOUtils;
import org.h2.util.NetUtils;
import org.h2.util.StringUtils;
import org.h2.util.Utils;
/**
* For each connection to a session, an object of this class is created.
......@@ -317,7 +317,7 @@ class WebThread extends WebApp implements Runnable {
if (multipart) {
// not supported
} else if (session != null && len > 0) {
byte[] bytes = DataUtils.newBytes(len);
byte[] bytes = Utils.newBytes(len);
for (int pos = 0; pos < len;) {
pos += input.read(bytes, pos, len - pos);
}
......
......@@ -23,11 +23,11 @@ import org.h2.api.ErrorCode;
import org.h2.engine.Constants;
import org.h2.engine.SysProperties;
import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.tools.SimpleResultSet;
import org.h2.util.Bits;
import org.h2.util.DateTimeUtils;
import org.h2.util.MathUtils;
import org.h2.util.Utils;
import org.h2.value.DataType;
import org.h2.value.Value;
import org.h2.value.ValueArray;
......@@ -745,7 +745,7 @@ public class Data {
case Value.DECIMAL: {
int scale = readVarInt();
int len = readVarInt();
byte[] buff = DataUtils.newBytes(len);
byte[] buff = Utils.newBytes(len);
read(buff, 0, len);
BigInteger b = new BigInteger(buff);
return ValueDecimal.get(new BigDecimal(b, scale));
......@@ -783,19 +783,19 @@ public class Data {
}
case Value.BYTES: {
int len = readVarInt();
byte[] b = DataUtils.newBytes(len);
byte[] b = Utils.newBytes(len);
read(b, 0, len);
return ValueBytes.getNoCopy(b);
}
case Value.GEOMETRY: {
int len = readVarInt();
byte[] b = DataUtils.newBytes(len);
byte[] b = Utils.newBytes(len);
read(b, 0, len);
return ValueGeometry.get(b);
}
case Value.JAVA_OBJECT: {
int len = readVarInt();
byte[] b = DataUtils.newBytes(len);
byte[] b = Utils.newBytes(len);
read(b, 0, len);
return ValueJavaObject.getNoCopy(null, b, handler);
}
......@@ -825,7 +825,7 @@ public class Data {
case Value.CLOB: {
int smallLen = readVarInt();
if (smallLen >= 0) {
byte[] small = DataUtils.newBytes(smallLen);
byte[] small = Utils.newBytes(smallLen);
read(small, 0, smallLen);
return ValueLobDb.createSmallLob(type, small);
} else if (smallLen == -3) {
......@@ -889,7 +889,7 @@ public class Data {
return ValueLong.get(type - LONG_0_7);
} else if (type >= BYTES_0_31 && type < BYTES_0_31 + 32) {
int len = type - BYTES_0_31;
byte[] b = DataUtils.newBytes(len);
byte[] b = Utils.newBytes(len);
read(b, 0, len);
return ValueBytes.getNoCopy(b);
} else if (type >= STRING_0_31 && type < STRING_0_31 + 32) {
......@@ -1316,7 +1316,7 @@ public class Data {
private void expand(int plus) {
// must copy everything, because pos could be 0 and data may be
// 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;
import java.io.InputStream;
import org.h2.engine.Constants;
import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.tools.CompressTool;
import org.h2.util.Utils;
/**
* An input stream that is backed by a file store.
......@@ -117,7 +117,7 @@ public class FileStoreInputStream extends InputStream {
page.readInt();
if (compress != null) {
int uncompressed = page.readInt();
byte[] buff = DataUtils.newBytes(remainingInBuffer);
byte[] buff = Utils.newBytes(remainingInBuffer);
page.read(buff, 0, remainingInBuffer);
page.reset();
page.checkCapacity(uncompressed);
......
......@@ -25,9 +25,9 @@ import org.h2.compress.LZFInputStream;
import org.h2.compress.LZFOutputStream;
import org.h2.engine.Constants;
import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.util.Bits;
import org.h2.util.StringUtils;
import org.h2.util.Utils;
/**
* A tool to losslessly compress data, and expand the compressed data again.
......@@ -44,10 +44,10 @@ public class CompressTool {
private byte[] getBuffer(int min) {
if (min > MAX_BUFFER_SIZE) {
return DataUtils.newBytes(min);
return Utils.newBytes(min);
}
if (cachedBuffer == null || cachedBuffer.length < min) {
cachedBuffer = DataUtils.newBytes(min);
cachedBuffer = Utils.newBytes(min);
}
return cachedBuffer;
}
......@@ -79,7 +79,7 @@ public class CompressTool {
Compressor compress = getCompressor(algorithm);
byte[] buff = getBuffer((len < 100 ? len + 100 : len) * 2);
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,
......@@ -108,7 +108,7 @@ public class CompressTool {
try {
int len = readVariableInt(in, 1);
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);
return buff;
} catch (Exception e) {
......
......@@ -209,6 +209,62 @@ public class Utils {
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
* array is zero, the same array is returned.
......
......@@ -22,7 +22,6 @@ import org.h2.api.ErrorCode;
import org.h2.engine.Constants;
import org.h2.engine.SessionInterface;
import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.security.SHA256;
import org.h2.store.Data;
import org.h2.store.DataReader;
......@@ -284,7 +283,7 @@ public class Transfer {
if (len == -1) {
return null;
}
byte[] b = DataUtils.newBytes(len);
byte[] b = Utils.newBytes(len);
in.readFully(b);
return b;
}
......
......@@ -19,7 +19,6 @@ import org.h2.engine.Constants;
import org.h2.engine.Mode;
import org.h2.engine.SysProperties;
import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.store.DataHandler;
import org.h2.store.FileStore;
import org.h2.store.FileStoreInputStream;
......@@ -430,11 +429,11 @@ public class ValueLob extends Value {
buff = IOUtils.readBytesAndClose(in, -1);
len = buff.length;
} else {
buff = DataUtils.newBytes(len);
buff = Utils.newBytes(len);
len = IOUtils.readFully(in, buff, len);
}
if (len <= handler.getMaxLengthInplaceLob()) {
byte[] small = DataUtils.copyBytes(buff, len);
byte[] small = Utils.copyBytes(buff, len);
return ValueLob.createSmallLob(Value.BLOB, small);
}
ValueLob lob = new ValueLob(Value.BLOB, null);
......@@ -796,7 +795,7 @@ public class ValueLob extends Value {
int tabId = tableId;
if (type == Value.BLOB) {
createFromStream(
DataUtils.newBytes(len), 0, getInputStream(), Long.MAX_VALUE, h);
Utils.newBytes(len), 0, getInputStream(), Long.MAX_VALUE, h);
} else {
createFromReader(
new char[len], 0, getReader(), Long.MAX_VALUE, h);
......
......@@ -19,7 +19,6 @@ import org.h2.engine.Constants;
import org.h2.engine.Mode;
import org.h2.engine.SysProperties;
import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.store.DataHandler;
import org.h2.store.FileStore;
import org.h2.store.FileStoreInputStream;
......@@ -611,11 +610,11 @@ public class ValueLobDb extends Value implements Value.ValueClob,
buff = IOUtils.readBytesAndClose(in, -1);
len = buff.length;
} else {
buff = DataUtils.newBytes(len);
buff = Utils.newBytes(len);
len = IOUtils.readFully(in, buff, len);
}
if (len <= handler.getMaxLengthInplaceLob()) {
byte[] small = DataUtils.copyBytes(buff, len);
byte[] small = Utils.copyBytes(buff, len);
return ValueLobDb.createSmallLob(Value.BLOB, small, small.length);
}
ValueLobDb lob = new ValueLobDb(handler, buff, len, in, remaining);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论