【问题标题】:Appending a byte[] to the end of another byte[] [duplicate]将一个字节 [] 附加到另一个字节 [] 的末尾 [重复]
【发布时间】:2011-03-20 13:13:49
【问题描述】:

我有两个长度未知的 byte[] 数组,我只想将一个附加到另一个的末尾,即:

byte[] ciphertext = blah;
byte[] mac = blah;
byte[] out = ciphertext + mac;

我尝试过使用arraycopy(),但似乎无法正常工作。

【问题讨论】:

    标签: java byte bytearray arrays


    【解决方案1】:

    使用System.arraycopy(),应该可以使用以下内容:

    // create a destination array that is the size of the two arrays
    byte[] destination = new byte[ciphertext.length + mac.length];
    
    // copy ciphertext into start of destination (from pos 0, copy ciphertext.length bytes)
    System.arraycopy(ciphertext, 0, destination, 0, ciphertext.length);
    
    // copy mac into end of destination (from pos ciphertext.length, copy mac.length bytes)
    System.arraycopy(mac, 0, destination, ciphertext.length, mac.length);
    

    【讨论】:

    • 太棒了!感谢您的快速解决方案。像魅力一样工作,现在看起来很简单!
    • 如果我们事先不知道最终数组的大小,我相信会造成困难。
    • @Shashwat 在这种情况下,我们已经知道 ciphertextmac 字节数组的长度。这意味着我们知道新字节数组的长度应该是原始 2 字节数组的组合长度。
    【解决方案2】:

    也许是最简单的方法:

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    
    output.write(ciphertext);
    output.write(mac);
    
    byte[] out = output.toByteArray();
    

    【讨论】:

    • 最简单,也许是最好的支持,因为 ByteArrayBuffer 似乎已弃用。
    • 最佳答案。不用自己计算就能保证容量。
    • 救命答案
    【解决方案3】:

    你需要将out声明为一个字节数组,长度等于ciphertextmac的长度相加,然后将ciphertext复制到outmac的开头最后,使用arraycopy。

    byte[] concatenateByteArrays(byte[] a, byte[] b) {
        byte[] result = new byte[a.length + b.length]; 
        System.arraycopy(a, 0, result, 0, a.length); 
        System.arraycopy(b, 0, result, a.length, b.length); 
        return result;
    } 
    

    【讨论】:

    • 我发现这更有用。
    【解决方案4】:

    当您只想添加 2 字节数组时,其他提供的解决方案非常棒,但如果您想继续附加几个 byte[] 块以制作单个:

    byte[] readBytes ; // Your byte array .... //for eg. readBytes = "TestBytes".getBytes();
    
    ByteArrayBuffer mReadBuffer = new ByteArrayBuffer(0 ) ; // Instead of 0, if you know the count of expected number of bytes, nice to input here
    
    mReadBuffer.append(readBytes, 0, readBytes.length); // this copies all bytes from readBytes byte array into mReadBuffer
    // Any new entry of readBytes, you can just append here by repeating the same call.
    
    // Finally, if you want the result into byte[] form:
    byte[] result = mReadBuffer.buffer();
    

    【讨论】:

      【解决方案5】:

      首先您需要分配一个组合长度的数组,然后使用 arraycopy 从两个来源填充它。

      byte[] ciphertext = blah;
      byte[] mac = blah;
      byte[] out = new byte[ciphertext.length + mac.length];
      
      
      System.arraycopy(ciphertext, 0, out, 0, ciphertext.length);
      System.arraycopy(mac, 0, out, ciphertext.length, mac.length);
      

      【讨论】:

        【解决方案6】:

        我写了以下几个数组连接的过程:

          static public byte[] concat(byte[]... bufs) {
            if (bufs.length == 0)
                return null;
            if (bufs.length == 1)
                return bufs[0];
            for (int i = 0; i < bufs.length - 1; i++) {
                byte[] res = Arrays.copyOf(bufs[i], bufs[i].length+bufs[i + 1].length);
                System.arraycopy(bufs[i + 1], 0, res, bufs[i].length, bufs[i + 1].length);
                bufs[i + 1] = res;
            }
            return bufs[bufs.length - 1];
        }
        

        它使用 Arrays.copyOf

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-09
          • 1970-01-01
          相关资源
          最近更新 更多