1 package net.sf.openrocket.file;
3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.zip.GZIPInputStream;
8 import net.sf.openrocket.document.OpenRocketDocument;
12 * A rocket loader that auto-detects the document type and uses the appropriate
13 * loading. Supports loading of GZIPed files as well with transparent
16 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
18 public class GeneralRocketLoader extends RocketLoader {
20 private static final int READ_BYTES = 300;
22 private static final byte[] GZIP_SIGNATURE = { 31, -117 }; // 0x1f, 0x8b
23 private static final byte[] OPENROCKET_SIGNATURE = "<openrocket".getBytes();
25 private static final OpenRocketLoader openRocketLoader = new OpenRocketLoader();
28 protected OpenRocketDocument loadFromStream(InputStream source) throws IOException,
31 // Check for mark() support
32 if (!source.markSupported()) {
33 source = new BufferedInputStream(source);
37 byte[] buffer = new byte[READ_BYTES];
39 source.mark(READ_BYTES + 10);
40 count = source.read(buffer);
44 throw new RocketLoadException("Unsupported or corrupt file.");
47 // Detect the appropriate loader
50 if (buffer[0] == GZIP_SIGNATURE[0] && buffer[1] == GZIP_SIGNATURE[1]) {
51 OpenRocketDocument doc = loadFromStream(new GZIPInputStream(source));
52 doc.getDefaultStorageOptions().setCompressionEnabled(true);
56 // Check for OpenRocket
58 for (int i=0; i < count; i++) {
59 if (buffer[i] == OPENROCKET_SIGNATURE[match]) {
61 if (match == OPENROCKET_SIGNATURE.length) {
62 return openRocketLoader.load(source);
69 throw new RocketLoadException("Unsupported or corrupt file.");