|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2014 Gustav Arngården |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | + |
| 24 | +"""Reading from Java DataInputStream format.""" |
| 25 | + |
| 26 | +import struct |
| 27 | + |
| 28 | + |
| 29 | +class DataInputStream: |
| 30 | + def __init__(self, stream): |
| 31 | + self.stream = stream |
| 32 | + |
| 33 | + def read(self, n=1): |
| 34 | + data = self.stream.read(n) |
| 35 | + if len(data) != n: |
| 36 | + # this is a problem but in most cases we have reached EOF |
| 37 | + raise EOFError |
| 38 | + return data |
| 39 | + |
| 40 | + def read_byte(self): |
| 41 | + return struct.unpack("b", self.read(1))[0] |
| 42 | + |
| 43 | + def read_long(self): |
| 44 | + return struct.unpack(">q", self.read(8))[0] |
| 45 | + |
| 46 | + def read_utf(self): |
| 47 | + utf_length = struct.unpack(">H", self.read(2))[0] |
| 48 | + return self.read(utf_length) |
| 49 | + |
| 50 | + def read_int(self): |
| 51 | + return struct.unpack(">i", self.read(4))[0] |
0 commit comments