Merge commit '42b2e5ca519766e37ce6941ba4faecc9691cc403' into upstream
[debian/openrocket] / core / src / net / sf / openrocket / file / GeneralRocketLoader.java
index 68555d1052b26b6d2ebc5c30cc361767ac955518..da6858d89cb912f9e424a172c79ccc6b7cbc71d9 100644 (file)
@@ -12,6 +12,8 @@ import java.util.zip.ZipInputStream;
 import net.sf.openrocket.document.OpenRocketDocument;
 import net.sf.openrocket.file.openrocket.importt.OpenRocketLoader;
 import net.sf.openrocket.file.rocksim.importt.RocksimLoader;
+import net.sf.openrocket.util.ArrayUtils;
+import net.sf.openrocket.util.TextUtil;
 
 
 /**
@@ -26,16 +28,16 @@ public class GeneralRocketLoader extends AbstractRocketLoader {
        private static final int READ_BYTES = 300;
        
        private static final byte[] GZIP_SIGNATURE = { 31, -117 }; // 0x1f, 0x8b
-       private static final byte[] ZIP_SIGNATURE = "PK".getBytes(Charset.forName("US-ASCII"));
-       private static final byte[] OPENROCKET_SIGNATURE = "<openrocket".getBytes(Charset.forName("US-ASCII"));
-       private static final byte[] ROCKSIM_SIGNATURE = "<RockSimDoc".getBytes(Charset.forName("US-ASCII"));
+       private static final byte[] ZIP_SIGNATURE = TextUtil.convertStringToBytes("PK",Charset.forName("US-ASCII"));
+       private static final byte[] OPENROCKET_SIGNATURE = TextUtil.convertStringToBytes("<openrocket",Charset.forName("US-ASCII"));
+       private static final byte[] ROCKSIM_SIGNATURE = TextUtil.convertStringToBytes("<RockSimDoc",Charset.forName("US-ASCII"));
        
        private final OpenRocketLoader openRocketLoader = new OpenRocketLoader();
        
        private final RocksimLoader rocksimLoader = new RocksimLoader();
        
        @Override
-       protected OpenRocketDocument loadFromStream(InputStream source) throws IOException,
+       protected OpenRocketDocument loadFromStream(InputStream source, MotorFinder motorFinder) throws IOException,
                        RocketLoadException {
                
                // Check for mark() support
@@ -58,7 +60,7 @@ public class GeneralRocketLoader extends AbstractRocketLoader {
                
                // Check for GZIP
                if (buffer[0] == GZIP_SIGNATURE[0] && buffer[1] == GZIP_SIGNATURE[1]) {
-                       OpenRocketDocument doc = loadFromStream(new GZIPInputStream(source));
+                       OpenRocketDocument doc = loadFromStream(new GZIPInputStream(source), motorFinder);
                        doc.getDefaultStorageOptions().setCompressionEnabled(true);
                        return doc;
                }
@@ -73,9 +75,12 @@ public class GeneralRocketLoader extends AbstractRocketLoader {
                                        throw new RocketLoadException("Unsupported or corrupt file.");
                                }
                                if (entry.getName().matches(".*\\.[oO][rR][kK]$")) {
-                                       OpenRocketDocument doc = loadFromStream(in);
+                                       OpenRocketDocument doc = loadFromStream(in, motorFinder);
                                        doc.getDefaultStorageOptions().setCompressionEnabled(true);
                                        return doc;
+                               } else if ( entry.getName().matches(".*\\.[rR][kK][tT]$")) {
+                                       OpenRocketDocument doc = loadFromStream(in, motorFinder);
+                                       return doc;
                                }
                        }
                }
@@ -86,24 +91,24 @@ public class GeneralRocketLoader extends AbstractRocketLoader {
                        if (buffer[i] == OPENROCKET_SIGNATURE[match]) {
                                match++;
                                if (match == OPENROCKET_SIGNATURE.length) {
-                                       return loadUsing(source, openRocketLoader);
+                                       return loadUsing(source, openRocketLoader, motorFinder);
                                }
                        } else {
                                match = 0;
                        }
                }
                
-               byte[] typeIdentifier = Arrays.copyOf(buffer, ROCKSIM_SIGNATURE.length);
+               byte[] typeIdentifier = ArrayUtils.copyOf(buffer, ROCKSIM_SIGNATURE.length);
                if (Arrays.equals(ROCKSIM_SIGNATURE, typeIdentifier)) {
-                       return loadUsing(source, rocksimLoader);
+                       return loadUsing(source, rocksimLoader, motorFinder);
                }
                throw new RocketLoadException("Unsupported or corrupt file.");
        }
        
-       private OpenRocketDocument loadUsing(InputStream source, RocketLoader loader)
+       private OpenRocketDocument loadUsing(InputStream source, RocketLoader loader, MotorFinder motorFinder)
                        throws RocketLoadException {
                warnings.clear();
-               OpenRocketDocument doc = loader.load(source);
+               OpenRocketDocument doc = loader.load(source, motorFinder);
                warnings.addAll(loader.getWarnings());
                return doc;
        }