如何将短数组转换为字节数组

2022-09-02 19:14:20

我发现将短数组转换为字节数组,将字节数组转换为短数组,但不能将短数组转换为字节数组。

这是导致转换的代码

while(!stopped)
        { 
            Log.i("Map", "Writing new data to buffer");
            short[] buffer = buffers[ix++ % buffers.length];

            N = recorder.read(buffer,0,buffer.length);
            track.write(buffer, 0, buffer.length);

            byte[] bytes2 = new byte[N];

我试过

              int i = 0;
              ByteBuffer byteBuf = ByteBuffer.allocate(N);
              while (buffer.length >= i) {
                  byteBuf.putShort(buffer[i]);
                  i++;
        }

bytes2 = byteBuf.array();

    ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(buffer);

但是,我在两者上都收到此错误(如果错误不完全相同,但两者都非常相似):

05-29 13:41:12.021: W/AudioTrack(9758): getBuffer() 轨道0x30efa0禁用,重新启动

05-29 13:41:12.857: W/AudioWorker(9758): 读取语音音频工作者时出错

05-29 13:41:12.857: W/AudioWorker(9758): java.nio.BufferOverflowException

05-29 13:41:12.857: W/AudioWorker(9758): at java.nio.ShortBuffer.put(ShortBuffer.java:422)

05-29 13:41:12.857: W/AudioWorker(9758): at java.nio.ShortToByteBufferAdapter.put(ShortToByteBufferAdapter.java:210)

05-29 13:41:12.857: W/AudioWorker(9758): at java.nio.ShortBuffer.put(ShortBuffer.java:391)

05-29 13:41:12.857: W/AudioWorker(9758): at com.avispl.nicu.audio.audioworker.run(AudioWorker.java:126)

为了在这里提供尽可能多的信息,代码使用字节数组

Log.i("Map", "test");
                //convert to ulaw
                read(bytes2, 0, N);

                //send to server
                os.write(bytes2,0,bytes2.length);

                System.out.println("bytesRead "+buffer.length);
                System.out.println("data "+Arrays.toString(buffer));
            }

答案 1

我发现ByteBuffer是我分析过的三种转换方法中最慢的转换方法。见下文...

平台: Nexus S, 安卓 4.1.1, 无 SIM 卡

方法#1:使用字节缓冲区

byte [] ShortToByte_ByteBuffer_Method(short [] input)
{
  int index;
  int iterations = input.length;

  ByteBuffer bb = ByteBuffer.allocate(input.length * 2);

  for(index = 0; index != iterations; ++index)
  {
    bb.putShort(input[index]);    
  }

  return bb.array();       
}

方法#2:直接摆动位

byte [] ShortToByte_Twiddle_Method(short [] input)
{
  int short_index, byte_index;
  int iterations = input.length;

  byte [] buffer = new byte[input.length * 2];

  short_index = byte_index = 0;

  for(/*NOP*/; short_index != iterations; /*NOP*/)
  {
    buffer[byte_index]     = (byte) (input[short_index] & 0x00FF); 
    buffer[byte_index + 1] = (byte) ((input[short_index] & 0xFF00) >> 8);

    ++short_index; byte_index += 2;
  }

  return buffer;
}

方法#3:通过JNI使用C

类型转换.java

package mynamespace.util;

public class TypeCast
{
  public static native byte [] shortToByte(short [] input);

  static
  {
    System.loadLibrary("type_conversion");
  }
}

native.c

#include <jni.h>
#include <string.h>

jbyteArray Java_mynamespace_util_TypeCast_shortToByte(JNIEnv *env, jobject obj, jshortArray input)
{
  jshort     *input_array_elements;
  int         input_length;

  jbyte      *output_array_elements;
  jbyteArray  output;

  input_array_elements = (*env)->GetShortArrayElements(env, input, 0);
  input_length         = (*env)->GetArrayLength(env, input);

  output                = (jbyteArray) ((*env)->NewByteArray(env, input_length * 2));
  output_array_elements = (*env)->GetByteArrayElements(env, output, 0);

  memcpy(output_array_elements, input_array_elements, input_length * 2);

  (*env)->ReleaseShortArrayElements(env, input, input_array_elements, JNI_ABORT);
  (*env)->ReleaseByteArrayElements(env, output, output_array_elements, 0);

  return output;
}

结果:

对于一百万个元素输入数组,执行时间如下:

方法 #1 字节缓冲区: 865 毫秒

方法#2摆动:299毫秒

方法#3 C:39 ms


答案 2

Java 是 16 位类型,也是 8 位类型。您有一个循环,该循环尝试将短整型插入到长度为 -字节的缓冲区中;它需要是字节长才能容纳所有数据。shortbyteNN2*N

ByteBuffer byteBuf = ByteBuffer.allocate(2*N);
while (N >= i) {
    byteBuf.putShort(buffer[i]);
    i++;
}