cpp: Implement peek() for TFileTransport
authorDavid Reiss <dreiss@apache.org>
Tue, 9 Mar 2010 05:20:21 +0000 (05:20 +0000)
committerDavid Reiss <dreiss@apache.org>
Tue, 9 Mar 2010 05:20:21 +0000 (05:20 +0000)
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@920688 13f79535-47bb-0310-9956-ffa450edef68

lib/cpp/src/transport/TFileTransport.cpp
lib/cpp/src/transport/TFileTransport.h

index 578bed7..0b41694 100644 (file)
@@ -498,6 +498,22 @@ uint32_t TFileTransport::readAll(uint8_t* buf, uint32_t len) {
   return have;
 }
 
+bool TFileTransport::peek() {
+  // check if there is an event ready to be read
+  if (!currentEvent_) {
+    currentEvent_ = readEvent();
+  }
+
+  // did not manage to read an event from the file. This could have happened
+  // if the timeout expired or there was some other error
+  if (!currentEvent_) {
+    return false;
+  }
+
+  // check if there is anything to read
+  return (currentEvent_->eventSize_ - currentEvent_->eventBuffPos_) > 0;
+}
+
 uint32_t TFileTransport::read(uint8_t* buf, uint32_t len) {
   // check if there an event is ready to be read
   if (!currentEvent_) {
index 7a6984a..30d3a9b 100644 (file)
@@ -181,6 +181,7 @@ class TFileTransport : public TFileReaderTransport,
 
   uint32_t readAll(uint8_t* buf, uint32_t len);
   uint32_t read(uint8_t* buf, uint32_t len);
+  bool peek();
 
   // log-file specific functions
   void seekToChunk(int32_t chunk);