import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceProperties;
import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Not a file. It's a directory: " + path); //$NON-NLS-1$
}
int confidence = 0;
+ try {
+ if (!TmfTraceUtils.isText(file)) {
+ return new TraceValidationStatus(confidence, Activator.PLUGIN_ID);
+ }
+ } catch (IOException e) {
+ Activator.logError("Error validating file: " + path, e); //$NON-NLS-1$
+ return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "IOException validating file: " + path, e); //$NON-NLS-1$
+ }
try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(path, "r")) { //$NON-NLS-1$
int lineCount = 0;
int matches = 0;
import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CustomTrace_FileNotFound + ": " + path); //$NON-NLS-1$
}
int confidence = 0;
+ try {
+ if (!TmfTraceUtils.isText(file)) {
+ return new TraceValidationStatus(confidence, Activator.PLUGIN_ID);
+ }
+ } catch (IOException e) {
+ Activator.logError("Error validating file: " + path, e); //$NON-NLS-1$
+ return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "IOException validating file: " + path, e); //$NON-NLS-1$
+ }
try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(path, "r")) { //$NON-NLS-1$
int lineCount = 0;
double matches = 0.0;
String line = rafile.getNextLine();
+
while ((line != null) && (lineCount++ < MAX_LINES)) {
for (InputLine inputLine : fDefinition.inputs) {
Matcher matcher = inputLine.getPattern().matcher(line);
import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
if (!file.exists() || !file.isFile() || !file.canRead()) {
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CustomTrace_FileNotFound + ": " + path); //$NON-NLS-1$
}
+ try {
+ if (!TmfTraceUtils.isText(file)) {
+ return new TraceValidationStatus(0, Activator.PLUGIN_ID);
+ }
+ } catch (IOException e) {
+ Activator.logError("Error validating file: " + path, e); //$NON-NLS-1$
+ return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "IOException validating file: " + path, e); //$NON-NLS-1$
+ }
try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(path, "r")) { //$NON-NLS-1$
int lineCount = 0;
long rawPos = 0;
package org.eclipse.tracecompass.tmf.core.trace;
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
@NonNullByDefault
public final class TmfTraceUtils {
+ private static final int MAX_NB_BINARY_BYTES = 2048;
+
private TmfTraceUtils() {
}
}
return null;
}
+
+ /**
+ * Checks for text file.
+ *
+ * Note that it checks for binary value 0 in the first MAX_NB_BINARY_BYTES
+ * bytes to determine if the file is text.
+ *
+ * @param file
+ * the file to check. Caller has to make sure that file exists.
+ * @return true if it is binary else false
+ * @throws IOException
+ * if IOException occurs
+ * @since 2.0
+ */
+ public static boolean isText(File file) throws IOException {
+ try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
+ int count = 0;
+ int val = bufferedInputStream.read();
+ while ((count < MAX_NB_BINARY_BYTES) && (val >= 0)) {
+ if (val == 0) {
+ return false;
+ }
+ count++;
+ val = bufferedInputStream.read();
+ }
+ }
+ return true;
+ }
}
import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
if (!file.isFile()) {
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Not a file. It's a directory: " + path); //$NON-NLS-1$
}
+ try {
+ if (!TmfTraceUtils.isText(file)) {
+ return new TraceValidationStatus(0, Activator.PLUGIN_ID);
+ }
+ } catch (IOException e) {
+ Activator.logError("Error validating file: " + path, e); //$NON-NLS-1$
+ return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "IOException validating file: " + path, e); //$NON-NLS-1$
+ }
int confidence = 0;
try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(path, "r")) { //$NON-NLS-1$
int lineCount = 0;