title: Java UDP packet size
body: <p>In Java a single char is 16 bit, 2 bytes. That means if I want to send an UDP packet to a server, I must find the length of the string (for example) and multiply by 2?</p>
  
  <pre><code>String string = "My awesome string!";
  byte[] buff = new byte[ string.length*2 ];
  buff = string.getBytes();
  ...
  packet = new DatagramPacket(buff, buff.length, address, port);
  socket.send(packet);
  </code></pre>
  
  <p>What about the UDP packet limit? 65k a packet. For example, if I want to send data files to a server I must send 65/2k data! I'm dividing 65 into 2, and what would be the buff limit? 65/2 or 65 kb?</p>
  
  <p>For example:</p>
  
  <pre><code>byte[] buff = new byte[ 65000 ]
  
  //file and bufferreader handle
  while( ( line = bufferedReader.readLine() ) != null ){
     buff = line.getBytes();
     packet = new DatagramPacket(buff, buff.length, address, port);
     socket.send(packet);
  }
  </code></pre>
  
  <p>I have read somewhere that I can send over than 65k data, because IPv4 protocol automatically divides the packet into pieces than the receiver will merge them. Is this true?</p>
  
  <p>Why I'm getting white space into my buffer? I have written a client and a server app, and I'm using a 250 size buffer. If I'm sending a word, for example "Test", which is 8 bytes long I'm getting a very long white space after the "Test" word:</p>
  
  <pre><code>byte[] buff = new byte[250];
  packet = new DatagramPacket(buff, buff.length);
  socket.receive(packet);
  System.out.println("GET: " + buff);
  </code></pre>
  
  <p>and the console says: </p>
  
  <blockquote>
    <p>GET: Test...........................................................................</p>
  </blockquote>
  
  <p>(dots represents white space)</p>