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