File: TextFile.java

package info (click to toggle)
bbmap 39.20%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,024 kB
  • sloc: java: 312,743; sh: 18,099; python: 5,247; ansic: 2,074; perl: 96; makefile: 39; xml: 38
file content (305 lines) | stat: -rwxr-xr-x 7,166 bytes parent folder | download | duplicates (4)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package fileIO;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import shared.Shared;
import shared.Timer;
import shared.Tools;


public class TextFile {
	
	
	public static void main(String[] args){
		TextFile tf=new TextFile(args.length>0 ? args[0] : "stdin", false);
		int first=0;
		long 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);
		
//		long lines=0;
//		long bytes=0;
//		if(args.length>1){
//			first=Integer.parseInt(args[1]);
//			last=first+100;
//		}
//		if(args.length>2){
//			last=Integer.parseInt(args[2]);
//		}
//		
//		for(int i=0; i<first; i++){tf.readLine();}
//		for(int i=first; i<last; i++){
//			String s=tf.readLine();
//			if(s==null){break;}
//
//			lines++;
//			bytes+=s.length();
//			System.out.println(s);
////			System.out.println(Arrays.toString(s.getBytes()));
//		}
//		
//		System.err.println("\n");
//		System.err.println("Lines: "+lines);
//		System.err.println("Bytes: "+bytes);
//		tf.close();
//		tf.reset();
//		tf.close();
//		
////		for(int i=first; i<last; i++){
////			String s=tf.readLine();
////			if(s==null){break;}
////
////			lines++;
////			bytes+=s.length();
////			System.out.println(s);
////		}
	}
	
	private static void speedtest(TextFile tf, long first, long last, boolean reprint){
		Timer t=new Timer();
		long lines=0;
		long bytes=0;
		for(long i=0; i<first; i++){tf.nextLine();}
		if(reprint){
			for(long i=first; i<last; i++){
				String s=tf.nextLine();
				if(s==null){break;}

				lines++;
				bytes+=s.length();
				System.out.println(s);
			}
			
			System.err.println("\n");
			System.err.println("Lines: "+lines);
			System.err.println("Bytes: "+bytes);
		}else{
			for(long i=first; i<last; i++){
				String s=tf.nextLine();
				if(s==null){break;}
				lines++;
				bytes+=s.length();
			}
		}
		t.stop();
		
		if(!reprint){
			System.err.println(Tools.timeLinesBytesProcessed(t, lines, bytes, 8));
		}
	}

	public TextFile(String name){this(name, false);}
	
	public TextFile(FileFormat ff){
		file=new File(ff.name());
		allowSubprocess=ff.allowSubprocess();
		name=ff.name();
		
		br=open();
	}
	
	public TextFile(String fname, boolean allowSubprocess_){
		fname=fname.replace('\\', '/');
		file=new File(fname);
		allowSubprocess=allowSubprocess_;
		name=fname;
		
		br=open();
	}
	
	public static final String[] toStringLines(FileFormat ff){
		TextFile tf=new TextFile(ff);
		String[] lines=tf.toStringLines();
		tf.close();
		return lines;
	}
	
	public static final String[] toStringLines(String fname){
		TextFile tf=new TextFile(fname);
		String[] lines=tf.toStringLines();
		tf.close();
		return lines;
	}
	
	/** Generate an array of the lines in this TextFile */
	public final String[] toStringLines(){
		
		String s=null;
		ArrayList<String> list=new ArrayList<String>(4096);
		
		for(s=nextLine(); s!=null; s=nextLine()){
			list.add(s);
		}
		
		return list.toArray(new String[list.size()]);
		
	}
	
	public final long countLines(){
		
		String s=null;
		long count=0;
		
		for(s=nextLine(); s!=null; s=nextLine()){count++;}
		
		reset();
		
		return count;
		
	}
	
	public static String[][] doublesplitTab(String[] lines, boolean trim){
		String[][] lines2=new String[lines.length][];
		for(int i=0; i<lines.length; i++){
			if(trim){
				lines2[i]=lines[i].trim().split("\t", -1);
			}else{
				lines2[i]=lines[i].split("\t", -1);
			}
		}
		return lines2;
	}
	
	
	public static String[][] doublesplitWhitespace(String[] lines, boolean trim){
		String[][] lines2=new String[lines.length][];
		for(int i=0; i<lines.length; i++){
			if(trim){
				lines2[i]=lines[i].trim().split("\\p{javaWhitespace}+");
			}else{
				lines2[i]=lines[i].split("\\p{javaWhitespace}+");
			}
		}
		return lines2;
	}
	
	public final void reset(){
		close();
		br=open();
	}
	
	public boolean exists(){
		return name.equals("stdin") || name.startsWith("stdin.") || name.startsWith("jar:") || file.exists(); //TODO Ugly and unsafe hack for files in jars
	}
	
	public final boolean close(){
		if(!open){return false;}
		open=false;
		assert(br!=null);
		
		errorState|=ReadWrite.finishReading(is, name, allowSubprocess, br, isr);
		
		br=null;
		is=null;
		isr=null;
		lineNum=-1;
		return false;
	}
	
	public String nextLine(){
		return readLine(true);
	}
	
	public final String readLine(){
		return readLine(true);
	}
	
	public final String readLine(boolean skipBlank){
		String currentLine=null;
		
		
		//Note:  Disabling this block seems to speed things up maybe 5%.
//		boolean ready=false;
//		try {
//			ready=br.ready();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		if(!ready){return null;}
		
		if(!open || br==null){
			if(Shared.WINDOWS){System.err.println("Attempting to read from a closed file: "+name);}
			return null;
		}
		try{
			lineNum++;
			currentLine=br.readLine();
//			System.out.println(lineNum+":\t"+currentLine);
		}catch(Exception e){
			System.err.println("Oops! Bad read in file "+name+" at line "+lineNum);
			System.err.println(""+open+", "+(br==null));
			try {
				File f=new File(name);
				System.err.println("path and length: \t"+f.getAbsolutePath()+"\t"+f.length());
			} catch (Exception e1) {
				//e1.printStackTrace();
			}
			throw new RuntimeException(e);
		}
		if(currentLine==null){return null;}
//		System.out.println("Read "+line);
		
//		currentLine=currentLine.trim();
		
		//Note! This may generate a new String for every line and thus be slow.
//		if(currentLine.trim().length()==0){return readLine();} //Skips blank lines
		if(skipBlank && (currentLine.length()==0 ||
				(Character.isWhitespace(currentLine.charAt(0)) &&
						(Character.isWhitespace(currentLine.charAt(currentLine.length()-1)))) &&
						currentLine.trim().length()==0)){
			return readLine(skipBlank); //Skips blank lines
		}
		
		return currentLine;
	}
	
	private final BufferedReader open(){
		
		if(open){
			throw new RuntimeException("Attempt to open already-opened TextFile "+name);
		}
		open=true;
		
		is=ReadWrite.getInputStream(name, true, allowSubprocess);
		isr=new InputStreamReader(is);
		
		BufferedReader b=new BufferedReader(isr, 32768);
		
		return b;
	}
	
	public boolean isOpen(){return open;}

	private boolean open=false;
	public boolean errorState=false;
	
	public final String name;
	public File file;
	private final boolean allowSubprocess;
	
	public InputStream is;
	public InputStreamReader isr;
	public BufferedReader br;
	
	public long lineNum=-1;

	public static boolean verbose=false;
	
}