Created
October 19, 2012 13:33
-
-
Save cloudaice/3918250 to your computer and use it in GitHub Desktop.
读取java中的字节流数据
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*-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