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..694136514e35b9d963d1371f2477b6796ac9b1a8 |
--- /dev/null |
+++ b/content/browser/speech/chunked_byte_buffer.h |
@@ -0,0 +1,71 @@ |
+// 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" |
+ |
+namespace speech { |
+ |
+// Models a chunk-oriented byte buffer. The term chunk is herein defined as an |
+// arbitrary sequence of bytes that is preceeded by a length field, 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 length field) is fully available in the buffer. |
+// The current implementation support only 4 byte Big Endian length fields. |
+// |
+// 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 { |
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
|
+ 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. |
Satish
2012/04/23 11:01:44
into -> in
Primiano Tucci (use gerrit)
2012/04/23 17:13:57
Done.
|
+ void Append(const std::string& string); |
+ |
+ // Returns whether a chunk is present in the buffer and can be extracted. |
+ // In that case its length is copied into |chunk_length| pointer, if not NULL. |
+ 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
|
+ |
+ // Returns whether a chunk is present in the buffer and can be extracted. |
+ // Equivalent to calling CanReadChunk(NULL). |
+ bool CanReadChunk() const; |
+ |
+ // If enough data is available, reads a chunk from the buffer, copying its |
+ // boundaries into the |start_ptr| and |length| pointers (MUST be non NULL). |
+ // The chunk is NOT removed from the buffer after this call. |
+ // Return value has the same semantic of CanReadChunk. |
+ 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.
|
+ |
+ // Shifts out the first chunk from the buffer. |
+ // 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.
|
+ void RemoveChunk(); |
+ |
+ // Clears all the content of the buffer. |
+ void Clear(); |
+ |
+ // Returns the number of raw bytes (including length fields) present. |
+ 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
|
+ |
+ private: |
+ static uint32 ReadBigEndian32(const uint8* buffer); |
+ 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.
|
+ |
+ std::vector<uint8> buffer_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ChunkedByteBuffer); |
+}; |
+ |
+} // namespace speech |
+ |
+#endif // CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_ |