upgrade to Jython 2.5.0

This commit is contained in:
Dmitry Jemerov
2009-07-30 19:38:38 +04:00
parent f221494a43
commit bb24ece14b
487 changed files with 155647 additions and 18427 deletions
+45 -18
View File
@@ -1,7 +1,24 @@
"""
The functions in this module allow compression and decompression using the
zlib library, which is based on GNU zip.
adler32(string[, start]) -- Compute an Adler-32 checksum.
compress(string[, level]) -- Compress string, with compression level in 1-9.
compressobj([level]) -- Return a compressor object.
crc32(string[, start]) -- Compute a CRC-32 checksum.
decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.
decompressobj([wbits]) -- Return a decompressor object.
'wbits' is window buffer size.
Compressor objects support compress() and flush() methods; decompressor
objects support decompress() and flush().
"""
import jarray, binascii
from java.util.zip import Adler32, Deflater, Inflater
from java.lang import Long, String, StringBuffer
from java.util.zip import Adler32, Deflater, Inflater, DataFormatException
from java.lang import Long, String
from cStringIO import StringIO
class error(Exception):
pass
@@ -27,11 +44,11 @@ Z_DEFAULT_STRATEGY = 0
Z_FINISH = 4
_valid_flush_modes = (Z_FINISH,)
def adler32(string, value=1):
def adler32(s, value=1):
if value != 1:
raise ValueError, "adler32 only support start value of 1"
checksum = Adler32()
checksum.update(String.getBytes(string))
checksum.update(String.getBytes(s, 'iso-8859-1'))
return Long(checksum.getValue()).intValue()
def crc32(string, value=0):
@@ -49,8 +66,8 @@ def compress(string, level=6):
def decompress(string, wbits=0, bufsize=16384):
inflater = Inflater(wbits < 0)
inflater.setInput(string)
return _get_inflate_data(inflater)
class compressobj:
# all jython uses wbits for is deciding whether to skip the header if it's negative
@@ -118,39 +135,49 @@ class decompressobj:
return inflated
def flush(self):
def flush(self, length=None):
if self._ended:
raise error("decompressobj may not be used after flush()")
last = _get_inflate_data(self.inflater)
if length is None:
length = 0
elif length <= 0:
raise ValueError('length must be greater than zero')
last = _get_inflate_data(self.inflater, length)
self.inflater.end()
return last
def _get_deflate_data(deflater):
buf = jarray.zeros(1024, 'b')
sb = StringBuffer()
s = StringIO()
while not deflater.finished():
l = deflater.deflate(buf)
if l == 0:
break
sb.append(String(buf, 0, 0, l))
return sb.toString()
s.write(String(buf, 0, 0, l))
s.seek(0)
return s.read()
def _get_inflate_data(inflater, max_length=0):
buf = jarray.zeros(1024, 'b')
sb = StringBuffer()
s = StringIO()
total = 0
while not inflater.finished():
if max_length:
l = inflater.inflate(buf, 0, min(1024, max_length - total))
else:
l = inflater.inflate(buf)
try:
if max_length:
l = inflater.inflate(buf, 0, min(1024, max_length - total))
else:
l = inflater.inflate(buf)
except DataFormatException, e:
raise error(str(e))
if l == 0:
break
total += l
sb.append(String(buf, 0, 0, l))
s.write(String(buf, 0, 0, l))
if max_length and total == max_length:
break
return sb.toString()
s.seek(0)
return s.read()