I have a very large char buffer in C and need to copy some part of it to a Java array.
Specifically, I need the elements starting at 16,384 and ending at 32000. How can I do this?
Initially I tried this:
jbyte * bytes = (* env) -> GetByteArrayElements (env, array, NULL);
memmove (bytes, (jbyte *) buffer, buffer_size);
(* Env) -> ReleaseByteArrayElements (env, array, (jbyte *) bytes, 0);
(* Env) -> CallStaticVoidMethod (env, cls, mid, buffer_size);
But with this code the entire buffer is transmitted, and it is very big (more than 40 MB). I need only a small part of buffer.
EDIT: Thank you very much, but your version is not quite working. I applied it as follows: memmove (array, (jbyte *) (buffer + numbers), 16385); in the buffer - at each iteration is copied to the "buffer" new 16384 bytes. such figures in the amount of buffer and numbers, respectively: 16384 - 0
32,768 - 16,385
49152 - 32769
65536 - 49153
81,920 - 65,537
98,304 - 81,921
Ie at each iteration by turns in the "numbers" of right-hand column. As a result, - bytes are copied are not always successful. the first iteration is always successful. Further, by turns both successful and unsuccessful. memtcpy gives the same result. What will advise? How to solve the problem?
EDIT2: My code:
JNIEXPORT jint work (JNIEnv * env, jobject obj, jbyteArray array)
{
int argc;
char * args [3];
char * argv [3];
argv [1] = "Music/Tg.mp3";
argv [2] = "testwavS3.flac";
argc = 3;
sox_effects_chain_t * chain;
sox_effect_t * e;
static sox_format_t * in, * out; / * input and output files * /
char * buffer;
size_t buffer_size;
size_t number_read;
/ * All libSoX applications must start by initialising the SoX library * /
sox_init ();
/ * Open the input file (with default parameters) * /
in = sox_open_read (argv [1], NULL, NULL, NULL);
# Define MAX_SAMPLES (size_t) 8192
__android_log_write (ANDROID_LOG_ERROR, "Read", "Haha");
sox_sample_t samples [MAX_SAMPLES]; / * Temporary store whilst copying. * /
jclass cls;
jmethodID mid, mid2;
cls = (* env) -> GetObjectClass (env, obj);
mid = (* env) -> GetStaticMethodID (env, cls, "testt",
"(I) V");
jbyteArray bytearrayBuffer = (* env) -> NewByteArray (env, & in-> signal.length); / / construct a new byte array
out = sox_open_memstream_write (& buffer, & buffer_size, & in-> signal, NULL, "sox", NULL);
int numbers = 0;
in-> encoding.bits_per_sample = 16;
out-> encoding.bits_per_sample = 16;
(* Env) -> GetByteArrayElements (env, array, NULL);
while (number_read = sox_read (in, samples, MAX_SAMPLES)) {
sox_write (out, samples, number_read);
memmove (array, (jbyte *) (buffer + numbers), (buffer_size-numbers));
numbers + = buffer_size-numbers;
(* Env) -> CallStaticVoidMethod (env, cls, mid, buffer_size);
}
sox_close (out);
sox_close (in);
# If! Defined FIXED_BUFFER
free (buffer);
# Endif
}
using SoX to decode the audio and give buffer in Java.