From: James Ahlborn Date: Sun, 9 Mar 2008 21:33:04 +0000 (+0000) Subject: formalize code for reading unsigned bytes X-Git-Tag: rel_1_1_13~37 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f37b7c544921f6554130fa91ee245658311df68e;p=jackcess.git formalize code for reading unsigned bytes git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@255 f203690c-595d-4dc9-a70b-905162fa7fd2 --- diff --git a/src/java/com/healthmarketscience/jackcess/ByteUtil.java b/src/java/com/healthmarketscience/jackcess/ByteUtil.java index 054affa..876187a 100644 --- a/src/java/com/healthmarketscience/jackcess/ByteUtil.java +++ b/src/java/com/healthmarketscience/jackcess/ByteUtil.java @@ -140,14 +140,38 @@ public final class ByteUtil { offset += 2; } - int rtn = toUnsignedInt(buffer.get(offset)); - rtn += (toUnsignedInt(buffer.get(offset + (1 * offInc))) << 8); - rtn += (toUnsignedInt(buffer.get(offset + (2 * offInc))) << 16); - rtn &= 0xFFFFFF; + int rtn = getUnsignedByte(buffer, offset); + rtn += (getUnsignedByte(buffer, offset + (1 * offInc)) << 8); + rtn += (getUnsignedByte(buffer, offset + (2 * offInc)) << 16); return rtn; } /** + * Read a 3 byte int from a buffer + * @param buffer Buffer containing the bytes + * @return The int + */ + public static int getUnsignedByte(ByteBuffer buffer) { + int pos = buffer.position(); + int rtn = getUnsignedByte(buffer, pos); + buffer.position(pos + 1); + return rtn; + } + + /** + * Read a 3 byte int from a buffer + * @param buffer Buffer containing the bytes + * @param offset Offset at which to read the byte + * @return The int + */ + public static int getUnsignedByte(ByteBuffer buffer, int offset) { + return asUnsignedByte(buffer.get(offset)); + } + + + /** + * @param buffer Buffer containing the bytes + * @param order the order of the bytes of the int * @return an int from the current position in the given buffer, read using * the given ByteOrder */ @@ -159,6 +183,9 @@ public final class ByteUtil { } /** + * @param buffer Buffer containing the bytes + * @param offset Offset at which to start reading the int + * @param order the order of the bytes of the int * @return an int from the given position in the given buffer, read using * the given ByteOrder */ @@ -174,6 +201,9 @@ public final class ByteUtil { /** * Writes an int at the current position in the given buffer, using the * given ByteOrder + * @param buffer buffer into which to insert the int + * @param val Int to insert + * @param order the order to insert the bytes of the int */ public static void putInt(ByteBuffer buffer, int val, ByteOrder order) { int offset = buffer.position(); @@ -184,6 +214,10 @@ public final class ByteUtil { /** * Writes an int at the given position in the given buffer, using the * given ByteOrder + * @param buffer buffer into which to insert the int + * @param val Int to insert + * @param offset offset at which to insert the int + * @param order the order to insert the bytes of the int */ public static void putInt(ByteBuffer buffer, int val, int offset, ByteOrder order) @@ -250,6 +284,15 @@ public final class ByteUtil { return toHexString(buffer, 0, size); } + /** + * Convert a byte array to a hexadecimal string for display + * @param buffer Buffer to display, starting at offset 0 + * @return The display String + */ + public static String toHexString(byte[] array) { + return toHexString(ByteBuffer.wrap(array), 0, array.length); + } + /** * Convert a byte buffer to a hexadecimal string for display * @param buffer Buffer to display, starting at offset 0 @@ -341,7 +384,7 @@ public final class ByteUtil { /** * @return the byte value converted to an unsigned int value */ - public static int toUnsignedInt(byte b) { + public static int asUnsignedByte(byte b) { return b & 0xFF; } diff --git a/src/java/com/healthmarketscience/jackcess/Column.java b/src/java/com/healthmarketscience/jackcess/Column.java index c262b84..9d3583a 100644 --- a/src/java/com/healthmarketscience/jackcess/Column.java +++ b/src/java/com/healthmarketscience/jackcess/Column.java @@ -452,7 +452,7 @@ public class Column implements Comparable { lvalDefinition.length); } - int rowNum = ByteUtil.toUnsignedInt(def.get()); + int rowNum = ByteUtil.getUnsignedByte(def); int pageNum = ByteUtil.get3ByteInt(def, def.position()); ByteBuffer lvalPage = getPageChannel().createPageBuffer(); @@ -486,7 +486,7 @@ public class Column implements Comparable { // read next page information lvalPage.position(rowStart); - rowNum = ByteUtil.toUnsignedInt(lvalPage.get()); + rowNum = ByteUtil.getUnsignedByte(lvalPage); pageNum = ByteUtil.get3ByteInt(lvalPage); // update rowEnd and remainingLen based on chunkLength diff --git a/src/java/com/healthmarketscience/jackcess/Index.java b/src/java/com/healthmarketscience/jackcess/Index.java index fb21194..8ba49f8 100644 --- a/src/java/com/healthmarketscience/jackcess/Index.java +++ b/src/java/com/healthmarketscience/jackcess/Index.java @@ -138,8 +138,8 @@ public class Index implements Comparable { ++pos; } if(pos < len) { - return ((ByteUtil.toUnsignedInt(left[pos]) < - ByteUtil.toUnsignedInt(right[pos])) ? -1 : 1); + return ((ByteUtil.asUnsignedByte(left[pos]) < + ByteUtil.asUnsignedByte(right[pos])) ? -1 : 1); } return ((left.length < right.length) ? -1 : ((left.length > right.length) ? 1 : 0)); @@ -466,8 +466,8 @@ public class Index implements Comparable { { // note, "header" data is in LITTLE_ENDIAN format, entry data is in // BIG_ENDIAN format - int numCompressedBytes = indexPage.get( - getFormat().OFFSET_INDEX_COMPRESSED_BYTE_COUNT); + int numCompressedBytes = ByteUtil.getUnsignedByte( + indexPage, getFormat().OFFSET_INDEX_COMPRESSED_BYTE_COUNT); int entryMaskLength = getFormat().SIZE_INDEX_ENTRY_MASK; int entryMaskPos = getFormat().OFFSET_INDEX_ENTRY_MASK; int entryPos = entryMaskPos + getFormat().SIZE_INDEX_ENTRY_MASK; @@ -1364,7 +1364,7 @@ public class Index implements Comparable { // read the rowId int page = ByteUtil.get3ByteInt(buffer, ByteOrder.BIG_ENDIAN); - int row = ByteUtil.toUnsignedInt(buffer.get()); + int row = ByteUtil.getUnsignedByte(buffer); _rowId = new RowId(page, row); _type = EntryType.NORMAL; diff --git a/src/java/com/healthmarketscience/jackcess/Table.java b/src/java/com/healthmarketscience/jackcess/Table.java index b87698e..8d09f8b 100644 --- a/src/java/com/healthmarketscience/jackcess/Table.java +++ b/src/java/com/healthmarketscience/jackcess/Table.java @@ -580,7 +580,7 @@ public class Table // Overflow page. the "row" data in the current page points to // another page/row - int overflowRowNum = ByteUtil.toUnsignedInt(rowBuffer.get(rowStart)); + int overflowRowNum = ByteUtil.getUnsignedByte(rowBuffer, rowStart); int overflowPageNum = ByteUtil.get3ByteInt(rowBuffer, rowStart + 1); rowBuffer = rowState.setOverflowRow( new RowId(overflowPageNum, overflowRowNum)); @@ -915,12 +915,12 @@ public class Table _indexSlotCount = tableBuffer.getInt(getFormat().OFFSET_NUM_INDEX_SLOTS); _indexCount = tableBuffer.getInt(getFormat().OFFSET_NUM_INDEXES); - int rowNum = ByteUtil.toUnsignedInt( - tableBuffer.get(getFormat().OFFSET_OWNED_PAGES)); + int rowNum = ByteUtil.getUnsignedByte( + tableBuffer, getFormat().OFFSET_OWNED_PAGES); int pageNum = ByteUtil.get3ByteInt(tableBuffer, getFormat().OFFSET_OWNED_PAGES + 1); _ownedPages = UsageMap.read(getDatabase(), pageNum, rowNum, false); - rowNum = ByteUtil.toUnsignedInt( - tableBuffer.get(getFormat().OFFSET_FREE_SPACE_PAGES)); + rowNum = ByteUtil.getUnsignedByte( + tableBuffer, getFormat().OFFSET_FREE_SPACE_PAGES); pageNum = ByteUtil.get3ByteInt(tableBuffer, getFormat().OFFSET_FREE_SPACE_PAGES + 1); _freeSpacePages = UsageMap.read(getDatabase(), pageNum, rowNum, false); @@ -1340,7 +1340,7 @@ public class Table Object obj = iter.next(); if (obj instanceof byte[]) { byte[] b = (byte[]) obj; - rtn.append(ByteUtil.toHexString(ByteBuffer.wrap(b), b.length)); + rtn.append(ByteUtil.toHexString(b)); //This block can be used to easily dump a binary column to a file /*java.io.File f = java.io.File.createTempFile("ole", ".bin"); java.io.FileOutputStream out = new java.io.FileOutputStream(f); diff --git a/test/src/java/com/healthmarketscience/jackcess/IndexTest.java b/test/src/java/com/healthmarketscience/jackcess/IndexTest.java index 51b5da4..917fc81 100644 --- a/test/src/java/com/healthmarketscience/jackcess/IndexTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/IndexTest.java @@ -60,10 +60,10 @@ public class IndexTest extends TestCase { byte b4 = (byte)0x80; byte b5 = (byte)0xFF; - assertTrue(ByteUtil.toUnsignedInt(b1) < ByteUtil.toUnsignedInt(b2)); - assertTrue(ByteUtil.toUnsignedInt(b2) < ByteUtil.toUnsignedInt(b3)); - assertTrue(ByteUtil.toUnsignedInt(b3) < ByteUtil.toUnsignedInt(b4)); - assertTrue(ByteUtil.toUnsignedInt(b4) < ByteUtil.toUnsignedInt(b5)); + assertTrue(ByteUtil.asUnsignedByte(b1) < ByteUtil.asUnsignedByte(b2)); + assertTrue(ByteUtil.asUnsignedByte(b2) < ByteUtil.asUnsignedByte(b3)); + assertTrue(ByteUtil.asUnsignedByte(b3) < ByteUtil.asUnsignedByte(b4)); + assertTrue(ByteUtil.asUnsignedByte(b4) < ByteUtil.asUnsignedByte(b5)); } public void testByteCodeComparator() {