27 import java.util.AbstractList; |
27 import java.util.AbstractList; |
28 |
28 |
29 import com.nokia.s60tools.analyzetool.engine.ParseAnalyzeData; |
29 import com.nokia.s60tools.analyzetool.engine.ParseAnalyzeData; |
30 |
30 |
31 /** |
31 /** |
32 * Reads thru trace file and generates statistic info |
32 * Reads thru trace file and generates statistic info. |
|
33 * |
33 * @author kihe |
34 * @author kihe |
34 * |
35 * |
35 */ |
36 */ |
36 public class ReadFile { |
37 public class ReadFile { |
37 |
38 |
38 /** Parser */ |
39 /** Parser. */ |
39 private ParseAnalyzeData parser; |
40 private ParseAnalyzeData parser; |
40 |
41 |
41 /** |
42 /** |
42 * Constructor |
43 * Constructor. |
43 */ |
44 */ |
44 public ReadFile() |
45 public ReadFile() { |
45 { |
|
46 } |
46 } |
47 |
47 |
48 |
|
49 /** |
48 /** |
50 * Reads thru file |
49 * Reads thru file. |
51 * @param path File location |
50 * |
52 * @return True if no errors occurs when reading file otherwise False |
51 * @param path |
|
52 * file location |
|
53 * @return true, if no errors occurs when reading file, otherwise false |
53 */ |
54 */ |
54 public boolean readFile(final String path) { |
55 public boolean readFile(final String path) { |
55 // return value |
56 // return value |
56 boolean retValue = true; |
57 boolean retValue = true; |
57 |
58 |
58 // input |
59 // input |
59 BufferedReader input = null; |
60 BufferedReader input = null; |
60 FileInputStream fis = null; |
61 FileInputStream fis = null; |
|
62 |
61 try { |
63 try { |
62 |
|
63 // check that file exists |
64 // check that file exists |
64 if (path == null || ("").equals(path)) { |
65 if (path == null || ("").equals(path)) { |
65 return false; |
66 return false; |
66 } |
67 } |
67 java.io.File file = new java.io.File(path); |
68 File file = new File(path); |
68 if (!file.exists()) { |
69 if (!file.exists()) { |
69 return false; |
70 return false; |
70 } |
71 } |
71 |
72 |
72 // get input |
73 // get input |
73 fis = new FileInputStream(file); |
74 fis = new FileInputStream(file); |
74 input = new BufferedReader(new InputStreamReader(fis,"UTF-8")); |
75 input = new BufferedReader(new InputStreamReader(fis, "UTF-8")); |
75 int linebreakSize = lineBreakSize(file); //important to determine file position for deferred callstack reading |
76 int linebreakSize = lineBreakSize(file); // important to determine |
|
77 // file position for deferred callstack reading |
76 |
78 |
77 // get first line of data file |
79 // get first line of data file |
78 parser = new ParseAnalyzeData(false, true, true, linebreakSize); |
80 parser = new ParseAnalyzeData(false, true, true, linebreakSize); |
79 String line = null; |
81 String line = null; |
80 // go thru file |
82 // go thru file |
81 while ((line = input.readLine()) != null) { |
83 while ((line = input.readLine()) != null) { |
82 parser.parse(line); |
84 boolean success = parser.parse(line); |
|
85 if (!success) { |
|
86 return false; |
|
87 } |
83 } |
88 } |
84 fis.close(); |
89 fis.close(); |
85 input.close(); |
90 input.close(); |
86 } catch (FileNotFoundException ex) { |
91 } catch (FileNotFoundException ex) { |
87 retValue = false; |
92 retValue = false; |
88 ex.printStackTrace(); |
93 ex.printStackTrace(); |
89 } catch (OutOfMemoryError oome){ |
94 } catch (OutOfMemoryError oome) { |
90 retValue = true; |
95 retValue = true; |
91 oome.printStackTrace(); |
96 oome.printStackTrace(); |
92 } catch (IOException ex) { |
97 } catch (IOException ex) { |
93 retValue = false; |
98 retValue = false; |
94 ex.printStackTrace(); |
99 ex.printStackTrace(); |
100 } catch (IOException ex) { |
105 } catch (IOException ex) { |
101 ex.printStackTrace(); |
106 ex.printStackTrace(); |
102 } |
107 } |
103 |
108 |
104 try { |
109 try { |
105 if( fis != null ) { |
110 if (fis != null) { |
106 fis.close(); |
111 fis.close(); |
107 } |
112 } |
108 }catch(Exception e) { |
113 } catch (Exception e) { |
109 e.printStackTrace(); |
114 e.printStackTrace(); |
110 } |
115 } |
111 } |
116 } |
112 return retValue; |
117 return retValue; |
113 } |
118 } |
114 |
119 |
115 /** |
120 /** |
116 * Returns statistic |
121 * Returns statistic |
|
122 * |
117 * @return Statistic |
123 * @return Statistic |
118 */ |
124 */ |
119 public AbstractList<ProcessInfo> getStatistic() |
125 public AbstractList<ProcessInfo> getStatistic() { |
120 { |
|
121 return parser.getStatistic(); |
126 return parser.getStatistic(); |
122 } |
127 } |
123 |
128 |
124 /** |
129 /** |
125 * Finish the file reading. |
130 * Finish the file reading. |
126 */ |
131 */ |
127 public void finish() |
132 public void finish() { |
128 { |
|
129 parser.finish(); |
133 parser.finish(); |
|
134 } |
130 |
135 |
131 } |
|
132 |
|
133 /** |
136 /** |
134 * Returns true if callstack reading from file is done |
137 * Returns true if callstack reading from file is done on demand; false if |
135 * on demand; false if callstacks are made available during parsing |
138 * callstacks are made available during parsing phase. |
136 * phase. |
139 * |
137 * @return true for deferred callstack reading |
140 * @return true for deferred callstack reading |
138 */ |
141 */ |
139 public boolean hasDeferredCallstacks(){ |
142 public boolean hasDeferredCallstacks() { |
140 return parser.hasDeferredCallstacks(); |
143 return parser.hasDeferredCallstacks(); |
141 } |
144 } |
142 |
145 |
143 /** |
146 /** |
144 * Determines the size of line breaks in the given file. File produced on the device-side |
147 * Determines the size of line breaks in the given file. File produced on |
145 * should have while Windows files have. |
148 * the device-side should have while Windows files have. |
146 * @param aFile the file to check |
149 * |
|
150 * @param aFile |
|
151 * the file to check |
147 * @return 1 or 2 for size of line break, or 0 if it cannot be determined |
152 * @return 1 or 2 for size of line break, or 0 if it cannot be determined |
148 */ |
153 */ |
149 private static int lineBreakSize(File aFile){ |
154 private static int lineBreakSize(File aFile) { |
150 int ret = 0; |
155 int ret = 0; |
151 try { |
156 try { |
152 BufferedReader br = new BufferedReader(new FileReader( aFile.getPath() )); |
157 BufferedReader br = new BufferedReader(new FileReader(aFile |
153 |
158 .getPath())); |
|
159 |
154 int ch; |
160 int ch; |
155 int cnt = 0; |
161 int cnt = 0; |
156 while ((ch = br.read()) >=0){ |
162 while ((ch = br.read()) >= 0) { |
157 cnt ++; |
163 cnt++; |
158 if (ch == '\r'){ |
164 if (ch == '\r') { |
159 ret ++; |
165 ret++; |
160 } else if (ch == '\n'){ |
166 } else if (ch == '\n') { |
161 ret ++; |
167 ret++; |
162 break; |
168 break; |
163 } |
169 } |
164 } |
170 } |
165 |
|
166 } catch (IOException e) { |
171 } catch (IOException e) { |
167 // do nothing, a return value of 0 will indicate some problem |
172 // do nothing, a return value of 0 will indicate some problem |
168 } |
173 } |
169 return ret; |
174 return ret; |
170 } |
175 } |
171 |
|
172 } |
176 } |