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.

MemInStream.h 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. // rdr::MemInStream is an InStream which streams from a given memory buffer.
  20. // If the deleteWhenDone parameter is true then the buffer will be delete[]d in
  21. // the destructor. Note that it is delete[]d as a U8* - strictly speaking this
  22. // means it ought to be new[]ed as a U8* as well, but on most platforms this
  23. // doesn't matter.
  24. //
  25. #ifndef __RDR_MEMINSTREAM_H__
  26. #define __RDR_MEMINSTREAM_H__
  27. #include <rdr/InStream.h>
  28. #include <rdr/Exception.h>
  29. namespace rdr {
  30. class MemInStream : public InStream {
  31. public:
  32. MemInStream(const void* data, size_t len, bool deleteWhenDone_=false)
  33. : start((const U8*)data), deleteWhenDone(deleteWhenDone_)
  34. {
  35. ptr = start;
  36. end = start + len;
  37. #ifdef RFB_INSTREAM_CHECK
  38. // MemInStream cannot add more data, so callers are assumed to already
  39. // new the total size
  40. avail();
  41. #endif
  42. }
  43. virtual ~MemInStream() {
  44. if (deleteWhenDone)
  45. delete [] start;
  46. }
  47. size_t pos() { return ptr - start; }
  48. void reposition(size_t pos) { ptr = start + pos; }
  49. private:
  50. bool overrun(size_t needed) { throw EndOfStream(); }
  51. const U8* start;
  52. bool deleteWhenDone;
  53. };
  54. }
  55. #endif