下面列出了android.content.res.AssetManager#AssetInputStream() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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;
}
}
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;
}
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;
}
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;
}
}
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;
}
}
/**
* 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);
}
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;
}
}
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();
}
}
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;
}
}
/**
* 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;
}
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;
}
}
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;
}
}
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;
}
}
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();
}
}
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;
}
}
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) {
}
}
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;
}
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) {
}
}
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;
}