提交 65e68391 authored 作者: Thomas Mueller's avatar Thomas Mueller

reuse empty arrays

上级 2e2a137a
......@@ -9,7 +9,7 @@ package org.h2.compress;
import java.io.IOException;
import java.io.InputStream;
import org.h2.util.ByteUtils;
import org.h2.util.MemoryUtils;
/**
* An input stream to read from an LZF stream.
......@@ -32,7 +32,7 @@ public class LZFInputStream extends InputStream {
}
private byte[] ensureSize(byte[] buff, int len) {
return buff == null || buff.length < len ? ByteUtils.newBytes(len) : buff;
return buff == null || buff.length < len ? MemoryUtils.newBytes(len) : buff;
}
private void fillBuffer() throws IOException {
......
......@@ -41,7 +41,6 @@ import org.h2.table.TableFilter;
import org.h2.tools.CompressTool;
import org.h2.tools.Csv;
import org.h2.util.AutoCloseInputStream;
import org.h2.util.ByteUtils;
import org.h2.util.DateTimeIso8601Utils;
import org.h2.util.DateTimeUtils;
import org.h2.util.FileUtils;
......@@ -1172,7 +1171,7 @@ public class Function extends Expression implements FunctionCall {
private byte[] getPaddedArrayCopy(byte[] data, int blockSize) {
int size = MathUtils.roundUp(data.length, blockSize);
byte[] newData = ByteUtils.newBytes(size);
byte[] newData = MemoryUtils.newBytes(size);
System.arraycopy(data, 0, newData, 0, data.length);
return newData;
}
......
......@@ -18,6 +18,7 @@ import org.h2.store.DataPage;
import org.h2.store.DiskFile;
import org.h2.table.Column;
import org.h2.util.IntArray;
import org.h2.util.MemoryUtils;
import org.h2.util.ObjectArray;
import org.h2.value.Value;
......@@ -37,7 +38,7 @@ public class BtreeNode extends BtreePage {
BtreeNode(BtreeIndex index, DataPage s) throws SQLException {
super(index);
int len = s.readInt();
int[] array = new int[len];
int[] array = MemoryUtils.newInts(len);
for (int i = 0; i < array.length; i++) {
array[i] = s.readInt();
}
......
......@@ -33,6 +33,7 @@ import org.h2.message.Message;
import org.h2.message.Trace;
import org.h2.message.TraceObject;
import org.h2.result.ResultInterface;
import org.h2.util.MemoryUtils;
import org.h2.util.ObjectUtils;
import org.h2.value.Value;
import org.h2.value.ValueInt;
......@@ -1403,7 +1404,7 @@ public class JdbcConnection extends TraceObject implements Connection {
int id = getNextId(TraceObject.CLOB);
debugCodeAssign("Clob", TraceObject.CLOB, id, "createClob()");
checkClosedForWrite();
ValueLob v = ValueLob.createSmallLob(Value.CLOB, new byte[0]);
ValueLob v = ValueLob.createSmallLob(Value.CLOB, MemoryUtils.EMPTY_BYTES);
return new JdbcClob(this, v, id);
} catch (Exception e) {
throw logAndConvert(e);
......@@ -1420,7 +1421,7 @@ public class JdbcConnection extends TraceObject implements Connection {
int id = getNextId(TraceObject.BLOB);
debugCodeAssign("Blob", TraceObject.BLOB, id, "createClob()");
checkClosedForWrite();
ValueLob v = ValueLob.createSmallLob(Value.BLOB, new byte[0]);
ValueLob v = ValueLob.createSmallLob(Value.BLOB, MemoryUtils.EMPTY_BYTES);
return new JdbcBlob(this, v, id);
} catch (Exception e) {
throw logAndConvert(e);
......@@ -1438,7 +1439,7 @@ public class JdbcConnection extends TraceObject implements Connection {
int id = getNextId(TraceObject.CLOB);
debugCodeAssign("NClob", TraceObject.CLOB, id, "createNClob()");
checkClosedForWrite();
ValueLob v = ValueLob.createSmallLob(Value.CLOB, new byte[0]);
ValueLob v = ValueLob.createSmallLob(Value.CLOB, ByteUtils.EMPTY);
return new JdbcClob(this, v, id);
} catch (Exception e) {
throw logAndConvert(e);
......
......@@ -21,9 +21,9 @@ import org.h2.store.DiskFile;
import org.h2.store.FileStore;
import org.h2.store.Record;
import org.h2.store.Storage;
import org.h2.util.ByteUtils;
import org.h2.util.FileUtils;
import org.h2.util.MathUtils;
import org.h2.util.MemoryUtils;
import org.h2.util.ObjectArray;
/**
......@@ -220,7 +220,7 @@ public class LogFile {
// Math.abs(Integer.MIN_VALUE) == Integer.MIN_VALUE
s.reset();
} else {
byte[] b2 = ByteUtils.newBytes(blocks * BLOCK_SIZE);
byte[] b2 = MemoryUtils.newBytes(blocks * BLOCK_SIZE);
System.arraycopy(buff, 0, b2, 0, BLOCK_SIZE);
buff = b2;
file.readFully(buff, BLOCK_SIZE, blocks * BLOCK_SIZE - BLOCK_SIZE);
......@@ -308,7 +308,7 @@ public class LogFile {
break;
}
int sumLength = in.readInt();
byte[] summary = ByteUtils.newBytes(sumLength);
byte[] summary = MemoryUtils.newBytes(sumLength);
if (sumLength > 0) {
in.read(summary, 0, sumLength);
}
......
......@@ -34,9 +34,9 @@ import org.h2.constant.SysProperties;
import org.h2.engine.ConnectionInfo;
import org.h2.jdbc.JdbcConnection;
import org.h2.message.Message;
import org.h2.util.ByteUtils;
import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils;
import org.h2.util.MemoryUtils;
import org.h2.util.New;
import org.h2.util.Resources;
import org.h2.util.ScriptReader;
......@@ -133,7 +133,7 @@ public class PgServerThread implements Runnable {
}
int len = dataInRaw.readInt();
len -= 4;
byte[] data = ByteUtils.newBytes(len);
byte[] data = MemoryUtils.newBytes(len);
dataInRaw.readFully(data, 0, len);
dataIn = new DataInputStream(new ByteArrayInputStream(data, 0, len));
switch (x) {
......@@ -245,7 +245,7 @@ public class PgServerThread implements Runnable {
int paramCount = readShort();
for (int i = 0; i < paramCount; i++) {
int paramLen = readInt();
byte[] d2 = ByteUtils.newBytes(paramLen);
byte[] d2 = MemoryUtils.newBytes(paramLen);
readFully(d2);
try {
setParameter(portal.prep, i, d2, formatCodes);
......
......@@ -62,7 +62,6 @@ import org.h2.tools.Restore;
import org.h2.tools.RunScript;
import org.h2.tools.Script;
import org.h2.tools.SimpleResultSet;
import org.h2.util.ByteUtils;
import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils;
......@@ -408,7 +407,7 @@ class WebThread extends Thread implements DatabaseEventListener {
}
}
if (session != null && len > 0) {
byte[] bytes = ByteUtils.newBytes(len);
byte[] bytes = MemoryUtils.newBytes(len);
for (int pos = 0; pos < len;) {
pos += input.read(bytes, pos, len - pos);
}
......
......@@ -253,23 +253,4 @@ public class ByteUtils {
return copy;
}
/**
* 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.
*
* @param len the number of bytes requested
* @return the byte array
* @throws OutOfMemoryError
*/
public static byte[] newBytes(int len) {
try {
return new byte[len];
} catch (OutOfMemoryError e) {
Error e2 = new OutOfMemoryError("Requested memory: " + len);
e2.initCause(e);
throw e2;
}
}
}
......@@ -13,6 +13,16 @@ import org.h2.constant.SysProperties;
*/
public class MemoryUtils {
/**
* An 0-size byte array.
*/
public static final byte[] EMPTY_BYTES = new byte[0];
/**
* An 0-size int array.
*/
public static final int[] EMPTY_INTS = new int[0];
private static long lastGC;
private static final int GC_DELAY = 50;
private static final int MAX_GC = 8;
......@@ -80,4 +90,39 @@ public class MemoryUtils {
reserveMemory = null;
}
/**
* 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.
*
* @param len the number of bytes requested
* @return the byte array
* @throws OutOfMemoryError
*/
public static byte[] newBytes(int len) {
try {
if (len == 0) {
return EMPTY_BYTES;
}
return new byte[len];
} catch (OutOfMemoryError e) {
Error e2 = new OutOfMemoryError("Requested memory: " + len);
e2.initCause(e);
throw e2;
}
}
/**
* Create an array of ints with the given size.
*
* @param len the number of bytes requested
* @return the int array
*/
public static int[] newInts(int len) {
if (len == 0) {
return EMPTY_INTS;
}
return new int[len];
}
}
......@@ -76,7 +76,7 @@ public class Resources {
} else {
data = FILES.get(name);
}
return data == null ? new byte[0] : data;
return data == null ? MemoryUtils.EMPTY_BYTES : data;
}
}
......@@ -33,9 +33,9 @@ import org.h2.engine.SessionInterface;
import org.h2.message.Message;
import org.h2.message.TraceSystem;
import org.h2.tools.SimpleResultSet;
import org.h2.util.ByteUtils;
import org.h2.util.ExactUTF8InputStreamReader;
import org.h2.util.IOUtils;
import org.h2.util.MemoryUtils;
import org.h2.util.NetUtils;
import org.h2.util.StringCache;
......@@ -274,7 +274,7 @@ public class Transfer {
if (len == -1) {
return null;
}
byte[] b = ByteUtils.newBytes(len);
byte[] b = MemoryUtils.newBytes(len);
in.readFully(b);
return b;
}
......
......@@ -8,13 +8,14 @@ package org.h2.value;
import org.h2.constant.SysProperties;
import org.h2.util.ByteUtils;
import org.h2.util.MemoryUtils;
/**
* Implementation of the BINARY data type.
*/
public class ValueBytes extends ValueBytesBase {
private static final ValueBytes EMPTY = new ValueBytes(new byte[0]);
private static final ValueBytes EMPTY = new ValueBytes(MemoryUtils.EMPTY_BYTES);
protected ValueBytes(byte[] v) {
super(v);
......
......@@ -11,6 +11,7 @@ import java.sql.SQLException;
import java.sql.Types;
import org.h2.constant.SysProperties;
import org.h2.util.MemoryUtils;
import org.h2.util.ObjectUtils;
/**
......@@ -18,7 +19,7 @@ import org.h2.util.ObjectUtils;
*/
public class ValueJavaObject extends ValueBytesBase {
private static final ValueJavaObject EMPTY = new ValueJavaObject(new byte[0]);
private static final ValueJavaObject EMPTY = new ValueJavaObject(MemoryUtils.EMPTY_BYTES);
protected ValueJavaObject(byte[] v) {
super(v);
......
......@@ -27,6 +27,7 @@ import org.h2.util.ByteUtils;
import org.h2.util.FileUtils;
import org.h2.util.IOUtils;
import org.h2.util.MathUtils;
import org.h2.util.MemoryUtils;
import org.h2.util.SmallLRUCache;
import org.h2.util.StringUtils;
......@@ -353,11 +354,11 @@ public class ValueLob extends Value {
buff = IOUtils.readBytesAndClose(in, -1);
len = buff.length;
} else {
buff = ByteUtils.newBytes(len);
buff = MemoryUtils.newBytes(len);
len = IOUtils.readFully(in, buff, 0, len);
}
if (len <= handler.getMaxLengthInplaceLob()) {
byte[] small = ByteUtils.newBytes(len);
byte[] small = MemoryUtils.newBytes(len);
System.arraycopy(buff, 0, small, 0, len);
return ValueLob.createSmallLob(Value.BLOB, small);
}
......@@ -723,7 +724,7 @@ public class ValueLob extends Value {
int len = getBufferSize(handler, compress, Long.MAX_VALUE);
int tabId = tableId;
if (type == Value.BLOB) {
createFromStream(ByteUtils.newBytes(len), 0, getInputStream(), Long.MAX_VALUE, handler);
createFromStream(MemoryUtils.newBytes(len), 0, getInputStream(), Long.MAX_VALUE, handler);
} else {
createFromReader(new char[len], 0, getReader(), Long.MAX_VALUE, handler);
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论