Java Code Examples for android.content.res.AssetManager#AssetInputStream

The following examples show how to use android.content.res.AssetManager#AssetInputStream . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KcaUtils.java    From kcanotify with GNU General Public License v3.0 6 votes vote down vote up
public static JsonArray getJsonArrayFromAsset(Context context, String name, KcaDBHelper helper) {
    ContextWrapper cw = new ContextWrapper(context);
    JsonArray data = new JsonArray();
    AssetManager am = cw.getAssets();
    try {
        AssetManager.AssetInputStream ais =
                (AssetManager.AssetInputStream) am.open(name);
        byte[] bytes = ByteStreams.toByteArray(ais);
        data = new JsonParser().parse(new String(bytes)).getAsJsonArray();
        ais.close();
    } catch (IOException e1) {
        e1.printStackTrace();
        if (helper != null) helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getJsonArrayFromStorage", "1", getStringFromException(e1));
    }
    return data;
}
 
Example 2
Source File: KcaUtils.java    From kcanotify with GNU General Public License v3.0 6 votes vote down vote up
public static JsonObject getJsonObjectFromAsset(Context context, String name, KcaDBHelper helper) {
    ContextWrapper cw = new ContextWrapper(context);
    JsonObject data = null;
    AssetManager am = cw.getAssets();
    try {
        AssetManager.AssetInputStream ais =
                (AssetManager.AssetInputStream) am.open(name);
        byte[] bytes = ByteStreams.toByteArray(ais);
        data = new JsonParser().parse(new String(bytes)).getAsJsonObject();
        ais.close();
    } catch (IOException e1) {
        e1.printStackTrace();
        if (helper != null) helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getJsonObjectFromStorage", "1", getStringFromException(e1));
    }
    return data;
}
 
Example 3
Source File: KcaApiData.java    From kcanotify with GNU General Public License v3.0 6 votes vote down vote up
public static int loadShipTranslationDataFromStorage(Context context, String locale) {
    try {
        locale = getResourceLocaleCode(locale);
        JsonObject data = getJsonObjectFromStorage(context, KcaUtils.format("ships-%s.json", locale));
        AssetManager.AssetInputStream ais_abbr =
                (AssetManager.AssetInputStream) context.getResources().getAssets().open("en-abbr.json");
        byte[] bytes_abbr = ByteStreams.toByteArray(ais_abbr);
        JsonElement data_abbr = new JsonParser().parse(new String(bytes_abbr));

        if (data != null) {
            kcShipTranslationData = data;
            kcShipAbbrData = data_abbr.getAsJsonObject();
            return 1;
        } else {
            return -1;
        }
    } catch (IOException e) {
        return 0;
    }
}
 
Example 4
Source File: KcaUtils.java    From kcanotify_h5-master with GNU General Public License v3.0 6 votes vote down vote up
public static JsonObject getJsonObjectFromAsset(Context context, String name, KcaDBHelper helper) {
    ContextWrapper cw = new ContextWrapper(context);
    JsonObject data = null;
    AssetManager am = cw.getAssets();
    try {
        AssetManager.AssetInputStream ais =
                (AssetManager.AssetInputStream) am.open(name);
        byte[] bytes = ByteStreams.toByteArray(ais);
        data = new JsonParser().parse(new String(bytes)).getAsJsonObject();
        ais.close();
    } catch (IOException e1) {
        e1.printStackTrace();
        if (helper != null) helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getJsonObjectFromStorage", "1", getStringFromException(e1));
    }
    return data;
}
 
Example 5
Source File: KcaApiData.java    From kcanotify_h5-master with GNU General Public License v3.0 5 votes vote down vote up
public static int loadSortieExpInfoFromAssets(AssetManager am) {
    try {
        AssetManager.AssetInputStream ais = (AssetManager.AssetInputStream) am.open("exp_sortie.json");
        byte[] bytes = ByteStreams.toByteArray(ais);
        JsonElement expSortieData = new JsonParser().parse(new String(bytes));
        if (expSortieData.isJsonObject()) {
            helper.putValue(DB_KEY_EXPSORTIE, expSortieData.toString());
            return 1;
        } else {
            return -1;
        }
    } catch (IOException e) {
        return 0;
    }
}
 
