Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: content/browser/speech/chunked_byte_buffer.cc

Issue 10123008: Introduced ChunkedByteBuffer class which will be needed by next speech recognition CLs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reimplemented according to Satish suggestions. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/logging.h"
11
12 namespace {
13 static const size_t kHeaderLength = sizeof(uint32);
14 COMPILE_ASSERT(sizeof(size_t) >= kHeaderLength,
15 ChunkedByteBufferNotSupportedOnThisArchitecture);
16 }
17
18 namespace speech {
19
20 ChunkedByteBuffer::ChunkedByteBuffer()
21 : partial_chunk_(new Chunk()),
22 total_bytes_stored_(0) {
23 }
24
25 ChunkedByteBuffer::~ChunkedByteBuffer() {
26 Clear();
27 }
28
29 void ChunkedByteBuffer::Append(const uint8* start, size_t length) {
30 DCHECK(length > 0);
31 size_t remaining_bytes = length;
32 const uint8* next_data = start;
33
34 while (remaining_bytes > 0) {
35 DCHECK(partial_chunk_ != NULL);
36 size_t insert_length = 0;
37 bool header_completed = false;
38 bool content_completed = false;
39 std::vector<uint8>* insert_target;
40
41 if (partial_chunk_->header.size() < kHeaderLength) {
42 const size_t bytes_to_complete_header =
43 kHeaderLength - partial_chunk_->header.size();
44 insert_length = std::min(bytes_to_complete_header, remaining_bytes);
45 insert_target = &partial_chunk_->header;
46 header_completed = (remaining_bytes >= bytes_to_complete_header);
47 } else {
48 DCHECK_LT(partial_chunk_->content.size(),
49 partial_chunk_->ExpectedContentLength());
50 const size_t bytes_to_complete_chunk =
51 partial_chunk_->ExpectedContentLength() -
52 partial_chunk_->content.size();
53 insert_length = std::min(bytes_to_complete_chunk, remaining_bytes);
54 insert_target = &partial_chunk_->content;
55 content_completed = (remaining_bytes >= bytes_to_complete_chunk);
56 }
57
58 DCHECK_GT(insert_length, 0U);
59 DCHECK_LE(insert_length, remaining_bytes);
60 DCHECK_LE(next_data + insert_length, start + length);
61 insert_target->insert(insert_target->end(),
62 next_data,
63 next_data + insert_length);
64 next_data += insert_length;
65 remaining_bytes -= insert_length;
66
67 if (header_completed) {
68 DCHECK_EQ(partial_chunk_->header.size(), kHeaderLength);
69 DCHECK_NE(partial_chunk_->ExpectedContentLength(), 0U);
70 partial_chunk_->content.reserve(partial_chunk_->ExpectedContentLength());
71 } else if (content_completed) {
72 DCHECK_EQ(partial_chunk_->content.size(),
73 partial_chunk_->ExpectedContentLength());
74 chunks_.push_back(partial_chunk_.release());
75 partial_chunk_.reset(new Chunk());
76 }
77 }
78 DCHECK_EQ(next_data, start + length);
79 total_bytes_stored_ += length;
80 }
81
82 void ChunkedByteBuffer::Append(const std::string& string) {
83 Append(reinterpret_cast<const uint8*>(string.data()), string.size());
84 }
85
86 size_t ChunkedByteBuffer::GetNextChunkLength() const {
87 return GetNextChunk(NULL);
88 }
89
90 size_t ChunkedByteBuffer::GetNextChunk(const uint8** start_ptr) const {
91 if (chunks_.empty())
92 return static_cast<size_t>(0U);
93 const Chunk& first_chunk = *(*chunks_.begin());
94 DCHECK_EQ(first_chunk.header.size(), kHeaderLength);
95 DCHECK_EQ(first_chunk.content.size(), first_chunk.ExpectedContentLength());
96 if (start_ptr != NULL)
97 *start_ptr = &first_chunk.content[0];
98 return first_chunk.content.size();
99 }
100
101 void ChunkedByteBuffer::RemoveNextChunk() {
102 DCHECK(!chunks_.empty());
103 total_bytes_stored_ -= GetNextChunkLength();
104 total_bytes_stored_ -= kHeaderLength;
105 chunks_.erase(chunks_.begin());
106 }
107
108 void ChunkedByteBuffer::Clear() {
109 chunks_.reset();
110 partial_chunk_.reset(new Chunk());
111 total_bytes_stored_ = 0;
112 }
113
114 size_t ChunkedByteBuffer::Chunk::ExpectedContentLength() const {
115 DCHECK_EQ(header.size(), kHeaderLength);
116 return static_cast<size_t>(ChunkedByteBuffer::ReadBigEndian32(&header[0]));
117 }
118
119 uint32 ChunkedByteBuffer::ReadBigEndian32(const uint8* buffer) {
Satish 2012/04/24 09:10:14 Move this out to the anonymous namespace at the to
120 return (static_cast<uint32>(buffer[3])) |
121 (static_cast<uint32>(buffer[2]) << 8) |
122 (static_cast<uint32>(buffer[1]) << 16) |
123 (static_cast<uint32>(buffer[0]) << 24);
124 }
125
126 } // namespace speech
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698