tmf: Bug 478578: Add weak binary validation to text trace types
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / TmfTraceUtils.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.trace;
14
15 import java.io.BufferedInputStream;
16 import java.io.File;
17 import java.io.FileInputStream;
18 import java.io.IOException;
19 import java.util.HashSet;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
25 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
26 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
27
28 /**
29 * Utility methods for ITmfTrace's.
30 *
31 * @author Alexandre Montplaisir
32 */
33 @NonNullByDefault
34 public final class TmfTraceUtils {
35
36 private static final int MAX_NB_BINARY_BYTES = 2048;
37
38 private TmfTraceUtils() {
39 }
40
41 /**
42 * Get an analysis module belonging to this trace, with the specified ID and
43 * class.
44 *
45 * @param trace
46 * The trace for which you want the modules
47 * @param moduleClass
48 * Returned modules must extend this class
49 * @param id
50 * The ID of the analysis module
51 * @return The analysis module with specified class and ID, or null if no
52 * such module exists.
53 */
54 public static @Nullable <T extends IAnalysisModule> T getAnalysisModuleOfClass(ITmfTrace trace,
55 Class<T> moduleClass, String id) {
56 Iterable<T> modules = getAnalysisModulesOfClass(trace, moduleClass);
57 for (T module : modules) {
58 if (id.equals(module.getId())) {
59 return module;
60 }
61 }
62 return null;
63 }
64
65 /**
66 * Return the analysis modules that are of a given class. Module will be
67 * casted to the requested class.
68 *
69 * @param trace
70 * The trace for which you want the modules
71 * @param moduleClass
72 * Returned modules must extend this class
73 * @return List of modules of class moduleClass
74 */
75 public static <T> Iterable<T> getAnalysisModulesOfClass(ITmfTrace trace, Class<T> moduleClass) {
76 Iterable<IAnalysisModule> analysisModules = trace.getAnalysisModules();
77 Set<T> modules = new HashSet<>();
78 for (IAnalysisModule module : analysisModules) {
79 if (moduleClass.isAssignableFrom(module.getClass())) {
80 modules.add(moduleClass.cast(module));
81 }
82 }
83 return modules;
84 }
85
86 /**
87 * Return the first result of the first aspect that resolves as non null for
88 * the event received in parameter. If the returned value is not null, it
89 * can be safely cast to the aspect's class proper return type.
90 *
91 * @param trace
92 * The trace for which you want the event aspects
93 * @param aspectClass
94 * The class of the aspect(s) to resolve
95 * @param event
96 * The event for which to get the aspect
97 * @return The first result of the
98 * {@link ITmfEventAspect#resolve(ITmfEvent)} that returns non null
99 * for the event or {@code null} otherwise
100 */
101 public static @Nullable <T extends ITmfEventAspect> Object resolveEventAspectOfClassForEvent(
102 ITmfTrace trace, Class<T> aspectClass, ITmfEvent event) {
103 Iterable<ITmfEventAspect> aspects = trace.getEventAspects();
104 for (ITmfEventAspect aspect : aspects) {
105 if (aspectClass.isAssignableFrom(aspect.getClass())) {
106 Object obj = aspect.resolve(event);
107 if (obj != null) {
108 return obj;
109 }
110 }
111 }
112 return null;
113 }
114
115 /**
116 * Checks for text file.
117 *
118 * Note that it checks for binary value 0 in the first MAX_NB_BINARY_BYTES
119 * bytes to determine if the file is text.
120 *
121 * @param file
122 * the file to check. Caller has to make sure that file exists.
123 * @return true if it is binary else false
124 * @throws IOException
125 * if IOException occurs
126 * @since 2.0
127 */
128 public static boolean isText(File file) throws IOException {
129 try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
130 int count = 0;
131 int val = bufferedInputStream.read();
132 while ((count < MAX_NB_BINARY_BYTES) && (val >= 0)) {
133 if (val == 0) {
134 return false;
135 }
136 count++;
137 val = bufferedInputStream.read();
138 }
139 }
140 return true;
141 }
142 }
This page took 0.034436 seconds and 5 git commands to generate.