File: chunked_byte_buffer.h

package info (click to toggle)
chromium 141.0.7390.122-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,246,384 kB
  • sloc: cpp: 35,265,044; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,635; asm: 950,788; xml: 751,771; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (73 lines) | stat: -rw-r--r-- 2,317 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SPEECH_CHUNKED_BYTE_BUFFER_H_
#define COMPONENTS_SPEECH_CHUNKED_BYTE_BUFFER_H_

#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <string_view>
#include <vector>

namespace speech {

// Models a chunk-oriented byte buffer. The term chunk is herein defined as an
// arbitrary sequence of bytes that is preceeded by N header bytes, indicating
// its size. Data may be appended to the buffer with no particular respect of
// chunks boundaries. However, chunks can be extracted (FIFO) only when their
// content (according to their header) is fully available in the buffer.
// The current implementation support only 4 byte Big Endian headers.
// Empty chunks (i.e. the sequence 00 00 00 00) are NOT allowed.
//
// E.g. 00 00 00 04 xx xx xx xx 00 00 00 02 yy yy 00 00 00 04 zz zz zz zz
//      [----- CHUNK 1 -------] [--- CHUNK 2 ---] [------ CHUNK 3 ------]
class ChunkedByteBuffer {
 public:
  ChunkedByteBuffer();

  ChunkedByteBuffer(const ChunkedByteBuffer&) = delete;
  ChunkedByteBuffer& operator=(const ChunkedByteBuffer&) = delete;

  ~ChunkedByteBuffer();

  // Appends bytes contained in the |string| to the buffer.
  void Append(std::string_view string);

  // Checks whether one or more complete chunks are available in the buffer.
  bool HasChunks() const;

  // If enough data is available, reads and removes the first complete chunk
  // from the buffer. Returns a NULL pointer if no complete chunk is available.
  std::unique_ptr<std::vector<uint8_t>> PopChunk();

  // Clears all the content of the buffer.
  void Clear();

  // Returns the number of raw bytes (including headers) present.
  size_t GetTotalLength() const { return total_bytes_stored_; }

 private:
  struct Chunk {
    Chunk();

    Chunk(const Chunk&) = delete;
    Chunk& operator=(const Chunk&) = delete;

    ~Chunk();

    std::vector<uint8_t> header;
    std::unique_ptr<std::vector<uint8_t>> content;
    size_t ExpectedContentLength() const;
  };

  std::vector<std::unique_ptr<Chunk>> chunks_;
  std::unique_ptr<Chunk> partial_chunk_;
  size_t total_bytes_stored_;
};

}  // namespace speech

#endif  // COMPONENTS_SPEECH_CHUNKED_BYTE_BUFFER_H_