将字节从一个数组复制到另一个数组而不用Java中的迭代

时间:2016-09-29 16:49:39

标签: java arrays bluetooth

有没有更快的方法将字节从一个数组复制到另一个数组而不进行迭代?

我正在通过蓝牙从输入流中读取字节

public void run() {
        byte[] buffer = new byte[100];  // buffer store for the stream
        int bytes; // bytes returned from read()
        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer); // Get number of bytes and message in "buffer"
                h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget();     // Send to message queue Handler
            } catch (IOException e) {
                break;
            }
        }
    }

这会向处理程序发送一个随机数字节。 然后我读取这些并将它们放入一个数组中,然后在下载完成后处理数据。从PIC micro发送的数据长度为6055字节。

h = new Handler() {
            public void handleMessage(android.os.Message msg) {
                switch (msg.what) {
                    case SUCCESS_CONNECT:
                        // Connected so start connected thread
                        btSocket = (BluetoothSocket) msg.obj;
                        byteCount = 0;
                        arrayCount = 0;
                        mConnectedThread = new FullDataActivity.ConnectedThread(btSocket);
                        mConnectedThread.start();
                        mConnectedThread.write(getFullDataConByte); // Send 255 to start
                        break;
                    case RECIEVE_MESSAGE:  // if receive massage
                        byte[] readBuf = (byte[]) msg.obj;
                        // iterate through obj and copy bytes to fullDataChunk array
                        for (int a = 0; a < msg.arg1; a++) {
                            fullDataChunk[byteCount] = readBuf[a];
                            Log.d("readBuf[a] = ",Integer.toString(readBuf[a] & 0xFF));
                            byteCount++;
                        }
                        // if all bytes done process
                        if (byteCount == 6055) {// process data when complete.

我的处理程序在某处丢失了字节,并在for循环中复制它们时破坏了数据。我不知道run方法是否在for循环中处理其余部分之前向处理程序发送新字节。 我已经完成了Run中发送的字节的记录,它们是正确的。当处理程序处理它们出错时。

我要么确保在发送新数据之前处理发送的数据,要么更快地将数据复制到数组? 任何想法。

2 个答案:

答案 0 :(得分:2)

复制数组的最快方法应该是使用System.arraycopy()

答案 1 :(得分:1)

在运行中这样做,然后处理程序接收完整数组而没有错误。

  public void run() {
            byte[] fullBuffer = new byte[6055];
            byte[] buffer = new byte[100];  // buffer store for the stream
            int bytes; // bytes returned from read()
            int bytesCount = 0;
            // Keep listening to the InputStream until an exception occurs
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);        // Get number of bytes and message in "buffer"
                    System.arraycopy(buffer,0,fullBuffer,bytesCount,bytes);
                    bytesCount = bytesCount + bytes;
                    Log.d("FD Read - ", Integer.toString(bytesCount));
                    if(bytesCount >= 6055){
                        h.obtainMessage(RECIEVE_MESSAGE, bytesCount, -1, fullBuffer).sendToTarget();     // Send to message queue Handler
                        Log.d("FD Read - ", "Message sent");
                        bytesCount = 0;
                        Log.d("FD Read - ", "bytesCount re-set");
                    }
                    //h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget();     // Send to message queue Handler
                } catch (IOException e) {
                    break;
                }
            }
        }