# Path: afkak/codec.py
# def gzip_decode(payload):
#     buffer = BytesIO(payload)
#     handle = gzip.GzipFile(fileobj=buffer, mode='r')
#     result = handle.read()
#     handle.close()
#     buffer.close()
#     return result
# 
# def gzip_encode(payload):
#     buffer = BytesIO()
#     handle = gzip.GzipFile(fileobj=buffer, mode="w")
#     handle.write(payload)
#     handle.close()
#     return buffer.getvalue()
# 
# def has_gzip():
#     return True
# 
# def has_snappy():
#     return _has_snappy
# 
# def snappy_decode(payload):
#     if not has_snappy():
#         raise NotImplementedError("Snappy codec is not available")
# 
#     if payload.startswith(_XERIAL_HEADER):
#         # TODO ? Should become a fileobj ?
#         view = memoryview(payload)
#         out = []
#         length = len(payload)
# 
#         cursor = 16
#         while cursor < length:
#             block_size = struct.unpack_from('!i', view, cursor)[0]
#             # Skip the block size
#             cursor += 4
#             end = cursor + block_size
#             # XXX snappy requires a bytes-like object but doesn't accept
#             # a memoryview, so we must copy.
#             out.append(snappy.decompress(view[cursor:end].tobytes()))
#             cursor = end
# 
#         # See https://atleastfornow.net/blog/not-all-bytes/
#         return b''.join(out)
#     else:
#         return snappy.decompress(payload)
# 
# def snappy_encode(payload, xerial_compatible=False,
#                   xerial_blocksize=32 * 1024):
#     """
#     Compress the given data with the Snappy algorithm.
# 
#     :param bytes payload: Data to compress.
#     :param bool xerial_compatible:
#         If set then the stream is broken into length-prefixed blocks in
#         a fashion compatible with the xerial snappy library.
# 
#         The format winds up being::
# 
#             +-------------+------------+--------------+------------+--------------+
#             |   Header    | Block1_len | Block1 data  | BlockN len | BlockN data  |
#             |-------------+------------+--------------+------------+--------------|
#             |  16 bytes   |  BE int32  | snappy bytes |  BE int32  | snappy bytes |
#             +-------------+------------+--------------+------------+--------------+
# 
#     :param int xerial_blocksize:
#         Number of bytes per chunk to independently Snappy encode. 32k is the
#         default in the xerial library.
# 
#     :returns: Compressed bytes.
#     :rtype: :class:`bytes`
#     """
#     if not has_snappy():  # FIXME This should be static, not checked every call.
#         raise NotImplementedError("Snappy codec is not available")
# 
#     if xerial_compatible:
#         def _chunker():
#             for i in range(0, len(payload), xerial_blocksize):
#                 yield payload[i:i+xerial_blocksize]
# 
#         out = BytesIO()
#         out.write(_XERIAL_HEADER)
# 
#         for chunk in _chunker():
#             block = snappy.compress(chunk)
#             out.write(struct.pack('!i', len(block)))
#             out.write(block)
# 
#         out.seek(0)
#         return out.read()
# 
#     else:
#         return snappy.compress(payload)


import importlib
import os
import struct
import unittest
import afkak
        import sys
from unittest.mock import patch
from afkak.codec import (
    gzip_decode, gzip_encode, has_gzip, has_snappy, snappy_decode,
    snappy_encode,
)
# Copyright (C) 2015 Cyan, Inc.
# Copyright 2018, 2019, 2021 Ciena Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.




class TestCodec(unittest.TestCase):
    @unittest.skipUnless(has_gzip(), "Gzip not available")
    def test_gzip(self):
        for _i in range(100):
            s1 = os.urandom(100)
            s2 = gzip_decode(gzip_encode(s1))
            self.assertEqual(s1, s2)

    @unittest.skipUnless(has_snappy(), "Snappy not available")
    def test_snappy(self):
        for _i in range(100):
            s1 = os.urandom(120)

# Path: afkak/codec.py
# def gzip_decode(payload):
#     buffer = BytesIO(payload)
#     handle = gzip.GzipFile(fileobj=buffer, mode='r')
#     result = handle.read()
#     handle.close()
#     buffer.close()
#     return result
# 
# def gzip_encode(payload):
#     buffer = BytesIO()
#     handle = gzip.GzipFile(fileobj=buffer, mode="w")
#     handle.write(payload)
#     handle.close()
#     return buffer.getvalue()
# 
# def has_gzip():
#     return True
# 
# def has_snappy():
#     return _has_snappy
# 
# def snappy_decode(payload):
#     if not has_snappy():
#         raise NotImplementedError("Snappy codec is not available")
# 
#     if payload.startswith(_XERIAL_HEADER):
#         # TODO ? Should become a fileobj ?
#         view = memoryview(payload)
#         out = []
#         length = len(payload)
# 
#         cursor = 16
#         while cursor < length:
#             block_size = struct.unpack_from('!i', view, cursor)[0]
#             # Skip the block size
#             cursor += 4
#             end = cursor + block_size
#             # XXX snappy requires a bytes-like object but doesn't accept
#             # a memoryview, so we must copy.
#             out.append(snappy.decompress(view[cursor:end].tobytes()))
#             cursor = end
# 
#         # See https://atleastfornow.net/blog/not-all-bytes/
#         return b''.join(out)
#     else:
#         return snappy.decompress(payload)
# 
# def snappy_encode(payload, xerial_compatible=False,
#                   xerial_blocksize=32 * 1024):
#     """
#     Compress the given data with the Snappy algorithm.
# 
#     :param bytes payload: Data to compress.
#     :param bool xerial_compatible:
#         If set then the stream is broken into length-prefixed blocks in
#         a fashion compatible with the xerial snappy library.
# 
#         The format winds up being::
# 
#             +-------------+------------+--------------+------------+--------------+
#             |   Header    | Block1_len | Block1 data  | BlockN len | BlockN data  |
#             |-------------+------------+--------------+------------+--------------|
#             |  16 bytes   |  BE int32  | snappy bytes |  BE int32  | snappy bytes |
#             +-------------+------------+--------------+------------+--------------+
# 
#     :param int xerial_blocksize:
#         Number of bytes per chunk to independently Snappy encode. 32k is the
#         default in the xerial library.
# 
#     :returns: Compressed bytes.
#     :rtype: :class:`bytes`
#     """
#     if not has_snappy():  # FIXME This should be static, not checked every call.
#         raise NotImplementedError("Snappy codec is not available")
# 
#     if xerial_compatible:
#         def _chunker():
#             for i in range(0, len(payload), xerial_blocksize):
#                 yield payload[i:i+xerial_blocksize]
# 
#         out = BytesIO()
#         out.write(_XERIAL_HEADER)
# 
#         for chunk in _chunker():
#             block = snappy.compress(chunk)
#             out.write(struct.pack('!i', len(block)))
#             out.write(block)
# 
#         out.seek(0)
#         return out.read()
# 
#     else:
#         return snappy.compress(payload)


# Path: afkak/test/test_codec.py
import importlib
import os
import struct
import unittest
import afkak
        import sys
from unittest.mock import patch
from afkak.codec import (
    gzip_decode, gzip_encode, has_gzip, has_snappy, snappy_decode,
    snappy_encode,
)

# Copyright (C) 2015 Cyan, Inc.
# Copyright 2018, 2019, 2021 Ciena Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.




class TestCodec(unittest.TestCase):
    @unittest.skipUnless(has_gzip(), "Gzip not available")
    def test_gzip(self):
        for _i in range(100):
            s1 = os.urandom(100)
            s2 = gzip_decode(gzip_encode(s1))
            self.assertEqual(s1, s2)

    @unittest.skipUnless(has_snappy(), "Snappy not available")
    def test_snappy(self):
        for _i in range(100):
            s1 = os.urandom(120)

            s2 = snappy_decode(snappy_encode(s1))