[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / speech / chunked_byte_buffer.h
blob8b9d237811180c54fc5d7d6061288d6e6008f6e6
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.
5 #ifndef CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_
6 #define CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "content/common/content_export.h"
16 namespace content {
18 // 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
20 // 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
22 // content (according to their header) is fully available in the buffer.
23 // The current implementation support only 4 byte Big Endian headers.
24 // Empty chunks (i.e. the sequence 00 00 00 00) are NOT allowed.
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
27 // [----- CHUNK 1 -------] [--- CHUNK 2 ---] [------ CHUNK 3 ------]
28 class CONTENT_EXPORT ChunkedByteBuffer {
29 public:
30 ChunkedByteBuffer();
31 ~ChunkedByteBuffer();
33 // Appends |length| bytes starting from |start| to the buffer.
34 void Append(const uint8* start, size_t length);
36 // Appends bytes contained in the |string| to the buffer.
37 void Append(const std::string& string);
39 // Checks whether one or more complete chunks are available in the buffer.
40 bool HasChunks() const;
42 // 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.
44 scoped_ptr< std::vector<uint8> > PopChunk();
46 // Clears all the content of the buffer.
47 void Clear();
49 // Returns the number of raw bytes (including headers) present.
50 size_t GetTotalLength() const { return total_bytes_stored_; }
52 private:
53 struct Chunk {
54 Chunk();
55 ~Chunk();
57 std::vector<uint8> header;
58 scoped_ptr< std::vector<uint8> > content;
59 size_t ExpectedContentLength() const;
61 private:
62 DISALLOW_COPY_AND_ASSIGN(Chunk);
65 ScopedVector<Chunk> chunks_;
66 scoped_ptr<Chunk> partial_chunk_;
67 size_t total_bytes_stored_;
69 DISALLOW_COPY_AND_ASSIGN(ChunkedByteBuffer);
73 } // namespace content
75 #endif // CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_