void BufferedFile::refill_buffer() { if (at_eof()) { return; } int bytes_to_read = buffer_size(); int max = file_size() - file_pos(); if (bytes_to_read > max) { bytes_to_read = max; } int count_read; { // The file_pointer() may be shared with a FileDecoder // object. This may cause BufferedFile::file_pos() to become out // of date. A call to OsFile_seek() makes everything consistent again. AllocationDisabler raw_pointers_used_in_this_block; Buffer::Raw fb = data_buffer(); OsFile_seek(file_pointer(), file_pos(), SEEK_SET); count_read = OsFile_read(file_pointer(), fb().base_address(), 1, bytes_to_read); } set_count(count_read); set_file_pos(long(file_pos() + count_read)); set_index(0); if (count_read <= 0) { set_at_eof(true); } return; }
void BufferedFile::init() { set_at_eof(false); set_count(0); set_index(0); set_file_pos(0); }
int BufferedFile::seek(jint offset, int origin) { int ndx = index(); int cnt = count(); switch (origin) { case SEEK_CUR: // we may have data in the buffer so seek relative to current index into // buffer if (cnt > 0) { // we have some data, so seek from current index if (offset + ndx >= 0 && offset + ndx < cnt) { // we are seeking somewhere in the buffer, just set the ndx and return set_index((int) (ndx + offset)); return 0; } else { // we are seeking out of buffer, do a SEEK_CUR to the right position offset -= (cnt - ndx); set_file_pos((long)(file_pos() + offset)); } } break; case SEEK_SET: if (cnt > 0) { if (offset >= (file_pos() - cnt) && offset < file_pos()) { // we're seeking in the buffer so just adjust pointer set_index((int) (cnt - (file_pos() - offset))); return 0; } } set_file_pos(offset); break; case SEEK_END: if (cnt > 0) { if (cnt == file_size()) { // The whole thing is in the buffer so just adjust the index if ((cnt + offset ) >= 0) { set_index((int) (cnt + offset)); return 0; } } // There's more data beyond what we have in the buffer so just seek there } set_file_pos(long(file_size() + offset)); break; } set_count(0); // flush buffer set_index(0); if (file_pos() < file_size()) { set_at_eof(false); } return (int) OsFile_seek(file_pointer(), offset, origin); }
//---------------------------------------------------------------------------------------- PRInt32 nsInputStream::read(void* s, PRInt32 n) //---------------------------------------------------------------------------------------- { if (!mInputStream) return 0; PRInt32 result = 0; PRInt32 status = mInputStream->Read((char*)s, n, (PRUint32*)&result); if (result == 0) set_at_eof(PR_TRUE); if (status < 0) return (status); return result; } // nsInputStream::read