| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/spdy/spdy_prefixed_buffer_reader.h" | 5 #include "net/spdy/spdy_prefixed_buffer_reader.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace net { | 9 namespace net { |
| 10 | 10 |
| 11 SpdyPrefixedBufferReader::SpdyPrefixedBufferReader( | 11 SpdyPrefixedBufferReader::SpdyPrefixedBufferReader( |
| 12 const char* prefix, | 12 const char* prefix, |
| 13 size_t prefix_length, | 13 size_t prefix_length, |
| 14 const char* suffix, | 14 const char* suffix, |
| 15 size_t suffix_length) | 15 size_t suffix_length) |
| 16 : prefix_(prefix), | 16 : prefix_(prefix), |
| 17 suffix_(suffix), | 17 suffix_(suffix), |
| 18 prefix_length_(prefix_length), | 18 prefix_length_(prefix_length), |
| 19 suffix_length_(suffix_length) {} | 19 suffix_length_(suffix_length) {} |
| 20 | 20 |
| 21 size_t SpdyPrefixedBufferReader::Available() { | 21 size_t SpdyPrefixedBufferReader::Available() { |
| 22 return prefix_length_ + suffix_length_; | 22 return prefix_length_ + suffix_length_; |
| 23 } | 23 } |
| 24 | 24 |
| 25 bool SpdyPrefixedBufferReader::ReadN(size_t count, char* out) { | 25 bool SpdyPrefixedBufferReader::ReadN(size_t count, char* out) { |
| 26 if (Available() < count) | 26 if (Available() < count) { |
| 27 return false; | 27 return false; |
| 28 } |
| 28 | 29 |
| 29 if (prefix_length_ >= count) { | 30 if (prefix_length_ >= count) { |
| 30 // Read is fully satisfied by the prefix. | 31 // Read is fully satisfied by the prefix. |
| 31 std::copy(prefix_, prefix_ + count, out); | 32 std::copy(prefix_, prefix_ + count, out); |
| 32 prefix_ += count; | 33 prefix_ += count; |
| 33 prefix_length_ -= count; | 34 prefix_length_ -= count; |
| 34 return true; | 35 return true; |
| 35 } else if (prefix_length_ != 0) { | 36 } else if (prefix_length_ != 0) { |
| 36 // Read is partially satisfied by the prefix. | 37 // Read is partially satisfied by the prefix. |
| 37 out = std::copy(prefix_, prefix_ + prefix_length_, out); | 38 out = std::copy(prefix_, prefix_ + prefix_length_, out); |
| 38 count -= prefix_length_; | 39 count -= prefix_length_; |
| 39 prefix_length_ = 0; | 40 prefix_length_ = 0; |
| 40 // Fallthrough to suffix read. | 41 // Fallthrough to suffix read. |
| 41 } | 42 } |
| 42 DCHECK(suffix_length_ >= count); | 43 DCHECK(suffix_length_ >= count); |
| 43 // Read is satisfied by the suffix. | 44 // Read is satisfied by the suffix. |
| 44 std::copy(suffix_, suffix_ + count, out); | 45 std::copy(suffix_, suffix_ + count, out); |
| 45 suffix_ += count; | 46 suffix_ += count; |
| 46 suffix_length_ -= count; | 47 suffix_length_ -= count; |
| 47 return true; | 48 return true; |
| 48 } | 49 } |
| 49 | 50 |
| 50 bool SpdyPrefixedBufferReader::ReadN(size_t count, | 51 bool SpdyPrefixedBufferReader::ReadN(size_t count, |
| 51 SpdyPinnableBufferPiece* out) { | 52 SpdyPinnableBufferPiece* out) { |
| 52 if (Available() < count) | 53 if (Available() < count) { |
| 53 return false; | 54 return false; |
| 55 } |
| 54 | 56 |
| 55 out->storage_.reset(); | 57 out->storage_.reset(); |
| 56 out->length_ = count; | 58 out->length_ = count; |
| 57 | 59 |
| 58 if (prefix_length_ >= count) { | 60 if (prefix_length_ >= count) { |
| 59 // Read is fully satisfied by the prefix. | 61 // Read is fully satisfied by the prefix. |
| 60 out->buffer_ = prefix_; | 62 out->buffer_ = prefix_; |
| 61 prefix_ += count; | 63 prefix_ += count; |
| 62 prefix_length_ -= count; | 64 prefix_length_ -= count; |
| 63 return true; | 65 return true; |
| 64 } else if (prefix_length_ != 0) { | 66 } else if (prefix_length_ != 0) { |
| 65 // Read is only partially satisfied by the prefix. We need to allocate | 67 // Read is only partially satisfied by the prefix. We need to allocate |
| 66 // contiguous storage as the read spans the prefix & suffix. | 68 // contiguous storage as the read spans the prefix & suffix. |
| 67 out->storage_.reset(new char[count]); | 69 out->storage_.reset(new char[count]); |
| 68 out->buffer_ = out->storage_.get(); | 70 out->buffer_ = out->storage_.get(); |
| 69 ReadN(count, out->storage_.get()); | 71 ReadN(count, out->storage_.get()); |
| 70 return true; | 72 return true; |
| 71 } else { | 73 } else { |
| 72 DCHECK(suffix_length_ >= count); | 74 DCHECK(suffix_length_ >= count); |
| 73 // Read is fully satisfied by the suffix. | 75 // Read is fully satisfied by the suffix. |
| 74 out->buffer_ = suffix_; | 76 out->buffer_ = suffix_; |
| 75 suffix_ += count; | 77 suffix_ += count; |
| 76 suffix_length_ -= count; | 78 suffix_length_ -= count; |
| 77 return true; | 79 return true; |
| 78 } | 80 } |
| 79 } | 81 } |
| 80 | 82 |
| 81 } // namespace net | 83 } // namespace net |
| OLD | NEW |