1
/* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
3
* PrimeBase Media Stream for MySQL
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
* Original Author: Paul McCullagh
20
* Continued development: Barry Leslie
25
* Basic input and output streams.
27
* These objects wrap the system streams, and simplify things.
28
* I also want to standardize exceptions and implement
29
* socket based streams.
34
#ifndef __CSSTREAM_H__
35
#define __CSSTREAM_H__
37
#define DEFAULT_BUFFER_SIZE 64000
43
class CSInputStream : public CSRefObject {
46
virtual ~CSInputStream() { }
49
* Closes this input stream and releases any system
50
* resources associated with the stream.
52
virtual void close() = 0;
55
* Reads up to len bytes of data from the input stream into an
56
* array of bytes. Return the number of bytes read.
58
* Zero will be returned in the case of EOF.
60
* This call blocks until at least one byte is
63
virtual size_t read(char *b, size_t len) = 0;
65
/* Reads the next byte of data from the input stream.
68
virtual int read() = 0;
70
/* Read one character ahead. */
71
virtual int peek() = 0;
74
* Reset this output stream to the start inorder to restart the write.
76
virtual void reset() = 0;
78
/* Return the name of the file, or whatever: */
79
virtual const char *identify() = 0;
82
* Read a line from the input stream. This function
83
* handles all types of line endings. The function
86
CSStringBuffer *readLine();
89
class CSOutputStream : public CSRefObject {
92
* Closes this input stream and releases any system
93
* resources associated with the stream.
95
virtual void close() = 0;
98
* Writes len bytes from the specified byte array starting at
99
* offset off to this output stream.
101
virtual void write(const char *b, size_t len) = 0;
104
* Returns the default EOL indicator.
105
* Will be \n, \r or \r\n.
107
virtual const char *getEOL() = 0;
110
* Flushes this output stream and forces any buffered output
111
* bytes to be written out.
113
virtual void flush() = 0;
116
* Writes the specified byte to this output stream.
118
virtual void write(char b) = 0;
121
* Reset this output stream to the start inorder to restart the write.
123
virtual void reset() = 0;
125
virtual const char *identify() = 0;
128
* Write a line. Terminator is specific to the
129
* type of output stream and may depend on the
132
void printLine(const char *cstr);
135
* Various write types:
137
virtual void print(const char *cstr);
138
virtual void print(CSString *s);
139
virtual void print(int value);
140
virtual void print(uint64_t value);
143
class CSStream : public CSObject {
145
static void pipe(CSOutputStream *out, CSInputStream *in);
150
class CSFileInputStream : public CSInputStream {
152
CSFileInputStream(): iFile(NULL), iReadOffset(0) { }
153
virtual ~CSFileInputStream();
155
virtual void close();
157
virtual size_t read(char *b, size_t len);
164
* Reset this output stream to the start inorder to restart the read.
166
virtual void reset();
168
virtual const char *identify();
170
static CSFileInputStream *newStream(CSFile* f);
171
static CSFileInputStream *newStream(CSFile* f, off64_t offset);
178
class CSFileOutputStream : public CSOutputStream {
180
CSFileOutputStream(): iFile(NULL), iWriteOffset(0) { }
181
virtual ~CSFileOutputStream();
183
virtual void close();
185
virtual void write(const char *b, size_t len);
187
virtual const char *getEOL();
189
virtual void flush();
191
virtual void write(char b);
193
virtual void reset();
195
virtual const char *identify();
197
static CSFileOutputStream *newStream(CSFile* f);
198
static CSFileOutputStream *newStream(CSFile* f, off64_t offset);
202
off64_t iWriteOffset;
207
class CSSocketInputStream : public CSInputStream {
209
CSSocketInputStream(): iSocket(NULL) { }
210
virtual ~CSSocketInputStream();
212
virtual void close();
214
virtual size_t read(char *b, size_t len);
220
virtual void reset();
222
virtual const char *identify();
224
static CSSocketInputStream *newStream(CSSocket *s);
230
class CSSocketOutputStream : public CSOutputStream {
232
CSSocketOutputStream(): iSocket(NULL) { }
233
virtual ~CSSocketOutputStream();
235
virtual void close();
237
virtual void write(const char *b, size_t len);
239
virtual const char *getEOL() { return "\n"; };
241
virtual void flush();
243
virtual void write(char b);
245
virtual void reset();
247
virtual const char *identify();
249
static CSSocketOutputStream *newStream(CSSocket *s);
255
/* Buffered Stream: */
256
#ifdef DEBUG_disabled
257
#define CS_STREAM_BUFFER_SIZE 80
259
#define CS_STREAM_BUFFER_SIZE (32 * 1024)
262
class CSBufferedInputStream : public CSInputStream {
264
CSBufferedInputStream(): iStream(NULL), iBuffTotal(0), iBuffPos(0) { }
265
virtual ~CSBufferedInputStream();
267
virtual void close();
269
virtual size_t read(char *b, size_t len);
275
virtual void reset();
277
virtual const char *identify();
279
static CSBufferedInputStream *newStream(CSInputStream* i);
282
CSInputStream* iStream;
283
u_char iBuffer[CS_STREAM_BUFFER_SIZE];
288
class CSBufferedOutputStream : public CSOutputStream {
290
CSBufferedOutputStream(): iStream(NULL), iBuffTotal(0) { }
291
virtual ~CSBufferedOutputStream();
293
virtual void close();
295
virtual void write(const char *b, size_t len);
297
virtual const char *getEOL() { return "\n"; };
299
virtual void flush();
301
virtual void write(char b);
303
virtual void reset();
305
virtual const char *identify();
307
static CSBufferedOutputStream *newStream(CSOutputStream* i);
310
CSOutputStream* iStream;
311
u_char iBuffer[CS_STREAM_BUFFER_SIZE];
316
class CSMemoryInputStream : public CSInputStream {
318
CSMemoryInputStream(): iMemory(NULL), iMemTotal(0), iMemPos(0) { }
319
~CSMemoryInputStream(){}
321
virtual void close() {}
323
virtual size_t read(char *b, size_t len)
325
if (len > (iMemTotal - iMemPos))
326
len = iMemTotal - iMemPos;
328
memcpy(b, iMemory + iMemPos, len);
336
if (iMemPos < iMemTotal)
337
b = iMemory[iMemPos++];
344
if (iMemPos < iMemTotal)
345
b = iMemory[iMemPos];
349
virtual void reset() {iMemPos = 0;}
351
virtual const char *identify()
353
return "memory stream";
356
static CSMemoryInputStream *newStream(const u_char* buffer, uint32_t length);
359
const u_char *iMemory;
365
class CSMemoryOutputStream : public CSOutputStream {
367
CSMemoryOutputStream(): iMemory(NULL), iMemTotal(0), iMemSpace(0), iMemMin(0), iMemPos(NULL){ }
368
virtual ~CSMemoryOutputStream();
370
virtual void close() {}
372
virtual void write(const char *b, size_t len);
373
virtual const char *getEOL() { return "\n"; };
375
virtual void flush() {}
377
virtual void write(char b);
379
const u_char *getMemory(size_t *len)
381
*len = iMemPos - iMemory;
385
virtual void reset();
387
virtual const char *identify();
389
static CSMemoryOutputStream *newStream(size_t init_length, size_t min_alloc);
399
class CSStaticMemoryOutputStream : public CSOutputStream {
401
CSStaticMemoryOutputStream(u_char *mem, off64_t size): iMemory(mem), iMemSpace(size), iMemSize(size), iMemPos(mem){ }
402
virtual ~CSStaticMemoryOutputStream() {}
404
virtual void close() {}
406
virtual void write(const char *b, size_t len);
407
virtual const char *getEOL() { return "\n"; };
409
virtual void flush() {}
411
virtual void write(char b);
416
iMemSpace = iMemSize;
419
virtual const char *identify()
421
return "memory stream";
424
off64_t getSize() { return iMemPos - iMemory; }
433
typedef size_t (* CSStreamReadCallbackFunc) (void *caller_data, char *buffer, size_t buffer_size, u_char reset);
435
class CSCallbackInputStream : public CSInputStream {
437
CSCallbackInputStream(): callback(NULL), cb_data(NULL), havePeek(false), doReset(false) { }
438
~CSCallbackInputStream(){}
440
virtual void close() {}
442
virtual size_t read(char *b, size_t len)
451
size = callback(cb_data, b, len, doReset);
456
size = callback(cb_data, b, len, doReset);
472
if (!callback(cb_data, &c, 1, doReset))
484
if (callback(cb_data, &peek_char, 1, doReset))
498
virtual const char *identify()
500
return "callback stream";
503
static CSCallbackInputStream *newStream(CSStreamReadCallbackFunc callback, void *user_data);
506
CSStreamReadCallbackFunc callback;