This commit is contained in:
tarsin committed 2022-07-24 20:20:04 +08:00
1 parent b61e46e956
commit e095627aee
36 files changed
+51 -1136

No files matched your search

@@ -50,7 +50,7 @@ public class ArchiveGalleryProvider extends GalleryProvider2 {
private final UriArchiveAccessor archiveAccessor;
private final Stack<Integer> requests = new Stack<>();
private final AtomicInteger extractingIndex = new AtomicInteger(GalleryPageView.INVALID_INDEX);
private final LinkedHashMap<Integer, InputStream> streams = new LinkedHashMap<>();
private final LinkedHashMap<Integer, Integer> streams = new LinkedHashMap<>();
private final AtomicInteger decodingIndex = new AtomicInteger(GalleryPageView.INVALID_INDEX);
private Thread archiveThread;
private Thread decodeThread;
@@ -271,7 +271,7 @@ public class ArchiveGalleryProvider extends GalleryProvider2 {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
int index;
InputStream stream;
Integer fd;
synchronized (streams) {
if (streams.isEmpty()) {
try {
@@ -283,16 +283,16 @@ public class ArchiveGalleryProvider extends GalleryProvider2 {
continue;
}
Iterator<Map.Entry<Integer, InputStream>> iterator = streams.entrySet().iterator();
Map.Entry<Integer, InputStream> entry = iterator.next();
Iterator<Map.Entry<Integer, Integer>> iterator = streams.entrySet().iterator();
Map.Entry<Integer, Integer> entry = iterator.next();
iterator.remove();
index = entry.getKey();
stream = entry.getValue();
fd = entry.getValue();
decodingIndex.lazySet(index);
}
try {
Image image = Image.decode(stream, true);
Image image = Image.decode(fd, true);
if (image != null) {
notifyPageSucceed(index, image);
} else {
@@ -245,7 +245,7 @@ public abstract class GalleryProvider {
}
public void add(Integer key, ImageWrapper value) {
if (value.getFormat() != Image.FORMAT_GIF && value.obtain()) {
if (value.getFormat() != Image.FORMAT_ANIMATED && value.obtain()) {
put(key, value);
}
}
@@ -253,7 +253,7 @@ public abstract class GalleryProvider {
@Override
protected int sizeOf(Integer key, ImageWrapper value) {
int size = value.getWidth() * value.getHeight() * 4;
if (value.getFormat() == Image.FORMAT_GIF) {
if (value.getFormat() == Image.FORMAT_ANIMATED) {
size *= 5;
}
return size;
@@ -28,31 +28,9 @@ import java.util.concurrent.atomic.AtomicInteger;
* The {@code Image} is a image which stored pixel data in native heap
*/
public final class Image {
public static final int FORMAT_NORMAL = 0;
/**
* Unknown image format
*/
public static final int FORMAT_UNKNOWN = -1;
/**
* Plain image format, for {@code Image} from {@link #create(Bitmap)}
*/
public static final int FORMAT_PLAIN = 0;
/**
* JPEG image format
*/
public static final int FORMAT_JPEG = 1;
/**
* PNG image format
*/
public static final int FORMAT_PNG = 2;
/**
* GIF image format
*/
public static final int FORMAT_GIF = 3;
public static final int FORMAT_ANIMATED = 1;
private static final AtomicInteger sImageCount = new AtomicInteger();
private final int mFormat;
@@ -74,11 +52,8 @@ public final class Image {
* Decode image from {@code InputStream}
*/
@Nullable
public static Image decode(InputStream is, boolean partially) {
if (!(is instanceof BufferedInputStream)) {
is = new BufferedInputStream(is);
}
return nativeDecode(is, partially);
public static Image decode(Integer fd, boolean partially) {
return nativeDecode(fd, partially);
}
/**
@@ -97,22 +72,7 @@ public final class Image {
return sImageCount.get();
}
/**
* Return all supported image formats, exclude {@link #FORMAT_PLAIN}
*/
public static int[] getSupportedImageFormats() {
return nativeGetSupportedImageFormats();
}
/**
* Return decoder description of the image format,
* {@code null} for invalid image format.
*/
public static String getDecoderDescription(int format) {
return nativeGetDecoderDescription(format);
}
private static native Image nativeDecode(InputStream is, boolean partially);
private static native Image nativeDecode(int fd, boolean partially);
private static native Image nativeCreate(Bitmap bitmap);
@@ -143,10 +103,6 @@ public final class Image {
private static native void nativeRecycle(long nativePtr, int format);
private static native int[] nativeGetSupportedImageFormats();
private static native String nativeGetDecoderDescription(int format);
/**
* Return the format of the image
*/
@@ -73,7 +73,7 @@ public final class ImageBitmap implements Animatable, Runnable {
}
private ImageBitmap(@NonNull Bitmap bitmap) {
mFormat = Image.FORMAT_PLAIN;
mFormat = Image.FORMAT_NORMAL;
mBitmap = bitmap;
mIsOpaque = !bitmap.hasAlpha();
mByteCount = bitmap.getRowBytes() * bitmap.getHeight();
@@ -84,8 +84,8 @@ public final class ImageBitmap implements Animatable, Runnable {
* Decode {@code InputStream}, then create image.
*/
@Nullable
public static ImageBitmap decode(@NonNull InputStream is) {
Image image = Image.decode(is, false);
public static ImageBitmap decode(@NonNull Integer fd) {
Image image = Image.decode(fd, false);
if (image != null) {
return create(image);
} else {
+21 -2
View File
@@ -13,6 +13,25 @@
# limitations under the License.
cmake_minimum_required(VERSION 3.4.1)
project(image C)
add_subdirectory(image)
add_subdirectory(stream)
set(IMAGE_SOURCES
image.c
image_utils.c
image_plain.c
filter/clahe.c
filter/gray.c
java_wrapper.c
)
set(IMAGE_INCLUDES
${CMAKE_CURRENT_SOURCE_DIR}
)
set(IMAGE_LIBRARIES
log
jnigraphics
GLESv2
)
add_library(image STATIC ${IMAGE_SOURCES})
target_include_directories(image PRIVATE ${IMAGE_INCLUDES})
target_link_libraries(image PRIVATE ${IMAGE_LIBRARIES})
File renamed without changes.
@@ -16,8 +16,8 @@
#include "clahe.h"
#include "pixel.h"
#include "../../utils.h"
#include "../../log.h"
#include "../utils.h"
#include "../log.h"
#define TILE_BLOCK 8
#define LIMIT_MULTIPLE 4
@@ -25,26 +25,21 @@
#include "image_plain.h"
#include "filter/clahe.h"
#include "filter/gray.h"
#include "../log.h"
#include "log.h"
void* decode(JNIEnv* env, InputStream* stream, bool partially, int* format)
void* decode(JNIEnv* env, int fd, bool partially, int* format)
{
AImageDecoder *decoder;
size_t len = 1024 * 1024 * 4;
void* buffer = calloc(len, sizeof(char));
len = read_input_stream(env, stream, buffer, 0, len);
AImageDecoder_createFromBuffer(buffer, len, &decoder);
free(buffer);
AImageDecoder_createFromFd(fd, &decoder);
*format = AImageDecoder_isAnimated(decoder);
AImageDecoderHeaderInfo *headerInfo = AImageDecoder_getHeaderInfo(decoder);
IMAGE *image = malloc(sizeof(IMAGE));
image->height = AImageDecoderHeaderInfo_getHeight(headerInfo);
image->width = AImageDecoderHeaderInfo_getWidth(headerInfo);
ssize_t bufferLen = 1024 * 1024 * 64;
unsigned long bufferLen = image->height * image->width * 4;
image->buffer = malloc(bufferLen);
AImageDecoder_decodeImage(decoder, image->buffer, AImageDecoder_getMinimumStride(decoder), bufferLen);
AImageDecoder_delete(decoder);
destroy_input_stream(env, &stream);
*format = IMAGE_FORMAT_JPEG;
return image;
}
@@ -145,23 +140,3 @@ void recycle(JNIEnv *env, void* image, int format)
{
PLAIN_recycle((PLAIN*) image);
}
int get_supported_formats(int *formats)
{
int i = 0;
#ifdef IMAGE_SUPPORT_JPEG
formats[i++] = IMAGE_FORMAT_JPEG;
#endif
#ifdef IMAGE_SUPPORT_PNG
formats[i++] = IMAGE_FORMAT_PNG;
#endif
#ifdef IMAGE_SUPPORT_GIF
formats[i++] = IMAGE_FORMAT_GIF;
#endif
return i;
}
const char *get_decoder_description(int format)
{
return NULL;
}
@@ -22,9 +22,9 @@
#define IMAGE_IMAGE_H
#include <stdbool.h>
#include <jni.h>
#include "config.h"
#include "input_stream.h"
#define IMAGE_FORMAT_UNKNOWN -1
#ifdef IMAGE_SUPPORT_PLAIN
@@ -49,7 +49,7 @@ typedef struct
void* buffer;
} IMAGE;
void* decode(JNIEnv* env, InputStream* stream, bool partially, int* format);
void* decode(JNIEnv* env, int fd, bool partially, int* format);
void* create(unsigned int width, unsigned int height, const void* data);
bool complete(JNIEnv* env, void* image, int format);
bool is_completed(void* image, int format);
@@ -1,60 +0,0 @@
# Copyright 2015 Hippo Seven
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
SUPPORT_FORMAT := plain jpeg png gif
LOCAL_MODULE := image
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../stream
LOCAL_SRC_FILES := \
image.c \
image_utils.c \
java_wrapper.c
LOCAL_LDLIBS := -llog -ljnigraphics -lGLESv2
LOCAL_STATIC_LIBRARIES := stream
ifeq ($(filter plain, $(SUPPORT_FORMAT)), plain)
LOCAL_SRC_FILES += image_plain.c
else
LOCAL_CFLAGS += -DIMAGE_NOT_SUPPORT_PLAIN
endif
ifeq ($(filter jpeg, $(SUPPORT_FORMAT)), jpeg)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../libjpeg-turbo
LOCAL_SRC_FILES += image_jpeg.c
LOCAL_STATIC_LIBRARIES += jpeg-turbo
else
LOCAL_CFLAGS += -DIMAGE_NOT_SUPPORT_JPEG
endif
ifeq ($(filter png, $(SUPPORT_FORMAT)), png)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../libpng
LOCAL_SRC_FILES += image_png.c
LOCAL_STATIC_LIBRARIES += png
else
LOCAL_CFLAGS += -DIMAGE_NOT_SUPPORT_PNG
endif
ifeq ($(filter gif, $(SUPPORT_FORMAT)), gif)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../giflib
LOCAL_SRC_FILES += image_gif.c
LOCAL_STATIC_LIBRARIES += gif
else
LOCAL_CFLAGS += -DIMAGE_NOT_SUPPORT_GIF
endif
include $(BUILD_SHARED_LIBRARY)
@@ -1,38 +0,0 @@
# Copyright 2018 Hippo Seven
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.4.1)
project(image C)
set(IMAGE_SOURCES
image.c
image_utils.c
image_plain.c
filter/clahe.c
filter/gray.c
java_wrapper.c
)
set(IMAGE_INCLUDES
${CMAKE_CURRENT_SOURCE_DIR}
)
set(IMAGE_LIBRARIES
log
jnigraphics
GLESv2
stream
)
add_library(image STATIC ${IMAGE_SOURCES})
target_include_directories(image PRIVATE ${IMAGE_INCLUDES})
target_link_libraries(image PRIVATE ${IMAGE_LIBRARIES})
@@ -26,7 +26,7 @@
#include "image_plain.h"
#include "image_utils.h"
#include "../log.h"
#include "log.h"
void* PLAIN_create(unsigned int width, unsigned int height, const void* data)
{
@@ -26,8 +26,6 @@
#include <stdbool.h>
#include "patch_head_input_stream.h"
#include "image.h"
typedef IMAGE PLAIN;
@@ -23,9 +23,8 @@
#include <GLES2/gl2.h>
#include "java_wrapper.h"
#include "input_stream.h"
#include "image.h"
#include "../log.h"
#include "log.h"
static JavaVM* jvm;
@@ -74,19 +73,13 @@ jobject create_image_object(JNIEnv* env, void* ptr, int format, int width, int h
JNIEXPORT jobject JNICALL
Java_com_hippo_image_Image_nativeDecode(JNIEnv* env,
jclass clazz, jobject is, jboolean partially)
jclass clazz, jint fd, jboolean partially)
{
InputStream* input_stream;
int format;
void* image;
jobject image_object;
input_stream = create_input_stream(env, is);
if (input_stream == NULL) {
return NULL;
}
image = decode(env, input_stream, partially, &format);
image = decode(env, fd, partially, &format);
if (image == NULL) {
return NULL;
}
@@ -105,7 +98,6 @@ JNIEXPORT jobject JNICALL
Java_com_hippo_image_Image_nativeCreate(JNIEnv* env,
jclass clazz, jobject bitmap)
{
#ifdef IMAGE_SUPPORT_PLAIN
AndroidBitmapInfo info;
void *pixels = NULL;
void* image = NULL;
@@ -134,9 +126,6 @@ Java_com_hippo_image_Image_nativeCreate(JNIEnv* env,
} else {
return image_object;
}
#else
return NULL;
#endif
}
JNIEXPORT jboolean JNICALL
@@ -261,32 +250,6 @@ Java_com_hippo_image_Image_nativeRecycle(JNIEnv* env,
recycle(env, (void*) (intptr_t) ptr, format);
}
JNIEXPORT jintArray JNICALL
Java_com_hippo_image_Image_nativeGetSupportedImageFormats(
JNIEnv *env, jclass clazz)
{
int formats[IMAGE_MAX_SUPPORTED_FORMAT_COUNT];
int count = get_supported_formats(formats);
jintArray array = (*env)->NewIntArray(env, count);
if (array == NULL) {
return NULL;
}
(*env)->SetIntArrayRegion(env, array, 0, count, formats);
return array;
}
JNIEXPORT jstring JNICALL
Java_com_hippo_image_Image_nativeGetDecoderDescription(
JNIEnv *env, jclass clazz, jint format)
{
const char *description = get_decoder_description(format);
if (description == NULL) {
return NULL;
} else {
return (*env)->NewStringUTF(env, description);
}
}
bool image_onLoad(JavaVM *vm)
{
jvm = vm;
@@ -1,27 +0,0 @@
# Copyright 2015 Hippo Seven
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := stream
LOCAL_SRC_FILES := \
input_stream.c \
input_stream_pipe.c \
output_stream.c \
output_stream_pipe.c \
patch_head_input_stream.c
include $(BUILD_STATIC_LIBRARY)
@@ -1,28 +0,0 @@
# Copyright 2018 Hippo Seven
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.4.1)
project(stream C)
set(STREAM_SOURCES
input_stream.c
input_stream_pipe.c
output_stream.c
output_stream_pipe.c
patch_head_input_stream.c
)
set(STREAM_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR})
add_library(stream STATIC ${STREAM_SOURCES})
target_include_directories(stream PUBLIC ${STREAM_INCLUDES})
-30
View File
@@ -1,30 +0,0 @@
/*
* Copyright 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Created by Hippo on 12/28/2015.
//
#ifndef STREAM_CONFIG_H
#define STREAM_CONFIG_H
#define STREAM_SUPPORT_INPUT
//#define STREAM_SUPPORT_INPUT_PIPE
#define STREAM_SUPPORT_INPUT_PATCH_HEAD
#define STREAM_SUPPORT_OUTPUT
//#define STREAM_SUPPORT_OUTPUT_PIPE
#endif // STREAM_CONFIG_H
@@ -1,117 +0,0 @@
/*
* Copyright 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Created by Hippo on 10/19/2015.
//
#include "config.h"
#ifdef STREAM_SUPPORT_INPUT
#include <stdlib.h>
#include "../log.h"
#include "../utils.h"
#include "input_stream.h"
#include "stream_utils.h"
InputStream* create_input_stream(JNIEnv* env, jobject is)
{
jclass streamCls = (*env)->GetObjectClass(env, is);
jmethodID readMID = (*env)->GetMethodID(env, streamCls, "read", "([BII)I");
jmethodID closeMID = (*env)->GetMethodID(env, streamCls, "close", "()V");
jbyteArray buffer;
if (readMID == NULL || closeMID == NULL) {
LOGE(MSG("Can't get read or close method id"));
return NULL;
}
buffer = (*env)->NewByteArray(env, BUFFER_SIZE);
buffer = (*env)->NewGlobalRef(env, buffer);
if (buffer == NULL) {
LOGE(MSG("Can't create buffer"));
return NULL;
}
InputStream* inputStream = (InputStream*) malloc(sizeof(InputStream));
if (inputStream == NULL) {
LOGE(MSG("Out of memory"));
return NULL;
}
inputStream->is = (*env)->NewGlobalRef(env, is);
inputStream->readMID = readMID;
inputStream->closeMID = closeMID;
inputStream->buffer = buffer;
return inputStream;
}
void destroy_input_stream(JNIEnv* env, InputStream** inputStream)
{
if (inputStream != NULL && *inputStream != NULL) {
(*env)->DeleteGlobalRef(env, (*inputStream)->is);
(*env)->DeleteGlobalRef(env, (*inputStream)->buffer);
free(*inputStream);
*inputStream = NULL;
}
}
size_t read_input_stream(JNIEnv* env, InputStream* inputStream, unsigned char* buffer, int offset, size_t size)
{
size_t remainSize = size;
size_t readSize = 0;
int bufferOffset = offset;
int len;
while (remainSize > 0) {
len = MIN(BUFFER_SIZE, remainSize);
len = (*env)->CallIntMethod(env, inputStream->is, inputStream->readMID, inputStream->buffer, 0, len);
if ((*env)->ExceptionCheck(env)) {
LOGE(MSG("Catch exception"));
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
len = -1;
}
// end of the stream or catch exception
if (len < 0) {
break;
}
(*env)->GetByteArrayRegion(env, inputStream->buffer, 0, len, (jbyte *) (buffer + bufferOffset));
remainSize -= len;
readSize += len;
bufferOffset += len;
}
return readSize;
}
void close_input_stream(JNIEnv* env, InputStream* inputStream)
{
(*env)->CallVoidMethod(env, inputStream->is, inputStream->closeMID);
if ((*env)->ExceptionCheck(env)) {
LOGE(MSG("Catch exception"));
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
}
}
#endif // STREAM_SUPPORT_INPUT
@@ -1,45 +0,0 @@
/*
* Copyright 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Created by Hippo on 10/19/2015.
//
#ifndef STREAM_INPUT_STREAM_H
#define STREAM_INPUT_STREAM_H
#include "config.h"
#ifdef STREAM_SUPPORT_INPUT
#include <jni.h>
#include <stddef.h>
typedef struct
{
jobject is;
jmethodID readMID;
jmethodID closeMID;
jbyteArray buffer;
} InputStream;
InputStream* create_input_stream(JNIEnv* env, jobject is);
void destroy_input_stream(JNIEnv* env, InputStream** inputStream);
size_t read_input_stream(JNIEnv* env, InputStream* inputStream, unsigned char* buffer, int offset, size_t size);
void close_input_stream(JNIEnv* env, InputStream* inputStream);
#endif // STREAM_SUPPORT_INPUT
#endif // STREAM_INPUT_STREAM_H
@@ -1,94 +0,0 @@
/*
* Copyright 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Created by Hippo on 10/19/2015.
//
#include "config.h"
#if defined(STREAM_SUPPORT_INPUT) && defined(STREAM_SUPPORT_INPUT_PIPE)
#include <stdlib.h>
#include "input_stream_pipe.h"
#include "../log.h"
InputStreamPipe* create_input_stream_pipe(JNIEnv* env, jobject isPipe)
{
jclass streamPipeCls = (*env)->GetObjectClass(env, isPipe);
jmethodID obtainMID = (*env)->GetMethodID(env, streamPipeCls, "obtain", "()V");
jmethodID releaseMID = (*env)->GetMethodID(env, streamPipeCls, "release", "()V");
jmethodID openMID = (*env)->GetMethodID(env, streamPipeCls, "open", "()Ljava/io/InputStream;");
jmethodID closeMID = (*env)->GetMethodID(env, streamPipeCls, "close", "()V");
if (obtainMID == NULL || releaseMID == NULL || openMID == NULL || closeMID == NULL) {
LOGE(MSG("Can't get method id"));
return NULL;
}
InputStreamPipe* inputStreamPipe = (InputStreamPipe*) malloc(sizeof(InputStreamPipe));
if (inputStreamPipe == NULL) {
LOGE(MSG("Out of memory"));
return NULL;
}
inputStreamPipe->isPipe = (*env)->NewGlobalRef(env, isPipe);
inputStreamPipe->obtainMID = obtainMID;
inputStreamPipe->releaseMID = releaseMID;
inputStreamPipe->openMID = openMID;
inputStreamPipe->closeMID = closeMID;
return inputStreamPipe;
}
void destroy_input_stream_pipe(JNIEnv* env, InputStreamPipe** inputStreamPipe)
{
if (inputStreamPipe != NULL && *inputStreamPipe != NULL) {
(*env)->DeleteGlobalRef(env, (*inputStreamPipe)->isPipe);
free(*inputStreamPipe);
*inputStreamPipe = NULL;
}
}
void obtain_input_stream_pipe(JNIEnv* env, InputStreamPipe* inputStreamPipe)
{
(*env)->CallVoidMethod(env, inputStreamPipe->isPipe, inputStreamPipe->obtainMID);
}
void release_input_stream_pipe(JNIEnv* env, InputStreamPipe* inputStreamPipe)
{
(*env)->CallVoidMethod(env, inputStreamPipe->isPipe, inputStreamPipe->releaseMID);
}
InputStream* open_input_stream_from_pipe(JNIEnv* env, InputStreamPipe* inputStreamPipe)
{
jobject is = (*env)->CallObjectMethod(env, inputStreamPipe->isPipe, inputStreamPipe->openMID);
if ((*env)->ExceptionCheck(env)) {
LOGE(MSG("Catch exception"));
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
return NULL;
}
return create_input_stream(env, is);
}
void close_input_stream_from_pipe(JNIEnv* env, InputStreamPipe* inputStreamPipe)
{
(*env)->CallVoidMethod(env, inputStreamPipe->isPipe, inputStreamPipe->closeMID);
}
#endif // STREAM_SUPPORT_INPUT && STREAM_SUPPORT_INPUT_PIPE
@@ -1,49 +0,0 @@
/*
* Copyright 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Created by Hippo on 10/19/2015.
//
#ifndef STREAM_INPUT_STREAM_PIPE_H
#define STREAM_INPUT_STREAM_PIPE_H
#include "config.h"
#if defined(STREAM_SUPPORT_INPUT) && defined(STREAM_SUPPORT_INPUT_PIPE)
#include <jni.h>
#include "input_stream.h"
typedef struct
{
jobject isPipe;
jmethodID obtainMID;
jmethodID releaseMID;
jmethodID openMID;
jmethodID closeMID;
} InputStreamPipe;
InputStreamPipe* create_input_stream_pipe(JNIEnv* env, jobject isPipe);
void destroy_input_stream_pipe(JNIEnv* env, InputStreamPipe** inputStreamPipe);
void obtain_input_stream_pipe(JNIEnv* env, InputStreamPipe* inputStreamPipe);
void release_input_stream_pipe(JNIEnv* env, InputStreamPipe* inputStreamPipe);
InputStream* open_input_stream_from_pipe(JNIEnv* env, InputStreamPipe* inputStreamPipe);
void close_input_stream_from_pipe(JNIEnv* env, InputStreamPipe* inputStreamPipe);
#endif // STREAM_SUPPORT_INPUT && STREAM_SUPPORT_INPUT_PIPE
#endif // STREAM_INPUT_STREAM_PIPE_H
@@ -1,116 +0,0 @@
/*
* Copyright 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Created by Hippo on 10/19/2015.
//
#include "config.h"
#ifdef STREAM_SUPPORT_OUTPUT
#include <stdlib.h>
#include "../log.h"
#include "../utils.h"
#include "output_stream.h"
#include "stream_utils.h"
OutputStream* create_output_stream(JNIEnv* env, jobject os)
{
jclass streamCls = (*env)->GetObjectClass(env, os);
jmethodID writeMID = (*env)->GetMethodID(env, streamCls, "write", "([BII)V");
jmethodID closeMID = (*env)->GetMethodID(env, streamCls, "close", "()V");
jbyteArray buffer;
if (writeMID == NULL || closeMID == NULL) {
LOGE(MSG("Can't get write or close method id"));
return NULL;
}
buffer = (*env)->NewByteArray(env, BUFFER_SIZE);
buffer = (*env)->NewGlobalRef(env, buffer);
if (buffer == NULL) {
LOGE(MSG("Can't create buffer"));
return NULL;
}
OutputStream* outputStream = (OutputStream*) malloc(sizeof(OutputStream));
if (outputStream == NULL) {
LOGE(MSG("Out of memory"));
return NULL;
}
outputStream->os = (*env)->NewGlobalRef(env, os);
outputStream->writeMID = writeMID;
outputStream->closeMID = closeMID;
outputStream->buffer = buffer;
return outputStream;
}
void destroy_output_stream(JNIEnv* env, OutputStream** outputStream)
{
if (outputStream != NULL && *outputStream != NULL) {
(*env)->DeleteGlobalRef(env, (*outputStream)->os);
(*env)->DeleteGlobalRef(env, (*outputStream)->buffer);
free(*outputStream);
*outputStream = NULL;
}
}
size_t write_output_stream(JNIEnv* env, OutputStream* outputStream, const unsigned char* buffer, int offset, size_t size)
{
size_t remainSize = size;
size_t readSize = 0;
int bufferOffset = offset;
int len;
while (remainSize > 0) {
len = MIN(BUFFER_SIZE, remainSize);
// Copy date from buffer to outputStream->buffer
(*env)->SetByteArrayRegion(env, outputStream->buffer, 0, len, (jbyte *) (buffer + bufferOffset));
// Invoke OutputStream.write
(*env)->CallVoidMethod(env, outputStream->os, outputStream->writeMID, outputStream->buffer, 0, len);
// Catch exception
if ((*env)->ExceptionCheck(env)) {
LOGE(MSG("Catch exception"));
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
break;
}
remainSize -= len;
readSize += len;
bufferOffset += len;
}
return readSize;
}
void close_output_stream(JNIEnv* env, OutputStream* outputStream)
{
(*env)->CallVoidMethod(env, outputStream->os, outputStream->closeMID);
if ((*env)->ExceptionCheck(env)) {
LOGE(MSG("Catch exception"));
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
}
}
#endif // STREAM_SUPPORT_OUTPUT
@@ -1,45 +0,0 @@
/*
* Copyright 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Created by Hippo on 10/19/2015.
//
#ifndef STREAM_OUTPUT_STREAM_H
#define STREAM_OUTPUT_STREAM_H
#include "config.h"
#ifdef STREAM_SUPPORT_OUTPUT
#include <jni.h>
#include <stddef.h>
typedef struct
{
jobject os;
jmethodID writeMID;
jmethodID closeMID;
jbyteArray buffer;
} OutputStream;
OutputStream* create_output_stream(JNIEnv* env, jobject os);
void destroy_output_stream(JNIEnv* env, OutputStream** outputStream);
size_t write_output_stream(JNIEnv* env, OutputStream* outputStream, const unsigned char* buffer, int offset, size_t size);
void close_output_stream(JNIEnv* env, OutputStream* outputStream);
#endif // STREAM_SUPPORT_OUTPUT
#endif // STREAM_OUTPUT_STREAM_H
@@ -1,94 +0,0 @@
/*
* Copyright 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Created by Hippo on 10/19/2015.
//
#include "config.h"
#if defined(STREAM_SUPPORT_OUTPUT) && defined(STREAM_SUPPORT_OUTPUT_PIPE)
#include <stdlib.h>
#include "output_stream_pipe.h"
#include "../log.h"
OutputStreamPipe* create_output_stream_pipe(JNIEnv* env, jobject osPipe)
{
jclass streamPipeCls = (*env)->GetObjectClass(env, osPipe);
jmethodID obtainMID = (*env)->GetMethodID(env, streamPipeCls, "obtain", "()V");
jmethodID releaseMID = (*env)->GetMethodID(env, streamPipeCls, "release", "()V");
jmethodID openMID = (*env)->GetMethodID(env, streamPipeCls, "open", "()Ljava/io/OutputStream;");
jmethodID closeMID = (*env)->GetMethodID(env, streamPipeCls, "close", "()V");
if (obtainMID == NULL || releaseMID == NULL || openMID == NULL || closeMID == NULL) {
LOGE(MSG("Can't get method id"));
return NULL;
}
OutputStreamPipe* outputStreamPipe = (OutputStreamPipe*) malloc(sizeof(OutputStreamPipe));
if (outputStreamPipe == NULL) {
LOGE(MSG("Out of memory"));
return NULL;
}
outputStreamPipe->osPipe = (*env)->NewGlobalRef(env, osPipe);
outputStreamPipe->obtainMID = obtainMID;
outputStreamPipe->releaseMID = releaseMID;
outputStreamPipe->openMID = openMID;
outputStreamPipe->closeMID = closeMID;
return outputStreamPipe;
}
void destroy_output_stream_pipe(JNIEnv* env, OutputStreamPipe** outputStreamPipe)
{
if (outputStreamPipe != NULL && *outputStreamPipe != NULL) {
(*env)->DeleteGlobalRef(env, (*outputStreamPipe)->osPipe);
free(*outputStreamPipe);
*outputStreamPipe = NULL;
}
}
void obtain_output_stream_pipe(JNIEnv* env, OutputStreamPipe* outputStreamPipe)
{
(*env)->CallVoidMethod(env, outputStreamPipe->osPipe, outputStreamPipe->obtainMID);
}
void release_output_stream_pipe(JNIEnv* env, OutputStreamPipe* outputStreamPipe)
{
(*env)->CallVoidMethod(env, outputStreamPipe->osPipe, outputStreamPipe->releaseMID);
}
OutputStream* open_output_stream_from_pipe(JNIEnv* env, OutputStreamPipe* outputStreamPipe)
{
jobject os = (*env)->CallObjectMethod(env, outputStreamPipe->osPipe, outputStreamPipe->openMID);
if ((*env)->ExceptionCheck(env)) {
LOGE(MSG("Catch exception"));
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
return NULL;
}
return create_output_stream(env, os);
}
void close_output_stream_from_pipe(JNIEnv* env, OutputStreamPipe* outputStreamPipe)
{
(*env)->CallVoidMethod(env, outputStreamPipe->osPipe, outputStreamPipe->closeMID);
}
#endif // STREAM_SUPPORT_OUTPUT && STREAM_SUPPORT_OUTPUT_PIPE
@@ -1,49 +0,0 @@
/*
* Copyright 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Created by Hippo on 10/19/2015.
//
#ifndef STREAM_OUTPUT_STREAM_PIPE_H
#define STREAM_OUTPUT_STREAM_PIPE_H
#include "config.h"
#if defined(STREAM_SUPPORT_OUTPUT) && defined(STREAM_SUPPORT_OUTPUT_PIPE)
#include <jni.h>
#include "output_stream.h"
typedef struct
{
jobject osPipe;
jmethodID obtainMID;
jmethodID releaseMID;
jmethodID openMID;
jmethodID closeMID;
} OutputStreamPipe;
OutputStreamPipe* create_output_stream_pipe(JNIEnv* env, jobject osPipe);
void destroy_output_stream_pipe(JNIEnv* env, OutputStreamPipe** outputStreamPipe);
void obtain_output_stream_pipe(JNIEnv* env, OutputStreamPipe* outputStreamPipe);
void release_output_stream_pipe(JNIEnv* env, OutputStreamPipe* outputStreamPipe);
OutputStream* open_output_stream_from_pipe(JNIEnv* env, OutputStreamPipe* outputStreamPipe);
void close_output_stream_from_pipe(JNIEnv* env, OutputStreamPipe* outputStreamPipe);
#endif // STREAM_SUPPORT_OUTPUT && STREAM_SUPPORT_OUTPUT_PIPE
#endif // STREAM_OUTPUT_STREAM_PIPE_H
@@ -1,131 +0,0 @@
/*
* Copyright 2016 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Created by Hippo on 1/1/2016.
//
#include "config.h"
#if defined(STREAM_SUPPORT_INPUT) && defined(STREAM_SUPPORT_INPUT_PATCH_HEAD)
#include <stdlib.h>
#include <string.h>
#include "patch_head_input_stream.h"
#include "../log.h"
#include "../utils.h"
#define READ_BUFFER_SIZE (256 * 1024) // 256KB
PatchHeadInputStream* create_patch_head_input_stream(
InputStream* input_stream, const unsigned char* patch, unsigned int patch_length)
{
unsigned char* patch_copy;
PatchHeadInputStream* patch_head_input_stream = (PatchHeadInputStream*) malloc(sizeof(PatchHeadInputStream));
if (patch_head_input_stream == NULL) {
WTF_OM;
return NULL;
}
// Copy patch
patch_copy = (unsigned char*) malloc(patch_length * sizeof(char));
if (patch_copy == NULL) {
WTF_OM;
free(patch_head_input_stream);
return NULL;
}
memcpy(patch_copy, patch, patch_length);
patch_head_input_stream->input_stream = input_stream;
patch_head_input_stream->patch = patch_copy;
patch_head_input_stream->patch_length = patch_length;
patch_head_input_stream->read_count = 0;
return patch_head_input_stream;
}
size_t read_patch_head_input_stream(JNIEnv* env, PatchHeadInputStream* patch_head_input_stream,
unsigned char* buffer, int offset, size_t size)
{
size_t len = MIN(size, patch_head_input_stream->patch_length - patch_head_input_stream->read_count);
int buffer_offset = offset;
if (len > 0) {
memcpy(buffer + buffer_offset, patch_head_input_stream->patch + patch_head_input_stream->read_count, len);
patch_head_input_stream->read_count += len;
buffer_offset += len;
}
if (size > len) {
len += read_input_stream(env, patch_head_input_stream->input_stream, buffer, buffer_offset, size - len);
}
return len;
}
void* read_patch_head_input_stream_all(JNIEnv* env, PatchHeadInputStream* patch_head_input_stream, size_t* length)
{
size_t len = 0;
size_t read;
void* buffer = NULL;
buffer = malloc(READ_BUFFER_SIZE);
if (buffer == NULL) {
WTF_OM;
return NULL;
}
for (;;) {
// Read from stream
read = read_patch_head_input_stream(env, patch_head_input_stream, buffer, len, READ_BUFFER_SIZE);
len += read;
// Check stream end
if (read < READ_BUFFER_SIZE) {
// Get the end, shrink the buffer
buffer = realloc(buffer, len);
*length = len;
return buffer;
}
// Extent buffer
buffer = realloc(buffer, len + READ_BUFFER_SIZE);
if (buffer == NULL) {
WTF_OM;
return NULL;
}
}
}
void destroy_patch_head_input_stream(JNIEnv* env, PatchHeadInputStream** patch_head_input_stream)
{
if (patch_head_input_stream != NULL && *patch_head_input_stream != NULL) {
// Free input stream
destroy_input_stream(env, &(*patch_head_input_stream)->input_stream);
// Free patch
free((*patch_head_input_stream)->patch);
(*patch_head_input_stream)->patch = NULL;
// Free itself
free(*patch_head_input_stream);
*patch_head_input_stream = NULL;
}
}
void close_patch_head_input_stream(JNIEnv* env, PatchHeadInputStream* patch_head_input_stream)
{
close_input_stream(env, patch_head_input_stream->input_stream);
}
#endif // STREAM_SUPPORT_INPUT && STREAM_SUPPORT_INPUT_PATCH_HEAD
@@ -1,47 +0,0 @@
/*
* Copyright 2016 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Created by Hippo on 1/1/2016.
//
#ifndef STREAM_PATCH_HEAD_INPUT_STREAM_H
#define STREAM_PATCH_HEAD_INPUT_STREAM_H
#include "config.h"
#if defined(STREAM_SUPPORT_INPUT) && defined(STREAM_SUPPORT_INPUT_PATCH_HEAD)
#include "input_stream.h"
typedef struct
{
InputStream* input_stream;
unsigned char* patch;
unsigned int patch_length;
unsigned int read_count;
} PatchHeadInputStream;
PatchHeadInputStream* create_patch_head_input_stream(
InputStream* input_stream, const unsigned char* patch, unsigned int patch_length);
void destroy_patch_head_input_stream(JNIEnv* env, PatchHeadInputStream** patch_head_input_stream);
size_t read_patch_head_input_stream(JNIEnv* env, PatchHeadInputStream* patch_head_input_stream,
unsigned char* buffer, int offset, size_t size);
void* read_patch_head_input_stream_all(JNIEnv* env, PatchHeadInputStream* patch_head_input_stream, size_t* length);
void close_patch_head_input_stream(JNIEnv* env, PatchHeadInputStream* patch_head_input_stream);
#endif // STREAM_SUPPORT_INPUT && STREAM_SUPPORT_INPUT_PATCH_HEAD
#endif // STREAM_PATCH_HEAD_INPUT_STREAM_H
@@ -1,26 +0,0 @@
/*
* Copyright 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Created by Hippo on 10/19/2015.
//
#ifndef STREAM_STREAM_UTILS_H
#define STREAM_STREAM_UTILS_H
#define BUFFER_SIZE 256
#endif // STREAM_STREAM_UTILS_H
+1 -1
View File
@@ -23,7 +23,7 @@
#include <zlib.h>
#include <nettle/config.h>
#include "java_wrapper.h"
#include "image/java_wrapper.h"
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env;