Advertisement
Dudugz-Contistente

Untitled

Oct 17th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. from struct import *
  2.  
  3. class ByteArray:
  4. def __init__(this, bytes=""):
  5. this.bytes = bytes
  6.  
  7. def writeByte(this, value):
  8. if type(value) == unicode:
  9. value = value.encode("utf-8")
  10. pass
  11. this.bytes += pack('!b', int(value))
  12. return this
  13.  
  14. def writeUnsignedByte(this, value):
  15. if type(value) == unicode:
  16. value = value.encode("utf-8")
  17. pass
  18. this.bytes += pack('!B', int(value))
  19. return this
  20.  
  21. def writeShort(this, value):
  22. if type(value) == unicode:
  23. value = value.encode("utf-8")
  24. pass
  25. this.bytes += pack('!h', int(value))
  26. return this
  27.  
  28. def writeUnsignedShort(this, value):
  29. if type(value) == unicode:
  30. value = value.encode("utf-8")
  31. pass
  32. this.bytes += pack('!H', int(value))
  33. return this
  34.  
  35. def writeInt(this, value):
  36. if type(value) == unicode:
  37. value = value.encode("utf-8")
  38. pass
  39. this.bytes += pack('!i', int(value))
  40. return this
  41.  
  42. def writeUnsignedInt(this, value):
  43. if type(value) == unicode:
  44. value = value.encode("utf-8")
  45. pass
  46. this.bytes += pack('!I', int(value))
  47. return this
  48.  
  49. def writeBoolean(this, value):
  50. if type(value) == unicode:
  51. value = value.encode("utf-8")
  52. pass
  53. this.bytes += pack('!?', int(value))
  54. return this
  55.  
  56. def writeUTF(this, value):
  57. if type(value) == unicode:
  58. value = value.encode("utf-8")
  59. pass
  60. value = str(value)
  61. size = len(value)
  62. this.writeShort(size)
  63. this.write(value)
  64. return this
  65.  
  66. def writeUTFBytes(this, value, size):
  67. if type(value) == unicode:
  68. value = value.encode("utf-8")
  69. pass
  70. for data in str(pack('!b', 0)) * int(size):
  71. if len(value) < int(size):
  72. value = value + pack('!b', 0)
  73. this.write(value)
  74. return this
  75.  
  76. def writeBytes(this, value):
  77. this.bytes += value
  78. return this
  79.  
  80. def write(this, value):
  81. this.bytes += value
  82.  
  83. def readByte(this):
  84. value = unpack('!b', this.bytes[:1])[0]
  85. this.bytes = this.bytes[1:]
  86. return value
  87.  
  88. def readUnsignedByte(this):
  89. value = unpack('!B', this.bytes[:1])[0]
  90. this.bytes = this.bytes[1:]
  91. return value
  92.  
  93. def readShort(this):
  94. value = unpack('!h', this.bytes[:2])[0]
  95. this.bytes = this.bytes[2:]
  96. return value
  97.  
  98. def readUnsignedShort(this):
  99. value = unpack('!H', this.bytes[:2])[0]
  100. this.bytes = this.bytes[2:]
  101. return value
  102.  
  103. def readInt(this):
  104. value = unpack('!i', this.bytes[:4])[0]
  105. this.bytes = this.bytes[4:]
  106. return value
  107.  
  108. def readUTF(this):
  109. size = unpack('!h', this.bytes[:2])[0]
  110. value = this.bytes[2:2 + size]
  111. this.bytes = this.bytes[size + 2:]
  112. return value
  113.  
  114. def readBoolean(this):
  115. value = unpack('!?', this.bytes[:1])[0]
  116. this.bytes = this.bytes[1:]
  117. return (True if value == 1 else False)
  118.  
  119. def readUTFBytes(this, size):
  120. value = this.bytes[:int(size)]
  121. this.bytes = this.bytes[int(size):]
  122. return value
  123.  
  124. def getLength(this):
  125. return len(this.bytes)
  126.  
  127. def bytesAvailable(this):
  128. return len(this.bytes) > 0
  129.  
  130. def toByteArray(this):
  131. return this.bytes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement