RomLoader { public static RomData getROMFrom(final String url) throws IOException { final URI uri; try { uri = new URI(url); } catch (URISyntaxException ex) { throw new IOException("Error in URL '" + url + "\'", ex); } final String scheme = uri.getScheme(); final String userInfo = uri.getUserInfo(); final String name; final String password; if (userInfo != null) { final String[] splitted = userInfo.split("\\:", -1); name = splitted[0]; password = splitted[1]; } else { name = null; password = null; } final byte[] loaded; if (scheme.startsWith("http")) { loaded = loadHTTPArchive(url); } else if (scheme.startsWith("ftp")) { loaded = loadFTPArchive(uri.getHost(), uri.getPath(), name, password); } else { throw new IllegalArgumentException("Unsupported scheme [" + scheme + ']'); } final ZipArchiveInputStream in = new ZipArchiveInputStream(new ByteArrayInputStream(loaded)); byte[] rom48 = null; byte[] rom128 = null; byte[] romTrDos = null; while (!Thread.currentThread().isInterrupted()) { final ZipArchiveEntry entry = in.getNextZipEntry(); if (entry == null) { break; } if (entry.isDirectory()) { continue; } if (ROM_48.equalsIgnoreCase(entry.getName())) { final int size = (int) entry.getSize(); if (size > 16384) { throw new IOException("ROM 48 has too big size"); } rom48 = new byte[16384]; IOUtils.readFully(in, rom48, 0, size); } else if (ROM_128TR.equalsIgnoreCase(entry.getName())) { final int size = (int) entry.getSize(); if (size > 16384) { throw new IOException("ROM 128TR has too big size"); } rom128 = new byte[16384]; IOUtils.readFully(in, rom128, 0, size); } else if (ROM_TRDOS.equalsIgnoreCase(entry.getName())) { final int size = (int) entry.getSize(); if (size > 16384) { throw new IOException("ROM TRDOS has too big size"); } romTrDos = new byte[16384]; IOUtils.readFully(in, romTrDos, 0, size); } } if (rom48 == null) { throw new IOException(ROM_48 + " not found"); } if (rom128 == null) { throw new IOException(ROM_128TR + " not found"); } if (romTrDos == null) { throw new IOException(ROM_TRDOS + " not found"); } return new RomData(url, rom128, rom48, romTrDos); } RomLoader(); static RomData getROMFrom(final String url);  }
@Test public void testLoadAndExtractROMFromArchiveVTRD() throws Exception { final RomData data = RomLoader.getROMFrom("http: assertEquals(0x4000*3,data.getAsArray().length); assertEquals("48.rom",0xAF,data.getAsArray()[0x01] & 0xFF); assertEquals("128tr.rom",0x01,data.getAsArray()[0x4001] & 0xFF); assertEquals("trdos.rom",0x11,data.getAsArray()[0x8001] & 0xFF); assertEquals(0xFFFF, data.getMask()); }
@Test public void testLoadAndExtractROMFromArchiveWOS() throws Exception { final RomData data = RomLoader.getROMFrom("ftp: assertEquals(0x4000*3,data.getAsArray().length); assertEquals("48.rom",0xAF,data.getAsArray()[0x01] & 0xFF); assertEquals("128tr.rom",0x01,data.getAsArray()[0x4001] & 0xFF); assertEquals("trdos.rom",0x11,data.getAsArray()[0x8001] & 0xFF); assertEquals(0xFFFF, data.getMask()); }