use of ru.playsoftware.j2meloader.util.ConverterException in project J2ME-Loader by nikita36078.
the class AppInstaller method loadInfo.
/**
* Load and check app info from source
*/
void loadInfo(SingleEmitter<Integer> emitter) throws IOException, ConverterException {
boolean isLocal;
boolean isContentUri = uri.getScheme().equals("content");
if ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme())) {
downloadJad();
isLocal = false;
} else {
srcFile = FileUtils.getFileForUri(context, uri);
isLocal = true;
}
String name = srcFile.getName();
if (name.toLowerCase().endsWith(".jad")) {
newDesc = new Descriptor(srcFile, true);
String url = newDesc.getJarUrl();
if (url == null) {
throw new ConverterException("Jad not have " + Descriptor.MIDLET_JAR_URL);
}
Uri uri = Uri.parse(url);
String scheme = uri.getScheme();
String host = uri.getHost();
if (isLocal && scheme == null && host == null) {
if (isContentUri) {
emitter.onSuccess(STATUS_NEED_JAD);
return;
} else if (!checkJarFile(srcFile)) {
emitter.onSuccess(STATUS_UNMATCHED);
return;
}
}
} else if (name.toLowerCase().endsWith(".kjx")) {
/* Load kjx file */
parseKjx();
newDesc = new Descriptor(srcFile, true);
} else {
srcJar = srcFile;
newDesc = loadManifest(srcFile);
}
int result = checkDescriptor();
emitter.onSuccess(result);
}
use of ru.playsoftware.j2meloader.util.ConverterException in project J2ME-Loader by nikita36078.
the class AppInstaller method checkJarFile.
/**
* return true if JAR exists and matches JAD *
*/
private boolean checkJarFile(File jad) throws IOException, ConverterException {
File dir = jad.getParentFile();
String jarUrl = newDesc.getJarUrl();
File jar = new File(dir, jarUrl);
if (!jar.exists()) {
String name = jad.getName();
jar = new File(dir, name.substring(0, name.length() - 4) + ".jar");
if (!jar.exists()) {
throw new ConverterException("Jar-file not found for url: " + jarUrl);
}
}
srcJar = jar;
manifest = loadManifest(jar);
return manifest.equals(newDesc);
}
use of ru.playsoftware.j2meloader.util.ConverterException in project J2ME-Loader by nikita36078.
the class AppInstaller method parseKjx.
private void parseKjx() throws ConverterException {
if (!cacheDir.exists() && !cacheDir.mkdirs()) {
throw new ConverterException("Can't create cache dir");
}
File kjxFile = srcFile;
File jadFile = null;
File jarFile = null;
try (InputStream inputStream = new FileInputStream(kjxFile);
DataInputStream dis = new DataInputStream(inputStream)) {
byte[] magic = new byte[3];
dis.read(magic, 0, 3);
if (!Arrays.equals(magic, "KJX".getBytes())) {
throw new ConverterException("Magic KJX does not match: " + new String(magic));
}
byte startJadPos = dis.readByte();
byte lenKjxFileName = dis.readByte();
dis.skipBytes(lenKjxFileName);
int lenJadFileContent = dis.readUnsignedShort();
byte lenJadFileName = dis.readByte();
byte[] jadFileName = new byte[lenJadFileName];
dis.read(jadFileName, 0, lenJadFileName);
String strJadFileName = new String(jadFileName);
int bufSize = 2048;
byte[] buf = new byte[bufSize];
jadFile = new File(cacheDir, strJadFileName);
try (FileOutputStream fos = new FileOutputStream(jadFile)) {
int restSize = lenJadFileContent;
while (restSize > 0) {
int readSize = dis.read(buf, 0, Math.min(restSize, bufSize));
fos.write(buf, 0, readSize);
restSize -= readSize;
}
}
jarFile = new File(cacheDir, strJadFileName.substring(0, strJadFileName.length() - 4) + ".jar");
try (FileOutputStream fos = new FileOutputStream(jarFile)) {
int length = 0;
while ((length = dis.read(buf)) > 0) {
fos.write(buf, 0, length);
}
}
srcFile = jadFile;
srcJar = jarFile;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
use of ru.playsoftware.j2meloader.util.ConverterException in project J2ME-Loader by nikita36078.
the class AppInstaller method install.
/**
* Install app
*/
void install(SingleEmitter<AppItem> emitter) throws ConverterException, IOException {
if (!cacheDir.exists() && !cacheDir.mkdirs()) {
throw new ConverterException("Can't create cache dir");
}
tmpDir = new File(targetDir.getParent(), ".tmp");
if (!tmpDir.isDirectory() && !tmpDir.mkdirs())
throw new ConverterException("Can't create directory: '" + targetDir + "'");
if (srcJar == null) {
srcJar = new File(cacheDir, "tmp.jar");
downloadJar();
manifest = loadManifest(srcJar);
if (!manifest.equals(newDesc)) {
throw new ConverterException("*Jad not matches with Jar");
}
}
File patchedJar = new File(cacheDir, "patched.jar");
AndroidProducer.processJar(srcJar, patchedJar);
try {
Main.main(new String[] { "--no-optimize", "--output=" + tmpDir + Config.MIDLET_DEX_FILE, patchedJar.getAbsolutePath() });
} catch (Throwable e) {
throw new ConverterException("Dexing error", e);
}
if (manifest != null) {
manifest.merge(newDesc);
newDesc = manifest;
}
File resJar = new File(tmpDir, Config.MIDLET_RES_FILE);
FileUtils.copyFileUsingChannel(srcJar, resJar);
String icon = newDesc.getIcon();
File iconFile = new File(tmpDir, Config.MIDLET_ICON_FILE);
if (icon != null) {
try {
ZipUtils.unzipEntry(resJar, icon, iconFile);
} catch (IOException e) {
Log.w(TAG, "Can't unzip icon: " + icon, e);
icon = null;
// noinspection ResultOfMethodCallIgnored
iconFile.delete();
}
}
newDesc.writeTo(new File(tmpDir, Config.MIDLET_MANIFEST_FILE));
FileUtils.deleteDirectory(targetDir);
if (!tmpDir.renameTo(targetDir)) {
throw new ConverterException("Can't rename '" + tmpDir + "' to '" + targetDir + "'");
}
String name = newDesc.getName();
String vendor = newDesc.getVendor();
AppItem app = new AppItem(appDirName, name, vendor, newDesc.getVersion());
if (icon != null) {
app.setImagePathExt(Config.MIDLET_ICON_FILE);
}
if (currentApp != null) {
app.setId(currentApp.getId());
String path = currentApp.getPath();
if (!path.equals(appDirName)) {
File rms = new File(Config.getDataDir(), path);
if (rms.exists()) {
File newRms = new File(Config.getDataDir(), appDirName);
FileUtils.deleteDirectory(newRms);
rms.renameTo(newRms);
}
File config = new File(Config.getConfigsDir(), path);
if (config.exists()) {
File newConfig = new File(Config.getConfigsDir(), appDirName);
FileUtils.deleteDirectory(newConfig);
config.renameTo(newConfig);
}
File appDir = new File(Config.getAppDir(), path);
FileUtils.deleteDirectory(appDir);
}
}
emitter.onSuccess(app);
}
use of ru.playsoftware.j2meloader.util.ConverterException in project J2ME-Loader by nikita36078.
the class AppInstaller method downloadJad.
private void downloadJad() throws ConverterException {
if (!cacheDir.exists() && !cacheDir.mkdirs()) {
throw new ConverterException("Can't create cache dir");
}
srcFile = new File(cacheDir, "tmp.jad");
String url = uri.toString();
Log.d(TAG, "Downloading " + url);
Exception exception;
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setInstanceFollowRedirects(true);
connection.setReadTimeout(3 * 60 * 1000);
connection.setConnectTimeout(15000);
int code = connection.getResponseCode();
if (code == HttpURLConnection.HTTP_MOVED_PERM || code == HttpURLConnection.HTTP_MOVED_TEMP) {
String urlStr = connection.getHeaderField("Location");
connection.disconnect();
connection = (HttpURLConnection) new URL(urlStr).openConnection();
connection.setInstanceFollowRedirects(true);
connection.setReadTimeout(3 * 60 * 1000);
connection.setConnectTimeout(15000);
}
try (InputStream inputStream = connection.getInputStream();
OutputStream outputStream = new FileOutputStream(srcFile)) {
byte[] buffer = new byte[2048];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
}
connection.disconnect();
Log.d(TAG, "Download complete");
return;
} catch (MalformedURLException e) {
exception = e;
} catch (FileNotFoundException e) {
exception = e;
} catch (IOException e) {
exception = e;
} finally {
if (connection != null) {
connection.disconnect();
}
}
deleteTemp();
throw new ConverterException("Can't download jad", exception);
}
Aggregations