Skip to content

Instantly share code, notes, and snippets.

@petrmanek
Created September 3, 2016 21:57
Show Gist options
  • Save petrmanek/2f4196a6dd5f1c0187cbeb0a0529fbc2 to your computer and use it in GitHub Desktop.
Save petrmanek/2f4196a6dd5f1c0187cbeb0a0529fbc2 to your computer and use it in GitHub Desktop.
XRootD to std::istream Adapter
//
// Created by Petr Mánek on 03.09.16.
//
#include "XRdInputStreamBuf.h"
using namespace std;
using namespace XrdCl;
XRdInputStreamBuf::XRdInputStreamBuf(string url) {
buffer_ = new char[bufferSize_];
offset_ = 0;
file_ = new File();
XRootDStatus result = file_->Open(url, OpenFlags::Read);
if (!result.IsOK()) {
LOG(ERROR) << "XRootD open operation failed: " << result.ToString();
}
}
XRdInputStreamBuf::~XRdInputStreamBuf() {
XRootDStatus result = file_->Close();
if (!result.IsOK()) {
LOG(ERROR) << "XRootD close operation failed: " << result.ToString();
}
delete file_;
delete[] buffer_;
}
int XRdInputStreamBuf::underflow() {
if (this->gptr() == this->egptr()) {
// Not enough data buffered. Retrieve some more.
uint32_t bytesRead = 0;
XRootDStatus result = file_->Read(offset_, bufferSize_, (void *) buffer_, bytesRead);
if (!result.IsOK()) {
LOG(ERROR) << "XRootD read operation failed: " << result.ToString();
}
// Update underlying data structures.
offset_ += bytesRead;
this->setg(this->buffer_, this->buffer_, this->buffer_ + bytesRead);
}
// Read from underlying data structures.
return this->gptr() == this->egptr()
? std::char_traits<char>::eof()
: std::char_traits<char>::to_int_type(*this->gptr());
}
//
// Created by Petr Mánek on 03.09.16.
//
#ifndef CLSTRD_XRDINPUTSTREAMBUF_H
#define CLSTRD_XRDINPUTSTREAMBUF_H
#include <streambuf>
#include <glog/logging.h>
#include <XrdCl/XrdClFile.hh>
class XRdInputStreamBuf : public std::streambuf {
public:
XRdInputStreamBuf(std::string url);
virtual ~XRdInputStreamBuf();
protected:
virtual int underflow() override;
private:
XrdCl::File *file_;
const size_t bufferSize_ = 1024;
char *buffer_;
uint64_t offset_;
};
#endif //CLSTRD_XRDINPUTSTREAMBUF_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment