fix file type detection for SCV files

This commit is contained in:
Eyck Jentzsch 2022-05-17 16:40:56 +02:00
parent d5e50a06b2
commit 8d94b517c1
5 changed files with 60 additions and 111 deletions

View File

@ -78,24 +78,24 @@ public class SQLiteDbLoader implements IWaveformDbLoader {
return streams;
}
@Override
public boolean canLoad(File inputFile) {
if (!inputFile.isDirectory() && inputFile.exists()) {
try(InputStream stream = new FileInputStream(inputFile)){
byte[] buffer = new byte[x.length];
int readCnt = stream.read(buffer, 0, x.length);
if (readCnt == x.length) {
for (int i = 0; i < x.length; i++)
if (buffer[i] != x[i])
return false;
}
return true;
} catch (Exception e) {
return false;
}
}
return false;
}
// @Override
// public boolean canLoad(File inputFile) {
// if (!inputFile.isDirectory() && inputFile.exists()) {
// try(InputStream stream = new FileInputStream(inputFile)){
// byte[] buffer = new byte[x.length];
// int readCnt = stream.read(buffer, 0, x.length);
// if (readCnt == x.length) {
// for (int i = 0; i < x.length; i++)
// if (buffer[i] != x[i])
// return false;
// }
// return true;
// } catch (Exception e) {
// return false;
// }
// }
// return false;
// }
@Override
public void load(IWaveformDb db, File file) throws InputFormatException {

View File

@ -185,33 +185,6 @@ public class TextDbLoader implements IWaveformDbLoader {
return relationTypes.values();
}
/**
* Can load.
*
* @param inputFile the input file
* @return true, if successful
*/
@Override
public boolean canLoad(File inputFile) {
if (!inputFile.isDirectory() && inputFile.exists()) {
FileType fType = getFileType(inputFile);
try(InputStream stream = fType==FileType.GZIP ? new GZIPInputStream(new FileInputStream(inputFile)) :
fType==FileType.LZ4? new FramedLZ4CompressorInputStream(new FileInputStream(inputFile)) : new FileInputStream(inputFile)){
byte[] buffer = new byte[x.length];
int readCnt = stream.read(buffer, 0, x.length);
if (readCnt == x.length) {
for (int i = 0; i < x.length; i++)
if (buffer[i] != x[i])
return false;
}
return true;
} catch (Exception e) {
return false;
}
}
return false;
}
/**
* Checks if is gzipped.
*
@ -222,9 +195,15 @@ public class TextDbLoader implements IWaveformDbLoader {
try (InputStream is = new FileInputStream(f)) {
byte[] signature = new byte[4];
int nread = is.read(signature); // read the gzip signature
if(nread > 2 && signature[0] == (byte) 0x1f && signature[1] == (byte) 0x8b)
if(nread >= 2 &&
signature[0] == (byte) 0x1f &&
signature[1] == (byte) 0x8b)
return FileType.GZIP;
else if(nread>4 && signature[0] == (byte) 0x04 && signature[1] == (byte) 0x22 && signature[2] == (byte) 0x4d && signature[3] == (byte) 0x18)
else if(nread>=4 &&
signature[0] == (byte) 0x04 &&
signature[1] == (byte) 0x22 &&
signature[2] == (byte) 0x4d &&
signature[3] == (byte) 0x18)
return FileType.LZ4;
else
return FileType.PLAIN;
@ -268,10 +247,11 @@ public class TextDbLoader implements IWaveformDbLoader {
parser.txSink = mapDb.hashMap("transactions", Serializer.LONG, Serializer.JAVA).create();
InputStream is = new BufferedInputStream(new FileInputStream(file));
parser.parseInput(fType==FileType.GZIP ? new GZIPInputStream(is) : fType==FileType.LZ4? new FramedLZ4CompressorInputStream(is) : is);
transactions = parser.txSink;
} catch (IllegalArgumentException | ArrayIndexOutOfBoundsException e) {
} catch (Exception e) {
throw new InputFormatException(e.toString());
} finally {
transactions = parser.txSink;
}
txStreams.values().parallelStream().forEach(TxStream::calculateConcurrency);
}
@ -351,7 +331,8 @@ public class TextDbLoader implements IWaveformDbLoader {
* @throws IOException Signals that an I/O exception has occurred.
* @throws InputFormatException Signals that the input format is wrong
*/
void parseInput(InputStream inputStream) throws IOException, InputFormatException {
void parseInput(InputStream inputStream) throws InputFormatException {
try {
reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
String curLine = reader.readLine();
String nextLine = null;
@ -365,6 +346,7 @@ public class TextDbLoader implements IWaveformDbLoader {
scvTx.endTime=loader.maxTime;
txSink.put(e.getKey(), scvTx);
}
} catch(IOException e) {}
}
/**

View File

@ -19,6 +19,7 @@ import java.util.zip.GZIPInputStream;
import com.minres.scviewer.database.IWaveformDbLoader;
import com.minres.scviewer.database.IWaveformDbLoaderFactory;
import com.minres.scviewer.database.text.TextDbLoader.FileType;
/**
* The Class TextDbLoader.
@ -52,21 +53,22 @@ public class TextDbLoaderFactory implements IWaveformDbLoaderFactory {
*/
@Override
public boolean canLoad(File inputFile) {
if (!inputFile.isDirectory() && inputFile.exists()) {
boolean gzipped = isGzipped(inputFile);
try(InputStream stream = gzipped ? new GZIPInputStream(new FileInputStream(inputFile)) : new FileInputStream(inputFile)){
byte[] buffer = new byte[x.length];
int readCnt = stream.read(buffer, 0, x.length);
if (readCnt == x.length) {
for (int i = 0; i < x.length; i++)
if (buffer[i] != x[i])
return false;
}
try (InputStream is = new FileInputStream(inputFile)) {
byte[] signature = new byte[4];
int nread = is.read(signature); // read the gzip signature
if(nread >= 2 &&
signature[0] == (byte) 0x1f &&
signature[1] == (byte) 0x8b)
return true;
} catch (Exception e) {
return false;
}
}
else if(nread>=4 &&
signature[0] == (byte) 0x04 &&
signature[1] == (byte) 0x22 &&
signature[2] == (byte) 0x4d &&
signature[3] == (byte) 0x18)
return true;
else
return true;
} catch (IOException e) {}
return false;
}

View File

@ -66,34 +66,6 @@ public class VCDDbLoader implements IWaveformDbLoader, IVCDDatabaseBuilder {
}
/**
* Can load.
*
* @param inputFile the input file
* @return true, if successful
*/
@Override
public boolean canLoad(File inputFile) {
if(!inputFile.isDirectory() || inputFile.exists()) {
String name = inputFile.getName();
if(!(name.endsWith(".vcd") ||
name.endsWith(".vcdz") ||
name.endsWith(".vcdgz") ||
name.endsWith(".vcd.gz")) )
return false;
boolean gzipped = isGzipped(inputFile);
try(InputStream stream = gzipped ? new GZIPInputStream(new FileInputStream(inputFile)) : new FileInputStream(inputFile)){
byte[] buffer = new byte[8];
if (stream.read(buffer, 0, buffer.length) == buffer.length) {
return buffer[0]=='$';
}
} catch (Exception e) {
return false;
}
}
return false;
}
/* (non-Javadoc)
* @see com.minres.scviewer.database.ITrDb#load(java.io.File)
*/

View File

@ -48,13 +48,6 @@ public interface IWaveformDbLoader {
*/
public void removePropertyChangeListener(PropertyChangeListener l) ;
/**
* Can load the given file.
*
* @param inputFile the input file
* @return true, if successful
*/
public boolean canLoad(File inputFile);
/**
* Load.
*