From 5afccb9943076b8c8ca67cce50be77fa05096073 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Wed, 27 Oct 2010 23:54:43 +0000 Subject: [PATCH] minor refactor git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@489 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/ByteUtil.java | 23 +++++++++++++++++-- .../healthmarketscience/jackcess/Column.java | 3 +-- .../jackcess/IndexPageCache.java | 4 +--- .../jackcess/JetFormat.java | 4 +--- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/java/com/healthmarketscience/jackcess/ByteUtil.java b/src/java/com/healthmarketscience/jackcess/ByteUtil.java index 5aa09eb..6fe9d73 100644 --- a/src/java/com/healthmarketscience/jackcess/ByteUtil.java +++ b/src/java/com/healthmarketscience/jackcess/ByteUtil.java @@ -531,6 +531,26 @@ public final class ByteUtil { return newPos; } + /** + * Returns a copy of the the given array of the given length. + */ + public static byte[] copyOf(byte[] arr, int newLength) + { + return copyOf(arr, 0, newLength); + } + + /** + * Returns a copy of the the given array of the given length starting at the + * given position. + */ + public static byte[] copyOf(byte[] arr, int offset, int newLength) + { + byte[] newArr = new byte[newLength]; + int srcLen = arr.length - offset; + System.arraycopy(arr, offset, newArr, 0, Math.min(srcLen, newLength)); + return newArr; + } + /** * Utility byte stream similar to ByteArrayOutputStream but with extended * accessibility to the bytes. @@ -608,8 +628,7 @@ public final class ByteUtil { result = _bytes; _bytes = null; } else { - result = new byte[_length]; - System.arraycopy(_bytes, 0, result, 0, _length); + result = copyOf(_bytes, _length); if(_lastLength == _length) { // if we get the same result length bytes twice in a row, clear the // _bytes so that the next _bytes will be _lastLength diff --git a/src/java/com/healthmarketscience/jackcess/Column.java b/src/java/com/healthmarketscience/jackcess/Column.java index a0391e7..666fa1e 100644 --- a/src/java/com/healthmarketscience/jackcess/Column.java +++ b/src/java/com/healthmarketscience/jackcess/Column.java @@ -1277,8 +1277,7 @@ public class Column implements Comparable { int dataLength = dataEnd - dataStart; if(inCompressedMode) { // handle compressed data - byte[] tmpData = new byte[dataLength]; - System.arraycopy(data, dataStart, tmpData, 0, dataLength); + byte[] tmpData = ByteUtil.copyOf(data, dataStart, dataLength); expander.reset(); textBuf.append(expander.expand(tmpData)); } else { diff --git a/src/java/com/healthmarketscience/jackcess/IndexPageCache.java b/src/java/com/healthmarketscience/jackcess/IndexPageCache.java index 6f9b6a7..62bd1c9 100644 --- a/src/java/com/healthmarketscience/jackcess/IndexPageCache.java +++ b/src/java/com/healthmarketscience/jackcess/IndexPageCache.java @@ -955,9 +955,7 @@ public class IndexPageCache } // need new prefix - byte[] tmpPrefix = new byte[len]; - System.arraycopy(prefix, 0, tmpPrefix, 0, len); - prefix = tmpPrefix; + prefix = ByteUtil.copyOf(prefix, len); } return prefix; diff --git a/src/java/com/healthmarketscience/jackcess/JetFormat.java b/src/java/com/healthmarketscience/jackcess/JetFormat.java index b9748b1..4689797 100644 --- a/src/java/com/healthmarketscience/jackcess/JetFormat.java +++ b/src/java/com/healthmarketscience/jackcess/JetFormat.java @@ -487,9 +487,7 @@ public abstract class JetFormat { protected int defineOffsetMaskedHeader() { return 24; } @Override protected byte[] defineHeaderMask() { - byte[] mask = new byte[BASE_HEADER_MASK.length - 2]; - System.arraycopy(BASE_HEADER_MASK, 0, mask, 0, mask.length); - return mask; + return ByteUtil.copyOf(BASE_HEADER_MASK, BASE_HEADER_MASK.length - 2); } @Override protected int defineOffsetHeaderDate() { return -1; } -- 2.39.5