提交 125671a9 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Use Arrays.copyOf() and Arrays.copyOfRange()

上级 8a670816
...@@ -188,9 +188,7 @@ public class Select extends Query { ...@@ -188,9 +188,7 @@ public class Select extends Query {
return row; return row;
} }
// remove columns so that 'distinct' can filter duplicate rows // remove columns so that 'distinct' can filter duplicate rows
Value[] r2 = new Value[distinctColumnCount]; return Arrays.copyOf(row, distinctColumnCount);
System.arraycopy(row, 0, r2, 0, distinctColumnCount);
return r2;
} }
private boolean isHavingNullOrFalse(Value[] row) { private boolean isHavingNullOrFalse(Value[] row) {
......
...@@ -305,10 +305,8 @@ public class ConnectionInfo implements Cloneable { ...@@ -305,10 +305,8 @@ public class ConnectionInfo implements Cloneable {
if (space < 0) { if (space < 0) {
throw DbException.get(ErrorCode.WRONG_PASSWORD_FORMAT); throw DbException.get(ErrorCode.WRONG_PASSWORD_FORMAT);
} }
char[] np = new char[password.length - space - 1]; char[] np = Arrays.copyOfRange(password, space + 1, password.length);
char[] filePassword = new char[space]; char[] filePassword = Arrays.copyOf(password, space);
System.arraycopy(password, space + 1, np, 0, np.length);
System.arraycopy(password, 0, filePassword, 0, space);
Arrays.fill(password, (char) 0); Arrays.fill(password, (char) 0);
password = np; password = np;
fileEncryptionKey = FilePathEncrypt.getPasswordBytes(filePassword); fileEncryptionKey = FilePathEncrypt.getPasswordBytes(filePassword);
......
...@@ -1786,9 +1786,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -1786,9 +1786,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);
byte[] newData = DataUtils.newBytes(size); return DataUtils.copyBytes(data, size);
System.arraycopy(data, 0, newData, 0, data.length);
return newData;
} }
private static byte[] decrypt(String algorithm, byte[] key, byte[] data) { private static byte[] decrypt(String algorithm, byte[] key, byte[] data) {
......
...@@ -9,6 +9,7 @@ import java.sql.Array; ...@@ -9,6 +9,7 @@ import java.sql.Array;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Types; import java.sql.Types;
import java.util.Arrays;
import java.util.Map; import java.util.Map;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
...@@ -283,9 +284,8 @@ public class JdbcArray extends TraceObject implements Array { ...@@ -283,9 +284,8 @@ public class JdbcArray extends TraceObject implements Array {
throw DbException.getInvalidValueException("index (1.." throw DbException.getInvalidValueException("index (1.."
+ array.length + ")", index); + array.length + ")", index);
} }
Object[] subset = new Object[count]; int offset = (int) (index - 1);
System.arraycopy(array, (int) (index - 1), subset, 0, count); return Arrays.copyOfRange(array, offset, offset + count);
return subset;
} }
/** /**
......
...@@ -13,6 +13,7 @@ import java.nio.channels.FileChannel; ...@@ -13,6 +13,7 @@ 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.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -913,6 +914,7 @@ public final class DataUtils { ...@@ -913,6 +914,7 @@ public final class DataUtils {
* This method should be used if the size of the array is user defined, or * 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 * stored in a file, so wrong size data can be distinguished from regular
* out-of-memory. * out-of-memory.
* </p>
* *
* @param len the number of bytes requested * @param len the number of bytes requested
* @return the byte array * @return the byte array
...@@ -931,6 +933,35 @@ public final class DataUtils { ...@@ -931,6 +933,35 @@ public final class DataUtils {
} }
} }
/**
* 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.
* *
......
...@@ -8,6 +8,8 @@ package org.h2.result; ...@@ -8,6 +8,8 @@ package org.h2.result;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import org.h2.engine.Database; import org.h2.engine.Database;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.expression.Expression; import org.h2.expression.Expression;
...@@ -281,9 +283,7 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -281,9 +283,7 @@ public class LocalResult implements ResultInterface, ResultTarget {
private ValueArray getArrayOfVisible(Value[] values) { private ValueArray getArrayOfVisible(Value[] values) {
if (values.length > visibleColumnCount) { if (values.length > visibleColumnCount) {
Value[] v2 = new Value[visibleColumnCount]; values = Arrays.copyOf(values, visibleColumnCount);
System.arraycopy(values, 0, v2, 0, visibleColumnCount);
values = v2;
} }
return ValueArray.get(values); return ValueArray.get(values);
} }
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
*/ */
package org.h2.result; package org.h2.result;
import java.util.Arrays;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.store.Data; import org.h2.store.Data;
import org.h2.util.StatementBuilder; import org.h2.util.StatementBuilder;
...@@ -35,8 +37,7 @@ public class RowImpl implements Row { ...@@ -35,8 +37,7 @@ public class RowImpl implements Row {
*/ */
@Override @Override
public Row getCopy() { public Row getCopy() {
Value[] d2 = new Value[data.length]; Value[] d2 = Arrays.copyOf(data, data.length);
System.arraycopy(data, 0, d2, 0, data.length);
RowImpl r2 = new RowImpl(d2, memory); RowImpl r2 = new RowImpl(d2, memory);
r2.key = key; r2.key = key;
r2.version = version + 1; r2.version = version + 1;
......
...@@ -8,6 +8,7 @@ package org.h2.schema; ...@@ -8,6 +8,7 @@ package org.h2.schema;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Arrays;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.api.Trigger; import org.h2.api.Trigger;
...@@ -243,8 +244,7 @@ public class TriggerObject extends SchemaObjectBase { ...@@ -243,8 +244,7 @@ public class TriggerObject extends SchemaObjectBase {
newList = convertToObjectList(newRow); newList = convertToObjectList(newRow);
Object[] newListBackup; Object[] newListBackup;
if (before && newList != null) { if (before && newList != null) {
newListBackup = new Object[newList.length]; newListBackup = Arrays.copyOf(newList, newList.length);
System.arraycopy(newList, 0, newListBackup, 0, newList.length);
} else { } else {
newListBackup = null; newListBackup = null;
} }
......
...@@ -17,6 +17,8 @@ import java.sql.ResultSet; ...@@ -17,6 +17,8 @@ import java.sql.ResultSet;
import java.sql.ResultSetMetaData; import java.sql.ResultSetMetaData;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Arrays;
import org.h2.api.ErrorCode; 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;
...@@ -1171,8 +1173,7 @@ public class Data { ...@@ -1171,8 +1173,7 @@ public class Data {
*/ */
public void truncate(int size) { public void truncate(int size) {
if (pos > size) { if (pos > size) {
byte[] buff = new byte[size]; byte[] buff = Arrays.copyOf(data, size);
System.arraycopy(data, 0, buff, 0, size);
this.pos = size; this.pos = size;
data = buff; data = buff;
} }
...@@ -1313,11 +1314,9 @@ public class Data { ...@@ -1313,11 +1314,9 @@ public class Data {
} }
private void expand(int plus) { private void expand(int plus) {
byte[] d = DataUtils.newBytes((data.length + plus) * 2);
// 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
System.arraycopy(data, 0, d, 0, data.length); data = DataUtils.copyBytes(data, (data.length + plus) * 2);
data = d;
} }
/** /**
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
package org.h2.store; package org.h2.store;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Arrays;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.tools.CompressTool; import org.h2.tools.CompressTool;
...@@ -49,9 +51,7 @@ public class FileStoreOutputStream extends OutputStream { ...@@ -49,9 +51,7 @@ public class FileStoreOutputStream extends OutputStream {
page.reset(); page.reset();
if (compress != null) { if (compress != null) {
if (off != 0 || len != buff.length) { if (off != 0 || len != buff.length) {
byte[] b2 = new byte[len]; buff = Arrays.copyOfRange(buff, off, off + len);
System.arraycopy(buff, off, b2, 0, len);
buff = b2;
off = 0; off = 0;
} }
int uncompressed = len; int uncompressed = len;
......
...@@ -368,8 +368,7 @@ public class LobStorageBackend implements LobStorageInterface { ...@@ -368,8 +368,7 @@ public class LobStorageBackend implements LobStorageInterface {
// if we had a short read, trim the buffer // if we had a short read, trim the buffer
byte[] b; byte[] b;
if (len != buff.length) { if (len != buff.length) {
b = new byte[len]; b = Arrays.copyOf(buff, len);
System.arraycopy(buff, 0, b, 0, len);
} else { } else {
b = buff; b = buff;
} }
......
...@@ -444,8 +444,7 @@ class FileMemData { ...@@ -444,8 +444,7 @@ class FileMemData {
static { static {
byte[] n = new byte[BLOCK_SIZE]; byte[] n = new byte[BLOCK_SIZE];
int len = LZF.compress(n, BLOCK_SIZE, BUFFER, 0); int len = LZF.compress(n, BLOCK_SIZE, BUFFER, 0);
COMPRESSED_EMPTY_BLOCK = new byte[len]; COMPRESSED_EMPTY_BLOCK = Arrays.copyOf(BUFFER, len);
System.arraycopy(BUFFER, 0, COMPRESSED_EMPTY_BLOCK, 0, len);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
...@@ -631,8 +630,7 @@ class FileMemData { ...@@ -631,8 +630,7 @@ class FileMemData {
synchronized (LZF) { synchronized (LZF) {
int len = LZF.compress(old, BLOCK_SIZE, BUFFER, 0); int len = LZF.compress(old, BLOCK_SIZE, BUFFER, 0);
if (len <= BLOCK_SIZE) { if (len <= BLOCK_SIZE) {
byte[] d = new byte[len]; byte[] d = Arrays.copyOf(BUFFER, len);
System.arraycopy(BUFFER, 0, d, 0, len);
// maybe data was changed in the meantime // maybe data was changed in the meantime
setPage(page, old, d, false); setPage(page, old, d, false);
} }
......
...@@ -10,6 +10,7 @@ import java.io.OutputStream; ...@@ -10,6 +10,7 @@ import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.channels.FileLock; import java.nio.channels.FileLock;
import java.util.Arrays;
/** /**
* A file system that records all write operations and can re-play them. * A file system that records all write operations and can re-play them.
...@@ -182,9 +183,8 @@ class FileRec extends FileBase { ...@@ -182,9 +183,8 @@ class FileRec extends FileBase {
byte[] buff = src.array(); byte[] buff = src.array();
int len = src.remaining(); int len = src.remaining();
if (src.position() != 0 || len != buff.length) { if (src.position() != 0 || len != buff.length) {
byte[] b = new byte[len]; int offset = src.arrayOffset() + src.position();
System.arraycopy(buff, src.arrayOffset() + src.position(), b, 0, len); buff = Arrays.copyOfRange(buff, offset, offset + len);
buff = b;
} }
int result = channel.write(src); int result = channel.write(src);
rec.log(Recorder.WRITE, name, buff, channel.position()); rec.log(Recorder.WRITE, name, buff, channel.position());
...@@ -196,9 +196,8 @@ class FileRec extends FileBase { ...@@ -196,9 +196,8 @@ class FileRec extends FileBase {
byte[] buff = src.array(); byte[] buff = src.array();
int len = src.remaining(); int len = src.remaining();
if (src.position() != 0 || len != buff.length) { if (src.position() != 0 || len != buff.length) {
byte[] b = new byte[len]; int offset = src.arrayOffset() + src.position();
System.arraycopy(buff, src.arrayOffset() + src.position(), b, 0, len); buff = Arrays.copyOfRange(buff, offset, offset + len);
buff = b;
} }
int result = channel.write(src, position); int result = channel.write(src, position);
rec.log(Recorder.WRITE, name, buff, position); rec.log(Recorder.WRITE, name, buff, position);
......
...@@ -79,9 +79,7 @@ public class CompressTool { ...@@ -79,9 +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);
byte[] out = DataUtils.newBytes(newLen); return DataUtils.copyBytes(buff, newLen);
System.arraycopy(buff, 0, out, 0, newLen);
return out;
} }
private static int compress(byte[] in, int len, Compressor compress, private static int compress(byte[] in, int len, Compressor compress,
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
*/ */
package org.h2.util; package org.h2.util;
import java.util.Arrays;
/** /**
* A list of bits. * A list of bits.
*/ */
...@@ -132,9 +134,7 @@ public final class BitField { ...@@ -132,9 +134,7 @@ public final class BitField {
private void expandCapacity(int size) { private void expandCapacity(int size) {
while (size >= data.length) { while (size >= data.length) {
int newSize = data.length == 0 ? 1 : data.length * 2; int newSize = data.length == 0 ? 1 : data.length * 2;
long[] d = new long[newSize]; data = Arrays.copyOf(data, newSize);
System.arraycopy(data, 0, d, 0, data.length);
data = d;
} }
} }
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
*/ */
package org.h2.util; package org.h2.util;
import java.util.Arrays;
import org.h2.engine.SysProperties; import org.h2.engine.SysProperties;
/** /**
...@@ -93,9 +95,7 @@ public class IntArray { ...@@ -93,9 +95,7 @@ public class IntArray {
public void ensureCapacity(int minCapacity) { public void ensureCapacity(int minCapacity) {
minCapacity = Math.max(4, minCapacity); minCapacity = Math.max(4, minCapacity);
if (minCapacity >= data.length) { if (minCapacity >= data.length) {
int[] d = new int[minCapacity]; data = Arrays.copyOf(data, minCapacity);
System.arraycopy(data, 0, d, 0, data.length);
data = d;
} }
} }
......
...@@ -9,6 +9,7 @@ import java.lang.ref.SoftReference; ...@@ -9,6 +9,7 @@ import java.lang.ref.SoftReference;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -828,9 +829,7 @@ public class StringUtils { ...@@ -828,9 +829,7 @@ public class StringUtils {
if (len == 0) { if (len == 0) {
return chars; return chars;
} }
char[] copy = new char[len]; return Arrays.copyOf(chars, len);
System.arraycopy(chars, 0, copy, 0, len);
return copy;
} }
/** /**
......
...@@ -225,9 +225,7 @@ public class Utils { ...@@ -225,9 +225,7 @@ public class Utils {
if (len == 0) { if (len == 0) {
return EMPTY_BYTES; return EMPTY_BYTES;
} }
byte[] copy = new byte[len]; return Arrays.copyOf(b, len);
System.arraycopy(b, 0, copy, 0, len);
return copy;
} }
/** /**
......
...@@ -149,8 +149,7 @@ public class ValueBytes extends Value { ...@@ -149,8 +149,7 @@ public class ValueBytes extends Value {
return this; return this;
} }
int len = MathUtils.convertLongToInt(precision); int len = MathUtils.convertLongToInt(precision);
byte[] buff = new byte[len]; byte[] buff = Arrays.copyOf(value, len);
System.arraycopy(value, 0, buff, 0, len);
return get(buff); return get(buff);
} }
......
...@@ -416,8 +416,7 @@ public class ValueLob extends Value { ...@@ -416,8 +416,7 @@ public class ValueLob extends Value {
len = IOUtils.readFully(in, buff, len); len = IOUtils.readFully(in, buff, len);
} }
if (len <= handler.getMaxLengthInplaceLob()) { if (len <= handler.getMaxLengthInplaceLob()) {
byte[] small = DataUtils.newBytes(len); byte[] small = DataUtils.copyBytes(buff, len);
System.arraycopy(buff, 0, small, 0, 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);
......
...@@ -600,8 +600,7 @@ public class ValueLobDb extends Value implements Value.ValueClob, ...@@ -600,8 +600,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
len = IOUtils.readFully(in, buff, len); len = IOUtils.readFully(in, buff, len);
} }
if (len <= handler.getMaxLengthInplaceLob()) { if (len <= handler.getMaxLengthInplaceLob()) {
byte[] small = DataUtils.newBytes(len); byte[] small = DataUtils.copyBytes(buff, len);
System.arraycopy(buff, 0, small, 0, 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);
......
...@@ -8,6 +8,7 @@ package org.h2.test.coverage; ...@@ -8,6 +8,7 @@ package org.h2.test.coverage;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.util.Arrays;
/** /**
* Helper class for the java file parser. * Helper class for the java file parser.
...@@ -154,9 +155,7 @@ public class Tokenizer { ...@@ -154,9 +155,7 @@ public class Tokenizer {
int i = 0; int i = 0;
do { do {
if (i >= chars.length) { if (i >= chars.length) {
char[] nb = new char[chars.length * 2]; chars = Arrays.copyOf(chars, chars.length * 2);
System.arraycopy(chars, 0, nb, 0, chars.length);
chars = nb;
} }
chars[i++] = (char) c; chars[i++] = (char) c;
c = read(); c = read();
...@@ -221,9 +220,7 @@ public class Tokenizer { ...@@ -221,9 +220,7 @@ public class Tokenizer {
} }
if (i >= chars.length) { if (i >= chars.length) {
char[] nb = new char[chars.length * 2]; chars = Arrays.copyOf(chars, chars.length * 2);
System.arraycopy(chars, 0, nb, 0, chars.length);
chars = nb;
} }
chars[i++] = (char) c; chars[i++] = (char) c;
} }
......
...@@ -8,6 +8,8 @@ package org.h2.test.db; ...@@ -8,6 +8,8 @@ package org.h2.test.db;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Arrays;
import org.h2.test.utils.SelfDestructor; import org.h2.test.utils.SelfDestructor;
/** /**
...@@ -34,9 +36,7 @@ public abstract class TaskDef { ...@@ -34,9 +36,7 @@ public abstract class TaskDef {
return; return;
} }
try { try {
String[] taskArgs = new String[args.length - 1]; task.run(Arrays.copyOf(args, args.length - 1));
System.arraycopy(args, 0, taskArgs, 0, args.length - 1);
task.run(taskArgs);
} catch (Throwable t) { } catch (Throwable t) {
System.out.println("error: " + t); System.out.println("error: " + t);
t.printStackTrace(); t.printStackTrace();
......
...@@ -187,8 +187,7 @@ public class TestCompress extends TestBase { ...@@ -187,8 +187,7 @@ public class TestCompress extends TestBase {
break; break;
} }
int b = compress.compress(buff2, pageSize, test, 0); int b = compress.compress(buff2, pageSize, test, 0);
byte[] data = new byte[b]; byte[] data = Arrays.copyOf(test, b);
System.arraycopy(test, 0, data, 0, b);
comp.add(data); comp.add(data);
} }
in.close(); in.close();
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
*/ */
package org.h2.build.doc; package org.h2.build.doc;
import java.util.Arrays;
/** /**
* This class implements a simple XML pull parser. * This class implements a simple XML pull parser.
* Only a subset of the XML pull parser API is implemented. * Only a subset of the XML pull parser API is implemented.
...@@ -94,9 +96,7 @@ public class XMLParser { ...@@ -94,9 +96,7 @@ public class XMLParser {
private void addAttributeName(String pre, String name) { private void addAttributeName(String pre, String name) {
if (attributeValues.length <= currentAttribute) { if (attributeValues.length <= currentAttribute) {
String[] temp = new String[attributeValues.length * 2]; attributeValues = Arrays.copyOf(attributeValues, attributeValues.length * 2);
System.arraycopy(attributeValues, 0, temp, 0, attributeValues.length);
attributeValues = temp;
} }
attributeValues[currentAttribute++] = pre; attributeValues[currentAttribute++] = pre;
attributeValues[currentAttribute++] = name; attributeValues[currentAttribute++] = name;
......
...@@ -426,8 +426,7 @@ public class ArchiveTool { ...@@ -426,8 +426,7 @@ public class ArchiveTool {
for (int pos = 0; pos < len;) { for (int pos = 0; pos < len;) {
int[] key = getKey(bytes, pos, len); int[] key = getKey(bytes, pos, len);
int l = key[3]; int l = key[3];
byte[] buff = new byte[l]; byte[] buff = Arrays.copyOfRange(bytes, pos, pos + l);
System.arraycopy(bytes, pos, buff, 0, l);
pos += l; pos += l;
Chunk c = new Chunk(null, key, buff); Chunk c = new Chunk(null, key, buff);
Chunk old = map.get(c); Chunk old = map.get(c);
......
...@@ -117,11 +117,11 @@ public class ArchiveToolStore { ...@@ -117,11 +117,11 @@ public class ArchiveToolStore {
if (buff.remaining() == 0) { if (buff.remaining() == 0) {
break; break;
} }
int c = getChunkLength(buff.array(), buff.position(), int position = buff.position();
buff.limit()) - buff.position(); int c = getChunkLength(buff.array(), position,
byte[] bytes = new byte[c]; buff.limit()) - position;
System.arraycopy(buff.array(), buff.position(), bytes, 0, c); byte[] bytes = Arrays.copyOfRange(buff.array(), position, position + c);
buff.position(buff.position() + c); buff.position(position + c);
int[] key = getKey(bucket, bytes); int[] key = getKey(bucket, bytes);
key[3] = segmentId; key[3] = segmentId;
while (true) { while (true) {
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
*/ */
package org.h2.dev.util; package org.h2.dev.util;
import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import org.h2.mvstore.DataUtils; import org.h2.mvstore.DataUtils;
...@@ -97,11 +98,7 @@ public final class ImmutableArray<K> implements Iterable<K> { ...@@ -97,11 +98,7 @@ public final class ImmutableArray<K> implements Iterable<K> {
* @return the new immutable array * @return the new immutable array
*/ */
public ImmutableArray<K> subArray(int fromIndex, int toIndex) { public ImmutableArray<K> subArray(int fromIndex, int toIndex) {
int len = toIndex - fromIndex; return new ImmutableArray<>(Arrays.copyOfRange(array, fromIndex, toIndex));
@SuppressWarnings("unchecked")
K[] array = (K[]) new Object[len];
System.arraycopy(this.array, fromIndex, array, 0, toIndex - fromIndex);
return new ImmutableArray<>(array);
} }
/** /**
......
...@@ -138,10 +138,7 @@ public final class ImmutableArray2<K> implements Iterable<K> { ...@@ -138,10 +138,7 @@ public final class ImmutableArray2<K> implements Iterable<K> {
if (fromIndex == 0) { if (fromIndex == 0) {
return new ImmutableArray2<>(array, len); return new ImmutableArray2<>(array, len);
} }
@SuppressWarnings("unchecked") return new ImmutableArray2<>(Arrays.copyOfRange(array, fromIndex, toIndex), len);
K[] a2 = (K[]) new Object[len];
System.arraycopy(array, fromIndex, a2, 0, toIndex - fromIndex);
return new ImmutableArray2<>(a2, len);
} }
/** /**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论