def testMultiple(self): buf = io.BytesIO() chunked.Write(buf, b"foo") chunked.Write(buf, b"bar") chunked.Write(buf, b"quux") buf.seek(0, io.SEEK_SET) self.assertEqual(list(chunked.ReadAll(buf)), [b"foo", b"bar", b"quux"])
def testMultipleEmptyChunks(self): buf = io.BytesIO() chunked.Write(buf, b"") chunked.Write(buf, b"") chunked.Write(buf, b"") buf.seek(0, io.SEEK_SET) self.assertEqual(chunked.Read(buf), b"") self.assertEqual(chunked.Read(buf), b"") self.assertEqual(chunked.Read(buf), b"") self.assertIsNone(chunked.Read(buf))
def Serialize( stream, chunk_size=DEFAULT_CHUNK_SIZE, ): """Serializes a stream of data to the stream of chunks. Args: stream: A stream of data to serialize. chunk_size: An (optional) approximate size of a chunk in bytes. Every non- final chunk will be slightly bigger than the specified number, but this should be negligible. Yields: Serialized chunks (in the gzchunked format). """ while True: buf = io.BytesIO() buf_entry_count = 0 with gzip.GzipFile(fileobj=buf, mode="wb") as filedesc: for data in stream: chunked.Write(filedesc, data) # pytype: disable=wrong-arg-types buf_entry_count += 1 if len(buf.getvalue()) >= chunk_size: break if buf_entry_count == 0: break yield buf.getvalue()
def testContentTooShort(self): buf = io.BytesIO() chunked.Write(buf, b"foobarbaz") buf = io.BytesIO(buf.getvalue()[:-2]) with self.assertRaises(ValueError): chunked.Read(buf)
def testSingleChunk(self): buf = io.BytesIO() chunked.Write(buf, b"foo") buf.seek(0, io.SEEK_SET) self.assertEqual(chunked.Read(buf), b"foo") self.assertIsNone(chunked.Read(buf))
def testMalformedInputWithoutMaxChunkSizeSet(self): buf1 = io.BytesIO() chunked.Write(buf1, b"foobarbaz") buf2 = io.BytesIO(buf1.getvalue()[:-2]) with self.assertRaises(chunked.ChunkTruncatedError): chunked.Read(buf2)
def testMalformedInputUnseekable(self): class UnseekableBytesIO(io.BytesIO): def seekable(self) -> bool: return False buf = io.BytesIO() chunked.Write(buf, b"foobarbaz") buf = UnseekableBytesIO(buf.getvalue()[:-2]) with self.assertRaisesRegex(ValueError, "Malformed input"): chunked.Read(buf)
def testSingle(self): buf = io.BytesIO() chunked.Write(buf, b"foo") buf.seek(0, io.SEEK_SET) self.assertEqual(list(chunked.ReadAll(buf)), [b"foo"])