Example 6
Source File: KcaUtils.java    From kcanotify with GNU General Public License v3.0 5 votes vote down vote up
public static int setDefaultGameData(Context context, KcaDBHelper helper) {
    boolean valid_data = false;
    String current_version = getStringPreferences(context, PREF_KCA_DATA_VERSION);
    String default_version = context.getString(R.string.default_gamedata_version);

    if (helper.getJsonObjectValue(DB_KEY_STARTDATA) != null && KcaUtils.compareVersion(current_version, default_version)) {
        if (KcaApiData.isGameDataLoaded()) return 1;
        JsonObject start_data = helper.getJsonObjectValue(DB_KEY_STARTDATA);
        if (start_data.has("api_data") && start_data.get("api_data").isJsonObject()) {
            KcaApiData.getKcGameData(start_data.getAsJsonObject("api_data"));
            valid_data = true;
        }
    }

    if (!valid_data) {
        try {
            AssetManager assetManager = context.getAssets();
            AssetManager.AssetInputStream ais =
                    (AssetManager.AssetInputStream) assetManager.open("api_start2");
            byte[] bytes = KcaUtils.gzipdecompress(ByteStreams.toByteArray(ais));
            helper.putValue(DB_KEY_STARTDATA, new String(bytes));
            JsonElement data = new JsonParser().parse(new String(bytes));
            JsonObject api_data = new Gson().fromJson(data, JsonObject.class).getAsJsonObject("api_data");
            KcaApiData.getKcGameData(api_data);
            setPreferences(context, PREF_KCA_VERSION, default_version);
            setPreferences(context, PREF_KCA_DATA_VERSION, default_version);
        } catch (Exception e) {
            return 0;
        }
        return 1;
    } else {
        return 1;
    }
}
 
Example 7
Source File: KcaFleetViewService.java    From kcanotify with GNU General Public License v3.0 5 votes vote down vote up
public static JsonObject loadGunfitData(AssetManager am) {
    try {
        AssetManager.AssetInputStream ais = (AssetManager.AssetInputStream) am.open("gunfit.json");
        byte[] bytes = ByteStreams.toByteArray(ais);
        return new JsonParser().parse(new String(bytes)).getAsJsonObject();
    } catch (IOException e) {
        return new JsonObject();
    }
}
 
Example 8
Source File: KcaApiData.java    From kcanotify with GNU General Public License v3.0 5 votes vote down vote up
public static JsonObject loadSpecialEquipmentShipInfo(AssetManager am) {
    try {
        AssetManager.AssetInputStream ais = (AssetManager.AssetInputStream) am.open("equip_special.json");
        byte[] bytes = ByteStreams.toByteArray(ais);
        JsonElement data = new JsonParser().parse(new String(bytes));
        if (data.isJsonObject()) {
            return data.getAsJsonObject();
        } else {
            return null;
        }
    } catch (IOException e) {
        return null;
    }
}
 
Example 9
Source File: KcaApiData.java    From kcanotify with GNU General Public License v3.0 5 votes vote down vote up
public static int loadSortieExpInfoFromAssets(AssetManager am) {
    try {
        AssetManager.AssetInputStream ais = (AssetManager.AssetInputStream) am.open("exp_sortie.json");
        byte[] bytes = ByteStreams.toByteArray(ais);
        JsonElement expSortieData = new JsonParser().parse(new String(bytes));
        if (expSortieData.isJsonObject()) {
            helper.putValue(DB_KEY_EXPSORTIE, expSortieData.toString());
            return 1;
        } else {
            return -1;
        }
    } catch (IOException e) {
        return 0;
    }
}
 
Example 10
Source File: KcaApiData.java    From kcanotify with GNU General Public License v3.0 5 votes vote down vote up
public static int loadShipExpInfoFromAssets(AssetManager am) {
    try {
        AssetManager.AssetInputStream ais = (AssetManager.AssetInputStream) am.open("exp_ship.json");
        byte[] bytes = ByteStreams.toByteArray(ais);
        JsonElement expShipData = new JsonParser().parse(new String(bytes));
        if (expShipData.isJsonObject()) {
            helper.putValue(DB_KEY_EXPSHIP, expShipData.toString());
            return 1;
        } else {
            return -1;
        }
    } catch (IOException e) {
        return 0;
    }
}
 
Example 11
Source File: InputStreamBinary.java    From NoHttp with Apache License 2.0 5 votes vote down vote up
/**
 * An input stream {@link Binary}.
 *
 * @param inputStream must be {@link FileInputStream}, {@link ByteArrayInputStream}.
 * @param fileName    file name. Had better pass this value, unless the server tube don't care about the file name.
 * @param mimeType    content type.
 */
