OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_ | |
6 #define CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/memory/scoped_vector.h" | |
15 | |
16 namespace speech { | |
17 | |
18 class ByteBufferChunk; | |
19 | |
20 // Models a chunk-oriented byte buffer. The term chunk is herein defined as an | |
21 // arbitrary sequence of bytes that is preceeded by N header bytes, indicating | |
22 // its size. Data may be appended to the buffer with no particular respect of | |
23 // chunks boundaries. However, chunks can be extracted (FIFO) only when their | |
24 // content (according to their header) is fully available in the buffer. | |
25 // The current implementation support only 4 byte Big Endian headers. | |
26 // Empty chunks (i.e. the sequence 00 00 00 00) are NOT allowed. | |
27 // | |
28 // E.g. 00 00 00 04 xx xx xx xx 00 00 00 02 yy yy 00 00 00 04 zz zz zz zz | |
29 // [----- CHUNK 1 -------] [--- CHUNK 2 ---] [------ CHUNK 3 ------] | |
30 class ChunkedByteBuffer { | |
31 public: | |
32 ChunkedByteBuffer(); | |
33 ~ChunkedByteBuffer(); | |
34 | |
35 // Appends |length| bytes starting from |start| to the buffer. | |
36 void Append(const uint8* start, size_t length); | |
37 | |
38 // Appends bytes contained into the |string| to the buffer. | |
hans
2012/04/24 14:48:08
s/into/in/ :)
| |
39 void Append(const std::string& string); | |
40 | |
41 // Checks whether one or more complete chunks are available in the buffer. | |
42 bool HasChunks() const; | |
43 | |
44 // Returns a reference to the first complete chunk, if any, or to an empty | |
45 // chunk otherwise. | |
46 const ByteBufferChunk& GetFirstChunk() const; | |
47 | |
48 // If enough data is available, reads and removes the first complete chunk | |
49 // from the buffer. Returns a NULL pointer if no complete chunk is available. | |
50 scoped_ptr<ByteBufferChunk> PopChunk(); | |
51 | |
52 // Clears all the content of the buffer. | |
53 void Clear(); | |
54 | |
55 // Returns the number of raw bytes (including headers) present. | |
56 size_t GetTotalLength() const { return total_bytes_stored_; } | |
57 | |
58 private: | |
59 ScopedVector<ByteBufferChunk> chunks_; | |
60 scoped_ptr<ByteBufferChunk> partial_chunk_; | |
61 size_t total_bytes_stored_; | |
62 | |
63 DISALLOW_COPY_AND_ASSIGN(ChunkedByteBuffer); | |
64 }; | |
65 | |
66 class ByteBufferChunk { | |
67 public: | |
68 ByteBufferChunk(); | |
69 ~ByteBufferChunk(); | |
70 | |
71 const uint8* GetData() const; | |
72 size_t GetLength() const; | |
73 | |
74 private: | |
75 friend class ChunkedByteBuffer; | |
76 std::vector<uint8> header; | |
77 std::vector<uint8> content; | |
78 size_t ExpectedContentLength() const; | |
79 | |
80 DISALLOW_COPY_AND_ASSIGN(ByteBufferChunk); | |
81 }; | |
82 | |
83 } // namespace speech | |
84 | |
85 #endif // CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_ | |
OLD | NEW |