1 package net.sf.openrocket.file;
3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.nio.charset.Charset;
7 import java.util.zip.GZIPInputStream;
8 import java.util.Arrays;
10 import net.sf.openrocket.document.OpenRocketDocument;
11 import net.sf.openrocket.file.openrocket.OpenRocketLoader;
12 import net.sf.openrocket.file.rocksim.RocksimLoader;
16 * A rocket loader that auto-detects the document type and uses the appropriate
17 * loading. Supports loading of GZIPed files as well with transparent
20 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
22 public class GeneralRocketLoader extends RocketLoader {
24 private static final int READ_BYTES = 300;
26 private static final byte[] GZIP_SIGNATURE = { 31, -117 }; // 0x1f, 0x8b
27 private static final byte[] OPENROCKET_SIGNATURE =
28 "<openrocket".getBytes(Charset.forName("US-ASCII"));
29 private static final byte[] ROCKSIM_SIGNATURE =
30 "<RockSimDoc".getBytes(Charset.forName("US-ASCII"));
32 private final OpenRocketLoader openRocketLoader = new OpenRocketLoader();
34 private final RocksimLoader rocksimLoader = new RocksimLoader();
37 protected OpenRocketDocument loadFromStream(InputStream source) throws IOException,
40 // Check for mark() support
41 if (!source.markSupported()) {
42 source = new BufferedInputStream(source);
46 byte[] buffer = new byte[READ_BYTES];
48 source.mark(READ_BYTES + 10);
49 count = source.read(buffer);
53 throw new RocketLoadException("Unsupported or corrupt file.");
56 // Detect the appropriate loader
59 if (buffer[0] == GZIP_SIGNATURE[0] && buffer[1] == GZIP_SIGNATURE[1]) {
60 OpenRocketDocument doc = loadFromStream(new GZIPInputStream(source));
61 doc.getDefaultStorageOptions().setCompressionEnabled(true);
65 // Check for OpenRocket
67 for (int i=0; i < count; i++) {
68 if (buffer[i] == OPENROCKET_SIGNATURE[match]) {
70 if (match == OPENROCKET_SIGNATURE.length) {
71 return loadUsing(source, openRocketLoader);
78 byte[] typeIdentifier = Arrays.copyOf(buffer, ROCKSIM_SIGNATURE.length);
79 if (Arrays.equals(ROCKSIM_SIGNATURE, typeIdentifier)) {
80 return loadUsing(source, rocksimLoader);
82 throw new RocketLoadException("Unsupported or corrupt file.");
85 private OpenRocketDocument loadUsing(InputStream source, RocketLoader loader)
86 throws RocketLoadException {
88 OpenRocketDocument doc = loader.load(source);
89 warnings.addAll(loader.getWarnings());