public InputStreamBinary(InputStream inputStream, String fileName, String mimeType) {
    super(fileName, mimeType);
    if (inputStream == null)
        throw new NullPointerException("The inputStream can't be null.");
    if (!(inputStream instanceof FileInputStream)
            && !(inputStream instanceof ByteArrayInputStream)
            && !(inputStream instanceof AssetManager.AssetInputStream))
        throw new IllegalArgumentException("The inputStream must be FileInputStream, ByteArrayInputStream and " +
                "AssetInputStream.");
    this.inputStream = inputStream;
}
 
Example 12
Source File: KcaUtils.java    From kcanotify_h5-master with GNU General Public License v3.0 5 votes vote down vote up
public static int setDefaultGameData(Context context, KcaDBHelper helper) {
    boolean valid_data = false;
    String current_version = getStringPreferences(context, PREF_KCA_DATA_VERSION);
    String default_version = context.getString(R.string.default_gamedata_version);

    if (helper.getJsonObjectValue(DB_KEY_STARTDATA) != null && KcaUtils.compareVersion(current_version, default_version)) {
        if (KcaApiData.isGameDataLoaded()) return 1;
        JsonObject start_data = helper.getJsonObjectValue(DB_KEY_STARTDATA);
        if (start_data.has("api_data") && start_data.get("api_data").isJsonObject()) {
            KcaApiData.getKcGameData(start_data.getAsJsonObject("api_data"));
            valid_data = true;
        }
    }

    if (!valid_data) {
        try {
            AssetManager assetManager = context.getAssets();
            AssetManager.AssetInputStream ais =
                    (AssetManager.AssetInputStream) assetManager.open("api_start2");
            byte[] bytes = KcaUtils.gzipdecompress(ByteStreams.toByteArray(ais));
            helper.putValue(DB_KEY_STARTDATA, new String(bytes));
            JsonElement data = new JsonParser().parse(new String(bytes));
            JsonObject api_data = new Gson().fromJson(data, JsonObject.class).getAsJsonObject("api_data");
            KcaApiData.getKcGameData(api_data);
            setPreferences(context, PREF_KCA_VERSION, default_version);
            setPreferences(context, PREF_KCA_DATA_VERSION, default_version);
        } catch (Exception e) {
            return 0;
        }
        return 1;
    } else {
        return 1;
    }
}
 
Example 13
Source File: KcaFleetViewService.java    From kcanotify_h5-master with GNU General Public License v3.0 5 votes vote down vote up
public static JsonObject loadGunfitData(AssetManager am) {
    try {
        AssetManager.AssetInputStream ais = (AssetManager.AssetInputStream) am.open("gunfit.json");
        byte[] bytes = ByteStreams.toByteArray(ais);
        return new JsonParser().parse(new String(bytes)).getAsJsonObject();
    } catch (IOException e) {
        return new JsonObject();
    }
}
 
Example 14
Source File: KcaApiData.java    From kcanotify_h5-master with GNU General Public License v3.0 5 votes vote down vote up
public static JsonObject loadSpecialEquipmentShipInfo(AssetManager am) {
    try {
        AssetManager.AssetInputStream ais = (AssetManager.AssetInputStream) am.open("equip_special.json");
        byte[] bytes = ByteStreams.toByteArray(ais);
        JsonElement data = new JsonParser().parse(new String(bytes));
        if (data.isJsonObject()) {
            return data.getAsJsonObject();
        } else {
            return null;
        }
    } catch (IOException e) {
        return null;
    }
}
 
Example 15
Source File: ExifInterface.java    From sketch with Apache License 2.0 5 votes vote down vote up
/**
 * Reads Exif tags from the specified image input stream. Attribute mutation is not supported
 * for input streams. The given input stream will proceed its current position. Developers
 * should close the input stream after use. This constructor is not intended to be used with
 * an input stream that performs any networking operations.
 */
public ExifInterface(InputStream inputStream) throws IOException {
    if (inputStream == null) {
        throw new IllegalArgumentException("inputStream cannot be null");
    }
    mFilename = null;
    if (inputStream instanceof AssetManager.AssetInputStream) {
        mAssetInputStream = (AssetManager.AssetInputStream) inputStream;
    } else {
        mAssetInputStream = null;
    }
    loadAttributes(inputStream);
}
 
