32#ifndef _QORE_BUFFEREDSTREAMREADER_H
33#define _QORE_BUFFEREDSTREAMREADER_H
37#include "qore/qore_bitopts.h"
38#include "qore/InputStream.h"
39#include "qore/intern/StreamReader.h"
42#define DefaultStreamBufferSize 4096
49 bufCapacity((
size_t)bufsize),
53 xsink->
raiseException(
"STREAM-BUFFER-ERROR",
"the buffer size must be > 0 (value provided: " QLLD
")", bufsize);
58 buf =
new char[bufCapacity + 1];
66 DLLLOCAL
virtual const char* getName()
const override {
return "BufferedStreamReader"; }
81 char* destPtr =
static_cast<char*
>(dest);
86 memmove(destPtr, buf,
read);
87 if (limit < bufCount) {
88 assert(limit ==
read);
99 size_t to_read = limit -
read;
100 if (to_read <= (bufCapacity / 2))
107 xsink->
raiseException(
"END-OF-STREAM-ERROR",
"there is not enough data available in the stream; "
108 QSD
" bytes were requested, and " QSD
" were read", limit,
read);
119 size_t to_read = limit -
read;
124 int64 rc = fillBuffer(bufCapacity, xsink);
129 xsink->
raiseException(
"END-OF-STREAM-ERROR",
"there is not enough data available in the stream; " QSD
" bytes were requested, and " QSD
" were read", limit,
read);
135 size_t len =
QORE_MIN((
size_t)rc, to_read);
136 memcpy(destPtr +
read, buf, len);
139 assert(((limit -
read) && !bufCount) || !(limit -
read));
152 int rc = fillBuffer(bufCapacity, xsink);
164 assert(bufCount + bytes <= bufCapacity);
172 DLLLOCAL
bool prepareEnoughData(
size_t bytes,
ExceptionSink* xsink) {
173 if (bytes > bufCapacity) {
174 xsink->
raiseException(
"STREAM-BUFFER-ERROR",
"a read of " QSD
" bytes was attempted on a BufferedStreamReader with a capacity of " QSD
" bytes", bytes, bufCapacity);
177 if (bufCount < bytes) {
179 int64 rc = fillBuffer(bytes - bufCount, xsink);
184 xsink->
raiseException(
"END-OF-STREAM-ERROR",
"a read of " QSD
" bytes cannot be performed because there is not enough data available in the stream to satisfy the request", bytes);
187 if (bufCount >= bytes)
194 DLLLOCAL
void shiftBuffer(
size_t bytes) {
195 assert(bytes <= bufCount && bytes > 0);
197 memmove(buf, buf+bytes, bufCount);
205 DLLLOCAL
const char* findEolInBuffer(
const QoreStringNode* eol,
size_t& eolLen,
bool endOfStream,
char& pmatch)
const {
208 const char* p = strstr(buf, eol->
getBuffer());
213 const char* p = strpbrk(buf,
"\n\r");
219 if (*(p+1) ==
'\n') {
224 else if (
static_cast<size_t>(p - buf + 1) == bufCount) {
#define QORE_MIN(a, b)
macro to return the minimum of 2 numbers
Definition: QoreLib.h:550
Private data for the Qore::BufferedStreamReader class.
Definition: BufferedStreamReader.h:45
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
DLLEXPORT size_t strlen() const
returns number of bytes in the string (not including the null pointer)
DLLEXPORT const char * getBuffer() const
returns the string's buffer; this data should not be changed
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
Private data for the Qore::StreamReader class.
Definition: StreamReader.h:45
virtual DLLLOCAL qore_offset_t read(ExceptionSink *xsink, void *dest, size_t limit, bool require_all=true)
Read data until a limit.
Definition: StreamReader.h:382
ReferenceHolder< InputStream > in
Source input stream.
Definition: StreamReader.h:393
intptr_t qore_offset_t
used for offsets that could be negative
Definition: common.h:76
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260