Introduced ChunkedByteBuffer class which will be required by next speech recognition...
[chromium-blink-merge.git] / content / browser / speech / chunked_byte_buffer.h
blob71ccc06cb23a182c84f26488321718796b280a62
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_
7 #pragma once
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "content/common/content_export.h"
17 namespace speech {
19 // Models a chunk-oriented byte buffer. The term chunk is herein defined as an
20 // arbitrary sequence of bytes that is preceeded by N header bytes, indicating
21 // its size. Data may be appended to the buffer with no particular respect of
22 // chunks boundaries. However, chunks can be extracted (FIFO) only when their
23 // content (according to their header) is fully available in the buffer.
24 // The current implementation support only 4 byte Big Endian headers.
25 // Empty chunks (i.e. the sequence 00 00 00 00) are NOT allowed.
27 // E.g. 00 00 00 04 xx xx xx xx 00 00 00 02 yy yy 00 00 00 04 zz zz zz zz
28 // [----- CHUNK 1 -------] [--- CHUNK 2 ---] [------ CHUNK 3 ------]
29 class CONTENT_EXPORT ChunkedByteBuffer {
30 public:
31 ChunkedByteBuffer();
32 ~ChunkedByteBuffer();
34 // Appends |length| bytes starting from |start| to the buffer.
35 void Append(const uint8* start, size_t length);
37 // Appends bytes contained in the |string| to the buffer.
38 void Append(const std::string& string);
40 // Checks whether one or more complete chunks are available in the buffer.
41 bool HasChunks() const;
43 // If enough data is available, reads and removes the first complete chunk
44 // from the buffer. Returns a NULL pointer if no complete chunk is available.
45 scoped_ptr< std::vector<uint8> > PopChunk();
47 // Clears all the content of the buffer.
48 void Clear();
50 // Returns the number of raw bytes (including headers) present.
51 size_t GetTotalLength() const { return total_bytes_stored_; }
53 private:
54 struct Chunk {
55 Chunk();
56 ~Chunk();
58 std::vector<uint8> header;
59 scoped_ptr< std::vector<uint8> > content;
60 size_t ExpectedContentLength() const;
62 private:
63 DISALLOW_COPY_AND_ASSIGN(Chunk);
66 ScopedVector<Chunk> chunks_;
67 scoped_ptr<Chunk> partial_chunk_;
68 size_t total_bytes_stored_;
70 DISALLOW_COPY_AND_ASSIGN(ChunkedByteBuffer);
74 } // namespace speech
76 #endif // CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_