OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "net/spdy/spdy_prefixed_buffer_reader.h" | |
6 | |
7 #include "base/logging.h" | |
8 | |
9 namespace net { | |
10 | |
11 SpdyPrefixedBufferReader::SpdyPrefixedBufferReader( | |
12 const char* prefix, | |
13 size_t prefix_length, | |
14 const char* suffix, | |
15 size_t suffix_length) | |
16 : prefix_(prefix), | |
17 suffix_(suffix), | |
18 prefix_length_(prefix_length), | |
19 suffix_length_(suffix_length) {} | |
20 | |
21 size_t SpdyPrefixedBufferReader::Available() { | |
22 return prefix_length_ + suffix_length_; | |
23 } | |
24 | |
25 bool SpdyPrefixedBufferReader::ReadN(size_t count, char* out) { | |
26 if (Available() < count) { | |
27 return false; | |
28 } | |
29 | |
30 if (prefix_length_ >= count) { | |
31 // Read is fully satisfied by the prefix. | |
32 std::copy(prefix_, prefix_ + count, out); | |
33 prefix_ += count; | |
34 prefix_length_ -= count; | |
35 return true; | |
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_; | |
40 prefix_length_ = 0; | |
41 // Fallthrough to suffix read. | |
42 } | |
43 DCHECK(suffix_length_ >= count); | |
44 // Read is satisfied by the suffix. | |
45 std::copy(suffix_, suffix_ + count, out); | |
46 suffix_ += count; | |
47 suffix_length_ -= count; | |
48 return true; | |
49 } | |
50 | |
51 bool SpdyPrefixedBufferReader::ReadN(size_t count, | |
52 SpdyPinnableBufferPiece* out) { | |
53 if (Available() < count) { | |
54 return false; | |
55 } | |
56 | |
57 out->storage_.reset(); | |
58 out->length_ = count; | |
59 | |
60 if (prefix_length_ >= count) { | |
61 // Read is fully satisfied by the prefix. | |
62 out->buffer_ = prefix_; | |
63 prefix_ += count; | |
64 prefix_length_ -= count; | |
65 return true; | |
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()); | |
72 return true; | |
73 } else { | |
74 DCHECK(suffix_length_ >= count); | |
75 // Read is fully satisfied by the suffix. | |
76 out->buffer_ = suffix_; | |
77 suffix_ += count; | |
78 suffix_length_ -= count; | |
79 return true; | |
80 } | |
81 } | |
82 | |
83 } // namespace net | |
OLD | NEW |