From 0aa749f025e5678a619eb8b3428f10ae91dfec1e Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Wed, 14 Mar 2012 01:24:16 +0000 Subject: [PATCH] add some more useful byte operations git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@616 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/ByteUtil.java | 42 +++++++++++++++++++ .../healthmarketscience/jackcess/Column.java | 6 +-- .../jackcess/IndexData.java | 3 +- .../jackcess/PropertyMaps.java | 6 +-- .../healthmarketscience/jackcess/Table.java | 6 +-- 5 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/java/com/healthmarketscience/jackcess/ByteUtil.java b/src/java/com/healthmarketscience/jackcess/ByteUtil.java index b39898a..b500268 100644 --- a/src/java/com/healthmarketscience/jackcess/ByteUtil.java +++ b/src/java/com/healthmarketscience/jackcess/ByteUtil.java @@ -290,6 +290,48 @@ public final class ByteUtil { } } + /** + * Reads an array of bytes from the given buffer + * @param buffer Buffer containing the desired bytes + * @param len length of the desired bytes + * @return a new buffer with the given number of bytes from the current + * position in the given buffer + */ + public static byte[] getBytes(ByteBuffer buffer, int len) + { + byte[] bytes = new byte[len]; + buffer.get(bytes); + return bytes; + } + + /** + * Reads an array of bytes from the given buffer at the given offset + * @param buffer Buffer containing the desired bytes + * @param offset Offset at which to read the bytes + * @param len length of the desired bytes + * @return a new buffer with the given number of bytes from the given + * position in the given buffer + */ + public static byte[] getBytes(ByteBuffer buffer, int offset, int len) + { + int origPos = buffer.position(); + try { + buffer.position(offset); + return getBytes(buffer, len); + } finally { + buffer.position(origPos); + } + } + + /** + * Concatenates and returns the given byte arrays. + */ + public static byte[] concat(byte[] b1, byte[] b2) { + byte[] out = new byte[b1.length + b2.length]; + System.arraycopy(b1, 0, out, 0, b1.length); + System.arraycopy(b2, 0, out, b1.length, b2.length); + return out; + } /** * Sets all bits in the given remaining byte range to 0. diff --git a/src/java/com/healthmarketscience/jackcess/Column.java b/src/java/com/healthmarketscience/jackcess/Column.java index 6023c26..cd28621 100644 --- a/src/java/com/healthmarketscience/jackcess/Column.java +++ b/src/java/com/healthmarketscience/jackcess/Column.java @@ -1011,8 +1011,7 @@ public class Column implements Comparable { { boolean negate = (buffer.get() != 0); - byte[] tmpArr = new byte[16]; - buffer.get(tmpArr); + byte[] tmpArr = ByteUtil.getBytes(buffer, 16); if(buffer.order() != ByteOrder.BIG_ENDIAN) { fixNumericByteOrder(tmpArr); @@ -1155,8 +1154,7 @@ public class Column implements Comparable { private static String readGUIDValue(ByteBuffer buffer, ByteOrder order) { if(order != ByteOrder.BIG_ENDIAN) { - byte[] tmpArr = new byte[16]; - buffer.get(tmpArr); + byte[] tmpArr = ByteUtil.getBytes(buffer, 16); // the first 3 guid components are integer components which need to // respect endianness, so swap 4-byte int, 2-byte int, 2-byte int diff --git a/src/java/com/healthmarketscience/jackcess/IndexData.java b/src/java/com/healthmarketscience/jackcess/IndexData.java index 637f763..c51a016 100644 --- a/src/java/com/healthmarketscience/jackcess/IndexData.java +++ b/src/java/com/healthmarketscience/jackcess/IndexData.java @@ -1690,8 +1690,7 @@ public abstract class IndexData { int colEntryLen = entryLen - (4 + extraTrailingLen); // read the entry bytes - _entryBytes = new byte[colEntryLen]; - buffer.get(_entryBytes); + _entryBytes = ByteUtil.getBytes(buffer, colEntryLen); // read the rowId int page = ByteUtil.get3ByteInt(buffer, ENTRY_BYTE_ORDER); diff --git a/src/java/com/healthmarketscience/jackcess/PropertyMaps.java b/src/java/com/healthmarketscience/jackcess/PropertyMaps.java index 2120b56..51853ee 100644 --- a/src/java/com/healthmarketscience/jackcess/PropertyMaps.java +++ b/src/java/com/healthmarketscience/jackcess/PropertyMaps.java @@ -236,8 +236,7 @@ public class PropertyMaps implements Iterable String propName = propNames.get(nameIdx); PropColumn col = getColumn(dataType, propName, dataSize); - byte[] data = new byte[dataSize]; - bbBlock.get(data); + byte[] data = ByteUtil.getBytes(bbBlock, dataSize); Object value = col.read(data); map.put(propName, dataType, flag, value); @@ -253,8 +252,7 @@ public class PropertyMaps implements Iterable */ private String readPropName(ByteBuffer buffer) { int nameLength = buffer.getShort(); - byte[] nameBytes = new byte[nameLength]; - buffer.get(nameBytes); + byte[] nameBytes = ByteUtil.getBytes(buffer, nameLength); return Column.decodeUncompressedText(nameBytes, _database.getCharset()); } diff --git a/src/java/com/healthmarketscience/jackcess/Table.java b/src/java/com/healthmarketscience/jackcess/Table.java index df53a88..2a00126 100644 --- a/src/java/com/healthmarketscience/jackcess/Table.java +++ b/src/java/com/healthmarketscience/jackcess/Table.java @@ -715,9 +715,8 @@ public class Table } // grab the column data - columnData = new byte[colDataLen]; rowBuffer.position(colDataPos); - rowBuffer.get(columnData); + columnData = ByteUtil.getBytes(rowBuffer, colDataLen); if((rawVarValues != null) && column.isVariableLength()) { // caller wants raw value as well @@ -1322,8 +1321,7 @@ public class Table */ private String readName(ByteBuffer buffer) { int nameLength = readNameLength(buffer); - byte[] nameBytes = new byte[nameLength]; - buffer.get(nameBytes); + byte[] nameBytes = ByteUtil.getBytes(buffer, nameLength); return Column.decodeUncompressedText(nameBytes, getDatabase().getCharset()); } -- 2.39.5