package fileIO; import java.io.InputStream; import java.util.Arrays; import java.util.concurrent.ArrayBlockingQueue; import shared.Timer; import shared.Tools; /** * Runs a ByteFile1 in a separate thread. Can speed up disk reading, particularly of compressed files, at cost of slightly more work done. * Drop-in compatible with ByteFile1. * @author Brian Bushnell * @date Sep 23, 2013 * */ public final class ByteFile2 extends ByteFile { public static void main(String[] args){ ByteFile2 tf=new ByteFile2(args.length>0 ? args[0] : "stdin", true); long first=0, last=100; boolean speedtest=false; if(args.length>1){ if(args[1].equalsIgnoreCase("speedtest")){ speedtest=true; first=0; last=Long.MAX_VALUE; }else{ first=Integer.parseInt(args[1]); last=first+100; } } if(args.length>2){ last=Integer.parseInt(args[2]); } speedtest(tf, first, last, !speedtest); tf.close(); tf.reset(); tf.close(); } private static void speedtest(ByteFile2 tf, long first, long last, boolean reprint){ Timer t=new Timer(); long lines=0; long bytes=0; for(long i=0; i=temp.length || temp[tempLoc]==null){ boolean b=getBuffer(); if(!b){ if(verbose2){System.err.println("nextLine()->getBuffer() returned false.");} return null; } temp=currentList; tempLoc=currentLoc; if(temp==null || temp==poison || temp[tempLoc]==null){ return null; } } //TODO: This is a race condition; currentList can be changed to null. A defensive copy could be created. //Note that I read the above warning and added "temp" and "temploc" but I'm not sure if that fixed anything. assert(temp!=null && temp!=poison); assert(tempLoc(buffs+2); // qEmpty=new ArrayBlockingQueue(buffs+2); // for(int i=0; i(buffs+2); qEmpty=new ArrayBlockingQueue(buffs+2); for(int i=0; i=bufflen || bases>=buffcapacity){ if(verbose2){System.err.println("Capacity exceeded.");} while(list!=null){ try { // synchronized(this){ // if(!shutdown){ if(verbose2){ System.err.println("A: Adding to qFull list of size "+loc); System.err.println(ByteFile2.toString(list)); } cntr+=list.length; qFull.put(list); if(verbose2){System.err.println("A: qFull.size()="+qFull.size());} // } // } list=null; loc=0; } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //At this point, list is null if(shutdown){ if(verbose2){System.err.println("Break 1");} break; } while(list==null){ if(verbose2){System.err.println("Taking empty list.");} try { list = qEmpty.take(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } //At this point, list is not null bases=0; if(list==poison){ if(verbose2){System.err.println("Break 2");} break; } //At this point, list is not null } } if(verbose2){System.err.println("Run loop exit.");} while(list!=null && loc>0){ try { // synchronized(this){ // if(!shutdown){ if(verbose2){System.err.println("B: Adding list of size "+loc);} qFull.put(list); if(verbose2){System.err.println("B: qFull.size()="+qFull.size());} // } // } list=null; loc=0; } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //At this point, list is null shutdown(); if(verbose){System.err.println("ByteFile2("+name()+").run() finished");} } synchronized void shutdown(){ if(verbose || verbose2){System.err.println("ByteFile2("+name()+").shutdown()");} if(shutdown){return;} shutdown=true; if(verbose2){System.err.println("Adding poison.");} qFull.add(poison); qEmpty.add(poison); if(verbose2){System.err.println("D: qFull.size()="+qFull.size());} if(verbose || verbose2){System.err.println("ByteFile2("+name()+").shutdown() finished");} } private boolean shutdown=false; final ByteFile1 bf1; final ArrayBlockingQueue qFull; final ArrayBlockingQueue qEmpty; } @Override public boolean isOpen(){ final byte[][] list=currentList; final int loc=currentLoc; if(list!=null && locline.length){ // bstart--; // buffer[bstart]='\n'; // for(int i=0, j=bstart-line.length; i0) : bstop+", "+bstart+", "+line.length; // while(newLen>buffer.length){ // //This could get big if pushback is used often, // //unless special steps are taken to prevent it, like leaving extra space for pushbacks. // buffer=Arrays.copyOf(buffer, buffer.length*2); // } // // Tools.shiftRight(buffer, rShift); // // for(int i=0; i