Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
异常:java.lang.ArrayStoreException_Java - Fatal编程技术网

异常:java.lang.ArrayStoreException

异常:java.lang.ArrayStoreException,java,Java,我收到一个异常java.lang.ArrayStoreException: 我正在发布我的完整代码。 这是编码wav文件的代码。 ArrayStoreException package org.xiph.speex; import static java.nio.file.Files.size; /** * Main Speex Encoder class. * This class encodes the given PCM 16bit samples into Speex pack

我收到一个异常java.lang.ArrayStoreException: 我正在发布我的完整代码。 这是编码wav文件的代码。 ArrayStoreException

 package org.xiph.speex;

import static java.nio.file.Files.size;

/**
 * Main Speex Encoder class.
 * This class encodes the given PCM 16bit samples into Speex packets.
 *
 * @author Marc Gimpel, Wimba S.A. (mgimpel@horizonwimba.com)
 * @version $Revision: 1.6 $
 */
public class SpeexEncoder
{
  /**
   * Version of the Speex Encoder
   */
  public static final String VERSION = "Java Speex Encoder v0.9.7 ($Revision: 1.6 $)";

  private Encoder encoder;
 private Bits    bits;
  private float[] rawData;
  private int     sampleRate;
  private int     channels;
  private int     frameSize;

  /**
   * Constructor
   */
  public SpeexEncoder()
  {
    bits = new Bits();
  }

  /**
   * initialization
   * @param mode       the mode of the encoder (0=NB, 1=WB, 2=UWB).
   * @param quality    the quality setting of the encoder (between 0 and 10).
   * @param sampleRate the number of samples per second.
   * @param channels   the number of audio channels (1=mono, 2=stereo, ...).
   * @return true if initialisation successful.
   */
  public boolean init(final int mode,
                      final int quality,
                      final int sampleRate,
                      final int channels)
  {
    switch (mode) {
      case 0:
        encoder = new NbEncoder();
        ((NbEncoder)encoder).nbinit();
        break;
//Wideband
      case 1:
        encoder = new SbEncoder();
        ((SbEncoder)encoder).wbinit();
        break;
      case 2:
        encoder = new SbEncoder();
        ((SbEncoder)encoder).uwbinit();
        break;
//*/
      default:
        return false;
    }

    /* initialize the speex decoder */
    encoder.setQuality(quality);

    /* set decoder format and properties */
    this.frameSize  = encoder.getFrameSize();
    this.sampleRate = sampleRate;
    this.channels   = channels;
    rawData         = new float[channels*frameSize];

    bits.init();
    return true;
  }

  /**
   * Returns the Encoder being used (Narrowband, Wideband or Ultrawideband).
   * @return the Encoder being used (Narrowband, Wideband or Ultrawideband).
   */
  public Encoder getEncoder()
  {
    return encoder;
  }

  /**
   * Returns the sample rate.
   * @return the sample rate.
   */
  public int getSampleRate()
  {
    return sampleRate;
  }

  /**
   * Returns the number of channels.
   * @return the number of channels.
   */
  public int getChannels()
  {
    return channels;
  }

  /**
   * Returns the size of a frame.
   * @return the size of a frame.
   */
  public int getFrameSize()
  {
    return frameSize;
  }

    public void setComplexity(int complexity) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

  /**
   * Pull the decoded data out into a byte array at the given offset
   * and returns the number of bytes of encoded data just read.

   * @param offset
   * @return the number of bytes of encoded data just read.
   */
  public int getProcessedData(final byte[] data, final int offset)
  {
      int size = bits.getBufferSize();
   System.out.println("value is:"+bits.getBuffer());
   System.arraycopy(bits.getBuffer(),0, data, offset,size);
   bits.init();
 // System.out.println("size is:"+ size);
   return size;
  }
 /**
   * Returns the number of bytes of encoded data ready to be read.
   * @return the number of bytes of encoded data ready to be read.
   */
  public void getProcessedDataByteSize()
  {
  }

  /**
   * This is where the actual encoding takes place
   * @param data
   * @param offset
   * @param len
   * @return true if successful.
   */
  public boolean processData(final byte[] data,
                             final int offset,
                             final int len)
  {
    // converty raw bytes into float samples
   mapPcm16bitLittleEndian2Float(data, offset, rawData, 0, len/2);
    // encode the bitstream
    return processData(rawData, len/2);
  }

  /**
   * Encode an array of shorts.
   * @param data
   * @param offset
   * @param numShorts
   * @return true if successful.
   */
  public boolean processData(final short[] data,
                             final int offset,
                             final int numShorts)
  {
    int numSamplesRequired = channels * frameSize;
    if (numShorts != numSamplesRequired) {
      throw new IllegalArgumentException("SpeexEncoder requires " + numSamplesRequired + " samples to process a Frame, not " + numShorts);
    }
    // convert shorts into float samples,
    for (int i=0; i<numShorts; i++) {
      rawData[i] = data[offset + i ];
    }
    // encode the bitstream
    return processData(rawData, numShorts);
  }

  /**
   * Encode an array of floats.
   * @param data
   * @param numSamples
   * @return true if successful.
   */
  public boolean processData(final float[] data, final int numSamples)
  {
    int numSamplesRequired = channels * frameSize;
    if (numSamples != numSamplesRequired) {
      throw new IllegalArgumentException("SpeexEncoder requires " + numSamplesRequired + " samples to process a Frame, not " + numSamples );
    }
    // encode the bitstream
    if (channels==2) {
      Stereo.encode(bits, data, frameSize);
    }
    encoder.encode(bits, data);
    return true;
  }

  /**
   * Converts a 16 bit linear PCM stream (in the form of a byte array)
   * into a floating point PCM stream (in the form of an float array).
   * Here are some important details about the encoding:
   * <ul>
   * <li> Java uses big endian for shorts and ints, and Windows uses little Endian.
   *      Therefore, shorts and ints must be read as sequences of bytes and
   *      combined with shifting operations.
   * </ul>
   * @param pcm16bitBytes - byte array of linear 16-bit PCM formated audio.
   * @param offsetInput
   * @param samples - float array to receive the 16-bit linear audio samples.
   * @param offsetOutput
   * @param length
   */
  public static void mapPcm16bitLittleEndian2Float(final byte[] pcm16bitBytes,
                                                   final int offsetInput,
                                                   final float[] samples,
                                                   final int offsetOutput,
                                                   final int length)
  {
    if (pcm16bitBytes.length - offsetInput < 2 * length) {
      throw new IllegalArgumentException("Insufficient Samples to convert to floats");
    }
    if (samples.length - offsetOutput < length) {
      throw new IllegalArgumentException("Insufficient float buffer to convert the samples");
    }
    for (int i = 0; i < length; i++) {
      samples[offsetOutput+i] = ((pcm16bitBytes[offsetInput+2*i] & 0xff) | (pcm16bitBytes[offsetInput+2*i+1] << 8)); // no & 0xff at the end to keep the sign
    }
  }

   // public int getProcessedData(byte[] temp, int i) {
      //  return 0;
  //      throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  //  }






    private static class Stereo {

        private static void encode(Bits bits, float[] data, int frameSize) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        private Stereo() {
        }
    }


     static class Bits {
      //  private Object getBuffer;
       // private Object getBufferSize;


       // private void Bits() {
     //   }

        private int getBufferSize() {
           // System.out.println("hello");
           return 0;
       //  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        public int getBuffer() {
           // return null;
          return 0;
        //   throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }



       public int init() {
           return 0;
      //      throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

       }
     }
}
请帮我摆脱这一切。 这是我的全部代码。
获取getProcessedData中的异常ArrayStoreException查看您提供的其余代码,Google从

您可以在其中看到和的实现

事实上,一个更简单的解决方案是

git clone https://github.com/phono/PhonoSDK
您将拥有可能需要的所有代码的副本

bits.getBuffer的返回类型为int

顾名思义,arraycopy方法仅适用于数组。您必须从一个数组复制到同一类型的另一个数组。您不能从say int[]复制到byte[],也不能从int复制到byte[]


顺便说一句,如果您有一个getBufferSize,您可能希望getBuffer返回一个类似getBufferSize大小的字节[]的缓冲区,那么您的API似乎有问题。

请确保数组的数据类型正确

System.arraycopy(bits.getBuffer(),0, data, offset,size);
在该行中,控制bits.getBuffer-type和数据类型


您为第一个参数传递了基元数据类型,它必须是Object。你说System.arraycopybits.getBuffer,0,数据,偏移量,大小;bits.getBuffer是基元而不是对象

我不确定您想做什么,但如果我是您,我将按如下方式修改代码。由于代码中充满了许多占位符,请用逻辑替换它们

package org.xiph.speex;


class Bits {
    byte[] backingArray;

    Bits() {
        this.backingArray = new byte[65535];
    }

    Bits(int size) {
        this.backingArray = new byte[size];
    }

    int getBufferSize() {
        return this.backingArray.length;
    }

    public byte[] getBuffer() {
        return this.backingArray;
    }

    public int feed(byte[] src, int offset, int sz) throws IllegalArgumentException {
        if ( sz > this.backingArray.length ) {
            throw new IllegalArgumentException("data is too big to fit in");
        }

        if ( src.length - offset < sz ) {
            sz = src.length - offset;
        }

        System.arraycopy(src, offset, this.backingArray, 0, sz);
        return sz;
    }

