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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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.
33
#ifndef __CSSTREAM_H__
34
#define __CSSTREAM_H__
36
#define DEFAULT_BUFFER_SIZE 64000
42
class CSInputStream : public CSRefObject {
45
virtual ~CSInputStream() { }
48
* Closes this input stream and releases any system
49
* resources associated with the stream.
51
virtual void close() = 0;
54
* Reads up to len bytes of data from the input stream into an
55
* array of bytes. Return the number of bytes read.
57
* Zero will be returned in the case of EOF.
59
* This call blocks until at least one byte is
62
virtual size_t read(char *b, size_t len) = 0;
64
/* Reads the next byte of data from the input stream.
67
virtual int read() = 0;
69
/* Read one character ahead. */
70
virtual int peek() = 0;
73
* Reset this output stream to the start inorder to restart the write.
75
virtual void reset() = 0;
78
* Read a line from the input stream. This function
79
* handles all types of line endings. The function
82
CSStringBuffer *readLine();
85
class CSOutputStream : public CSRefObject {
88
* Closes this input stream and releases any system
89
* resources associated with the stream.
91
virtual void close() = 0;
94
* Writes len bytes from the specified byte array starting at
95
* offset off to this output stream.
97
virtual void write(const char *b, size_t len) = 0;
100
* Returns the default EOL indicator.
101
* Will be \n, \r or \r\n.
103
virtual const char *getEOL() = 0;
106
* Flushes this output stream and forces any buffered output
107
* bytes to be written out.
109
virtual void flush() = 0;
112
* Writes the specified byte to this output stream.
114
virtual void write(char b) = 0;
117
* Reset this output stream to the start inorder to restart the write.
119
virtual void reset() = 0;
122
* Write a line. Terminator is specific to the
123
* type of output stream and may depend on the
126
void printLine(const char *cstr);
129
* Various write types:
131
virtual void print(const char *cstr);
132
virtual void print(CSString *s);
133
virtual void print(int value);
134
virtual void print(uint64_t value);
137
class CSStream : public CSObject {
139
static void pipe(CSOutputStream *out, CSInputStream *in);
144
class CSFileInputStream : public CSInputStream {
146
CSFileInputStream(): iFile(NULL), iReadOffset(0) { }
147
virtual ~CSFileInputStream();
149
virtual void close();
151
virtual size_t read(char *b, size_t len);
158
* Reset this output stream to the start inorder to restart the read.
160
virtual void reset();
162
static CSFileInputStream *newStream(CSFile* f);
163
static CSFileInputStream *newStream(CSFile* f, off64_t offset);
170
class CSFileOutputStream : public CSOutputStream {
172
CSFileOutputStream(): iFile(NULL), iWriteOffset(0) { }
173
virtual ~CSFileOutputStream();
175
virtual void close();
177
virtual void write(const char *b, size_t len);
179
virtual const char *getEOL();
181
virtual void flush();
183
virtual void write(char b);
185
virtual void reset();
187
static CSFileOutputStream *newStream(CSFile* f);
188
static CSFileOutputStream *newStream(CSFile* f, off64_t offset);
192
off64_t iWriteOffset;
197
class CSSocketInputStream : public CSInputStream {
199
CSSocketInputStream(): iSocket(NULL) { }
200
virtual ~CSSocketInputStream();
202
virtual void close();
204
virtual size_t read(char *b, size_t len);
210
virtual void reset();
212
static CSSocketInputStream *newStream(CSSocket *s);
218
class CSSocketOutputStream : public CSOutputStream {
220
CSSocketOutputStream(): iSocket(NULL) { }
221
virtual ~CSSocketOutputStream();
223
virtual void close();
225
virtual void write(const char *b, size_t len);
227
virtual const char *getEOL() { return "\n"; };
229
virtual void flush();
231
virtual void write(char b);
233
virtual void reset();
235
static CSSocketOutputStream *newStream(CSSocket *s);
241
/* Buffered Stream: */
243
#define CS_STREAM_BUFFER_SIZE 11
244
//#define CS_STREAM_BUFFER_SIZE (64 * 1024)
246
#define CS_STREAM_BUFFER_SIZE (64 * 1024)
249
class CSBufferedInputStream : public CSInputStream {
251
CSBufferedInputStream(): iStream(NULL), iBuffTotal(0), iBuffPos(0) { }
252
virtual ~CSBufferedInputStream();
254
virtual void close();
256
virtual size_t read(char *b, size_t len);
262
virtual void reset();
264
static CSBufferedInputStream *newStream(CSInputStream* i);
267
CSInputStream* iStream;
268
u_char iBuffer[CS_STREAM_BUFFER_SIZE];
273
class CSBufferedOutputStream : public CSOutputStream {
275
CSBufferedOutputStream(): iStream(NULL), iBuffTotal(0) { }
276
virtual ~CSBufferedOutputStream();
278
virtual void close();
280
virtual void write(const char *b, size_t len);
282
virtual const char *getEOL() { return "\n"; };
284
virtual void flush();
286
virtual void write(char b);
288
virtual void reset();
290
static CSBufferedOutputStream *newStream(CSOutputStream* i);
293
CSOutputStream* iStream;
294
u_char iBuffer[CS_STREAM_BUFFER_SIZE];
299
class CSMemoryInputStream : public CSInputStream {
301
CSMemoryInputStream(): iMemory(NULL), iMemTotal(0), iMemPos(0) { }
302
~CSMemoryInputStream(){}
304
virtual void close() {}
306
virtual size_t read(char *b, size_t len)
308
if (len > (iMemTotal - iMemPos))
309
len = iMemTotal - iMemPos;
311
memcpy(b, iMemory + iMemPos, len);
319
if (iMemPos < iMemTotal)
320
b = iMemory[iMemPos++];
327
if (iMemPos < iMemTotal)
328
b = iMemory[iMemPos];
332
virtual void reset() {iMemPos = 0;}
334
static CSMemoryInputStream *newStream(const u_char* buffer, uint32_t length);
337
const u_char *iMemory;
343
class CSMemoryOutputStream : public CSOutputStream {
345
CSMemoryOutputStream(): iMemory(NULL), iMemTotal(0), iMemSpace(0), iMemMin(0), iMemPos(NULL){ }
346
virtual ~CSMemoryOutputStream();
348
virtual void close() {}
350
virtual void write(const char *b, size_t len);
351
virtual const char *getEOL() { return "\n"; };
353
virtual void flush() {}
355
virtual void write(char b);
357
const u_char *getMemory(size_t *len)
359
*len = iMemPos - iMemory;
363
virtual void reset();
365
static CSMemoryOutputStream *newStream(size_t init_length, size_t min_alloc);
375
class CSStaticMemoryOutputStream : public CSOutputStream {
377
CSStaticMemoryOutputStream(u_char *mem, off64_t size): iMemory(mem), iMemSpace(size), iMemSize(size), iMemPos(mem){ }
378
virtual ~CSStaticMemoryOutputStream() {}
380
virtual void close() {}
382
virtual void write(const char *b, size_t len);
383
virtual const char *getEOL() { return "\n"; };
385
virtual void flush() {}
387
virtual void write(char b);
392
iMemSpace = iMemSize;
395
off64_t getSize() { return iMemPos - iMemory; }
404
typedef size_t (* CSStreamReadCallbackFunc) (void *caller_data, char *buffer, size_t buffer_size, u_char reset);
406
class CSCallbackInputStream : public CSInputStream {
408
CSCallbackInputStream(): callback(NULL), cb_data(NULL), havePeek(false), doReset(false) { }
409
~CSCallbackInputStream(){}
411
virtual void close() {}
413
virtual size_t read(char *b, size_t len)
422
size = callback(cb_data, b, len, doReset);
427
size = callback(cb_data, b, len, doReset);
443
if (!callback(cb_data, &c, 1, doReset))
455
if (callback(cb_data, &peek_char, 1, doReset))
469
static CSCallbackInputStream *newStream(CSStreamReadCallbackFunc callback, void *user_data);
472
CSStreamReadCallbackFunc callback;