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 // 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. |
| 25 // |
| 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 ChunkedByteBuffer { |
| 29 public: |
| 30 ChunkedByteBuffer(); |
| 31 ~ChunkedByteBuffer(); |
| 32 |
| 33 // Appends |length| bytes starting from |start| to the buffer. |
| 34 void Append(const uint8* start, size_t length); |
| 35 |
| 36 // Appends bytes contained into the |string| to the buffer. |
| 37 void Append(const std::string& string); |
| 38 |
| 39 // Checks whether one or more complete chunks are available in the buffer. |
| 40 bool HasChunks() const; |
| 41 |
| 42 // Returns a reference to the first complete chunk, if any, or to an empty |
| 43 // chunk otherwise. |
| 44 const std::vector<uint8>& GetFirstChunk() const; |
| 45 |
| 46 // If enough data is available, reads and removes the first complete chunk |
| 47 // from the buffer. Returns a NULL pointer if no complete chunk is available. |
| 48 scoped_ptr< std::vector<uint8> > PopChunk(); |
| 49 |
| 50 // Clears all the content of the buffer. |
| 51 void Clear(); |
| 52 |
| 53 // Returns the number of raw bytes (including headers) present. |
| 54 size_t GetTotalLength() const { return total_bytes_stored_; } |
| 55 |
| 56 private: |
| 57 struct Chunk { |
| 58 Chunk(); |
| 59 ~Chunk(); |
| 60 |
| 61 std::vector<uint8> header; |
| 62 scoped_ptr< std::vector<uint8> > content; |
| 63 size_t ExpectedContentLength() const; |
| 64 |
| 65 private: |
| 66 DISALLOW_COPY_AND_ASSIGN(Chunk); |
| 67 }; |
| 68 |
| 69 ScopedVector<Chunk> chunks_; |
| 70 scoped_ptr<Chunk> partial_chunk_; |
| 71 size_t total_bytes_stored_; |
| 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(ChunkedByteBuffer); |
| 74 }; |
| 75 |
| 76 |
| 77 } // namespace speech |
| 78 |
| 79 #endif // CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_ |
OLD | NEW |