Skip to content

Instantly share code, notes, and snippets.

@cloudaice
Created October 19, 2012 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloudaice/3918250 to your computer and use it in GitHub Desktop.
Save cloudaice/3918250 to your computer and use it in GitHub Desktop.
读取java中的字节流数据
# -*-coding: utf-8 -*-
from struct import *
class BinaryStream:
def __init__(self,base_stream):
self.base_stream = base_stream
self.offset = 0
def readBytes(self,length):
string, = unpack_from(str(length) + 's',self.base_stream[self.offset:])
self.offset += calcsize(str(length) + 's')
self.offset +=1
return string
def readUTF(self):
length, = unpack_from('!H',self.base_stream[self.offset:])
self.offset +=calcsize('!H')
string, = unpack_from(str(length) + 's',self.base_stream[self.offset:])
self.offset +=calcsize(str(length) + 's')
return string
def readLong(self):
num, = unpack_from('!Q',self.base_stream[self.offset:])
self.offset +=calcsize('!Q')
return num
def readInt(self):
num, = unpack_from('!I',self.base_stream[self.offset:])
self.offset += calcsize('!I')
return num
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment