1.1 --- a/iixr/files.py Fri Jan 28 01:36:25 2011 +0100
1.2 +++ b/iixr/files.py Thu Feb 03 01:26:35 2011 +0100
1.3 @@ -30,6 +30,7 @@
1.4
1.5 def __init__(self, f):
1.6 self.f = f
1.7 + self.data = array('B')
1.8 self.reset()
1.9
1.10 def reset(self):
1.11 @@ -46,8 +47,14 @@
1.12 self.f.seek(0)
1.13 self.reset()
1.14
1.15 + def flush(self):
1.16 + if self.f is not None:
1.17 + self.data.tofile(self.f)
1.18 + self.data = array('B')
1.19 +
1.20 def close(self):
1.21 if self.f is not None:
1.22 + self.data.tofile(self.f)
1.23 self.f.close()
1.24 self.f = None
1.25
1.26 @@ -67,23 +74,21 @@
1.27
1.28 "Writing basic data types to files."
1.29
1.30 - def __init__(self, f):
1.31 - File.__init__(self, f)
1.32 + def tell(self):
1.33 + return self.f.tell() + len(self.data)
1.34
1.35 def write_number(self, number):
1.36
1.37 "Write 'number' to the file using a variable length encoding."
1.38
1.39 - self.f.write(vint(number))
1.40 + vint_to_array(number, self.data)
1.41
1.42 def write_numbers(self, numbers):
1.43
1.44 "Write 'numbers' to the file using a variable length encoding."
1.45
1.46 - output = array('B')
1.47 for number in numbers:
1.48 - vint_to_array(number, output)
1.49 - output.tofile(self.f)
1.50 + vint_to_array(number, self.data)
1.51
1.52 def write_string(self, s, compress=0):
1.53
1.54 @@ -116,9 +121,9 @@
1.55 # Write the length of the data before the data itself.
1.56
1.57 length = len(s)
1.58 - self.f.write("".join([flag, vint(length), s]))
1.59 + self.data.fromstring("".join([flag, vint(length), s]))
1.60
1.61 - def write_sequence(self, output, value, last, size, monotonic=1):
1.62 + def write_sequence(self, value, last, size, monotonic=1):
1.63 if size:
1.64 emit_delta = 1
1.65 for v, l in map(None, value, last)[:size]:
1.66 @@ -130,9 +135,9 @@
1.67 emit_delta = 0
1.68 else:
1.69 v_out = v + 1
1.70 - vint_to_array(v_out, output)
1.71 + vint_to_array(v_out, self.data)
1.72 else:
1.73 - vint_to_array(value - last, output)
1.74 + vint_to_array(value - last, self.data)
1.75
1.76 return value
1.77
1.78 @@ -140,28 +145,28 @@
1.79
1.80 "Reading basic data types from files."
1.81
1.82 - def __init__(self, f):
1.83 - File.__init__(self, f)
1.84 -
1.85 def read_number(self):
1.86
1.87 "Read a number from the file."
1.88
1.89 # Read each byte, adding it to the number.
1.90
1.91 - a = array('B')
1.92 + f = self.f
1.93 + a = self.data
1.94 fromfile = a.fromfile
1.95 - f = self.f
1.96
1.97 - fromfile(f, 1)
1.98 - csd = a[-1]
1.99 - if csd < 128:
1.100 - return csd
1.101 - else:
1.102 - while csd & 128:
1.103 - fromfile(f, 1)
1.104 - csd = a[-1]
1.105 - return vint_from_array(a)
1.106 + try:
1.107 + fromfile(f, 1)
1.108 + csd = a[-1]
1.109 + if csd < 128:
1.110 + return csd
1.111 + else:
1.112 + while csd & 128:
1.113 + fromfile(f, 1)
1.114 + csd = a[-1]
1.115 + return vint_from_array(self.data)
1.116 + finally:
1.117 + self.data = array('B')
1.118
1.119 def read_string(self, decompress=0):
1.120
1.121 @@ -170,26 +175,30 @@
1.122 'decompress' is set to a true value.
1.123 """
1.124
1.125 - read = self.f.read
1.126 -
1.127 # Decompress the data if requested.
1.128
1.129 if decompress:
1.130 - flag = read(1)
1.131 + flag = self.f.read(1)
1.132 else:
1.133 flag = "-"
1.134
1.135 length = self.read_number()
1.136 - s = read(length)
1.137
1.138 - # Perform decompression if applicable.
1.139 + try:
1.140 + self.data.fromfile(self.f, length)
1.141 + s = self.data.tostring()
1.142 +
1.143 + # Perform decompression if applicable.
1.144
1.145 - if flag == "z":
1.146 - s = zlib.decompress(s)
1.147 + if flag == "z":
1.148 + s = zlib.decompress(s)
1.149 +
1.150 + # Convert strings to Unicode objects.
1.151
1.152 - # Convert strings to Unicode objects.
1.153 + return unicode(s, "utf-8")
1.154
1.155 - return unicode(s, "utf-8")
1.156 + finally:
1.157 + self.data = array('B')
1.158
1.159 def read_sequence(self, last, size, monotonic=1):
1.160 if size: