Posted By

weilawei on 05/09/12


Tagged

streaming block chunk


Versions (?)

Chunk Data for Streaming


 / Published in: Python
 

Chunks data into block_size blocks for streaming, adds null padding.

  1. import math
  2.  
  3. is_debug = False
  4.  
  5. def chunk(data, block_size, padding=b'\x00'):
  6. data_size = len(data)
  7. padding_size = block_size - (data_size % block_size) if (data_size > block_size) else (block_size - data_size)
  8. is_padded = (padding_size > 0) and (padding_size != block_size)
  9. total_blocks = math.ceil(data_size / block_size)
  10. current_block = 0
  11. block_slice = [(block_size * current_block), (block_size * (current_block + 1))]
  12.  
  13. if is_debug:
  14. print("data_size: %d" % (data_size,))
  15. print("padding_size: %d" % (padding_size,))
  16. print("is_padded: %s" % (is_padded,))
  17. print("total_blocks: %d\n" % (total_blocks,))
  18.  
  19. while current_block < total_blocks:
  20. a_block = slice(block_slice[0], block_slice[1])
  21.  
  22. if (is_padded and (current_block == (total_blocks - 1))):
  23. yield [current_block, total_blocks, data_size, data[a_block] + (padding_size * padding)]
  24. return
  25.  
  26. yield [current_block, total_blocks, data_size, data[a_block]]
  27.  
  28. current_block += 1
  29. block_slice[0] += block_size
  30. block_slice[1] += block_size

Report this snippet  

You need to login to post a comment.