@@ -91,12 +91,6 @@ public class PathAndFillManager extends DefaultHandler implements
9191
9292 static { net .imagej .patcher .LegacyInjector .preinit (); } // required for _every_ class that imports ij. classes
9393
94- protected static final int TRACES_FILE_TYPE_COMPRESSED_XML = 1 ;
95- protected static final int TRACES_FILE_TYPE_UNCOMPRESSED_XML = 2 ;
96- protected static final int TRACES_FILE_TYPE_SWC = 3 ;
97- protected static final int TRACES_FILE_TYPE_ML_JSON = 4 ;
98- protected static final int TRACES_FILE_TYPE_NDF = 5 ;
99- protected static final int TRACES_FILE_TYPE_NEUROLUCIDA = 6 ;
10094
10195 private static final DecimalFormat fileIndexFormatter = new DecimalFormat ("000" );
10296
@@ -2906,53 +2900,16 @@ public boolean importSWC(final String filePath,
29062900
29072901 }
29082902
2909- protected int guessTracesFileType (final String filename ) {
2903+ /** Detects the format of a file. Returns null (and reports an error) if the file could not be read */
2904+ protected IOUtils .ReconstructionFormat guessTracesFileType (final String filename ) {
29102905 try {
2911- return guessTracesFileType ( Files . newInputStream ( Paths . get (filename )), true );
2912- } catch (IOException e ) {
2906+ return IOUtils . detectFormat ( new File (filename ));
2907+ } catch (final IOException e ) {
29132908 errorStatic ("The file '" + filename + "' could not be parsed." );
2914- return - 1 ;
2909+ return null ;
29152910 }
29162911 }
29172912
2918- private int guessTracesFileType (InputStream is , final boolean closeStreamAfterGuess ) throws IOException {
2919- /*
2920- * Look at the magic bytes at the start of the file:
2921- *
2922- * If this looks as if it's gzip compressed, assume it's a compressed traces
2923- * file. If it begins "<?xml", check for Neurolucida's <mbf> root element;
2924- * otherwise assume it's an uncompressed traces file. If it begins with '{"'
2925- * assume it is a ML JSON file, otherwise assume it's an SWC file.
2926- */
2927- if (!headless )
2928- SNTUtils .log ("Guessing file type..." );
2929- if (is .markSupported ())
2930- is .mark (4096 );
2931- final byte [] buf = new byte [4096 ];
2932- final int bytesRead = is .read (buf , 0 , buf .length );
2933- if (closeStreamAfterGuess )
2934- is .close ();
2935- else if (is .markSupported ()) {
2936- is .reset ();
2937- }
2938- if (bytesRead < 2 ) return TRACES_FILE_TYPE_SWC ;
2939- if (buf [ 0 ] == (byte ) 0x1f && buf [ 1 ] == (byte ) 0x8b ) { //check if matches standard gzip magic number
2940- return TRACES_FILE_TYPE_COMPRESSED_XML ;
2941- } else if (bytesRead >= 6 && buf [0 ] == '<' && buf [1 ] == '?' && buf [2 ] == 'x' && buf [3 ] == 'm'
2942- && buf [4 ] == 'l' && buf [5 ] == ' ' ) {
2943- // XML file: distinguish SNT traces from Neurolucida by checking for <mbf> root
2944- final String header = new String (buf , 0 , bytesRead );
2945- if (header .contains ("<mbf" ))
2946- return TRACES_FILE_TYPE_NEUROLUCIDA ;
2947- return TRACES_FILE_TYPE_UNCOMPRESSED_XML ;
2948- } else if (((char ) (buf [0 ] & 0xFF ) == '{' )) {
2949- return TRACES_FILE_TYPE_ML_JSON ;
2950- } else if (((char ) (buf [0 ] & 0xFF ) == '/' )) {
2951- return TRACES_FILE_TYPE_NDF ;
2952- }
2953- return TRACES_FILE_TYPE_SWC ;
2954- }
2955-
29562913 protected boolean loadCompressedXML (final String filename ) {
29572914 try {
29582915 SNTUtils .log ("Loading gzipped file..." );
@@ -3052,25 +3009,29 @@ private boolean loadNeurolucida(final Object filenameOrInputStream) {
30523009 public boolean load (final String filePath , final int ... swcTypes ) {
30533010 final boolean existingEnableUiUpdates = enableUIupdates ;
30543011 enableUIupdates = false ;
3055- final int guessedType = guessTracesFileType (filePath );
3012+ final IOUtils .ReconstructionFormat guessedType = guessTracesFileType (filePath );
3013+ if (guessedType == null ) {
3014+ enableUIupdates = existingEnableUiUpdates ;
3015+ return false ;
3016+ }
30563017 boolean result ;
30573018 switch (guessedType ) {
3058- case TRACES_FILE_TYPE_COMPRESSED_XML :
3019+ case COMPRESSED_XML :
30593020 result = loadCompressedXML (filePath );
30603021 break ;
3061- case TRACES_FILE_TYPE_UNCOMPRESSED_XML :
3022+ case UNCOMPRESSED_XML :
30623023 result = loadUncompressedXML (filePath );
30633024 break ;
3064- case TRACES_FILE_TYPE_ML_JSON :
3025+ case ML_JSON :
30653026 result = loadJSON (filePath , swcTypes );
30663027 break ;
3067- case TRACES_FILE_TYPE_NDF :
3028+ case NDF :
30683029 result = loadNDF (filePath );
30693030 break ;
3070- case TRACES_FILE_TYPE_NEUROLUCIDA :
3031+ case NEUROLUCIDA :
30713032 result = loadNeurolucida (filePath );
30723033 break ;
3073- case TRACES_FILE_TYPE_SWC :
3034+ case SWC :
30743035 result = importSWC (filePath , false , 0 , 0 , 0 , 1 , 1 , 1 , 1 , true , swcTypes );
30753036 break ;
30763037 default :
@@ -3099,32 +3060,32 @@ public boolean loadGuessingType(final String filePathOrURL) throws IOException,
30993060
31003061 public boolean loadGuessingType (final String optionalDescription , final InputStream is ) throws IOException {
31013062 final BufferedInputStream bis = (is instanceof BufferedInputStream ) ? ((BufferedInputStream )is ) : new BufferedInputStream (is );
3102- final int guessedType = guessTracesFileType (bis , false );
3063+ final IOUtils . ReconstructionFormat guessedType = IOUtils . detectFormat (bis , false );
31033064 final boolean existingEnableUiUpdates = enableUIupdates ;
31043065 enableUIupdates = false ;
31053066 boolean result ;
31063067 switch (guessedType ) {
3107- case TRACES_FILE_TYPE_COMPRESSED_XML :
3068+ case COMPRESSED_XML :
31083069 SNTUtils .log ("Loading gzipped file..." );
31093070 result = load (new GZIPInputStream (bis ));
31103071 break ;
3111- case TRACES_FILE_TYPE_UNCOMPRESSED_XML :
3072+ case UNCOMPRESSED_XML :
31123073 SNTUtils .log ("Loading uncompressed file..." );
31133074 result = load (bis );
31143075 break ;
3115- case TRACES_FILE_TYPE_ML_JSON :
3076+ case ML_JSON :
31163077 final Map <String , TreeSet <SWCPoint >> nMap = MouseLightLoader .extractNodes (bis , "all" );
31173078 final Map <String , Tree > outMap = importNeurons (nMap , null , GuiUtils .micrometer ());
31183079 result = outMap .values ().stream ().anyMatch (tree -> tree != null && !tree .isEmpty ());
31193080 break ;
3120- case TRACES_FILE_TYPE_SWC :
3081+ case SWC :
31213082 final BufferedReader br = new BufferedReader (new InputStreamReader (bis , StandardCharsets .UTF_8 ));
31223083 result = importSWC (br , optionalDescription , false , 0 , 0 , 0 , 1 , 1 , 1 , 1 , true );
31233084 break ;
3124- case TRACES_FILE_TYPE_NDF :
3085+ case NDF :
31253086 result = loadNDF (bis );
31263087 break ;
3127- case TRACES_FILE_TYPE_NEUROLUCIDA :
3088+ case NEUROLUCIDA :
31283089 result = loadNeurolucida (bis );
31293090 break ;
31303091 default :
0 commit comments