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