postgresql-jdbc.git / summary / log / commit / refs
commit 348f1790aaa6bbd00b2b1a1c3c8562724f12446c
Author: Kris Jurka <books@ejurka.com>
Date: Wed Nov 18 12:41:05 2009 +0000
Support reading the new hex escaped bytea format that will be coming
in the 8.5 release. We already send bytea data in true binary for
V3 protocol connections, so there's nothing to be gained there. For
V2 protocol connections bytea send could be sped up to use this
escape format as well, but not tonight.
org/postgresql/util/PGbytea.java | 39 ++++++++++++++++++++++++++++++++++++++-
1 file changed, 38 insertions(+), 1 deletion(-)
diff --git a/org/postgresql/util/PGbytea.java b/org/postgresql/util/PGbytea.java
index 36a888a5..6408cc63 100644
--- a/org/postgresql/util/PGbytea.java
+++ b/org/postgresql/util/PGbytea.java
@@ -3,7 +3,7 @@
* Copyright (c) 2003-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/util/PGbytea.java,v 1.14 2006/11/06 05:49:48 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/util/PGbytea.java,v 1.15 2008/01/08 06:56:31 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -26,6 +26,43 @@ public class PGbytea
{
if (s == null)
return null;
+
+ // Starting with PG 8.5, a new hex format is supported
+ // that starts with "\x". Figure out which format we're
+ // dealing with here.
+ //
+ if (s.length < 2 || s[0] != '\\' || s[1] != 'x') {
+ return toBytesOctalEscaped(s);
+ }
+ return toBytesHexEscaped(s);
+ }
+
+ private static byte[] toBytesHexEscaped(byte[] s)
+ {
+ byte[] output = new byte[(s.length - 2) / 2];
+ for (int i=0; i<output.length; i++) {
+ byte b1 = gethex(s[2 + i*2]);
+ byte b2 = gethex(s[2 + i*2 + 1]);
+ output[i] = (byte)((b1 << 4) | b2);
+ }
+ return output;
+ }
+
+ private static byte gethex(byte b) {
+ // 0-9 == 48-57
+ if (b <= 57)
+ return (byte)(b - 48);
+
+ // a-f == 97-102
+ if (b >= 97)
+ return (byte)(b - 97 + 10);
+
+ // A-F == 65-70
+ return (byte)(b - 65 + 10);
+ }
+
+ private static byte[] toBytesOctalEscaped(byte[] s)
+ {
final int slength = s.length;
byte[] buf = null;
int correctSize = slength;
[parent: 3f525f1afb33]