mirror of
https://github.com/fumiama/android-base16384.git
synced 2026-06-06 10:40:30 +08:00
v3.1
1. 不使用压缩时字符串在内存直接编解码 2. 减少读取配置次数
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.10.2)
|
||||
|
||||
project("base16384")
|
||||
SET(CMAKE_BUILD_TYPE "Release")
|
||||
|
||||
add_subdirectory(base14)
|
||||
add_subdirectory(lzma1900)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.0.0)
|
||||
project(base1432 VERSION 2.0)
|
||||
|
||||
add_library(base1432 ./base1432le.c)
|
||||
add_library(base14c STATIC base1432le.c)
|
||||
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.0.0)
|
||||
project(base1464 VERSION 2.0)
|
||||
|
||||
add_library(base1464 ./base1464le.c)
|
||||
add_library(base14c STATIC base1464le.c)
|
||||
|
||||
@@ -1,19 +1,14 @@
|
||||
cmake_minimum_required(VERSION 3.10.2)
|
||||
|
||||
project("base16384")
|
||||
|
||||
add_library(base14 SHARED base16384.cpp)
|
||||
|
||||
add_library(base14c STATIC base16384.c)
|
||||
|
||||
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||
add_definitions("-DCPUBIT64")
|
||||
add_subdirectory("./64")
|
||||
target_link_libraries(base14c base1464)
|
||||
ELSE()
|
||||
add_definitions("-DCPUBIT32")
|
||||
add_subdirectory("./32")
|
||||
target_link_libraries(base14c base1432)
|
||||
ENDIF()
|
||||
|
||||
target_link_libraries(base14 base14c)
|
||||
add_library(base14 SHARED base16384.cpp)
|
||||
add_library(base14s STATIC base16384.c)
|
||||
target_link_libraries(base14 base14s base14c)
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#include <jni.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "base16384.h"
|
||||
#include <cstring>
|
||||
#include "base16384.hpp"
|
||||
|
||||
#define execute(function){\
|
||||
const char *inputFileDir = env->GetStringUTFChars(sf, JNI_FALSE);\
|
||||
@@ -12,11 +13,26 @@
|
||||
return re;\
|
||||
}
|
||||
|
||||
extern "C" int encode_file(const char* input, const char* output);
|
||||
extern "C" int decode_file(const char* input, const char* output);
|
||||
#define exe_byte(fun) {\
|
||||
uint32_t len = env->GetArrayLength(buf);\
|
||||
const uint8_t* data = (uint8_t*)env->GetByteArrayElements(buf, JNI_FALSE);\
|
||||
LENDAT* ld = fun(data, len);\
|
||||
jbyteArray out = env->NewByteArray(ld->len);\
|
||||
auto out_data = env->GetByteArrayElements(out, JNI_FALSE);\
|
||||
memcpy(out_data, ld->data, ld->len);\
|
||||
free(ld);\
|
||||
env->ReleaseByteArrayElements(out, out_data, JNI_COMMIT);\
|
||||
return out;\
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT int JNICALL
|
||||
Java_top_fumiama_base16384_MainActivity_encode(JNIEnv* env, jobject, jstring sf, jstring df) execute(encode_file)
|
||||
|
||||
extern "C" JNIEXPORT int JNICALL
|
||||
Java_top_fumiama_base16384_MainActivity_decode(JNIEnv* env, jobject, jstring sf, jstring df) execute(decode_file)
|
||||
Java_top_fumiama_base16384_MainActivity_decode(JNIEnv* env, jobject, jstring sf, jstring df) execute(decode_file)
|
||||
|
||||
extern "C" JNIEXPORT jbyteArray JNICALL
|
||||
Java_top_fumiama_base16384_MainActivity_encodeByteArray(JNIEnv* env, jobject, jbyteArray buf) exe_byte(encode)
|
||||
|
||||
extern "C" JNIEXPORT jbyteArray JNICALL
|
||||
Java_top_fumiama_base16384_MainActivity_decodeByteArray(JNIEnv* env, jobject, jbyteArray buf) exe_byte(decode)
|
||||
40
app/src/main/cpp/base14/base16384.hpp
Normal file
40
app/src/main/cpp/base14/base16384.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// Created by fumiama on 2021/5/20.
|
||||
//
|
||||
|
||||
#ifndef BASE16384_BASE16384_HPP
|
||||
#define BASE16384_BASE16384_HPP
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef CPUBIT32
|
||||
#ifndef CPUBIT64
|
||||
#define CPUBIT32
|
||||
#endif
|
||||
#endif
|
||||
#ifdef CPUBIT32
|
||||
#define B14BUFSIZ 8192
|
||||
struct LENDAT {
|
||||
uint8_t* data;
|
||||
uint32_t len;
|
||||
};
|
||||
typedef struct LENDAT LENDAT;
|
||||
|
||||
extern "C" LENDAT* encode(const uint8_t* data, const u_int32_t len);
|
||||
extern "C" LENDAT* decode(const uint8_t* data, const u_int32_t len);
|
||||
#endif
|
||||
#ifdef CPUBIT64
|
||||
#define B14BUFSIZ 16384
|
||||
struct LENDAT {
|
||||
uint8_t* data;
|
||||
uint64_t len;
|
||||
};
|
||||
typedef struct LENDAT LENDAT;
|
||||
|
||||
extern "C" LENDAT* encode(const uint8_t* data, const u_int64_t len);
|
||||
extern "C" LENDAT* decode(const uint8_t* data, const u_int64_t len);
|
||||
#endif
|
||||
|
||||
extern "C" int encode_file(const char* input, const char* output);
|
||||
extern "C" int decode_file(const char* input, const char* output);
|
||||
|
||||
#endif //BASE16384_BASE16384_HPP
|
||||
@@ -1,5 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.10.2)
|
||||
|
||||
project("base16384")
|
||||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_7ZIP_ST")
|
||||
|
||||
@@ -9,6 +9,8 @@ import android.content.Intent
|
||||
import android.graphics.Rect
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.util.Base64
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.Switch
|
||||
@@ -156,15 +158,23 @@ class MainActivity : Activity() {
|
||||
val tou = if(isEncode)tde else ten
|
||||
tin.text?.let {
|
||||
if(it.isNotEmpty()){
|
||||
val inputFile = generateCacheFile("input")
|
||||
val outputFile = generateCacheFile("output")
|
||||
|
||||
inputFile.writeText(it.toString(), getCharset(getCustomCharsetPosition(isEncode, pc)))
|
||||
val re = base16(isEncode, inputFile.absolutePath, outputFile.absolutePath)
|
||||
runOnUiThread {
|
||||
tou.setText(outputFile.readText(getCharset(getCustomCharsetPosition(!isEncode, pc))))
|
||||
copyText(tou, cm)
|
||||
if(re != "") Toast.makeText(this, re, Toast.LENGTH_SHORT).show()
|
||||
if(sl.isChecked) {
|
||||
val inputFile = generateCacheFile("input")
|
||||
val outputFile = generateCacheFile("output")
|
||||
|
||||
inputFile.writeText(it.toString(), getCharset(getCustomCharsetPosition(isEncode, pc)))
|
||||
val re = base16(isEncode, inputFile.absolutePath, outputFile.absolutePath)
|
||||
runOnUiThread {
|
||||
tou.setText(outputFile.readText(getCharset(getCustomCharsetPosition(!isEncode, pc))))
|
||||
copyText(tou, cm)
|
||||
if(re != "") Toast.makeText(this, re, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
} else {
|
||||
val re = base16(isEncode, it.toString().toByteArray(getCharset(getCustomCharsetPosition(isEncode, pc))))
|
||||
if(re.isNotEmpty()) runOnUiThread {
|
||||
tou.setText(re.toString(getCharset(getCustomCharsetPosition(!isEncode, pc))))
|
||||
copyText(tou, cm)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -185,6 +195,8 @@ class MainActivity : Activity() {
|
||||
return re
|
||||
}
|
||||
|
||||
private fun base16(isEncode: Boolean, input: ByteArray) = if(isEncode) encodeByteArray(input) else decodeByteArray(input)
|
||||
|
||||
private fun base16384(isEncode: Boolean, sf: String, of: String): Int = if(isEncode) encode(sf, of) else decode(sf, of)
|
||||
|
||||
private fun setViewsVisibility(){
|
||||
@@ -246,6 +258,8 @@ class MainActivity : Activity() {
|
||||
*/
|
||||
private external fun encode(sf: String, df: String): Int
|
||||
private external fun decode(sf: String, df: String): Int
|
||||
private external fun encodeByteArray(buf: ByteArray): ByteArray
|
||||
private external fun decodeByteArray(buf: ByteArray): ByteArray
|
||||
private external fun lzma(sf: String, df: String, isEncode: Boolean): String
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -7,45 +7,55 @@ import java.io.InputStream
|
||||
import java.util.*
|
||||
|
||||
class PropertiesTools(private val f: File):Properties() {
|
||||
private val propfile:File
|
||||
get() {
|
||||
private var cache = hashMapOf<String, String>()
|
||||
|
||||
init {
|
||||
if(!f.exists()) {
|
||||
if(f.parentFile?.exists() != true) f.parentFile?.mkdirs()
|
||||
if(f.parentFile?.canWrite() != true) f.parentFile?.setWritable(true)
|
||||
createNew(f)
|
||||
}else if(f.isDirectory) {
|
||||
} else if(f.isDirectory) {
|
||||
if(f.parentFile?.canWrite() != true) f.parentFile?.setWritable(true)
|
||||
f.delete()
|
||||
createNew(f)
|
||||
}
|
||||
if(f.parentFile?.canWrite() != true) f.parentFile?.setWritable(true)
|
||||
if(f.parentFile?.canRead() != true) f.parentFile?.setReadable(true)
|
||||
return f
|
||||
}
|
||||
private fun createNew(f: File){
|
||||
|
||||
private fun createNew(f: File) {
|
||||
f.createNewFile()
|
||||
val o = f.outputStream()
|
||||
this.storeToXML(o, "store")
|
||||
Log.d("MyPT", "Generate new prop.")
|
||||
o.close()
|
||||
}
|
||||
|
||||
private fun loadFromXml(`in`: InputStream?): PropertiesTools {
|
||||
this.loadFromXML(`in`)
|
||||
return this
|
||||
}
|
||||
|
||||
private fun setProp(key: String?, value: String?): PropertiesTools {
|
||||
this.setProperty(key, value)
|
||||
return this
|
||||
}
|
||||
|
||||
operator fun get(key: String): String{
|
||||
val i = propfile.inputStream()
|
||||
val re = this.loadFromXml(i).getProperty(key)?:"null"
|
||||
Log.d("MyPT", "Get $key = $re")
|
||||
i.close()
|
||||
return re
|
||||
return if(cache.containsKey(key)) cache[key]?:"null"
|
||||
else {
|
||||
val i = f.inputStream()
|
||||
val re = this.loadFromXml(i).getProperty(key)?:"null"
|
||||
Log.d("MyPT", "Read $key = $re")
|
||||
i.close()
|
||||
cache[key] = re
|
||||
re
|
||||
}
|
||||
}
|
||||
operator fun set(key: String, value: String){
|
||||
val o = propfile.outputStream()
|
||||
|
||||
operator fun set(key: String, value: String) {
|
||||
cache[key] = value
|
||||
val o = f.outputStream()
|
||||
this.setProp(key, value).storeToXML(o, "store")
|
||||
Log.d("MyPT", "Set $key = $value")
|
||||
o.close()
|
||||
|
||||
Reference in New Issue
Block a user