1 // Copyright 2014 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 #include "net/spdy/spdy_prefixed_buffer_reader.h"
7 #include "base/logging.h"
11 SpdyPrefixedBufferReader::SpdyPrefixedBufferReader(
18 prefix_length_(prefix_length
),
19 suffix_length_(suffix_length
) {}
21 size_t SpdyPrefixedBufferReader::Available() {
22 return prefix_length_
+ suffix_length_
;
25 bool SpdyPrefixedBufferReader::ReadN(size_t count
, char* out
) {
26 if (Available() < count
) {
30 if (prefix_length_
>= count
) {
31 // Read is fully satisfied by the prefix.
32 std::copy(prefix_
, prefix_
+ count
, out
);
34 prefix_length_
-= count
;
36 } else if (prefix_length_
!= 0) {
37 // Read is partially satisfied by the prefix.
38 out
= std::copy(prefix_
, prefix_
+ prefix_length_
, out
);
39 count
-= prefix_length_
;
41 // Fallthrough to suffix read.
43 DCHECK(suffix_length_
>= count
);
44 // Read is satisfied by the suffix.
45 std::copy(suffix_
, suffix_
+ count
, out
);
47 suffix_length_
-= count
;
51 bool SpdyPrefixedBufferReader::ReadN(size_t count
,
52 SpdyPinnableBufferPiece
* out
) {
53 if (Available() < count
) {
57 out
->storage_
.reset();
60 if (prefix_length_
>= count
) {
61 // Read is fully satisfied by the prefix.
62 out
->buffer_
= prefix_
;
64 prefix_length_
-= count
;
66 } else if (prefix_length_
!= 0) {
67 // Read is only partially satisfied by the prefix. We need to allocate
68 // contiguous storage as the read spans the prefix & suffix.
69 out
->storage_
.reset(new char[count
]);
70 out
->buffer_
= out
->storage_
.get();
71 ReadN(count
, out
->storage_
.get());
74 DCHECK(suffix_length_
>= count
);
75 // Read is fully satisfied by the suffix.
76 out
->buffer_
= suffix_
;
78 suffix_length_
-= count
;