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

View File

@ -185,33 +185,6 @@ public class TextDbLoader implements IWaveformDbLoader {
return relationTypes.values(); 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. * Checks if is gzipped.
* *
@ -222,9 +195,15 @@ public class TextDbLoader implements IWaveformDbLoader {
try (InputStream is = new FileInputStream(f)) { try (InputStream is = new FileInputStream(f)) {
byte[] signature = new byte[4]; byte[] signature = new byte[4];
int nread = is.read(signature); // read the gzip signature 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; 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; return FileType.LZ4;
else else
return FileType.PLAIN; return FileType.PLAIN;
@ -268,10 +247,11 @@ public class TextDbLoader implements IWaveformDbLoader {
parser.txSink = mapDb.hashMap("transactions", Serializer.LONG, Serializer.JAVA).create(); parser.txSink = mapDb.hashMap("transactions", Serializer.LONG, Serializer.JAVA).create();
InputStream is = new BufferedInputStream(new FileInputStream(file)); InputStream is = new BufferedInputStream(new FileInputStream(file));
parser.parseInput(fType==FileType.GZIP ? new GZIPInputStream(is) : fType==FileType.LZ4? new FramedLZ4CompressorInputStream(is) : is); parser.parseInput(fType==FileType.GZIP ? new GZIPInputStream(is) : fType==FileType.LZ4? new FramedLZ4CompressorInputStream(is) : is);
transactions = parser.txSink;
} catch (IllegalArgumentException | ArrayIndexOutOfBoundsException e) { } catch (IllegalArgumentException | ArrayIndexOutOfBoundsException e) {
} catch (Exception e) { } catch (Exception e) {
throw new InputFormatException(e.toString()); throw new InputFormatException(e.toString());
} finally {
transactions = parser.txSink;
} }
txStreams.values().parallelStream().forEach(TxStream::calculateConcurrency); txStreams.values().parallelStream().forEach(TxStream::calculateConcurrency);
} }
@ -351,20 +331,22 @@ public class TextDbLoader implements IWaveformDbLoader {
* @throws IOException Signals that an I/O exception has occurred. * @throws IOException Signals that an I/O exception has occurred.
* @throws InputFormatException Signals that the input format is wrong * @throws InputFormatException Signals that the input format is wrong
*/ */
void parseInput(InputStream inputStream) throws IOException, InputFormatException { void parseInput(InputStream inputStream) throws InputFormatException {
reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); try {
String curLine = reader.readLine(); reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
String nextLine = null; String curLine = reader.readLine();
while ((nextLine = reader.readLine()) != null && curLine != null) { String nextLine = null;
curLine = parseLine(curLine, nextLine, false); while ((nextLine = reader.readLine()) != null && curLine != null) {
} curLine = parseLine(curLine, nextLine, false);
if (curLine != null) }
parseLine(curLine, nextLine, true); if (curLine != null)
for(Entry<Long, ScvTx> e: transactionById.entrySet()) { parseLine(curLine, nextLine, true);
ScvTx scvTx = e.getValue(); for(Entry<Long, ScvTx> e: transactionById.entrySet()) {
scvTx.endTime=loader.maxTime; ScvTx scvTx = e.getValue();
txSink.put(e.getKey(), scvTx); 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.IWaveformDbLoader;
import com.minres.scviewer.database.IWaveformDbLoaderFactory; import com.minres.scviewer.database.IWaveformDbLoaderFactory;
import com.minres.scviewer.database.text.TextDbLoader.FileType;
/** /**
* The Class TextDbLoader. * The Class TextDbLoader.
@ -52,21 +53,22 @@ public class TextDbLoaderFactory implements IWaveformDbLoaderFactory {
*/ */
@Override @Override
public boolean canLoad(File inputFile) { public boolean canLoad(File inputFile) {
if (!inputFile.isDirectory() && inputFile.exists()) { try (InputStream is = new FileInputStream(inputFile)) {
boolean gzipped = isGzipped(inputFile); byte[] signature = new byte[4];
try(InputStream stream = gzipped ? new GZIPInputStream(new FileInputStream(inputFile)) : new FileInputStream(inputFile)){ int nread = is.read(signature); // read the gzip signature
byte[] buffer = new byte[x.length]; if(nread >= 2 &&
int readCnt = stream.read(buffer, 0, x.length); signature[0] == (byte) 0x1f &&
if (readCnt == x.length) { signature[1] == (byte) 0x8b)
for (int i = 0; i < x.length; i++)
if (buffer[i] != x[i])
return false;
}
return true; return true;
} catch (Exception e) { else if(nread>=4 &&
return false; 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; 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) /* (non-Javadoc)
* @see com.minres.scviewer.database.ITrDb#load(java.io.File) * @see com.minres.scviewer.database.ITrDb#load(java.io.File)
*/ */

View File

@ -48,13 +48,6 @@ public interface IWaveformDbLoader {
*/ */
public void removePropertyChangeListener(PropertyChangeListener l) ; 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. * Load.
* *