Chromium Code Reviews| 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 #include "content/browser/speech/chunked_byte_buffer.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/logging.h" | |
| 9 | |
| 10 namespace speech { | |
| 11 | |
| 12 const int ChunkedByteBuffer::kLengthFieldSize = sizeof(uint32); | |
| 13 | |
| 14 ChunkedByteBuffer::ChunkedByteBuffer() { | |
| 15 } | |
| 16 | |
| 17 ChunkedByteBuffer::~ChunkedByteBuffer() { | |
| 18 } | |
| 19 | |
| 20 void ChunkedByteBuffer::Append(const uint8* start, size_t length) { | |
| 21 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.
| |
| 22 std::copy(start, start+length, std::back_inserter(buffer_)); | |
| 23 } | |
| 24 | |
| 25 void ChunkedByteBuffer::Append(const std::string& string) { | |
| 26 Append(reinterpret_cast<const uint8*>(string.data()), string.size()); | |
| 27 } | |
| 28 | |
| 29 bool ChunkedByteBuffer::CanReadChunk(size_t* chunk_length) const { | |
| 30 const size_t buffer_size = buffer_.size(); | |
| 31 if (buffer_size < kLengthFieldSize) | |
| 32 return false; | |
| 33 | |
| 34 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.
| |
| 35 ChunkedByteBufferNotSupportedOnThisArchitecture); | |
| 36 | |
| 37 size_t next_chunk_length = ReadBigEndian32(&buffer_[0]); | |
| 38 const size_t bytes_required_to_read_next_chunk = kLengthFieldSize + | |
| 39 next_chunk_length; | |
| 40 if (buffer_size < bytes_required_to_read_next_chunk) | |
| 41 return false; | |
| 42 | |
| 43 if (chunk_length != NULL) | |
| 44 *chunk_length = next_chunk_length; | |
| 45 return true; | |
| 46 } | |
| 47 | |
| 48 bool ChunkedByteBuffer::CanReadChunk() const { | |
| 49 return CanReadChunk(NULL); | |
| 50 } | |
| 51 | |
| 52 bool ChunkedByteBuffer::ReadChunk(const uint8** start_ptr, | |
| 53 size_t* length) const { | |
| 54 DCHECK(start_ptr != NULL); | |
| 55 DCHECK(length != NULL); | |
| 56 const bool can_read_chunk = CanReadChunk(length); | |
| 57 if (!can_read_chunk) | |
| 58 return false; | |
| 59 *start_ptr = &buffer_[kLengthFieldSize]; | |
| 60 return true; | |
| 61 } | |
| 62 | |
| 63 void ChunkedByteBuffer::RemoveChunk() { | |
| 64 size_t chunk_length; | |
| 65 const bool can_read_chunk = CanReadChunk(&chunk_length); | |
| 66 | |
| 67 if (!can_read_chunk) { | |
| 68 NOTREACHED(); | |
| 69 return; | |
| 70 } | |
| 71 buffer_.erase(buffer_.begin(), | |
| 72 buffer_.begin() + kLengthFieldSize + chunk_length); | |
| 73 } | |
| 74 | |
| 75 void ChunkedByteBuffer::Clear() { | |
| 76 buffer_.clear(); | |
| 77 } | |
| 78 | |
| 79 uint32 ChunkedByteBuffer::ReadBigEndian32(const uint8* buffer) { | |
| 80 return (static_cast<uint32>(buffer[3])) | | |
| 81 (static_cast<uint32>(buffer[2]) << 8) | | |
| 82 (static_cast<uint32>(buffer[1]) << 16) | | |
| 83 (static_cast<uint32>(buffer[0]) << 24); | |
| 84 } | |
| 85 | |
| 86 } // namespace speech | |
| OLD | NEW |