2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "platform/SharedBufferChunkReader.h"
34 #include "platform/SharedBuffer.h"
38 SharedBufferChunkReader::SharedBufferChunkReader(SharedBuffer
* buffer
, const Vector
<char>& separator
)
44 , m_reachedEndOfFile(false)
45 , m_separator(separator
)
50 SharedBufferChunkReader::SharedBufferChunkReader(SharedBuffer
* buffer
, const char* separator
)
56 , m_reachedEndOfFile(false)
59 setSeparator(separator
);
62 void SharedBufferChunkReader::setSeparator(const Vector
<char>& separator
)
64 m_separator
= separator
;
67 void SharedBufferChunkReader::setSeparator(const char* separator
)
70 m_separator
.append(separator
, strlen(separator
));
73 bool SharedBufferChunkReader::nextChunk(Vector
<char>& chunk
, bool includeSeparator
)
75 if (m_reachedEndOfFile
)
80 while (m_segmentIndex
< m_segmentLength
) {
81 char currentCharacter
= m_segment
[m_segmentIndex
++];
82 if (currentCharacter
!= m_separator
[m_separatorIndex
]) {
83 if (m_separatorIndex
> 0) {
84 ASSERT_WITH_SECURITY_IMPLICATION(m_separatorIndex
<= m_separator
.size());
85 chunk
.append(m_separator
.data(), m_separatorIndex
);
88 chunk
.append(currentCharacter
);
92 if (m_separatorIndex
== m_separator
.size()) {
94 chunk
.appendVector(m_separator
);
100 // Read the next segment.
102 m_bufferPosition
+= m_segmentLength
;
103 m_segmentLength
= m_buffer
->getSomeData(m_segment
, m_bufferPosition
);
104 if (!m_segmentLength
) {
105 m_reachedEndOfFile
= true;
106 if (m_separatorIndex
> 0)
107 chunk
.append(m_separator
.data(), m_separatorIndex
);
108 return !chunk
.isEmpty();
111 ASSERT_NOT_REACHED();
115 String
SharedBufferChunkReader::nextChunkAsUTF8StringWithLatin1Fallback(bool includeSeparator
)
118 if (!nextChunk(data
, includeSeparator
))
121 return data
.size() ? String::fromUTF8WithLatin1Fallback(data
.data(), data
.size()) : emptyString();
124 size_t SharedBufferChunkReader::peek(Vector
<char>& data
, size_t requestedSize
)
127 if (requestedSize
<= m_segmentLength
- m_segmentIndex
) {
128 data
.append(m_segment
+ m_segmentIndex
, requestedSize
);
129 return requestedSize
;
132 size_t readBytesCount
= m_segmentLength
- m_segmentIndex
;
133 data
.append(m_segment
+ m_segmentIndex
, readBytesCount
);
135 size_t bufferPosition
= m_bufferPosition
+ m_segmentLength
;
136 const char* segment
= 0;
137 while (size_t segmentLength
= m_buffer
->getSomeData(segment
, bufferPosition
)) {
138 if (requestedSize
<= readBytesCount
+ segmentLength
) {
139 data
.append(segment
, requestedSize
- readBytesCount
);
140 readBytesCount
+= (requestedSize
- readBytesCount
);
143 data
.append(segment
, segmentLength
);
144 readBytesCount
+= segmentLength
;
145 bufferPosition
+= segmentLength
;
147 return readBytesCount
;