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.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Copyright (C) 2002-2003 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, int length) {
  35. check(length);
  36. memcpy(ptr, data, length);
  37. ptr += length;
  38. }
  39. int length() { return ptr - start; }
  40. void clear() { ptr = start; };
  41. void reposition(int pos) { ptr = start + pos; }
  42. // data() returns a pointer to the buffer.
  43. const void* data() { return (const void*)start; }
  44. private:
  45. // overrun() either doubles the buffer or adds enough space for nItems of
  46. // size itemSize bytes.
  47. int overrun(int itemSize, int nItems) {
  48. int len = ptr - start + itemSize * nItems;
  49. if (len < (end - start) * 2)
  50. len = (end - start) * 2;
  51. U8* newStart = new U8[len];
  52. memcpy(newStart, start, ptr - start);
  53. ptr = newStart + (ptr - start);
  54. delete [] start;
  55. start = newStart;
  56. end = newStart + len;
  57. return nItems;
  58. }
  59. U8* start;
  60. };
  61. }
  62. #endif