title: Trimming empty elements off end of byte array - Java
body: <p>I am doing some network programming with UDP and am trying to break down a large packet into many smaller packets and then send them across the network. Everything works great until it is time for the last packet to be sent. The problem with the last packet is that it that the associated byte[] has some trailing space at the end because the byte[] is always 1024 bytes, but the loop only fills the array when there is still file data to be sent. What is the best way to remove the empty elements of the byte[] before putting the array in a DatagramPacket to be sent across the network?</p>
  
  <pre><code>    while(count &lt; fileData.length) {
              for(int i=0; i&lt;smallBuffSize; i++){
                  System.out.println(count);
                  System.out.println(fileData.length);
                  if(count &lt; fileData.length){
                      data[i] = fileData[count];
                      count++;
                  }
              }
  
              DatagramPacket dataPacket = new DatagramPacket(data, data.length, serverAddress, 5000); 
              socket2.send(dataPacket);
          }
  </code></pre>