Home | History | Annotate | Download | only in images
      1 #include "SkStream.h"
      2 #include <unistd.h>
      3 
      4 //#define TRACE_FDSTREAM
      5 
      6 SkFDStream::SkFDStream(int fileDesc, bool closeWhenDone)
      7     : fFD(fileDesc), fCloseWhenDone(closeWhenDone) {
      8 }
      9 
     10 SkFDStream::~SkFDStream() {
     11     if (fFD >= 0 && fCloseWhenDone) {
     12         ::close(fFD);
     13     }
     14 }
     15 
     16 bool SkFDStream::rewind() {
     17     if (fFD >= 0) {
     18         off_t value = ::lseek(fFD, 0, SEEK_SET);
     19 #ifdef TRACE_FDSTREAM
     20         if (value) {
     21             SkDebugf("xxxxxxxxxxxxxx rewind failed %d\n", value);
     22         }
     23 #endif
     24         return value == 0;
     25     }
     26     return false;
     27 }
     28 
     29 size_t SkFDStream::read(void* buffer, size_t size) {
     30     if (fFD >= 0) {
     31         if (buffer == NULL && size == 0) {  // request total size
     32             off_t curr = ::lseek(fFD, 0, SEEK_CUR);
     33             if (curr < 0) {
     34 #ifdef TRACE_FDSTREAM
     35                 SkDebugf("xxxxxxxxxxxxx lseek failed 0 CURR\n");
     36 #endif
     37                 return 0;   // error
     38             }
     39             off_t size = ::lseek(fFD, 0, SEEK_END);
     40             if (size < 0) {
     41 #ifdef TRACE_FDSTREAM
     42                 SkDebugf("xxxxxxxxxxxxx lseek failed 0 END\n");
     43 #endif
     44                 size = 0;   // error
     45             }
     46             if (::lseek(fFD, curr, SEEK_SET) != curr) {
     47                 // can't restore, error
     48 #ifdef TRACE_FDSTREAM
     49                 SkDebugf("xxxxxxxxxxxxx lseek failed %d SET\n", curr);
     50 #endif
     51                 return 0;
     52             }
     53             return size;
     54         } else if (NULL == buffer) {        // skip
     55             off_t oldCurr = ::lseek(fFD, 0, SEEK_CUR);
     56             if (oldCurr < 0) {
     57 #ifdef TRACE_FDSTREAM
     58                 SkDebugf("xxxxxxxxxxxxx lseek1 failed %d CUR\n", oldCurr);
     59 #endif
     60                 return 0;   // error;
     61             }
     62             off_t newCurr = ::lseek(fFD, size, SEEK_CUR);
     63             if (newCurr < 0) {
     64 #ifdef TRACE_FDSTREAM
     65                 SkDebugf("xxxxxxxxxxxxx lseek2 failed %d CUR\n", newCurr);
     66 #endif
     67                 return 0;   // error;
     68             }
     69             // return the actual amount we skipped
     70             return newCurr - oldCurr;
     71         } else {                            // read
     72             ssize_t actual = ::read(fFD, buffer, size);
     73             // our API can't return an error, so we return 0
     74             if (actual < 0) {
     75 #ifdef TRACE_FDSTREAM
     76                 SkDebugf("xxxxxxxxxxxxx read failed %d actual %d\n", size, actual);
     77 #endif
     78                 actual = 0;
     79             }
     80             return actual;
     81         }
     82     }
     83     return 0;
     84 }
     85 
     86