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
.NonNull
;
23 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
24 import org
.eclipse
.jdt
.annotation
.Nullable
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
30 * Utility methods for ITmfTrace's.
32 * @author Alexandre Montplaisir
35 public final class TmfTraceUtils
{
37 private static final int MAX_NB_BINARY_BYTES
= 2048;
39 private TmfTraceUtils() {
43 * Get an analysis module belonging to this trace, with the specified ID and
47 * The trace for which you want the modules
49 * Returned modules must extend this class
51 * The ID of the analysis module
52 * @return The analysis module with specified class and ID, or null if no
55 public static @Nullable <T
extends IAnalysisModule
> T
getAnalysisModuleOfClass(ITmfTrace trace
,
56 Class
<T
> moduleClass
, String id
) {
57 Iterable
<T
> modules
= getAnalysisModulesOfClass(trace
, moduleClass
);
58 for (T module
: modules
) {
59 if (id
.equals(module
.getId())) {
67 * Return the analysis modules that are of a given class. Module will be
68 * casted to the requested class.
71 * The trace for which you want the modules
73 * Returned modules must extend this class
74 * @return List of modules of class moduleClass
76 public static <T
> Iterable
<@NonNull T
> getAnalysisModulesOfClass(ITmfTrace trace
, Class
<T
> moduleClass
) {
77 Iterable
<IAnalysisModule
> analysisModules
= trace
.getAnalysisModules();
78 Set
<@NonNull T
> modules
= new HashSet
<>();
79 for (IAnalysisModule module
: analysisModules
) {
80 if (moduleClass
.isAssignableFrom(module
.getClass())) {
81 modules
.add(moduleClass
.cast(module
));
88 * Return the first result of the first aspect that resolves as non null for
89 * the event received in parameter. If the returned value is not null, it
90 * can be safely cast to the aspect's class proper return type.
93 * The trace for which you want the event aspects
95 * The class of the aspect(s) to resolve
97 * The event for which to get the aspect
98 * @return The first result of the
99 * {@link ITmfEventAspect#resolve(ITmfEvent)} that returns non null
100 * for the event or {@code null} otherwise
102 public static @Nullable <T
extends ITmfEventAspect
> Object
resolveEventAspectOfClassForEvent(
103 ITmfTrace trace
, Class
<T
> aspectClass
, ITmfEvent event
) {
104 Iterable
<ITmfEventAspect
> aspects
= trace
.getEventAspects();
105 for (ITmfEventAspect aspect
: aspects
) {
106 if (aspectClass
.isAssignableFrom(aspect
.getClass())) {
107 Object obj
= aspect
.resolve(event
);
117 * Checks for text file.
119 * Note that it checks for binary value 0 in the first MAX_NB_BINARY_BYTES
120 * bytes to determine if the file is text.
123 * the file to check. Caller has to make sure that file exists.
124 * @return true if it is binary else false
125 * @throws IOException
126 * if IOException occurs
129 public static boolean isText(File file
) throws IOException
{
130 try (BufferedInputStream bufferedInputStream
= new BufferedInputStream(new FileInputStream(file
))) {
132 int val
= bufferedInputStream
.read();
133 while ((count
< MAX_NB_BINARY_BYTES
) && (val
>= 0)) {
138 val
= bufferedInputStream
.read();