Example 16
Source File: KcaApiData.java    From kcanotify_h5-master with GNU General Public License v3.0 5 votes vote down vote up
public static int loadShipExpInfoFromAssets(AssetManager am) {
    try {
        AssetManager.AssetInputStream ais = (AssetManager.AssetInputStream) am.open("exp_ship.json");
        byte[] bytes = ByteStreams.toByteArray(ais);
        JsonElement expShipData = new JsonParser().parse(new String(bytes));
        if (expShipData.isJsonObject()) {
            helper.putValue(DB_KEY_EXPSHIP, expShipData.toString());
            return 1;
        } else {
            return -1;
        }
    } catch (IOException e) {
        return 0;
    }
}
 
Example 17
Source File: KcaUtils.java    From kcanotify_h5-master with GNU General Public License v3.0 4 votes vote down vote up
public static String getRSAEncodedString(Context context, String value) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IOException, BadPaddingException, IllegalBlockSizeException {
    /*
    Example:
    try {
        JsonObject data = new JsonObject();
        data.addProperty("userid", 20181234);
        data.addProperty("data", "416D341GX141JI0318W");
        String encoded = KcaUtils.getRSAEncodedString(getApplicationContext(), data.toString());
        Log.e("KCA", encoded);
        data.remove("data");
        encoded = KcaUtils.getRSAEncodedString(getApplicationContext(), data.toString());
        Log.e("KCA", data.toString());
        Log.e("KCA", encoded);
    } catch (Exception e) {
        e.printStackTrace();
    }
    */

    List<String> value_list = new ArrayList<>();
    for (int i = 0; i < (int) Math.ceil(value.length() / 96.0); i++) {
        value_list.add(value.substring(i*96, Math.min((i+1)*96, value.length()) ));
    }

    AssetManager am = context.getAssets();
    AssetManager.AssetInputStream ais =
            (AssetManager.AssetInputStream) am.open("kcaqsync_pubkey.txt");
    byte[] bytes = ByteStreams.toByteArray(ais);
    String publicKeyContent = new String(bytes)
            .replaceAll("\\n", "")
            .replace("-----BEGIN PUBLIC KEY-----", "")
            .replace("-----END PUBLIC KEY-----", "").trim();
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(Base64.decode(publicKeyContent, Base64.DEFAULT));
    Key encryptionKey = keyFactory.generatePublic(pubSpec);
    Cipher rsa = Cipher.getInstance("RSA/None/PKCS1Padding");
    rsa.init(Cipher.ENCRYPT_MODE, encryptionKey);

    byte[] data_all = {};
    for (String item : value_list) {
        byte[] item_byte = rsa.doFinal(item.getBytes("utf-8"));
        data_all = addAll(data_all, item_byte);
    }

    String result = Base64.encodeToString(rsa.doFinal(value.getBytes("utf-8")), Base64.DEFAULT).replace("\n", "");
    return result;
}
 
Example 18
Source File: MainActivity.java    From kcanotify_h5-master with GNU General Public License v3.0 4 votes vote down vote up
private void loadDefaultAsset() {
    AssetManager am = getAssets();
    byte[] bytes;
    String kca_data_version = KcaUtils.getStringPreferences(getApplicationContext(), PREF_KCA_DATA_VERSION);
    String internal_kca_version = getString(R.string.default_gamedata_version);
    int currentKcaResVersion = dbHelper.getTotalResVer();
    try {
        if (kca_data_version == null || compareVersion(internal_kca_version, kca_data_version)) {
            InputStream api_ais = am.open("api_start2");
            bytes = gzipdecompress(ByteStreams.toByteArray(api_ais));
            String asset_start2_data = new String(bytes);
            dbHelper.putValue(DB_KEY_STARTDATA, asset_start2_data);
            KcaUtils.setPreferences(getApplicationContext(), PREF_KCA_VERSION, internal_kca_version);
            KcaUtils.setPreferences(getApplicationContext(), PREF_KCA_DATA_VERSION, internal_kca_version);
        }

        AssetManager.AssetInputStream ais = (AssetManager.AssetInputStream) am.open("list.json");
        bytes = ByteStreams.toByteArray(ais);
        JsonArray data = new JsonParser().parse(new String(bytes)).getAsJsonArray();

        for (JsonElement item: data) {
            JsonObject res_info = item.getAsJsonObject();
            String name = res_info.get("name").getAsString();
            int version = res_info.get("version").getAsInt();
            if (currentKcaResVersion < version) {
                final File root_dir = getDir("data", Context.MODE_PRIVATE);
                final File new_data = new File(root_dir, name);
                if (new_data.exists()) new_data.delete();
                InputStream file_is = am.open(name);
                OutputStream file_out = new FileOutputStream(new_data);

                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = file_is.read(buffer)) != -1) {
                    file_out.write(buffer, 0, bytesRead);
                }
                file_is.close();
                file_out.close();
                dbHelper.putResVer(name, version);
            }
        }
        ais.close();
    } catch (IOException e) {

    }
}
 
