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; }
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); }
int FileDecoder::get_bytes_raw(address dest_address, int count) { OsFile_Handle handle = file_handle(); GUARANTEE(handle != NULL, "What are we reading from?"); int pos = file_pos(); OsFile_seek(handle, pos, SEEK_SET); int bytes_read = OsFile_read(handle, dest_address, 1, count); set_file_pos(pos + bytes_read); return bytes_read; }
inline jint last_unread_file_pos() { return file_pos() - (count() - index()); }
static int modfile_ftell( INSTANCE * my, int * params ) { return file_pos(( file * )params[0] ) ; }