#include <st.h>
Public Member Functions | |
st_reader (st_netfd_t fd, char *buf, size_t bufsize) | |
size_t | unread () |
The size of the unconsumed range of bytes. | |
size_t | rem () |
The remaining number of bytes in the buffer. | |
sized_array< char > & | buf () |
The entire read buffer. | |
void | reset_range (char *start, char *end) |
Manually update/adjust the pointers; useful after changing buf_. | |
void | skip (size_t req, st_utime_t to=ST_UTIME_NO_TIMEOUT) |
Discard the requested number of bytes. | |
managed_array< char > | read (size_t req, st_utime_t to=ST_UTIME_NO_TIMEOUT) |
Returns a char array that contains the requested number of bytes. | |
template<typename T> | |
T | read (st_utime_t to=ST_UTIME_NO_TIMEOUT) |
Definition at line 382 of file st.h.
managed_array<char> commons::st_reader::read | ( | size_t | req, | |
st_utime_t | to = ST_UTIME_NO_TIMEOUT | |||
) | [inline] |
Returns a char array that contains the requested number of bytes.
If we hit an error or EOF, then an exception is thrown.
Definition at line 445 of file st.h.
References commons::sized_array< T >::end(), commons::sized_array< T >::get(), commons::managed_array< T >::get(), rem(), commons::sized_array< T >::size(), and unread().
00445 { 00446 // Do we already have the requested data? 00447 if (unread() >= req) { 00448 managed_array<char> p(start_, false); 00449 start_ += req; 00450 return p; 00451 } 00452 00453 // Handle large arrays specially. 00454 if (req > buf_.size()) { 00455 managed_array<char> p(new char[req], true); 00456 memcpy(p.get(), start_, unread()); 00457 checkeqnneg(st_read_fully(fd_, p + unread(), req - unread(), to), static_cast<ssize_t>(req - unread())); 00458 start_ = end_ = buf_.get(); 00459 return p; 00460 } 00461 00462 // Shift things down if necessary. 00463 if (req > static_cast<size_t>(buf_.end() - end_)) { 00464 memmove(buf_.get(), start_, unread()); 00465 size_t diff = start_ - buf_.get(); 00466 start_ -= diff; 00467 end_ -= diff; 00468 } 00469 00470 // Keep reading until we have enough. 00471 while (unread() < req) { 00472 ssize_t res = checknnegerr(st_read(fd_, end_, rem(), to)); 00473 if (res == 0) break; 00474 else end_ += res; 00475 } 00476 00477 // If we got a premature EOF. 00478 if (unread() < req) 00479 throw eof_exception(); 00480 00481 managed_array<char> p(start_, false); 00482 start_ += req; 00483 return p; 00484 }