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 | |
14 namespace speech { | |
15 | |
16 // Models a chunk-oriented byte buffer. The term chunk is herein defined as an | |
17 // arbitrary sequence of bytes that is preceeded by a length field, indicating | |
18 // its size. Data may be appended to the buffer with no particular respect of | |
19 // chunks boundaries. However, chunks can be extracted (FIFO) only when their | |
20 // content (according to length field) is fully available in the buffer. | |
21 // The current implementation support only 4 byte Big Endian length fields. | |
22 // | |
23 // E.g. 00 00 00 04 xx xx xx xx 00 00 00 02 yy yy 00 00 00 04 zz zz zz zz | |
24 // [----- CHUNK 1 -------] [--- CHUNK 2 ---] [------ CHUNK 3 ------] | |
25 class ChunkedByteBuffer { | |
Satish
2012/04/23 11:01:44
A general comment about this class - you are inser
Primiano Tucci (use gerrit)
2012/04/23 17:13:57
Hmm I like it. it will increase a bit the complexi
| |
26 public: | |
27 ChunkedByteBuffer(); | |
28 ~ChunkedByteBuffer(); | |
29 | |
30 // Appends |length| bytes starting from |start| to the buffer. | |
31 void Append(const uint8* start, size_t length); | |
32 | |
33 // Appends bytes contained into the |string| to the buffer. | |
Satish
2012/04/23 11:01:44
into -> in
Primiano Tucci (use gerrit)
2012/04/23 17:13:57
Done.
| |
34 void Append(const std::string& string); | |
35 | |
36 // Returns whether a chunk is present in the buffer and can be extracted. | |
37 // In that case its length is copied into |chunk_length| pointer, if not NULL. | |
38 bool CanReadChunk(size_t* chunk_length) const; | |
Satish
2012/04/23 11:01:44
perhaps simpler to combine these two methods into
Primiano Tucci (use gerrit)
2012/04/23 17:13:57
I originally adopted this pattern in order to cont
| |
39 | |
40 // Returns whether a chunk is present in the buffer and can be extracted. | |
41 // Equivalent to calling CanReadChunk(NULL). | |
42 bool CanReadChunk() const; | |
43 | |
44 // If enough data is available, reads a chunk from the buffer, copying its | |
45 // boundaries into the |start_ptr| and |length| pointers (MUST be non NULL). | |
46 // The chunk is NOT removed from the buffer after this call. | |
47 // Return value has the same semantic of CanReadChunk. | |
48 bool ReadChunk(const uint8** start_ptr, size_t* length) const; | |
Satish
2012/04/23 11:01:44
If you like the above idea, could rewrite this as
Primiano Tucci (use gerrit)
2012/04/23 17:13:57
Done.
| |
49 | |
50 // Shifts out the first chunk from the buffer. | |
51 // Fails (NOTREACHED) if there is not enough data for an entire chunk. | |
Satish
2012/04/23 11:01:44
this is an implementation detail and shouldn't be
Primiano Tucci (use gerrit)
2012/04/23 17:13:57
Done.
| |
52 void RemoveChunk(); | |
53 | |
54 // Clears all the content of the buffer. | |
55 void Clear(); | |
56 | |
57 // Returns the number of raw bytes (including length fields) present. | |
58 size_t Size() const { return buffer_.size(); } | |
Satish
2012/04/23 11:01:44
rename to GetSize() so the method name is the acti
Primiano Tucci (use gerrit)
2012/04/23 17:13:57
Renamed to GetTotalLength(), since it is more clea
| |
59 | |
60 private: | |
61 static uint32 ReadBigEndian32(const uint8* buffer); | |
62 static const int kLengthFieldSize; | |
Satish
2012/04/23 11:01:44
I'd prefer these two to be moved to the anonymous
Primiano Tucci (use gerrit)
2012/04/23 17:13:57
Done.
| |
63 | |
64 std::vector<uint8> buffer_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(ChunkedByteBuffer); | |
67 }; | |
68 | |
69 } // namespace speech | |
70 | |
71 #endif // CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_ | |
OLD | NEW |