summaryrefslogtreecommitdiff
path: root/kafka/util.py
diff options
context:
space:
mode:
authorDavid Arthur <mumrah@gmail.com>2013-02-13 10:22:13 -0500
committerDavid Arthur <mumrah@gmail.com>2013-04-02 20:19:29 -0400
commit71fef1b1555c2fb15a89411a5a6f79baebe4d3ae (patch)
tree44a22260928f75fa557bd36ce06a31b999c58d59 /kafka/util.py
parent0bc2afe910e29431cf6effad6ba3464d4c10597e (diff)
downloadkafka-python-71fef1b1555c2fb15a89411a5a6f79baebe4d3ae.tar.gz
Starting work on 0.8 compat
Diffstat (limited to 'kafka/util.py')
-rw-r--r--kafka/util.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/kafka/util.py b/kafka/util.py
new file mode 100644
index 0000000..6f27637
--- /dev/null
+++ b/kafka/util.py
@@ -0,0 +1,38 @@
+import struct
+
+def write_int_string(s):
+ return struct.pack('>i%ds' % len(s), len(s), s)
+
+def write_short_string(s):
+ return struct.pack('>H%ds' % len(s), len(s), s)
+
+def read_short_string(data, cur):
+ if len(data) < cur+2:
+ raise IOError("Not enough data left")
+ (strLen,) = struct.unpack('>H', data[cur:cur+2])
+ if strLen == -1:
+ return (None, cur+2)
+ cur += 2
+ if len(data) < cur+strLen:
+ raise IOError("Not enough data left")
+ out = data[cur:cur+strLen]
+ return (out, cur+strLen)
+
+def read_int_string(data, cur):
+ if len(data) < cur+4:
+ raise IOError("Not enough data left")
+ (strLen,) = struct.unpack('>i', data[cur:cur+4])
+ if strLen == -1:
+ return (None, cur+4)
+ cur += 4
+ if len(data) < cur+strLen:
+ raise IOError("Not enough data left")
+ out = data[cur:cur+strLen]
+ return (out, cur+strLen)
+
+def relative_unpack(fmt, data, cur):
+ size = struct.calcsize(fmt)
+ if len(data) < cur+size:
+ raise IOError("Not enough data left")
+ out = struct.unpack(fmt, data[cur:cur+size])
+ return (out, cur+size)