comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/fileIO/TextFile.java @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 68:5028fdace37b
1 package fileIO;
2 import java.io.BufferedReader;
3 import java.io.File;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.util.ArrayList;
7
8 import shared.Shared;
9 import shared.Timer;
10 import shared.Tools;
11
12
13 public class TextFile {
14
15
16 public static void main(String[] args){
17 TextFile tf=new TextFile(args.length>0 ? args[0] : "stdin", false);
18 int first=0;
19 long last=100;
20 boolean speedtest=false;
21 if(args.length>1){
22 if(args[1].equalsIgnoreCase("speedtest")){
23 speedtest=true;
24 first=0;
25 last=Long.MAX_VALUE;
26 }else{
27 first=Integer.parseInt(args[1]);
28 last=first+100;
29 }
30 }
31 if(args.length>2){
32 last=Integer.parseInt(args[2]);
33 }
34 speedtest(tf, first, last, !speedtest);
35
36 // long lines=0;
37 // long bytes=0;
38 // if(args.length>1){
39 // first=Integer.parseInt(args[1]);
40 // last=first+100;
41 // }
42 // if(args.length>2){
43 // last=Integer.parseInt(args[2]);
44 // }
45 //
46 // for(int i=0; i<first; i++){tf.readLine();}
47 // for(int i=first; i<last; i++){
48 // String s=tf.readLine();
49 // if(s==null){break;}
50 //
51 // lines++;
52 // bytes+=s.length();
53 // System.out.println(s);
54 //// System.out.println(Arrays.toString(s.getBytes()));
55 // }
56 //
57 // System.err.println("\n");
58 // System.err.println("Lines: "+lines);
59 // System.err.println("Bytes: "+bytes);
60 // tf.close();
61 // tf.reset();
62 // tf.close();
63 //
64 //// for(int i=first; i<last; i++){
65 //// String s=tf.readLine();
66 //// if(s==null){break;}
67 ////
68 //// lines++;
69 //// bytes+=s.length();
70 //// System.out.println(s);
71 //// }
72 }
73
74 private static void speedtest(TextFile tf, long first, long last, boolean reprint){
75 Timer t=new Timer();
76 long lines=0;
77 long bytes=0;
78 for(long i=0; i<first; i++){tf.nextLine();}
79 if(reprint){
80 for(long i=first; i<last; i++){
81 String s=tf.nextLine();
82 if(s==null){break;}
83
84 lines++;
85 bytes+=s.length();
86 System.out.println(s);
87 }
88
89 System.err.println("\n");
90 System.err.println("Lines: "+lines);
91 System.err.println("Bytes: "+bytes);
92 }else{
93 for(long i=first; i<last; i++){
94 String s=tf.nextLine();
95 if(s==null){break;}
96 lines++;
97 bytes+=s.length();
98 }
99 }
100 t.stop();
101
102 if(!reprint){
103 System.err.println(Tools.timeLinesBytesProcessed(t, lines, bytes, 8));
104 }
105 }
106
107 public TextFile(String name){this(name, false);}
108
109 public TextFile(FileFormat ff){
110 file=new File(ff.name());
111 allowSubprocess=ff.allowSubprocess();
112 name=ff.name();
113
114 br=open();
115 }
116
117 public TextFile(String fname, boolean allowSubprocess_){
118 fname=fname.replace('\\', '/');
119 file=new File(fname);
120 allowSubprocess=allowSubprocess_;
121 name=fname;
122
123 br=open();
124 }
125
126 public static final String[] toStringLines(FileFormat ff){
127 TextFile tf=new TextFile(ff);
128 String[] lines=tf.toStringLines();
129 tf.close();
130 return lines;
131 }
132
133 public static final String[] toStringLines(String fname){
134 TextFile tf=new TextFile(fname);
135 String[] lines=tf.toStringLines();
136 tf.close();
137 return lines;
138 }
139
140 /** Generate an array of the lines in this TextFile */
141 public final String[] toStringLines(){
142
143 String s=null;
144 ArrayList<String> list=new ArrayList<String>(4096);
145
146 for(s=nextLine(); s!=null; s=nextLine()){
147 list.add(s);
148 }
149
150 return list.toArray(new String[list.size()]);
151
152 }
153
154 public final long countLines(){
155
156 String s=null;
157 long count=0;
158
159 for(s=nextLine(); s!=null; s=nextLine()){count++;}
160
161 reset();
162
163 return count;
164
165 }
166
167 public static String[][] doublesplitTab(String[] lines, boolean trim){
168 String[][] lines2=new String[lines.length][];
169 for(int i=0; i<lines.length; i++){
170 if(trim){
171 lines2[i]=lines[i].trim().split("\t", -1);
172 }else{
173 lines2[i]=lines[i].split("\t", -1);
174 }
175 }
176 return lines2;
177 }
178
179
180 public static String[][] doublesplitWhitespace(String[] lines, boolean trim){
181 String[][] lines2=new String[lines.length][];
182 for(int i=0; i<lines.length; i++){
183 if(trim){
184 lines2[i]=lines[i].trim().split("\\p{javaWhitespace}+");
185 }else{
186 lines2[i]=lines[i].split("\\p{javaWhitespace}+");
187 }
188 }
189 return lines2;
190 }
191
192 public final void reset(){
193 close();
194 br=open();
195 }
196
197 public boolean exists(){
198 return name.equals("stdin") || name.startsWith("stdin.") || name.startsWith("jar:") || file.exists(); //TODO Ugly and unsafe hack for files in jars
199 }
200
201 public final boolean close(){
202 if(!open){return false;}
203 open=false;
204 assert(br!=null);
205
206 errorState|=ReadWrite.finishReading(is, name, allowSubprocess, br, isr);
207
208 br=null;
209 is=null;
210 isr=null;
211 lineNum=-1;
212 return false;
213 }
214
215 public String nextLine(){
216 return readLine(true);
217 }
218
219 public final String readLine(){
220 return readLine(true);
221 }
222
223 public final String readLine(boolean skipBlank){
224 String currentLine=null;
225
226
227 //Note: Disabling this block seems to speed things up maybe 5%.
228 // boolean ready=false;
229 // try {
230 // ready=br.ready();
231 // } catch (IOException e) {
232 // // TODO Auto-generated catch block
233 // e.printStackTrace();
234 // }
235 // if(!ready){return null;}
236
237 if(!open || br==null){
238 if(Shared.WINDOWS){System.err.println("Attempting to read from a closed file: "+name);}
239 return null;
240 }
241 try{
242 lineNum++;
243 currentLine=br.readLine();
244 // System.out.println(lineNum+":\t"+currentLine);
245 }catch(Exception e){
246 System.err.println("Oops! Bad read in file "+name+" at line "+lineNum);
247 System.err.println(""+open+", "+(br==null));
248 try {
249 File f=new File(name);
250 System.err.println("path and length: \t"+f.getAbsolutePath()+"\t"+f.length());
251 } catch (Exception e1) {
252 //e1.printStackTrace();
253 }
254 throw new RuntimeException(e);
255 }
256 if(currentLine==null){return null;}
257 // System.out.println("Read "+line);
258
259 // currentLine=currentLine.trim();
260
261 //Note! This may generate a new String for every line and thus be slow.
262 // if(currentLine.trim().length()==0){return readLine();} //Skips blank lines
263 if(skipBlank && (currentLine.length()==0 ||
264 (Character.isWhitespace(currentLine.charAt(0)) &&
265 (Character.isWhitespace(currentLine.charAt(currentLine.length()-1)))) &&
266 currentLine.trim().length()==0)){
267 return readLine(skipBlank); //Skips blank lines
268 }
269
270 return currentLine;
271 }
272
273 private final BufferedReader open(){
274
275 if(open){
276 throw new RuntimeException("Attempt to open already-opened TextFile "+name);
277 }
278 open=true;
279
280 is=ReadWrite.getInputStream(name, true, allowSubprocess);
281 isr=new InputStreamReader(is);
282
283 BufferedReader b=new BufferedReader(isr, 32768);
284
285 return b;
286 }
287
288 public boolean isOpen(){return open;}
289
290 private boolean open=false;
291 public boolean errorState=false;
292
293 public final String name;
294 public File file;
295 private final boolean allowSubprocess;
296
297 public InputStream is;
298 public InputStreamReader isr;
299 public BufferedReader br;
300
301 public long lineNum=-1;
302
303 public static boolean verbose=false;
304
305 }