1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
package driver;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;
import fileIO.ReadWrite;
import fileIO.TextFile;
import shared.Parse;
import shared.Parser;
import shared.PreParser;
import shared.Timer;
import tracker.ReadStats;
public class ConcatenateTextFiles {
/** Format: infile1,infile2,...infileN,outfile */
public static void main(String[] args){
{//Preparse block for help, config files, and outstream
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false);
args=pp.args;
//outstream=pp.outstream;
}
Timer t=new Timer();
if(ReadWrite.ZIPLEVEL<6){ReadWrite.ZIPLEVEL=6;}
for(int i=0; i<args.length; i++){
final String arg=args[i];
final String[] split=arg.split("=");
String a=split[0].toLowerCase();
String b=split.length>1 ? split[1] : null;
if(Parser.parseZip(arg, a, b)){
//do nothing
}else if(a.equals("append") || a.equals("app")){
append=ReadStats.append=Parse.parseBoolean(b);
}else if(a.equals("overwrite") || a.equals("ow")){
overwrite=Parse.parseBoolean(b);
}else{
concatenate(args[i].split(","));
}
}
t.stop();
System.out.println();
System.out.println("Time: \t"+t);
}
private static void concatenate(String[] split) {
String outname=split[split.length-1];
assert(overwrite || !new File(outname).exists()) : outname+" exists.";
WriteThread wt=new WriteThread(outname);
wt.start();
ArrayList<String>[] bufferptr=new ArrayList[] {new ArrayList<String>(LIST_SIZE)};
for(int i=0; i<split.length-1; i++){
processTerm(split[i], bufferptr, wt);
}
ArrayList<String> buffer=bufferptr[0];
if(buffer==null){
wt.add(new ArrayList<String>(1));
}else if(buffer.isEmpty()){
wt.add(buffer);
}else{
wt.add(buffer);
wt.add(new ArrayList<String>(1));
}
}
private static void processTerm(String term, ArrayList<String>[] bufferptr, WriteThread wt){
System.out.println("Processing term "+term);
File f=new File(term);
if(!f.isDirectory()){
TextFile tf=new TextFile(term, false);
ArrayList<String> buffer=bufferptr[0];
String s=null;
for(s=tf.nextLine(); s!=null; s=tf.nextLine()){
buffer.add(s);
// System.out.println("Added to buffer");
if(buffer.size()>=LIST_SIZE){
// System.out.println("Sent buffer");
// System.out.println("****** "+term+" ******");
// for(String b : buffer){
// System.out.println(b);
// }
wt.add(buffer);
bufferptr[0]=buffer=new ArrayList<String>(LIST_SIZE);
}
}
tf.close();
}else{
assert(f.isDirectory());
File[] contents=f.listFiles();
for(File c : contents){
String abs=c.getAbsolutePath();
if(!abs.equals(wt.fname)){
// System.out.println(c+" == "+new File(wt.fname)+" : "+c.equals(new File(wt.fname)));
processTerm(abs, bufferptr, wt);
}
}
}
}
private static class WriteThread extends Thread{
public WriteThread(String fname_){
String temp=fname_;
try {
temp=new File(fname_).getCanonicalPath();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fname=temp;
os=ReadWrite.getOutputStream(fname, append, true, true);
writer=new PrintWriter(os);
}
public void add(ArrayList<String> list){
assert(list!=null);
while(list!=null){
// System.out.println("Adding list to queue "+queue.size());
try {
queue.put(list);
list=null;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void run(){
ArrayList<String> list=null;
while(list==null){
// System.out.println("Waiting for list...");
try {
list=queue.take();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println("Took list of size "+(list==null ? "null" : list.size()+""));
if(list!=null){
if(list.isEmpty()){
ReadWrite.finishWriting(writer, os, fname, allowSubprocess);
return;
}
for(String s : list){
if(s!=null){writer.println(s);}
}
}
list=null;
}
}
private final OutputStream os;
private final PrintWriter writer;
private final ArrayBlockingQueue<ArrayList<String>> queue=new ArrayBlockingQueue<ArrayList<String>>(MAX_LISTS);
final String fname;
}
public static final int MAX_LISTS=8;
public static final int LIST_SIZE=100;
public static boolean overwrite=true;
public static boolean append=false;
public static boolean allowSubprocess=true;
}
|