Chromium Code Reviews| Index: content/browser/speech/chunked_byte_buffer.h |
| diff --git a/content/browser/speech/chunked_byte_buffer.h b/content/browser/speech/chunked_byte_buffer.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7f7f8f0ad10034480af2155c62baf11e39e7a676 |
| --- /dev/null |
| +++ b/content/browser/speech/chunked_byte_buffer.h |
| @@ -0,0 +1,82 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_ |
| +#define CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_ |
| +#pragma once |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/scoped_vector.h" |
| + |
| +namespace speech { |
| + |
| +// Models a chunk-oriented byte buffer. The term chunk is herein defined as an |
| +// arbitrary sequence of bytes that is preceeded by N header bytes, indicating |
| +// its size. Data may be appended to the buffer with no particular respect of |
| +// chunks boundaries. However, chunks can be extracted (FIFO) only when their |
| +// content (according to their header) is fully available in the buffer. |
| +// The current implementation support only 4 byte Big Endian headers. |
| +// Empty chunks (i.e. the sequence 00 00 00 00) are NOT allowed. |
| +// |
| +// E.g. 00 00 00 04 xx xx xx xx 00 00 00 02 yy yy 00 00 00 04 zz zz zz zz |
| +// [----- CHUNK 1 -------] [--- CHUNK 2 ---] [------ CHUNK 3 ------] |
| +class ChunkedByteBuffer { |
| + public: |
| + ChunkedByteBuffer(); |
| + ~ChunkedByteBuffer(); |
| + |
| + // Appends |length| bytes starting from |start| to the buffer. |
| + void Append(const uint8* start, size_t length); |
| + |
| + // Appends bytes contained into the |string| to the buffer. |
| + void Append(const std::string& string); |
| + |
| + // Returns the length of the next complete chunk, or 0 if no complete chunk |
| + // is available. |
| + size_t GetNextChunkLength() const; |
| + |
| + // If enough data is available, reads a chunk from the buffer, copying its |
| + // start pointer in |start_ptr| and returning its length. Returns 0 if no |
| + // complete chunk is available in the buffer. |
| + // The chunk is NOT removed from the buffer after this call. |
| + size_t GetNextChunk(const uint8** start_ptr) const; |
|
Satish
2012/04/24 09:10:14
since this class doesn't allow iterating through a
|
| + |
| + // Shifts out the first chunk from the buffer. |
| + void RemoveNextChunk(); |
| + |
| + // Clears all the content of the buffer. |
| + void Clear(); |
| + |
| + // Returns the number of raw bytes (including headers) present. |
| + size_t GetTotalLength() const { return total_bytes_stored_; } |
| + |
| + private: |
| + static uint32 ReadBigEndian32(const uint8* buffer); |
| + |
| + struct Chunk { |
| + Chunk() {} |
|
Satish
2012/04/24 09:10:14
ctor/dtor shouldn't be inlined, move these to the
|
| + ~Chunk() {} |
| + |
| + std::vector<uint8> header; |
| + std::vector<uint8> content; |
| + inline size_t ExpectedContentLength() const; |
|
Satish
2012/04/24 09:10:14
remove 'inline'
|
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(Chunk); |
| + }; |
| + |
| + ScopedVector<Chunk> chunks_; |
| + scoped_ptr<Chunk> partial_chunk_; |
| + size_t total_bytes_stored_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ChunkedByteBuffer); |
| +}; |
| + |
| +} // namespace speech |
| + |
| +#endif // CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_ |