| 160 | } |
| 161 | |
| 162 | int64 File::readToString(std::string* const output, uint64 max_length) { |
| 163 | CHECK_NOTNULL(output); |
| 164 | output->clear(); |
| 165 | |
| 166 | if (max_length == 0) return 0; |
| 167 | // if (max_length < 0) return -1; |
| 168 | |
| 169 | int64 needed = max_length; |
| 170 | int bufsize = (needed < (2 << 20) ? needed : (2 << 20)); |
| 171 | |
| 172 | std::unique_ptr<char[]> buf(new char[bufsize]); |
| 173 | |
| 174 | int64 nread = 0; |
| 175 | while (needed > 0) { |
| 176 | nread = read(buf.get(), (bufsize < needed ? bufsize : needed)); |
| 177 | if (nread > 0) { |
| 178 | output->append(buf.get(), nread); |
| 179 | needed -= nread; |
| 180 | } else { |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | return (nread >= 0 ? static_cast<int64>(output->size()) : -1); |
| 185 | } |
| 186 | |
| 187 | size_t File::writeString(const std::string& line) { |
| 188 | return write(line.c_str(), line.size()); |
no test coverage detected