I think here should be 
```
std::string contents((const char*)data.getBytes(), data.getSize());
```
Since the data is not \0 terminal string.