    public void init() {
        // whatever
    }
}

class Stereo {

    static void encode(Bits bits, float[] data, int frameSize) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    Stereo() {
    }
}

interface Encoder {
    void init();
    void setQuality(int quality);
    int getFrameSize();
    void encode(Bits bits, float[] data);
}

class NarrowBandEncoder implements Encoder {
    @Override
    public void init() {
        // whatever
    }

    @Override
    public void setQuality(int quality) {
        // TODO Auto-generated method stub

    }

    @Override
    public int getFrameSize() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void encode(Bits bits, float[] data) {
        // TODO Auto-generated method stub

    }
}

class WidebandEncoder implements Encoder {
    @Override
    public void init() {
        // whatever
    }

    @Override
    public void setQuality(int quality) {
        // TODO Auto-generated method stub

    }

    @Override
    public int getFrameSize() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void encode(Bits bits, float[] data) {
        // TODO Auto-generated method stub

    }
}

class UltraWidebandEncoder implements Encoder {
    @Override
    public void init() {
        // whatever
    }

    @Override
    public void setQuality(int quality) {
        // TODO Auto-generated method stub

    }

    @Override
    public int getFrameSize() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void encode(Bits bits, float[] data) {
        // TODO Auto-generated method stub

    }
}

enum EncodingMode {
    NARROW_BAND(NarrowBandEncoder::new),
    WIDE_BAND(WidebandEncoder::new),
    ULTRA_WIDE_BAND(UltraWidebandEncoder::new);

    private Supplier<? extends Encoder> encoder;

    private EncodingMode(Supplier<? extends Encoder> supplier) {
        this.encoder = supplier;
    }

    public Encoder createEncoder() {
        return this.encoder.get();
    }
}


/**
 * Main Speex Encoder class.
 * This class encodes the given PCM 16bit samples into Speex packets.
 *
 * @author Marc Gimpel, Wimba S.A. (mgimpel@horizonwimba.com)
 * @version $Revision: 1.6 $
 */
public class SpeexEncoder
{
    /**
     * Version of the Speex Encoder
     */
    public static final String VERSION = "Java Speex Encoder v0.9.7 ($Revision: 1.6 $)";

    private Encoder encoder;
    private Bits    bits;
    private float[] rawData;
    private int     sampleRate;
    private int     channels;
    private int     frameSize;

    /**
     * Constructor
     */
    public SpeexEncoder()
    {
        bits = new Bits();
    }

    /**
     * initialization
     * @param mode       the mode of the encoder (0=NB, 1=WB, 2=UWB).
     * @param quality    the quality setting of the encoder (between 0 and 10).
     * @param sampleRate the number of samples per second.
     * @param channels   the number of audio channels (1=mono, 2=stereo, ...).
     * @return true if initialisation successful.
     */
    public boolean init(final EncodingMode mode,
            final int quality,
            final int sampleRate,
            final int channels)
    {
        this.encoder = mode.createEncoder();
        this.encoder.init();

        /* initialize the speex decoder */
        this.encoder.setQuality(quality);

        /* set decoder format and properties */
        this.frameSize  = encoder.getFrameSize();
        this.sampleRate = sampleRate;
        this.channels   = channels;
        this.rawData    = new float[channels*frameSize];

        this.bits.init();
        return true;
    }

    /**
     * Returns the Encoder being used (Narrowband, Wideband or Ultrawideband).
     * @return the Encoder being used (Narrowband, Wideband or Ultrawideband).
     */
    public Encoder getEncoder()
    {
        return encoder;
    }

    /**
     * Returns the sample rate.
     * @return the sample rate.
     */
    public int getSampleRate()
    {
        return sampleRate;
    }

    /**
     * Returns the number of channels.
     * @return the number of channels.
     */
    public int getChannels()
    {
        return channels;
    }

    /**
     * Returns the size of a frame.
     * @return the size of a frame.
     */
    public int getFrameSize()
    {
        return frameSize;
    }

