From 9c8332a3265d16afc7fca8fa3b90d46410ed0839 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Thu, 9 Mar 2006 16:57:52 +0000 Subject: [PATCH] implement GUID reading using patch from joniles, 1445653; replace StringBuffer from StringBuilder git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@38 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/ByteUtil.java | 44 ++++++++++++++----- .../healthmarketscience/jackcess/Column.java | 30 +++++++++++-- .../jackcess/DataType.java | 9 ++-- .../healthmarketscience/jackcess/Index.java | 4 +- .../healthmarketscience/jackcess/Table.java | 4 +- 5 files changed, 68 insertions(+), 23 deletions(-) diff --git a/src/java/com/healthmarketscience/jackcess/ByteUtil.java b/src/java/com/healthmarketscience/jackcess/ByteUtil.java index 5e8d276..408e825 100644 --- a/src/java/com/healthmarketscience/jackcess/ByteUtil.java +++ b/src/java/com/healthmarketscience/jackcess/ByteUtil.java @@ -86,29 +86,49 @@ public final class ByteUtil { * @return The display String */ public static String toHexString(ByteBuffer buffer, int offset, int size) { - - StringBuffer rtn = new StringBuffer(); + return toHexString(buffer, offset, size, true); + } + + /** + * 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 + * @param size Number of bytes to read from the buffer + * @param formatted flag indicating if formatting is required + * @return The display String + */ + public static String toHexString(ByteBuffer buffer, + int offset, int size, boolean formatted) { + + StringBuilder rtn = new StringBuilder(); int position = buffer.position(); buffer.position(offset); - + for (int i = 0; i < size; i++) { byte b = buffer.get(); byte h = (byte) (b & 0xF0); h = (byte) (h >>> 4); - h = (byte) (h & 0x0F); + h = (byte) (h & 0x0F); + rtn.append(HEX_CHARS[(int) h]); + h = (byte) (b & 0x0F); rtn.append(HEX_CHARS[(int) h]); - h = (byte) (b & 0x0F); - rtn.append(HEX_CHARS[(int) h] + " "); - if ((i + 1) % 4 == 0) { + + if (formatted == true) + { rtn.append(" "); - } - if ((i + 1) % 24 == 0) { - rtn.append("\n"); + + if ((i + 1) % 4 == 0) { + rtn.append(" "); + } + if ((i + 1) % 24 == 0) { + rtn.append("\n"); + } } } - + buffer.position(position); return rtn.toString(); - } + } + } diff --git a/src/java/com/healthmarketscience/jackcess/Column.java b/src/java/com/healthmarketscience/jackcess/Column.java index 250c3cf..50bc36a 100644 --- a/src/java/com/healthmarketscience/jackcess/Column.java +++ b/src/java/com/healthmarketscience/jackcess/Column.java @@ -308,8 +308,30 @@ public class Column implements Comparable { intVal = intVal.negate(); } return new BigDecimal(intVal, getScale()); + + } else if (_type == DataType.GUID) { + + StringBuilder sb = new StringBuilder(22); + sb.append("{"); + sb.append(ByteUtil.toHexString(buffer, 0, 4, + false)); + sb.append("-"); + sb.append(ByteUtil.toHexString(buffer, 4, 2, + false)); + sb.append("-"); + sb.append(ByteUtil.toHexString(buffer, 6, 2, + false)); + sb.append("-"); + sb.append(ByteUtil.toHexString(buffer, 8, 2, + false)); + sb.append("-"); + sb.append(ByteUtil.toHexString(buffer, 10, 6, + false)); + sb.append("}"); + return (sb.toString()); + + } else if (_type == DataType.UNKNOWN_0D) { - } else if (_type == DataType.UNKNOWN_0D || _type == DataType.GUID) { return null; } else { throw new IOException("Unrecognized data type: " + _type); @@ -560,7 +582,9 @@ public class Column implements Comparable { return _format.SIZE_LONG_VALUE_DEF; } else if (_type == DataType.NUMERIC) { return 17; - } else if (_type == DataType.UNKNOWN_0D || _type == DataType.GUID) { + } else if (_type == DataType.GUID) { + return 16; + } else if (_type == DataType.UNKNOWN_0D) { throw new IllegalArgumentException("FIX ME"); } else { throw new IllegalArgumentException("Unrecognized data type: " + _type); @@ -568,7 +592,7 @@ public class Column implements Comparable { } public String toString() { - StringBuffer rtn = new StringBuffer(); + StringBuilder rtn = new StringBuilder(); rtn.append("\tName: " + _name); rtn.append("\n\tType: 0x" + Integer.toHexString((int)_type.getValue())); rtn.append("\n\tNumber: " + _columnNumber); diff --git a/src/java/com/healthmarketscience/jackcess/DataType.java b/src/java/com/healthmarketscience/jackcess/DataType.java index 44ad18d..85358ec 100644 --- a/src/java/com/healthmarketscience/jackcess/DataType.java +++ b/src/java/com/healthmarketscience/jackcess/DataType.java @@ -51,7 +51,7 @@ public enum DataType { OLE((byte) 0x0B, Types.LONGVARBINARY, 12), MEMO((byte) 0x0C, Types.LONGVARCHAR, 12), UNKNOWN_0D((byte) 0x0D), - GUID((byte) 0x0F), + GUID((byte) 0x0F, null, 16), NUMERIC((byte) 0x10, Types.NUMERIC, 17); /** Map of SQL types to Access data types */ @@ -87,17 +87,18 @@ public enum DataType { /** SQL type equivalent, or null if none defined */ private Integer _sqlType; - DataType(byte value) { + private DataType(byte value) { _value = value; } - DataType(byte value, int sqlType, int size) { + private DataType(byte value, Integer sqlType, Integer size) { this(value); _sqlType = sqlType; _size = size; } - DataType(byte value, int sqlType, int size, boolean variableLength) { + private DataType(byte value, Integer sqlType, Integer size, + boolean variableLength) { this(value, sqlType, size); _variableLength = variableLength; } diff --git a/src/java/com/healthmarketscience/jackcess/Index.java b/src/java/com/healthmarketscience/jackcess/Index.java index 41bd0c4..294d450 100644 --- a/src/java/com/healthmarketscience/jackcess/Index.java +++ b/src/java/com/healthmarketscience/jackcess/Index.java @@ -245,7 +245,7 @@ public class Index implements Comparable { } public String toString() { - StringBuffer rtn = new StringBuffer(); + StringBuilder rtn = new StringBuilder(); rtn.append("\tName: " + _name); rtn.append("\n\tNumber: " + _indexNumber); rtn.append("\n\tPage number: " + _pageNumber); @@ -399,7 +399,7 @@ public class Index implements Comparable { byte flag = buffer.get(); if (flag != (byte) 0) { if (col.getType() == DataType.TEXT) { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); byte b; while ( (b = buffer.get()) != (byte) 1) { if ((int) b == 43) { diff --git a/src/java/com/healthmarketscience/jackcess/Table.java b/src/java/com/healthmarketscience/jackcess/Table.java index 575d374..d27a826 100644 --- a/src/java/com/healthmarketscience/jackcess/Table.java +++ b/src/java/com/healthmarketscience/jackcess/Table.java @@ -537,7 +537,7 @@ public class Table { } public String toString() { - StringBuffer rtn = new StringBuffer(); + StringBuilder rtn = new StringBuilder(); rtn.append("Type: " + _tableType); rtn.append("\nRow count: " + _rowCount); rtn.append("\nColumn count: " + _columnCount); @@ -569,7 +569,7 @@ public class Table { */ public String display(long limit) throws IOException { reset(); - StringBuffer rtn = new StringBuffer(); + StringBuilder rtn = new StringBuilder(); Iterator iter = _columns.iterator(); while (iter.hasNext()) { Column col = (Column) iter.next(); -- 2.39.5