Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / quic / quic_data_writer.h
blob762c3aaee49ee4ac98d6154423f4771ff994aa5a
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.
5 #ifndef NET_QUIC_QUIC_DATA_WRITER_H_
6 #define NET_QUIC_QUIC_DATA_WRITER_H_
8 #include <cstddef>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/logging.h"
13 #include "base/port.h"
14 #include "base/strings/string_piece.h"
15 #include "net/base/int128.h"
16 #include "net/base/net_export.h"
17 #include "net/quic/quic_protocol.h"
19 namespace net {
21 // This class provides facilities for packing QUIC data.
23 // The QuicDataWriter supports appending primitive values (int, string, etc)
24 // to a frame instance. The internal memory buffer is exposed as the "data"
25 // of the QuicDataWriter.
26 class NET_EXPORT_PRIVATE QuicDataWriter {
27 public:
28 // Creates a QuicDataWriter where |buffer| is not owned.
29 QuicDataWriter(size_t size, char* buffer);
31 ~QuicDataWriter();
33 // Returns the size of the QuicDataWriter's data.
34 size_t length() const { return length_; }
36 // Retrieves the buffer from the QuicDataWriter without changing ownership.
37 char* data();
39 // Methods for adding to the payload. These values are appended to the end
40 // of the QuicDataWriter payload. Note - binary integers are written in
41 // host byte order (little endian) not network byte order (big endian).
42 bool WriteUInt8(uint8 value);
43 bool WriteUInt16(uint16 value);
44 bool WriteUInt32(uint32 value);
45 bool WriteUInt48(uint64 value);
46 bool WriteUInt64(uint64 value);
47 // Write unsigned floating point corresponding to the value. Large values are
48 // clamped to the maximum representable (kUFloat16MaxValue). Values that can
49 // not be represented directly are rounded down.
50 bool WriteUFloat16(uint64 value);
51 bool WriteStringPiece16(base::StringPiece val);
52 bool WriteIOVector(const IOVector& data);
53 bool WriteBytes(const void* data, size_t data_len);
54 bool WriteRepeatedByte(uint8 byte, size_t count);
55 // Fills the remaining buffer with null characters.
56 void WritePadding();
58 size_t capacity() const {
59 return capacity_;
62 private:
63 // Returns the location that the data should be written at, or nullptr if
64 // there is not enough room. Call EndWrite with the returned offset and the
65 // given length to pad out for the next write.
66 char* BeginWrite(size_t length);
68 char* buffer_;
69 size_t capacity_; // Allocation size of payload (or -1 if buffer is const).
70 size_t length_; // Current length of the buffer.
72 DISALLOW_COPY_AND_ASSIGN(QuicDataWriter);
75 } // namespace net
77 #endif // NET_QUIC_QUIC_DATA_WRITER_H_