jstream.ipp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef JSTREAM_IPP
  14. #define JSTREAM_IPP
  15. #include "jsocket.hpp"
  16. #include "jstream.hpp"
  17. #include "jstring.hpp"
  18. #ifdef _WIN32
  19. #include <winsock2.h>
  20. #endif
  21. // Use this for input streams where you can only see a byte at a time
  22. class jlib_decl CByteInputStream : public IByteInputStream
  23. {
  24. private:
  25. bool pushedback;
  26. char ungot;
  27. protected:
  28. bool eofseen;
  29. public:
  30. CByteInputStream();
  31. virtual bool eof();
  32. virtual int readByte();
  33. virtual int readBytes(void *buf, int size);
  34. virtual void unget(int c);
  35. virtual int readNext() = 0;
  36. };
  37. class jlib_decl CStringBufferInputStream : public CInterface, public CByteInputStream
  38. {
  39. private:
  40. size32_t pos;
  41. StringBuffer str;
  42. public:
  43. IMPLEMENT_IINTERFACE;
  44. CStringBufferInputStream(const char *);
  45. virtual int readNext();
  46. };
  47. class jlib_decl CUserStringBufferInputStream : public CInterface, public CByteInputStream
  48. {
  49. private:
  50. size32_t pos;
  51. StringBuffer &str;
  52. public:
  53. IMPLEMENT_IINTERFACE;
  54. CUserStringBufferInputStream(StringBuffer &);
  55. virtual int readNext();
  56. };
  57. class jlib_decl CFileInputStream : public CInterface, public CByteInputStream
  58. {
  59. private:
  60. unsigned char buffer[1024];
  61. int handle;
  62. int remaining;
  63. int next;
  64. void refill();
  65. public:
  66. IMPLEMENT_IINTERFACE;
  67. CFileInputStream(int handle);
  68. virtual int readNext();
  69. };
  70. class jlib_decl CSocketInputStream : public CInterface, public CByteInputStream
  71. {
  72. private:
  73. unsigned char buffer[1024];
  74. ISocket *sock;
  75. size32_t remaining;
  76. int next;
  77. void refill();
  78. public:
  79. IMPLEMENT_IINTERFACE;
  80. CSocketInputStream(ISocket *s);
  81. ~CSocketInputStream();
  82. virtual int readNext();
  83. };
  84. class jlib_decl CSocketOutputStream : public CInterface, public IByteOutputStream
  85. {
  86. private:
  87. ISocket *sock;
  88. public:
  89. IMPLEMENT_IINTERFACE;
  90. CSocketOutputStream(ISocket *s);
  91. ~CSocketOutputStream();
  92. virtual void writeByte(byte b);
  93. virtual void writeBytes(const void *, int);
  94. virtual void writeString(const char *str) { writeBytes(str, strlen(str)); }
  95. };
  96. class jlib_decl CFileOutputStream : public CInterface, public IByteOutputStream
  97. {
  98. private:
  99. int handle;
  100. public:
  101. IMPLEMENT_IINTERFACE;
  102. CFileOutputStream(int _handle);
  103. virtual void writeByte(byte b);
  104. virtual void writeBytes(const void *, int);
  105. virtual void writeString(const char *str) { writeBytes(str, strlen(str)); }
  106. };
  107. class jlib_decl CStringBufferOutputStream : public CInterface, public IByteOutputStream
  108. {
  109. private:
  110. StringBuffer & out;
  111. public:
  112. IMPLEMENT_IINTERFACE;
  113. CStringBufferOutputStream(StringBuffer & _out) : out(_out) {}
  114. virtual void writeByte(byte b);
  115. virtual void writeBytes(const void *, int);
  116. virtual void writeString(const char *str) { writeBytes(str, strlen(str)); }
  117. };
  118. #endif