【问题标题】:how to convert short array to byte array如何将短数组转换为字节数组
【发布时间】:2012-06-04 00:19:13
【问题描述】:

我找到了converting a short to byte arraybyte array to short array,但不是短数组到字节数组。

这是导致转换的代码

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): gainBuffer() 轨道 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):在 java.nio.ShortBuffer.put(ShortBuffer.java:422)

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

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

05-29 13:41:12.857:W/AudioWorker(9758):在 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));
            }

【问题讨论】:

标签: java arrays


【解决方案1】:

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

平台:Nexus S,Android 4.1.1,无 SIM 卡

方法 #1:使用 ByteBuffer

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

TypeCast.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 ByteBuffer:865 毫秒

方法 #2 旋转:299 毫秒

方法 #3 C:39 毫秒

【讨论】:

  • 请您上传并提供C方法的.SO文件的链接。这真的很有帮助。我不想为此下载ndk。谢谢。
【解决方案2】:

Java short 是 16 位类型,byte 是 8 位类型。您有一个循环尝试将 N 短裤插入到长度为 N-bytes 的缓冲区中;它需要为 2*N 字节长以适合您的所有数据。

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-05-27
  • 1970-01-01
  • 2013-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多