1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.trace
;
15 import java
.io
.BufferedInputStream
;
17 import java
.io
.FileInputStream
;
18 import java
.io
.IOException
;
19 import java
.util
.HashSet
;
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
;
29 * Utility methods for ITmfTrace's.
31 * @author Alexandre Montplaisir
34 public final class TmfTraceUtils
{
36 private static final int MAX_NB_BINARY_BYTES
= 2048;
38 private TmfTraceUtils() {
42 * Get an analysis module belonging to this trace, with the specified ID and
46 * The trace for which you want the modules
48 * Returned modules must extend this class
50 * The ID of the analysis module
51 * @return The analysis module with specified class and ID, or null if no
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())) {
66 * Return the analysis modules that are of a given class. Module will be
67 * casted to the requested class.
70 * The trace for which you want the modules
72 * Returned modules must extend this class
73 * @return List of modules of class moduleClass
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
));
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.
92 * The trace for which you want the event aspects
94 * The class of the aspect(s) to resolve
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
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
);
116 * Checks for text file.
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.
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
128 public static boolean isText(File file
) throws IOException
{
129 try (BufferedInputStream bufferedInputStream
= new BufferedInputStream(new FileInputStream(file
))) {
131 int val
= bufferedInputStream
.read();
132 while ((count
< MAX_NB_BINARY_BYTES
) && (val
>= 0)) {
137 val
= bufferedInputStream
.read();