Index: content/browser/speech/chunked_byte_buffer.cc |
diff --git a/content/browser/speech/chunked_byte_buffer.cc b/content/browser/speech/chunked_byte_buffer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..cbc88effe56c2cf9812ad0215e68866ef017babd |
--- /dev/null |
+++ b/content/browser/speech/chunked_byte_buffer.cc |
@@ -0,0 +1,86 @@ |
+// 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. |
+ |
+#include "content/browser/speech/chunked_byte_buffer.h" |
+ |
+#include "base/basictypes.h" |
+#include "base/logging.h" |
+ |
+namespace speech { |
+ |
+const int ChunkedByteBuffer::kLengthFieldSize = sizeof(uint32); |
+ |
+ChunkedByteBuffer::ChunkedByteBuffer() { |
+} |
+ |
+ChunkedByteBuffer::~ChunkedByteBuffer() { |
+} |
+ |
+void ChunkedByteBuffer::Append(const uint8* start, size_t length) { |
+ buffer_.reserve(buffer_.size() + length); |
Satish
2012/04/23 11:01:44
I haven't verified it but can we use
buffer_.ins
Primiano Tucci (use gerrit)
2012/04/23 17:13:57
Done.
|
+ std::copy(start, start+length, std::back_inserter(buffer_)); |
+} |
+ |
+void ChunkedByteBuffer::Append(const std::string& string) { |
+ Append(reinterpret_cast<const uint8*>(string.data()), string.size()); |
+} |
+ |
+bool ChunkedByteBuffer::CanReadChunk(size_t* chunk_length) const { |
+ const size_t buffer_size = buffer_.size(); |
+ if (buffer_size < kLengthFieldSize) |
+ return false; |
+ |
+ COMPILE_ASSERT(sizeof(size_t) >= kLengthFieldSize, |
Satish
2012/04/23 11:01:44
move this outside to line 13
Primiano Tucci (use gerrit)
2012/04/23 17:13:57
Done.
|
+ ChunkedByteBufferNotSupportedOnThisArchitecture); |
+ |
+ size_t next_chunk_length = ReadBigEndian32(&buffer_[0]); |
+ const size_t bytes_required_to_read_next_chunk = kLengthFieldSize + |
+ next_chunk_length; |
+ if (buffer_size < bytes_required_to_read_next_chunk) |
+ return false; |
+ |
+ if (chunk_length != NULL) |
+ *chunk_length = next_chunk_length; |
+ return true; |
+} |
+ |
+bool ChunkedByteBuffer::CanReadChunk() const { |
+ return CanReadChunk(NULL); |
+} |
+ |
+bool ChunkedByteBuffer::ReadChunk(const uint8** start_ptr, |
+ size_t* length) const { |
+ DCHECK(start_ptr != NULL); |
+ DCHECK(length != NULL); |
+ const bool can_read_chunk = CanReadChunk(length); |
+ if (!can_read_chunk) |
+ return false; |
+ *start_ptr = &buffer_[kLengthFieldSize]; |
+ return true; |
+} |
+ |
+void ChunkedByteBuffer::RemoveChunk() { |
+ size_t chunk_length; |
+ const bool can_read_chunk = CanReadChunk(&chunk_length); |
+ |
+ if (!can_read_chunk) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ buffer_.erase(buffer_.begin(), |
+ buffer_.begin() + kLengthFieldSize + chunk_length); |
+} |
+ |
+void ChunkedByteBuffer::Clear() { |
+ buffer_.clear(); |
+} |
+ |
+uint32 ChunkedByteBuffer::ReadBigEndian32(const uint8* buffer) { |
+ return (static_cast<uint32>(buffer[3])) | |
+ (static_cast<uint32>(buffer[2]) << 8) | |
+ (static_cast<uint32>(buffer[1]) << 16) | |
+ (static_cast<uint32>(buffer[0]) << 24); |
+} |
+ |
+} // namespace speech |