changeset 713:c25ef52d9d7c

ReaderMain - automatic file detection
author Devel 2
date Wed, 29 Nov 2017 13:28:12 +0100
parents d243915067e7
children 875bc31cea61
files stress-tester/src/main/java/com/passus/st/ReaderMain.java
diffstat 1 files changed, 19 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/ReaderMain.java	Wed Nov 29 12:36:57 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/ReaderMain.java	Wed Nov 29 13:28:12 2017 +0100
@@ -5,14 +5,20 @@
 import com.passus.st.client.SessionStatusEvent;
 import com.passus.st.client.http.HttpSessionPayloadEvent;
 import com.passus.st.emitter.SessionInfo;
+import com.passus.st.reader.nc.NcHeader;
+import com.passus.st.reader.pcap.PcapFileHeader;
 import com.passus.st.source.NcEventSource;
 import com.passus.st.source.PcapSessionEventSource;
 import static com.passus.st.utils.CliUtils.option;
+import java.io.File;
+import java.io.InputStream;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.DefaultParser;
 import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.Options;
+import static org.apache.commons.io.FileUtils.openInputStream;
 import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.IOUtils;
 
 /**
  *
@@ -126,9 +132,20 @@
 
             String filename = clArgs[0];
             String ext = FilenameUtils.getExtension(filename);
-            if ("nc".equals(ext)) {
+            File srcFile = new File(filename);
+            InputStream in = null;
+            
+            byte[] preambule = null;
+            try {
+                in = openInputStream(srcFile);
+                preambule = IOUtils.toByteArray(in, 20);
+            } finally {
+                IOUtils.closeQuietly(in);
+            }
+
+            if (NcHeader.isPreambule(preambule, 0)) {
                 readNcFile(filename);
-            } else if ("pcap".equals(ext)) {
+            } else if (PcapFileHeader.isFormatSupported(preambule)) {
                 readPcapFile(filename, cl);
             } else {
                 printError("Not supported file extension '" + ext + "'.");