Introduced function to create byte buffers from the output of a byte buffer reader.

pull/2117/head
David Garcia Quintas 10 years ago
parent 81889aa833
commit 3df5c44c84
  1. 4
      include/grpc/byte_buffer.h
  2. 14
      src/core/surface/byte_buffer.c
  3. 25
      test/core/surface/byte_buffer_reader_test.c

@ -103,6 +103,10 @@ void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader);
int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader,
gpr_slice *slice);
/** Returns a RAW byte buffer instance from the output of \a reader. */
grpc_byte_buffer *grpc_raw_byte_buffer_from_reader(
grpc_byte_buffer_reader *reader);
#ifdef __cplusplus
}
#endif

@ -55,6 +55,20 @@ grpc_byte_buffer *grpc_raw_compressed_byte_buffer_create(
return bb;
}
grpc_byte_buffer *grpc_raw_byte_buffer_from_reader(
grpc_byte_buffer_reader *reader) {
grpc_byte_buffer *bb = malloc(sizeof(grpc_byte_buffer));
gpr_slice slice;
bb->type = GRPC_BB_RAW;
bb->data.raw.compression = GRPC_COMPRESS_NONE;
gpr_slice_buffer_init(&bb->data.raw.slice_buffer);
while (grpc_byte_buffer_reader_next(reader, &slice)) {
gpr_slice_buffer_add(&bb->data.raw.slice_buffer, slice);
}
return bb;
}
grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb) {
switch (bb->type) {
case GRPC_BB_RAW:

@ -160,6 +160,30 @@ static void test_read_deflate_compressed_slice(void) {
read_compressed_slice(GRPC_COMPRESS_DEFLATE, INPUT_SIZE);
}
static void test_byte_buffer_from_reader(void) {
gpr_slice slice;
grpc_byte_buffer *buffer, *buffer_from_reader;
grpc_byte_buffer_reader reader;
LOG_TEST("test_byte_buffer_from_reader");
slice = gpr_slice_malloc(4);
memcpy(GPR_SLICE_START_PTR(slice), "test", 4);
buffer = grpc_raw_byte_buffer_create(&slice, 1);
gpr_slice_unref(slice);
grpc_byte_buffer_reader_init(&reader, buffer);
buffer_from_reader = grpc_raw_byte_buffer_from_reader(&reader);
GPR_ASSERT(buffer->type == buffer_from_reader->type);
GPR_ASSERT(buffer_from_reader->data.raw.compression == GRPC_COMPRESS_NONE);
GPR_ASSERT(buffer_from_reader->data.raw.slice_buffer.count == 1);
GPR_ASSERT(memcmp(GPR_SLICE_START_PTR(
buffer_from_reader->data.raw.slice_buffer.slices[0]),
"test", 4) == 0);
grpc_byte_buffer_destroy(buffer);
grpc_byte_buffer_destroy(buffer_from_reader);
}
int main(int argc, char **argv) {
grpc_test_init(argc, argv);
test_read_one_slice();
@ -167,6 +191,7 @@ int main(int argc, char **argv) {
test_read_none_compressed_slice();
test_read_gzip_compressed_slice();
test_read_deflate_compressed_slice();
test_byte_buffer_from_reader();
return 0;
}

Loading…
Cancel
Save