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.
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;
77
/* Return the name of the file, or whatever: */
78
virtual const char *identify() = 0;
81
* Read a line from the input stream. This function
82
* handles all types of line endings. The function
85
CSStringBuffer *readLine();
88
class CSOutputStream : public CSRefObject {
91
* Closes this input stream and releases any system
92
* resources associated with the stream.
94
virtual void close() = 0;
97
* Writes len bytes from the specified byte array starting at
98
* offset off to this output stream.
100
virtual void write(const char *b, size_t len) = 0;
103
* Returns the default EOL indicator.
104
* Will be \n, \r or \r\n.
106
virtual const char *getEOL() = 0;
109
* Flushes this output stream and forces any buffered output
110
* bytes to be written out.
112
virtual void flush() = 0;
115
* Writes the specified byte to this output stream.
117
virtual void write(char b) = 0;
120
* Reset this output stream to the start inorder to restart the write.
122
virtual void reset() = 0;
124
virtual const char *identify() = 0;
127
* Write a line. Terminator is specific to the
128
* type of output stream and may depend on the
131
void printLine(const char *cstr);
134
* Various write types:
136
virtual void print(const char *cstr);
137
virtual void print(CSString *s);
138
virtual void print(int value);
139
virtual void print(uint64_t value);
142
class CSStream : public CSObject {
144
static void pipe(CSOutputStream *out, CSInputStream *in);
149
class CSFileInputStream : public CSInputStream {
151
CSFileInputStream(): iFile(NULL), iReadOffset(0) { }
152
virtual ~CSFileInputStream();
154
virtual void close();
156
virtual size_t read(char *b, size_t len);
163
* Reset this output stream to the start inorder to restart the read.
165
virtual void reset();
167
virtual const char *identify();
169
static CSFileInputStream *newStream(CSFile* f);
170
static CSFileInputStream *newStream(CSFile* f, off64_t offset);
177
class CSFileOutputStream : public CSOutputStream {
179
CSFileOutputStream(): iFile(NULL), iWriteOffset(0) { }
180
virtual ~CSFileOutputStream();
182
virtual void close();
184
virtual void write(const char *b, size_t len);
186
virtual const char *getEOL();
188
virtual void flush();
190
virtual void write(char b);
192
virtual void reset();
194
virtual const char *identify();
196
static CSFileOutputStream *newStream(CSFile* f);
197
static CSFileOutputStream *newStream(CSFile* f, off64_t offset);
201
off64_t iWriteOffset;
206
class CSSocketInputStream : public CSInputStream {
208
CSSocketInputStream(): iSocket(NULL) { }
209
virtual ~CSSocketInputStream();
211
virtual void close();
213
virtual size_t read(char *b, size_t len);
219
virtual void reset();
221
virtual const char *identify();
223
static CSSocketInputStream *newStream(CSSocket *s);
229
class CSSocketOutputStream : public CSOutputStream {
231
CSSocketOutputStream(): iSocket(NULL) { }
232
virtual ~CSSocketOutputStream();
234
virtual void close();
236
virtual void write(const char *b, size_t len);
238
virtual const char *getEOL() { return "\n"; };
240
virtual void flush();
242
virtual void write(char b);
244
virtual void reset();
246
virtual const char *identify();
248
static CSSocketOutputStream *newStream(CSSocket *s);
254
/* Buffered Stream: */
255
#ifdef DEBUG_disabled
256
#define CS_STREAM_BUFFER_SIZE 80
258
#define CS_STREAM_BUFFER_SIZE (32 * 1024)
261
class CSBufferedInputStream : public CSInputStream {
263
CSBufferedInputStream(): iStream(NULL), iBuffTotal(0), iBuffPos(0) { }
264
virtual ~CSBufferedInputStream();
266
virtual void close();
268
virtual size_t read(char *b, size_t len);
274
virtual void reset();
276
virtual const char *identify();
278
static CSBufferedInputStream *newStream(CSInputStream* i);
281
CSInputStream* iStream;
282
u_char iBuffer[CS_STREAM_BUFFER_SIZE];
287
class CSBufferedOutputStream : public CSOutputStream {
289
CSBufferedOutputStream(): iStream(NULL), iBuffTotal(0) { }
290
virtual ~CSBufferedOutputStream();
292
virtual void close();
294
virtual void write(const char *b, size_t len);
296
virtual const char *getEOL() { return "\n"; };
298
virtual void flush();
300
virtual void write(char b);
302
virtual void reset();
304
virtual const char *identify();
306
static CSBufferedOutputStream *newStream(CSOutputStream* i);
309
CSOutputStream* iStream;
310
u_char iBuffer[CS_STREAM_BUFFER_SIZE];
315
class CSMemoryInputStream : public CSInputStream {
317
CSMemoryInputStream(): iMemory(NULL), iMemTotal(0), iMemPos(0) { }
318
~CSMemoryInputStream(){}
320
virtual void close() {}
322
virtual size_t read(char *b, size_t len)
324
if (len > (iMemTotal - iMemPos))
325
len = iMemTotal - iMemPos;
327
memcpy(b, iMemory + iMemPos, len);
335
if (iMemPos < iMemTotal)
336
b = iMemory[iMemPos++];
343
if (iMemPos < iMemTotal)
344
b = iMemory[iMemPos];
348
virtual void reset() {iMemPos = 0;}
350
virtual const char *identify()
352
return "memory stream";
355
static CSMemoryInputStream *newStream(const u_char* buffer, uint32_t length);
358
const u_char *iMemory;
364
class CSMemoryOutputStream : public CSOutputStream {
366
CSMemoryOutputStream(): iMemory(NULL), iMemTotal(0), iMemSpace(0), iMemMin(0), iMemPos(NULL){ }
367
virtual ~CSMemoryOutputStream();
369
virtual void close() {}
371
virtual void write(const char *b, size_t len);
372
virtual const char *getEOL() { return "\n"; };
374
virtual void flush() {}
376
virtual void write(char b);
378
const u_char *getMemory(size_t *len)
380
*len = iMemPos - iMemory;
384
virtual void reset();
386
virtual const char *identify();
388
static CSMemoryOutputStream *newStream(size_t init_length, size_t min_alloc);
398
class CSStaticMemoryOutputStream : public CSOutputStream {
400
CSStaticMemoryOutputStream(u_char *mem, off64_t size): iMemory(mem), iMemSpace(size), iMemSize(size), iMemPos(mem){ }
401
virtual ~CSStaticMemoryOutputStream() {}
403
virtual void close() {}
405
virtual void write(const char *b, size_t len);
406
virtual const char *getEOL() { return "\n"; };
408
virtual void flush() {}
410
virtual void write(char b);
415
iMemSpace = iMemSize;
418
virtual const char *identify()
420
return "memory stream";
423
off64_t getSize() { return iMemPos - iMemory; }
432
typedef size_t (* CSStreamReadCallbackFunc) (void *caller_data, char *buffer, size_t buffer_size, u_char reset);
434
class CSCallbackInputStream : public CSInputStream {
436
CSCallbackInputStream(): callback(NULL), cb_data(NULL), havePeek(false), doReset(false) { }
437
~CSCallbackInputStream(){}
439
virtual void close() {}
441
virtual size_t read(char *b, size_t len)
450
size = callback(cb_data, b, len, doReset);
455
size = callback(cb_data, b, len, doReset);
471
if (!callback(cb_data, &c, 1, doReset))
483
if (callback(cb_data, &peek_char, 1, doReset))
497
virtual const char *identify()
499
return "callback stream";
502
static CSCallbackInputStream *newStream(CSStreamReadCallbackFunc callback, void *user_data);
505
CSStreamReadCallbackFunc callback;