2)、文件缓冲区中现有数据的长度,它的大小由apr_file_t结构内的dataRead指示。通常情况下,dataRead的大小与filePtr指向的位置偏移相等。
3)、最终用户在文件缓冲区中读取的数据的当前指针,由apr_file_t结构内的bufpos指针指示。
4)、用户缓冲区的长度,其最新读写位置由pos指示。
基于缓冲区,整个读取操作的流程发生了根本的变化。任何读取首先尝试从文件缓冲区中读取,如果请求读取的长度在文件缓冲区的长度范围之内,那么直接返回数据。如果需要读取的内容超出了文件缓冲区的范围,那么我们还必须再去实际的磁盘文件中去读取,并返回,同时更新缓存区中的数据。
if (thefile->buffered) {
char *pos = (char *)buf;
apr_uint64_t blocksize;
apr_uint64_t size = *nbytes;
#if APR_HAS_THREADS
if (thefile->thlock) {
apr_thread_mutex_lock(thefile->thlock);
}
#endif
如果支持多线程的操作,那么在对文件进行操作之前必须互斥量锁定,确保操作的安全性。同样在读取结束后还必须是unlock该互斥变量。
if (thefile->direction == 1) {
apr_file_flush(thefile);
thefile->bufpos = 0;
thefile->direction = 0;
thefile->dataRead = 0;
}
rv = 0;
if (thefile->ungetchar != -1) {
*pos = (char)thefile->ungetchar;
++pos;
--size;
thefile->ungetchar = -1;
}
while (rv == 0 && size > 0) {
if (thefile->bufpos >= thefile->dataRead) {
int bytesread = read(thefile->filedes, thefile->buffer, APR_FILE_BUFSIZE);
if (bytesread == 0) {
thefile->eof_hit = TRUE;