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;
9 import net.sf.openrocket.document.OpenRocketDocument;
13 * A rocket loader that auto-detects the document type and uses the appropriate
14 * loading. Supports loading of GZIPed files as well with transparent
17 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
19 public class GeneralRocketLoader extends RocketLoader {
21 private static final int READ_BYTES = 300;
23 private static final byte[] GZIP_SIGNATURE = { 31, -117 }; // 0x1f, 0x8b
24 private static final byte[] OPENROCKET_SIGNATURE =
25 "<openrocket".getBytes(Charset.forName("US-ASCII"));
27 private final OpenRocketLoader openRocketLoader = new OpenRocketLoader();
30 protected OpenRocketDocument loadFromStream(InputStream source) throws IOException,
33 // Check for mark() support
34 if (!source.markSupported()) {
35 source = new BufferedInputStream(source);
39 byte[] buffer = new byte[READ_BYTES];
41 source.mark(READ_BYTES + 10);
42 count = source.read(buffer);
46 throw new RocketLoadException("Unsupported or corrupt file.");
49 // Detect the appropriate loader
52 if (buffer[0] == GZIP_SIGNATURE[0] && buffer[1] == GZIP_SIGNATURE[1]) {
53 OpenRocketDocument doc = loadFromStream(new GZIPInputStream(source));
54 doc.getDefaultStorageOptions().setCompressionEnabled(true);
58 // Check for OpenRocket
60 for (int i=0; i < count; i++) {
61 if (buffer[i] == OPENROCKET_SIGNATURE[match]) {
63 if (match == OPENROCKET_SIGNATURE.length) {
64 return loadUsing(source, openRocketLoader);
71 throw new RocketLoadException("Unsupported or corrupt file.");
74 private OpenRocketDocument loadUsing(InputStream source, RocketLoader loader)
75 throws RocketLoadException {
77 OpenRocketDocument doc = loader.load(source);
78 warnings.addAll(loader.getWarnings());