Example 19
Source File: InitStartActivity.java    From kcanotify with GNU General Public License v3.0 4 votes vote down vote up
private void loadDefaultAsset() {
    AssetManager am = getAssets();
    byte[] bytes;
    String kca_data_version = KcaUtils.getStringPreferences(getApplicationContext(), PREF_KCA_DATA_VERSION);
    String internal_kca_version = getString(R.string.default_gamedata_version);
    int currentKcaResVersion = dbHelper.getTotalResVer();
    try {
        if (kca_data_version == null || compareVersion(internal_kca_version, kca_data_version)) {
            InputStream api_ais = am.open("api_start2");
            bytes = gzipdecompress(ByteStreams.toByteArray(api_ais));
            String asset_start2_data = new String(bytes);
            dbHelper.putValue(DB_KEY_STARTDATA, asset_start2_data);
            KcaUtils.setPreferences(getApplicationContext(), PREF_KCA_VERSION, internal_kca_version);
            KcaUtils.setPreferences(getApplicationContext(), PREF_KCA_DATA_VERSION, internal_kca_version);
        }

        AssetManager.AssetInputStream ais = (AssetManager.AssetInputStream) am.open("list.json");
        bytes = ByteStreams.toByteArray(ais);
        JsonArray data = new JsonParser().parse(new String(bytes)).getAsJsonArray();

        for (JsonElement item: data) {
            JsonObject res_info = item.getAsJsonObject();
            String name = res_info.get("name").getAsString();
            int version = res_info.get("version").getAsInt();
            if (currentKcaResVersion < version) {
                final File root_dir = getDir("data", Context.MODE_PRIVATE);
                final File new_data = new File(root_dir, name);
                if (new_data.exists()) new_data.delete();
                InputStream file_is = am.open(name);
                OutputStream file_out = new FileOutputStream(new_data);

                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = file_is.read(buffer)) != -1) {
                    file_out.write(buffer, 0, bytesRead);
                }
                file_is.close();
                file_out.close();
                dbHelper.putResVer(name, version);
            }
        }
        ais.close();
    } catch (IOException e) {

    }
}
 
Example 20
Source File: KcaUtils.java    From kcanotify with GNU General Public License v3.0 4 votes vote down vote up
public static String getRSAEncodedString(Context context, String value) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IOException, BadPaddingException, IllegalBlockSizeException {
    /*
    Example:
    try {
        JsonObject data = new JsonObject();
        data.addProperty("userid", 20181234);
        data.addProperty("data", "416D341GX141JI0318W");
        String encoded = KcaUtils.getRSAEncodedString(getApplicationContext(), data.toString());
        Log.e("KCA", encoded);
        data.remove("data");
        encoded = KcaUtils.getRSAEncodedString(getApplicationContext(), data.toString());
        Log.e("KCA", data.toString());
        Log.e("KCA", encoded);
    } catch (Exception e) {
        e.printStackTrace();
    }
    */

    List<String> value_list = new ArrayList<>();
    for (int i = 0; i < (int) Math.ceil(value.length() / 96.0); i++) {
        value_list.add(value.substring(i*96, Math.min((i+1)*96, value.length()) ));
    }

    AssetManager am = context.getAssets();
    AssetManager.AssetInputStream ais =
            (AssetManager.AssetInputStream) am.open("kcaqsync_pubkey.txt");
    byte[] bytes = ByteStreams.toByteArray(ais);
    String publicKeyContent = new String(bytes)
            .replaceAll("\\n", "")
            .replace("-----BEGIN PUBLIC KEY-----", "")
            .replace("-----END PUBLIC KEY-----", "").trim();
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(Base64.decode(publicKeyContent, Base64.DEFAULT));
    Key encryptionKey = keyFactory.generatePublic(pubSpec);
    Cipher rsa = Cipher.getInstance("RSA/None/PKCS1Padding");
    rsa.init(Cipher.ENCRYPT_MODE, encryptionKey);

    byte[] data_all = {};
    for (String item : value_list) {
        byte[] item_byte = rsa.doFinal(item.getBytes("utf-8"));
        data_all = addAll(data_all, item_byte);
    }

    String result = Base64.encodeToString(rsa.doFinal(value.getBytes("utf-8")), Base64.DEFAULT).replace("\n", "");
    return result;
}