Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // |
| 20 | // A MemOutStream grows as needed when data is written to it. |
| 21 | // |
| 22 | |
| 23 | #ifndef __RDR_MEMOUTSTREAM_H__ |
| 24 | #define __RDR_MEMOUTSTREAM_H__ |
| 25 | |
| 26 | #include <rdr/OutStream.h> |
| 27 | |
| 28 | namespace rdr { |
| 29 | |
| 30 | class MemOutStream : public OutStream { |
| 31 | |
| 32 | public: |
| 33 | |
| 34 | MemOutStream(int len=1024) { |
| 35 | start = ptr = new U8[len]; |
| 36 | end = start + len; |
| 37 | } |
| 38 | |
| 39 | virtual ~MemOutStream() { |
| 40 | delete [] start; |
| 41 | } |
| 42 | |
| 43 | void writeBytes(const void* data, int length) { |
| 44 | check(length); |
| 45 | memcpy(ptr, data, length); |
| 46 | ptr += length; |
| 47 | } |
| 48 | |
| 49 | int length() { return ptr - start; } |
| 50 | void clear() { ptr = start; }; |
| 51 | void clearAndZero() { memset(start, 0, ptr-start); clear(); } |
| 52 | void reposition(int pos) { ptr = start + pos; } |
| 53 | |
| 54 | // data() returns a pointer to the buffer. |
| 55 | |
| 56 | const void* data() { return (const void*)start; } |
| 57 | |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 58 | protected: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 59 | |
| 60 | // overrun() either doubles the buffer or adds enough space for nItems of |
| 61 | // size itemSize bytes. |
| 62 | |
| 63 | int overrun(int itemSize, int nItems) { |
| 64 | int len = ptr - start + itemSize * nItems; |
| 65 | if (len < (end - start) * 2) |
| 66 | len = (end - start) * 2; |
| 67 | |
| 68 | U8* newStart = new U8[len]; |
| 69 | memcpy(newStart, start, ptr - start); |
| 70 | ptr = newStart + (ptr - start); |
| 71 | delete [] start; |
| 72 | start = newStart; |
| 73 | end = newStart + len; |
| 74 | |
| 75 | return nItems; |
| 76 | } |
| 77 | |
| 78 | U8* start; |
| 79 | }; |
| 80 | |
| 81 | } |
| 82 | |
| 83 | #endif |