Robot Framework Integrated Development Environment (RIDE)
compress.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import base64
17 
18 from .platform import JYTHON, PY2
19 
20 
21 def compress_text(text):
22  result = base64.b64encode(_compress(text.encode('UTF-8')))
23  return result if PY2 else result.decode('ASCII')
24 
25 
26 if not JYTHON:
27 
28  import zlib
29 
30  def _compress(text):
31  return zlib.compress(text, 9)
32 
33 else:
34 
35  # Custom compress implementation was originally used to avoid memory leak
36  # (http://bugs.jython.org/issue1775). Kept around still because it is a bit
37  # faster than Jython's standard zlib.compress.
38 
39  from java.util.zip import Deflater
40  import jarray
41 
42 
45  _DEFLATOR = Deflater(9, False)
46 
47  def _compress(text):
48  _DEFLATOR.setInput(text)
49  _DEFLATOR.finish()
50  buf = jarray.zeros(1024, 'b')
51  compressed = []
52  while not _DEFLATOR.finished():
53  length = _DEFLATOR.deflate(buf, 0, 1024)
54  compressed.append(buf[:length].tostring())
55  _DEFLATOR.reset()
56  return ''.join(compressed)