title: how to copy one byte array into another byte array?
body: <p>I'm working on a simple application to demonstrate P2P file transfers. The application takes a file, and sends over packet sized chunks of data. The receiver waits for these chunks to arrive. Its suppose to continuously add the incoming byte arrays to a larger byte array which will later become the completed file once all bytes have arrived. This is assuming there is no packet loss. For the sake of debugging I've been attempting to transfer a small txt file. I tried using a for loop and now System.arraycopy but the byte array for packetData does not appear to be copying over to the completeFile array correctly. </p>
  
  <pre><code>public void run() {
      try {
          byte[] completeFile = new byte[fileSize];//will become the complete file when all bytes have arrived
  
          receivingSocket = new DatagramSocket(49000);
          while (bytesReceived&lt;fileSize) {
              System.out.println("@@@ Receiver waiting for packet");
              byte[] buf = new byte[1500]; // Actual Ethernet packet size is 1500 bytes
              // receive request
              DatagramPacket packet = new DatagramPacket(buf, buf.length);
              receivingSocket.receive(packet);
              byte[] packetData = Arrays.copyOf(packet.getData(), packet.getLength());
              deliverData(packetData);
              if(headerReceived &amp;&amp; packetsReceived&gt;1)
              {
                  //add incoming bytes to the completeFile
                  System.arraycopy(packetData, 0, completeFile, bytesReceived, packetData.length);
                  bytesReceived = bytesReceived + packetData.length;
              }
          }
          FileOutputStream fops = new FileOutputStream("D:\\Desktop\\testing2"+fileExtension);
          System.out.println("@@@ BUILDING FILE"); 
          fops.write(completeFile);
          fops.close(); 
      } catch (Exception e) {
          System.out.println("@@@ SOMETHING WENT WRONG!!!!!!!!!!!!!!");
          stopListening();
      }
  }
  </code></pre>