//noinspection unchecked
listeners.dispatch().indexingStarting( attachment, starting_line == 0 );
InputStream root_stream = null;
PositionTrackingInputStream in = null;
try {
root_stream = openStreamForFile( file );
in = new PositionTrackingInputStream(
new BufferedInputStream( root_stream ) );
int line = 0;
// If we're skipping some data, do it now
if ( starting_position > 0 ) {
long actual = in.skip( starting_position );
// If we weren't able to skip the desired number of bytes, we'll need
// to do a full index because we don't know what line we're at.
if ( actual != starting_position ) {
// If mark is support, we can just reset to the beginning and
// go on
if ( in.markSupported() ) {
in.reset(); // pop to beginning (no mark set)
// Indicate that we're starting a full index
//noinspection unchecked
listeners.dispatch().indexingStarting( attachment, true );
}
// If it's not supported, we'll need to close the files are run
// a new instance.
else {
// Close out the files
// IOKit.close( line_reader );
IOKit.close( in );
// Run new instance
Indexer sub = new Indexer( 0, 0 );
sub.run();
return;
}
}
else line = starting_line;
}
// At this point, ready to do the work, so grab a lock...
row_index_map_lock.lock();
try {
// If this is a full index, clear the existing map
if ( line == 0 ) {
row_index_map.clear();
row_skip_mod = 1;
}
// This is not very efficient, but buffering messes up the position
int bite;
while( ( bite = in.read() ) != -1 ) {
// If it's not a newline, ignore.
// WARNING: this doesn't handle different line endings well
if ( bite != '\n' ) continue;
// NOTE: increment line right away since we're now at the end
// of the preceding line.
line++;
if ( line % row_skip_mod == 0 ) {
row_index_map.put( line, in.position() );
// printRowIndexMap( "after add" );
if ( row_index_map.size() > max_index_size ) {
row_skip_mod = increaseRowSkipMod( row_skip_mod, line );
printRowIndexMap( "after grow" );
}
}
}
}
finally {
row_index_map_lock.unlock();
}
num_lines = line;
last_index_size = in.position();
// System.out.println( " Found " + num_lines + " lines");
// System.out.println( " Map size: " + row_index_map.size() );
// System.out.println( "Map: " );
// for( int i = 0; i <= num_lines; i++ ) {