You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MemOutStream.h 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // A MemOutStream grows as needed when data is written to it.
  20. //
  21. #ifndef __RDR_MEMOUTSTREAM_H__
  22. #define __RDR_MEMOUTSTREAM_H__
  23. #include <rdr/OutStream.h>
  24. namespace rdr {
  25. class MemOutStream : public OutStream {
  26. public:
  27. MemOutStream(int len=1024) {
  28. start = ptr = new U8[len];
  29. end = start + len;
  30. }
  31. virtual ~MemOutStream() {
  32. delete [] start;
  33. }
  34. void writeBytes(const void* data, size_t length) {
  35. check(length);
  36. memcpy(ptr, data, length);
  37. ptr += length;
  38. }
  39. size_t length() { return ptr - start; }
  40. void clear() { ptr = start; };
  41. void clearAndZero() { memset(start, 0, ptr-start); clear(); }
  42. void reposition(size_t pos) { ptr = start + pos; }
  43. // data() returns a pointer to the buffer.
  44. const void* data() { return (const void*)start; }
  45. protected:
  46. // overrun() either doubles the buffer or adds enough space for nItems of
  47. // size itemSize bytes.
  48. size_t overrun(size_t itemSize, size_t nItems) {
  49. size_t len = ptr - start + itemSize * nItems;
  50. if (len < (size_t)(end - start) * 2)
  51. len = (end - start) * 2;
  52. U8* newStart = new U8[len];
  53. memcpy(newStart, start, ptr - start);
  54. ptr = newStart + (ptr - start);
  55. delete [] start;
  56. start = newStart;
  57. end = newStart + len;
  58. return nItems;
  59. }
  60. U8* start;
  61. };
  62. }
  63. #endif