    public void setComplexity(int complexity) {
        //        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    /**
     * Pull the decoded data out into a byte array at the given offset
     * and returns the number of bytes of encoded data just read.

     * @param offset
     * @return the number of bytes of encoded data just read.
     */
    public int getProcessedData(final byte[] data, final int offset)
    {
        int sz = this.bits.feed(data, offset, this.bits.getBufferSize());
        this.bits.init();
        return sz;
    }
    /**
     * Returns the number of bytes of encoded data ready to be read.
     * @return the number of bytes of encoded data ready to be read.
     */
    public void getProcessedDataByteSize()
    {
        // ?
    }

    /**
     * This is where the actual encoding takes place
     * @param data
     * @param offset
     * @param len
     * @return true if successful.
     */
    public boolean processData(final byte[] data,
            final int offset,
            final int len)
    {
        // converty raw bytes into float samples
        mapPcm16bitLittleEndian2Float(data, offset, rawData, 0, len/2);
        // encode the bitstream
        return processData(rawData, len/2);
    }

    /**
     * Encode an array of shorts.
     * @param data
     * @param offset
     * @param numShorts
     * @return true if successful.
     */
    public boolean processData(final short[] data,
            final int offset,
            final int numShorts)
    {
        int numSamplesRequired = channels * frameSize;
        if (numShorts != numSamplesRequired) {
            throw new IllegalArgumentException("SpeexEncoder requires " + numSamplesRequired + " samples to process a Frame, not " + numShorts);
        }
        // convert shorts into float samples,
        for (int i=0; i<numShorts; i++) {
            rawData[i] = data[offset + i ];
        }
        // encode the bitstream
        return processData(rawData, numShorts);
    }

    /**
     * Encode an array of floats.
     * @param data
     * @param numSamples
     * @return true if successful.
     */
    public boolean processData(final float[] data, final int numSamples)
    {
        int numSamplesRequired = channels * frameSize;
        if (numSamples != numSamplesRequired) {
            throw new IllegalArgumentException("SpeexEncoder requires " + numSamplesRequired + " samples to process a Frame, not " + numSamples );
        }
        // encode the bitstream
        if (channels==2) {
            Stereo.encode(bits, data, frameSize);
        }
        encoder.encode(bits, data);
        return true;
    }

    /**
     * Converts a 16 bit linear PCM stream (in the form of a byte array)
     * into a floating point PCM stream (in the form of an float array).
     * Here are some important details about the encoding:
     * <ul>
     * <li> Java uses big endian for shorts and ints, and Windows uses little Endian.
     *      Therefore, shorts and ints must be read as sequences of bytes and
     *      combined with shifting operations.
     * </ul>
     * @param pcm16bitBytes - byte array of linear 16-bit PCM formated audio.
     * @param offsetInput
     * @param samples - float array to receive the 16-bit linear audio samples.
     * @param offsetOutput
     * @param length
     */
    public static void mapPcm16bitLittleEndian2Float(final byte[] pcm16bitBytes,
            final int offsetInput,
            final float[] samples,
            final int offsetOutput,
            final int length)
    {
        if (pcm16bitBytes.length - offsetInput < 2 * length) {
            throw new IllegalArgumentException("Insufficient Samples to convert to floats");
        }
        if (samples.length - offsetOutput < length) {
            throw new IllegalArgumentException("Insufficient float buffer to convert the samples");
        }
        for (int i = 0; i < length; i++) {
            samples[offsetOutput+i] = ((pcm16bitBytes[offsetInput+2*i] & 0xff) | (pcm16bitBytes[offsetInput+2*i+1] << 8)); // no & 0xff at the end to keep the sign
        }
    }

    // public int getProcessedData(byte[] temp, int i) {
    //  return 0;
    //      throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    //  }
}

你能提供stacktrace吗?bits.getBuffer的返回类型是什么?它是否返回一个足够大的数组来保存数组数据?将引发ArrayStoreException,并且不会修改目标:src参数引用的对象不是数组。dest参数引用的对象不是数组。src参数和dest参数引用的数组的组件类型是不同的基元类型。src参数是指具有基元组件类型的数组,dest参数是指具有引用组件类型的数组。src参数是指具有引用组件类型的数组,dest参数是指具有基元组件类型的数组。@1337请在我的代码上下文中解释我。@Anika什么是位?。。。都是同一类型。我已经发布了我的完整代码。请帮帮我,我该怎么办。。。将此代码用于问题行数组。copyOforiginal,newLength;它返回数组并分配给数组。你可以使用Arrays.copyOfRange@Peter Lawrey我已经发布了我的完整代码。“请帮帮我该怎么办。”赛米·奥肯·佩利文:我没有得到你的陈述。将此代码用于问题行–我已发布了完整代码。请帮帮我,我该怎么办do@Anika我认为你应该弄清楚比特的用途,以及你打算如何实现它。@Anika我假设你没有写剩下的代码,为什么你不从你得到代码的同一个地方得到比特呢?也许你可以向作者索要他/她的电子邮件地址的副本?@Anika既然你复制了第一个类,我建议你用谷歌搜索并复制其他类。我已经添加了到“在我的答案中加入”的链接。@Peter Lawrey我已经添加了类位的代码,但仍然存在相同的异常…我正在尝试,但这会产生很多错误