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
)
29 if (prefix_length_
>= count
) {
30 // Read is fully satisfied by the prefix.
31 std::copy(prefix_
, prefix_
+ count
, out
);
33 prefix_length_
-= count
;
35 } else if (prefix_length_
!= 0) {
36 // Read is partially satisfied by the prefix.
37 out
= std::copy(prefix_
, prefix_
+ prefix_length_
, out
);
38 count
-= prefix_length_
;
40 // Fallthrough to suffix read.
42 DCHECK(suffix_length_
>= count
);
43 // Read is satisfied by the suffix.
44 std::copy(suffix_
, suffix_
+ count
, out
);
46 suffix_length_
-= count
;
50 bool SpdyPrefixedBufferReader::ReadN(size_t count
,
51 SpdyPinnableBufferPiece
* out
) {
52 if (Available() < count
)
55 out
->storage_
.reset();
58 if (prefix_length_
>= count
) {
59 // Read is fully satisfied by the prefix.
60 out
->buffer_
= prefix_
;
62 prefix_length_
-= count
;
64 } else if (prefix_length_
!= 0) {
65 // Read is only partially satisfied by the prefix. We need to allocate
66 // contiguous storage as the read spans the prefix & suffix.
67 out
->storage_
.reset(new char[count
]);
68 out
->buffer_
= out
->storage_
.get();
69 ReadN(count
, out
->storage_
.get());
72 DCHECK(suffix_length_
>= count
);
73 // Read is fully satisfied by the suffix.
74 out
->buffer_
= suffix_
;
76 suffix_length_
-= count
;