[adding TStringBuffer for serialization/deserialization from a string]
Summary:
/**
 * A string buffer is a tranpsort that simply reads from and writes to a
 * string. Anytime you call write on it, the data is serialized
 * into the underlying buffer, you can call getString() to get the serialized
 * string.  Before you call read, you should call resetString(data) to set the
 * underlying buffer, you can then call read to get the
 * de-serialized data structure.
 *
 * The string buffer is inherited from the memory buffer
 * Thus, buffers are allocated using C constructs malloc,realloc, and the size
 * doubles as necessary.
 */

Reviewed by: aditya

Test Plan:
int main(int argc, char** argv) {
  shared_ptr<TStringBuffer> strBuffer(new TStringBuffer());
  shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(strBuffer));

  testStruct a;
  a.i1 = 10;
  a.i2 = 30;
  a.s1 = string("holla back a");

  a.write(binaryProtcol.get());
  string serialized = strBuffer->getString();

  shared_ptr<TStringBuffer> strBuffer2(new TStringBuffer());
  shared_ptr<TBinaryProtocol> binaryProtcol2(new TBinaryProtocol(strBuffer2));

  strBuffer2->resetString(serialized);
  testStruct a2;
  a2.read(binaryProtcol2.get());

  if (a == a2) {
    printf("serialization working\n");
  } else {
    printf("serialization not working\n");
  }
}


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665209 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/transport/TTransportUtils.h b/lib/cpp/src/transport/TTransportUtils.h
index 3db22e7..102e848 100644
--- a/lib/cpp/src/transport/TTransportUtils.h
+++ b/lib/cpp/src/transport/TTransportUtils.h
@@ -8,6 +8,7 @@
 #define _THRIFT_TRANSPORT_TTRANSPORTUTILS_H_ 1
 
 #include <string>
+#include <sstream>
 #include <transport/TTransport.h>
 #include <transport/TFileTransport.h>
 
@@ -330,25 +331,83 @@
     return wPos_ - rPos_;
   }
 
- private:
+ protected:
   // Data buffer
   uint8_t* buffer_;
   
   // Allocated buffer size
   uint32_t bufferSize_;
 
+  // Is this object the owner of the buffer?
+  bool owner_;
+
+ private: 
   // Where the write is at
   uint32_t wPos_;
   
   // Where the reader is at
   uint32_t rPos_;
 
-  // Is this object the owner of the buffer?
-  bool owner_;
 
 };
 
 /**
+ * A string buffer is a tranpsort that simply reads from and writes to a 
+ * string. Anytime you call write on it, the data is serialized
+ * into the underlying buffer, you can call getString() to get the serialized
+ * string.  Before you call read, you should call resetString(data) to set the 
+ * underlying buffer, you can then call read to get the 
+ * de-serialized data structure. 
+ *
+ * The string buffer is inherited from the memory buffer 
+ * Thus, buffers are allocated using C constructs malloc,realloc, and the size
+ * doubles as necessary.
+ *
+ * @author Yun-Fang Juan <yunfang@facebook.com>
+ */
+
+class TStringBuffer : public TMemoryBuffer {
+ public: 
+  TStringBuffer() : TMemoryBuffer() {};
+
+  TStringBuffer(const std::string& inputString) : TMemoryBuffer() {
+    resetString(inputString);  
+  };
+
+  //destructor. basically the same as TMemoryBuffer
+  ~TStringBuffer() { 
+    if (owner_) {
+      if (buffer_ != NULL) {
+        free(buffer_);
+        buffer_ = NULL;
+      }
+    }
+  }  
+
+  //from buffer to string
+  std::string getString() {
+    std::stringstream ss;
+    ss.write((char*)buffer_, bufferSize_);
+    return ss.str();
+  };
+
+  //from string to buffer
+  void resetString(const std::string& inputString) {
+    char* data;
+    std::stringstream ss;
+    bufferSize_ = inputString.size();
+
+    ss.str(inputString);
+    data = (char*)malloc(bufferSize_);
+    ss.read(data, bufferSize_);
+
+    resetBuffer((uint8_t*)data, bufferSize_);
+    //set the owner_ to true so the buffer_ will be freed later on
+    owner_ = true; 
+  };
+};
+
+/**
  * TPipedTransport. This transport allows piping of a request from one 
  * transport to another either when readEnd() or writeEnd(). The typicAL
  * use case for this is to log a request or a reply to disk.