From 4f8ad5e2727d407274c979d08b9b0bed2387adbb Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Mon, 31 Mar 2008 16:25:42 +0000 Subject: [PATCH] add ByteUtil.getUnsignedShort git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@301 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/ByteUtil.java | 37 +++++++++++++++++-- .../healthmarketscience/jackcess/Table.java | 4 +- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/java/com/healthmarketscience/jackcess/ByteUtil.java b/src/java/com/healthmarketscience/jackcess/ByteUtil.java index 876187a..d27a432 100644 --- a/src/java/com/healthmarketscience/jackcess/ByteUtil.java +++ b/src/java/com/healthmarketscience/jackcess/ByteUtil.java @@ -147,9 +147,9 @@ public final class ByteUtil { } /** - * Read a 3 byte int from a buffer + * Read an unsigned byte from a buffer * @param buffer Buffer containing the bytes - * @return The int + * @return The unsigned byte as an int */ public static int getUnsignedByte(ByteBuffer buffer) { int pos = buffer.position(); @@ -159,14 +159,36 @@ public final class ByteUtil { } /** - * Read a 3 byte int from a buffer + * Read an unsigned byte from a buffer * @param buffer Buffer containing the bytes * @param offset Offset at which to read the byte - * @return The int + * @return The unsigned byte as an int */ public static int getUnsignedByte(ByteBuffer buffer, int offset) { return asUnsignedByte(buffer.get(offset)); } + + /** + * Read an unsigned short from a buffer + * @param buffer Buffer containing the short + * @return The unsigned short as an int + */ + public static int getUnsignedShort(ByteBuffer buffer) { + int pos = buffer.position(); + int rtn = getUnsignedShort(buffer, pos); + buffer.position(pos + 2); + return rtn; + } + + /** + * Read an unsigned short from a buffer + * @param buffer Buffer containing the short + * @param offset Offset at which to read the short + * @return The unsigned short as an int + */ + public static int getUnsignedShort(ByteBuffer buffer, int offset) { + return asUnsignedShort(buffer.getShort(offset)); + } /** @@ -388,4 +410,11 @@ public final class ByteUtil { return b & 0xFF; } + /** + * @return the short value converted to an unsigned int value + */ + public static int asUnsignedShort(short s) { + return s & 0xFFFF; + } + } diff --git a/src/java/com/healthmarketscience/jackcess/Table.java b/src/java/com/healthmarketscience/jackcess/Table.java index 650bce5..11397fa 100644 --- a/src/java/com/healthmarketscience/jackcess/Table.java +++ b/src/java/com/healthmarketscience/jackcess/Table.java @@ -1070,7 +1070,7 @@ public class Table * 2) bytes encoded using the {@link JetFormat#CHARSET} */ private String readName(ByteBuffer buffer) { - short nameLength = buffer.getShort(); + int nameLength = ByteUtil.getUnsignedShort(buffer); byte[] nameBytes = new byte[nameLength]; buffer.get(nameBytes); return Column.decodeUncompressedText(nameBytes, getFormat()); @@ -1081,7 +1081,7 @@ public class Table * expected name format is the same as that for {@link #readName}. */ private void skipName(ByteBuffer buffer) { - short nameLength = buffer.getShort(); + int nameLength = ByteUtil.getUnsignedShort(buffer); buffer.position(buffer.position() + nameLength); } -- 2.39.5