OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2014 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 22 matching lines...) Expand all Loading... |
33 #include "platform/SharedBuffer.h" | 33 #include "platform/SharedBuffer.h" |
34 #include "wtf/PassRefPtr.h" | 34 #include "wtf/PassRefPtr.h" |
35 #include "wtf/RefPtr.h" | 35 #include "wtf/RefPtr.h" |
36 | 36 |
37 #include <algorithm> | 37 #include <algorithm> |
38 #include <cstring> | 38 #include <cstring> |
39 | 39 |
40 namespace blink { | 40 namespace blink { |
41 | 41 |
42 SharedBufferReader::SharedBufferReader(PassRefPtr<const SharedBuffer> buffer) | 42 SharedBufferReader::SharedBufferReader(PassRefPtr<const SharedBuffer> buffer) |
43 : m_buffer(buffer), m_currentOffset(0) {} | 43 : m_buffer(std::move(buffer)), m_currentOffset(0) {} |
44 | 44 |
45 SharedBufferReader::~SharedBufferReader() {} | 45 SharedBufferReader::~SharedBufferReader() {} |
46 | 46 |
47 int SharedBufferReader::readData(char* outputBuffer, int askedToRead) { | 47 int SharedBufferReader::readData(char* outputBuffer, int askedToRead) { |
48 if (!m_buffer || m_currentOffset > m_buffer->size()) | 48 if (!m_buffer || m_currentOffset > m_buffer->size()) |
49 return 0; | 49 return 0; |
50 | 50 |
51 size_t bytesCopied = 0; | 51 size_t bytesCopied = 0; |
52 size_t bytesLeft = m_buffer->size() - m_currentOffset; | 52 size_t bytesLeft = m_buffer->size() - m_currentOffset; |
53 size_t lenToCopy = std::min(safeCast<size_t>(askedToRead), bytesLeft); | 53 size_t lenToCopy = std::min(safeCast<size_t>(askedToRead), bytesLeft); |
54 | 54 |
55 while (bytesCopied < lenToCopy) { | 55 while (bytesCopied < lenToCopy) { |
56 const char* data; | 56 const char* data; |
57 size_t segmentSize = m_buffer->getSomeData(data, m_currentOffset); | 57 size_t segmentSize = m_buffer->getSomeData(data, m_currentOffset); |
58 if (!segmentSize) | 58 if (!segmentSize) |
59 break; | 59 break; |
60 | 60 |
61 segmentSize = std::min(segmentSize, lenToCopy - bytesCopied); | 61 segmentSize = std::min(segmentSize, lenToCopy - bytesCopied); |
62 memcpy(outputBuffer + bytesCopied, data, segmentSize); | 62 memcpy(outputBuffer + bytesCopied, data, segmentSize); |
63 bytesCopied += segmentSize; | 63 bytesCopied += segmentSize; |
64 m_currentOffset += segmentSize; | 64 m_currentOffset += segmentSize; |
65 } | 65 } |
66 | 66 |
67 return safeCast<int>(bytesCopied); | 67 return safeCast<int>(bytesCopied); |
68 } | 68 } |
69 | 69 |
70 } // namespace blink | 70 } // namespace blink |
OLD | NEW |