summaryrefslogtreecommitdiffstats
path: root/src/java/com/healthmarketscience/jackcess
diff options
context:
space:
mode:
authorJames Ahlborn <jtahlborn@yahoo.com>2008-03-09 21:33:04 +0000
committerJames Ahlborn <jtahlborn@yahoo.com>2008-03-09 21:33:04 +0000
commitf37b7c544921f6554130fa91ee245658311df68e (patch)
tree0f11c5ec606e4ac71a02e3f15742da71efe6918b /src/java/com/healthmarketscience/jackcess
parentcefe53cf3bf0381eeb02ec7a9e04d639fbba2d51 (diff)
downloadjackcess-f37b7c544921f6554130fa91ee245658311df68e.tar.gz
jackcess-f37b7c544921f6554130fa91ee245658311df68e.zip
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
Diffstat (limited to 'src/java/com/healthmarketscience/jackcess')
-rw-r--r--src/java/com/healthmarketscience/jackcess/ByteUtil.java53
-rw-r--r--src/java/com/healthmarketscience/jackcess/Column.java4
-rw-r--r--src/java/com/healthmarketscience/jackcess/Index.java10
-rw-r--r--src/java/com/healthmarketscience/jackcess/Table.java12
4 files changed, 61 insertions, 18 deletions
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)
@@ -251,6 +285,15 @@ public final class ByteUtil {
}
/**
+ * 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
* @param offset Offset at which to start reading the buffer
@@ -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<Column> {
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<Column> {
// 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<Index> {
++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<Index> {
{
// 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<Index> {
// 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);