Debian Codegraph @

current/fileIO/Concatenate.java

raw symbols SCIP
1 package fileIO;
2
3 import java.io.File;
4 import java.util.ArrayList;
5
6 import fileIO.FileFormat;
7 import fileIO.ReadWrite;
8 import shared.Parser;
9 import shared.PreParser;
10 import shared.Shared;
11 import shared.Timer;
12 import shared.Tools;
13 import stream.ConcurrentReadInputStream;
14 import stream.ConcurrentReadOutputStream;
15 import stream.Read;
16 import structures.ListNum;
17
18 /**
19 * Accepts multiple input files.
20 * Reads them each sequentially, and outputs everything to a single output file.
21 * Generically, it can be used to concatenate files while recompressing them
22 * and avoiding the use of stdio.
23 * @author Brian Bushnell
24 * @date January 21, 2025
25 *
26 */
27 public class Concatenate {
28
29 public static void main(String[] args){
30 //Start a timer immediately upon code entrance.
31 Timer t=new Timer();
32
33 //Create an instance of this class
34 Concatenate x=new Concatenate(args);
35
36 //Run the object
37 x.process(t);
38
39 //Close the print stream if it was redirected
40 Shared.closeStream(x.outstream);
41 }
42
43 public Concatenate(String[] args){
44
45 {//Preparse block for help, config files, and outstream
46 PreParser pp=new PreParser(args, getClass(), false);
47 args=pp.args;
48 outstream=pp.outstream;
49 }
50
51 Parser parser=new Parser();
52 parser.out1=out1;
53 for(int i=0; i<args.length; i++){
54 String arg=args[i];
55 String[] split=arg.split("=");
56 String a=split[0].toLowerCase();
57 String b=split.length>1 ? split[1] : null;
58 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
59
60 if(a.equals("parse_flag_goes_here")){
61 //Set a variable here
62 }else if(a.equals("in")){
63 in.clear();
64 String[] b2=(b==null) ? null : (new File(b).exists() ? new String[] {b} : b.split(","));
65 for(String b3 : b2){in.add(b3);}
66 }else if(b==null && new File(arg).exists()){
67 in.add(arg);
68 }else if(parser.parse(arg, a, b)){
69 //do nothing
70 }else{
71 assert(false) : "Unknown parameter "+args[i];
72 outstream.println("Unknown parameter "+args[i]);
73 }
74 }
75
76 {//Process parser fields
77 Parser.processQuality();
78
79 maxReads=parser.maxReads;
80 out1=parser.out1;
81 }
82
83 ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, true, false, false);
84 }
85
86 void process(Timer t){
87
88 final ByteStreamWriter bsw;
89 if(out1!=null){
90 final int buff=4;
91
92 for(String s : in) {
93 assert(!out1.equalsIgnoreCase(s)) : "Input file and output file have same name.";
94 }
95
96 bsw=ByteStreamWriter.makeBSW(ffout1);
97 }else{bsw=null;}
98
99 for(String s : in) {
100 processInner(s, bsw);
101 }
102
103 bsw.poisonAndWait();
104 if(verbose){outstream.println("Finished.");}
105
106 t.stop();
107 Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 12);
108 }
109
110 void processInner(String fname, ByteStreamWriter bsw) {
111 FileFormat ffin=FileFormat.testInput(fname, FileFormat.TXT, null, true, true);
112
113 final ByteFile bf=ByteFile.makeByteFile(ffin);
114
115 ListNum<byte[]> ln;
116
117 while((ln=bf.nextList())!=null) {
118 for(byte[] line : ln.list) {
119 linesProcessed++;
120 bytesProcessed+=(line.length+1);
121 if(bsw!=null) {bsw.println(line);}
122 }
123 }
124 bf.close();
125 }
126
127 /*--------------------------------------------------------------*/
128
129 /*--------------------------------------------------------------*/
130
131 private ArrayList<String> in=new ArrayList<String>();
132 private String out1="stdout.txt";
133
134 private final FileFormat ffout1;
135
136 /*--------------------------------------------------------------*/
137
138 private long maxReads=-1;
139 private long linesProcessed=0, bytesProcessed=0;
140
141 /*--------------------------------------------------------------*/
142
143 private java.io.PrintStream outstream=System.err;
144 public static boolean verbose=false;
145
146 }