Map ret = null; TileSet set = new TileSet(); int major, minor; major = in.read(); minor = in.read(); in.skip(2); ret = new Map(Util.readShort(in), Util.readShort(in)); ret.setOrientation(Map.MDO_ORTHO); ret.addProperty("(s)fmap reader","Don't modify properties marked (s) unless you really know what you're doing."); ret.addProperty("version",""+major+"."+minor); in.skip(4); twidth = Util.readShort(in); theight = Util.readShort(in); set.setStandardWidth(twidth); set.setStandardHeight(theight); ret.setTileWidth(twidth); ret.setTileHeight(theight); set.setName("Static tiles"); ret.addTileset(set); int depth = Util.readShort(in); if(depth<16) { throw new IOException("Tile bitdepths less than 16 are not supported!"); } ret.addProperty("(s)depth",""+depth); in.skip(2); int numBlocks = Util.readShort(in); int numBlocksGfx = Util.readShort(in); Chunk c = findChunk("BKDT"); if(c == null) { throw new IOException("No BKDT block found!"); } MapLayer ml = new TileLayer(ret, ret.getWidth(),ret.getHeight()); ml.setName("bg"); ret.addLayer(ml); for(int i=1;i<8;i++) {  ml = new ObjectGroup(ret, 0, 0); ml.setName("ObjectGroup "+i); ret.addLayer(ml); } ml = new TileLayer(ret, ret.getWidth(),ret.getHeight()); ml.setName("fg 1"); ret.addLayer(ml); ml = new TileLayer(ret, ret.getWidth(),ret.getHeight()); ml.setName("fg 2"); ret.addLayer(ml); ml = new TileLayer(ret, ret.getWidth(),ret.getHeight()); ml.setName("fg 3"); ret.addLayer(ml);  readBKDTChunk(ret, c.getInputStream(), numBlocks);  c = findChunk("BGFX"); if(c != null) {
Map ret = null; TileSet set = new TileSet(); int major, minor; major = in.read(); minor = in.read(); in.skip(2); ret = new Map(Util.readShort(in), Util.readShort(in)); Properties retProps = ret.getProperties(); ret.setOrientation(Map.MDO_ORTHO); retProps.setProperty("(s)fmap reader", "Don't modify properties marked (s) unless you really know what you're doing."); retProps.setProperty("version", "" + major + "." + minor); in.skip(4); twidth = Util.readShort(in); theight = Util.readShort(in); set.setStandardWidth(twidth); set.setStandardHeight(theight); ret.setTileWidth(twidth); ret.setTileHeight(theight); set.setName("Static tiles"); ret.addTileset(set); int depth = Util.readShort(in); if (depth < 16) { throw new IOException("Tile bitdepths less than 16 are not supported!"); } retProps.setProperty("(s)depth", "" + depth); in.skip(2); int numBlocks = Util.readShort(in); int numBlocksGfx = Util.readShort(in); Chunk c = findChunk("BKDT"); if(c == null) { throw new IOException("No BKDT block found!"); } MapLayer ml = new TileLayer(ret, ret.getWidth(),ret.getHeight()); ml.setName("bg"); ret.addLayer(ml); for(int i=1;i<8;i++) {  ml = new ObjectGroup(ret, 0, 0); ml.setName("ObjectGroup "+i); ret.addLayer(ml); } ml = new TileLayer(ret, ret.getWidth(),ret.getHeight()); ml.setName("fg 1"); ret.addLayer(ml); ml = new TileLayer(ret, ret.getWidth(),ret.getHeight()); ml.setName("fg 2"); ret.addLayer(ml); ml = new TileLayer(ret, ret.getWidth(),ret.getHeight()); ml.setName("fg 3"); ret.addLayer(ml);  readBKDTChunk(ret, c.getInputStream(), numBlocks);  c = findChunk("BGFX"); if(c != null) {
    private Map readMPHDChunk(InputStream in) throws IOException {		Map ret = null;		TileSet set = new TileSet();		int major, minor;		major = in.read();		minor = in.read();		in.skip(2);	//skip lsb and reserved bytes - always msb		ret = new Map(Util.readShort(in), Util.readShort(in));		ret.setOrientation(Map.MDO_ORTHO);        //be sure to set the orientation!		ret.addProperty("(s)fmap reader","Don't modify properties marked (s) unless you really know what you're doing.");		ret.addProperty("version",""+major+"."+minor);		in.skip(4);	//reserved		twidth = Util.readShort(in);		theight = Util.readShort(in);		set.setStandardWidth(twidth);		set.setStandardHeight(theight);		ret.setTileWidth(twidth);		ret.setTileHeight(theight);		set.setName("Static tiles");		ret.addTileset(set);		int depth = Util.readShort(in);		if(depth<16) {			throw new IOException("Tile bitdepths less than 16 are not supported!");		}		ret.addProperty("(s)depth",""+depth);		in.skip(2);		int numBlocks = Util.readShort(in);		int numBlocksGfx = Util.readShort(in);		Chunk c = findChunk("BKDT");		if(c == null) {			throw new IOException("No BKDT block found!");		}		MapLayer ml = new TileLayer(ret, ret.getWidth(),ret.getHeight());		ml.setName("bg");		ret.addLayer(ml);		for(int i=1;i<8;i++) {			//TODO: I believe this should be ObjectGroup			ml = new ObjectGroup(ret, 0, 0);			ml.setName("ObjectGroup "+i);			ret.addLayer(ml);		}		ml = new TileLayer(ret, ret.getWidth(),ret.getHeight());		ml.setName("fg 1");		ret.addLayer(ml);		ml = new TileLayer(ret, ret.getWidth(),ret.getHeight());		ml.setName("fg 2");		ret.addLayer(ml);		ml = new TileLayer(ret, ret.getWidth(),ret.getHeight());		ml.setName("fg 3");		ret.addLayer(ml);						readBKDTChunk(ret, c.getInputStream(), numBlocks);			c = findChunk("BGFX");		if(c != null) {            readBGFXChunk(ret, c.getInputStream(), numBlocksGfx);        }else{            throw new IOException("No BGFX chunk found!");        }			System.out.println(ret.toString());		return ret;    }