20 if ( back.available() >= bytes ) {
21 char * ptr = back._buffer.data() + back.tail;
30 back._buffer.insert( back._buffer.end(), std::max<int64_t>(
_defaultChunkSize, bytes ),
'\0' );
57 const int64_t bytesToDiscard = std::min(bytes,
size());
58 if ( bytesToDiscard ==
size() ) {
60 return bytesToDiscard;
63 int64_t discardedSoFar = 0;
67 while ( discardedSoFar < bytesToDiscard ) {
69 const auto bytesInChunk = chunk.len();
71 if ( discardedSoFar + bytesInChunk > bytesToDiscard ) {
72 chunk.head += ( bytesToDiscard - discardedSoFar );
73 discardedSoFar = bytesToDiscard;
76 discardedSoFar += bytesInChunk;
81 return bytesToDiscard;
92 bytes = std::min( bytes,
size() );
93 if ( bytes ==
size() ) {
98 int64_t choppedSoFar = 0;
99 while ( choppedSoFar < bytes &&
_chunks.size() ) {
100 auto bytesStillToChop = bytes - choppedSoFar;
103 if ( chunk.len() > bytesStillToChop ) {
104 chunk.tail -= bytesStillToChop;
107 choppedSoFar += chunk.len();
124 ::memcpy( buf, data, count );
130 append( data.data(), data.size() );
135 const size_t bytesToRead = std::min(
size(), max );
136 size_t readSoFar = 0;
138 while ( readSoFar < bytesToRead &&
_chunks.size() ) {
141 const auto toRead = std::min<size_t>( bytesToRead - readSoFar, chunk.len() );
142 ::memcpy( buffer+readSoFar, chunk.data(), toRead );
146 chunk.head += toRead;
147 if( chunk.head >= chunk.tail )
157 for (
const auto &c :
_chunks )
172 maxCount = std::min<size_t>( maxCount,
size() );
174 int64_t scannedSoFar = 0;
175 for (
const auto &chunk :
_chunks ) {
178 if ( scannedSoFar+chunk.len() - 1 < pos ) {
179 scannedSoFar += chunk.len();
183 const char *
const chunkBegin = chunk.data();
184 const char *s = chunkBegin;
186 size_t lengthToScan = std::min<size_t>( chunk.len() , maxCount - scannedSoFar );
187 if ( pos > 0 && scannedSoFar < pos ) {
188 const auto adjust = (pos-scannedSoFar);
190 lengthToScan -= adjust;
193 const char *ptr =
reinterpret_cast<const char*
>(::memchr( s, c, lengthToScan ));
195 return ( ( ptr - chunkBegin ) + scannedSoFar );
198 scannedSoFar += chunk.len();
199 if ( scannedSoFar >= maxCount )
209 const auto idx =
indexOf( delim, max == 0 ?
size() : max );
214 read(
b.data(), idx+1 );
220 assert( buffer !=
nullptr && max > 1 );
221 const auto maxRead = max - 1;
222 const auto idx =
indexOf( delim, maxRead );
223 const auto bytesRead =
read( buffer, idx == -1 ? maxRead : idx + 1 );
224 buffer[bytesRead] =
'\0';
IOBuffer(int64_t chunkSize=0)
int64_t indexOf(const char c) const
ByteArray readLine(const int64_t max=0)
void append(const char *data, int64_t count)
static std::size_t maxSize()
int64_t discard(int64_t bytes)
int64_t frontSize() const
int64_t _defaultChunkSize
bool canReadUntil(const char delim) const
std::vector< Chunk > _chunks
int64_t read(char *buffer, int64_t max)
ByteArray readUntil(const char delim, const int64_t max=0)
char * reserve(int64_t bytes)
std::vector< Chunk >::size_type chunks() const