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 <algorithm> | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/lazy_instance.h" | |
| 11 #include "base/logging.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 static const size_t kHeaderLength = sizeof(uint32); | |
| 16 | |
| 17 COMPILE_ASSERT(sizeof(size_t) >= kHeaderLength, | |
| 18 ChunkedByteBufferNotSupportedOnThisArchitecture); | |
| 19 | |
| 20 uint32 ReadBigEndian32(const uint8* buffer) { | |
| 21 return (static_cast<uint32>(buffer[3])) | | |
| 22 (static_cast<uint32>(buffer[2]) << 8) | | |
| 23 (static_cast<uint32>(buffer[1]) << 16) | | |
| 24 (static_cast<uint32>(buffer[0]) << 24); | |
| 25 } | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 namespace speech { | |
| 30 | |
| 31 ChunkedByteBuffer::ChunkedByteBuffer() | |
| 32 : partial_chunk_(new Chunk()), | |
|
Satish
2012/04/24 15:05:33
indent by 4 spaces
Primiano Tucci (use gerrit)
2012/04/24 15:24:43
Done.
| |
| 33 total_bytes_stored_(0) { | |
| 34 } | |
| 35 | |
| 36 ChunkedByteBuffer::~ChunkedByteBuffer() { | |
| 37 Clear(); | |
| 38 } | |
| 39 | |
| 40 void ChunkedByteBuffer::Append(const uint8* start, size_t length) { | |
| 41 DCHECK(length > 0); | |
| 42 size_t remaining_bytes = length; | |
| 43 const uint8* next_data = start; | |
| 44 | |
| 45 while (remaining_bytes > 0) { | |
| 46 DCHECK(partial_chunk_ != NULL); | |
| 47 size_t insert_length = 0; | |
| 48 bool header_completed = false; | |
| 49 bool content_completed = false; | |
| 50 std::vector<uint8>* insert_target; | |
| 51 | |
| 52 if (partial_chunk_->header.size() < kHeaderLength) { | |
| 53 const size_t bytes_to_complete_header = | |
| 54 kHeaderLength - partial_chunk_->header.size(); | |
| 55 insert_length = std::min(bytes_to_complete_header, remaining_bytes); | |
| 56 insert_target = &partial_chunk_->header; | |
| 57 header_completed = (remaining_bytes >= bytes_to_complete_header); | |
| 58 } else { | |
| 59 DCHECK_LT(partial_chunk_->content->size(), | |
| 60 partial_chunk_->ExpectedContentLength()); | |
| 61 const size_t bytes_to_complete_chunk = | |
| 62 partial_chunk_->ExpectedContentLength() - | |
| 63 partial_chunk_->content->size(); | |
| 64 insert_length = std::min(bytes_to_complete_chunk, remaining_bytes); | |
| 65 insert_target = partial_chunk_->content.get(); | |
| 66 content_completed = (remaining_bytes >= bytes_to_complete_chunk); | |
| 67 } | |
| 68 | |
| 69 DCHECK_GT(insert_length, 0U); | |
| 70 DCHECK_LE(insert_length, remaining_bytes); | |
| 71 DCHECK_LE(next_data + insert_length, start + length); | |
| 72 insert_target->insert(insert_target->end(), | |
| 73 next_data, | |
| 74 next_data + insert_length); | |
| 75 next_data += insert_length; | |
| 76 remaining_bytes -= insert_length; | |
| 77 | |
| 78 if (header_completed) { | |
| 79 DCHECK_EQ(partial_chunk_->header.size(), kHeaderLength); | |
| 80 DCHECK_NE(partial_chunk_->ExpectedContentLength(), 0U); | |
| 81 partial_chunk_->content->reserve(partial_chunk_->ExpectedContentLength()); | |
| 82 } else if (content_completed) { | |
| 83 DCHECK_EQ(partial_chunk_->content->size(), | |
| 84 partial_chunk_->ExpectedContentLength()); | |
| 85 chunks_.push_back(partial_chunk_.release()); | |
| 86 partial_chunk_.reset(new Chunk()); | |
| 87 } | |
| 88 } | |
| 89 DCHECK_EQ(next_data, start + length); | |
| 90 total_bytes_stored_ += length; | |
| 91 } | |
| 92 | |
| 93 void ChunkedByteBuffer::Append(const std::string& string) { | |
| 94 Append(reinterpret_cast<const uint8*>(string.data()), string.size()); | |
| 95 } | |
| 96 | |
| 97 bool ChunkedByteBuffer::HasChunks() const { | |
| 98 return !chunks_.empty(); | |
| 99 } | |
| 100 | |
| 101 const std::vector<uint8>& ChunkedByteBuffer::GetFirstChunk() const { | |
|
Satish
2012/04/24 15:05:33
since we have PopChunk now, perhaps this can be re
Primiano Tucci (use gerrit)
2012/04/24 15:24:43
Done.
| |
| 102 CR_DEFINE_STATIC_LOCAL(std::vector<uint8>, kEmptyChunk, ()); | |
| 103 if (chunks_.empty()) | |
| 104 return kEmptyChunk; | |
| 105 return *((*chunks_.begin())->content); | |
| 106 } | |
| 107 | |
| 108 scoped_ptr< std::vector<uint8> > ChunkedByteBuffer::PopChunk() { | |
| 109 if (chunks_.empty()) | |
| 110 return scoped_ptr< std::vector<uint8> >(); | |
| 111 scoped_ptr<Chunk> chunk(*chunks_.begin()); | |
| 112 chunks_.weak_erase(chunks_.begin()); | |
| 113 DCHECK_EQ(chunk->header.size(), kHeaderLength); | |
| 114 DCHECK_EQ(chunk->content->size(), chunk->ExpectedContentLength()); | |
| 115 total_bytes_stored_ -= chunk->content->size(); | |
| 116 total_bytes_stored_ -= kHeaderLength; | |
| 117 return chunk->content.Pass(); | |
| 118 } | |
| 119 | |
| 120 void ChunkedByteBuffer::Clear() { | |
| 121 chunks_.reset(); | |
| 122 partial_chunk_.reset(new Chunk()); | |
| 123 total_bytes_stored_ = 0; | |
| 124 } | |
| 125 | |
| 126 ChunkedByteBuffer::Chunk::Chunk() | |
| 127 : content(new std::vector<uint8>()) { | |
| 128 } | |
| 129 | |
| 130 ChunkedByteBuffer::Chunk::~Chunk() { | |
| 131 } | |
| 132 | |
| 133 size_t ChunkedByteBuffer::Chunk::ExpectedContentLength() const { | |
| 134 DCHECK_EQ(header.size(), kHeaderLength); | |
| 135 return static_cast<size_t>(ReadBigEndian32(&header[0])); | |
| 136 } | |
| 137 | |
| 138 } // namespace speech | |
| OLD | NEW |