• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_
6 #define CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "content/common/content_export.h"
15 
16 namespace content {
17 
18 // Models a chunk-oriented byte buffer. The term chunk is herein defined as an
19 // arbitrary sequence of bytes that is preceeded by N header bytes, indicating
20 // its size. Data may be appended to the buffer with no particular respect of
21 // chunks boundaries. However, chunks can be extracted (FIFO) only when their
22 // content (according to their header) is fully available in the buffer.
23 // The current implementation support only 4 byte Big Endian headers.
24 // Empty chunks (i.e. the sequence 00 00 00 00) are NOT allowed.
25 //
26 // E.g. 00 00 00 04 xx xx xx xx 00 00 00 02 yy yy 00 00 00 04 zz zz zz zz
27 //      [----- CHUNK 1 -------] [--- CHUNK 2 ---] [------ CHUNK 3 ------]
28 class CONTENT_EXPORT ChunkedByteBuffer {
29  public:
30   ChunkedByteBuffer();
31   ~ChunkedByteBuffer();
32 
33   // Appends |length| bytes starting from |start| to the buffer.
34   void Append(const uint8* start, size_t length);
35 
36   // Appends bytes contained in the |string| to the buffer.
37   void Append(const std::string& string);
38 
39   // Checks whether one or more complete chunks are available in the buffer.
40   bool HasChunks() const;
41 
42   // If enough data is available, reads and removes the first complete chunk
43   // from the buffer. Returns a NULL pointer if no complete chunk is available.
44   scoped_ptr< std::vector<uint8> > PopChunk();
45 
46   // Clears all the content of the buffer.
47   void Clear();
48 
49   // Returns the number of raw bytes (including headers) present.
GetTotalLength()50   size_t GetTotalLength() const { return total_bytes_stored_; }
51 
52  private:
53   struct Chunk {
54     Chunk();
55     ~Chunk();
56 
57     std::vector<uint8> header;
58     scoped_ptr< std::vector<uint8> > content;
59     size_t ExpectedContentLength() const;
60 
61    private:
62     DISALLOW_COPY_AND_ASSIGN(Chunk);
63   };
64 
65   ScopedVector<Chunk> chunks_;
66   scoped_ptr<Chunk> partial_chunk_;
67   size_t total_bytes_stored_;
68 
69   DISALLOW_COPY_AND_ASSIGN(ChunkedByteBuffer);
70 };
71 
72 
73 }  // namespace content
74 
75 #endif  // CONTENT_BROWSER_SPEECH_CHUNKED_BYTE_BUFFER_H_
76