title: Writing a string to disk without any character conversion in Groovy
body: <p>I have a String object, now because it comes a diff of a folder containing different file types, not everything in it is encoded in the same character set.</p>
  
  <p>The correct codes are in the string, but whenever I try to access the string, groovy tries to be helpful and decode the string, which messes things up.</p>
  
  <p>Now the following seems to do what I need</p>
  
  <pre><code>String decoded_diff = "String that contains codes from different character encodings"
  patch_file_name = 'changes.patch'
  patch_file =  new File(pwd(), patch_file_name)
  patch_file.delete()
  max_block_size = 1024 * 1024
  char[] char_buffer = new char[max_block_size]
  block_start = 0
  patch_length = decoded_diff.length()
  while (true) {
      block_size = Math.min(patch_length - block_start, max_block_size)
      decoded_diff.getChars(block_start, block_start + block_size, char_buffer, 0)
      block_start += block_size
      byte[] byte_buffer = new byte[block_size]
      for (int i = 0; i &lt; block_size; i++) {
          byte_buffer[i] = (int) char_buffer[i]
      }
      patch_file.append(byte_buffer)
      if (block_start == patch_length) break
  }
  </code></pre>
  
  <p>However, it is sloooow</p>
  
  <p>Is there a faster way to achieve the same thing? The final patch file must be identical to the original diff to work. Unfortunately I can't send the file itself (jenkins currently doesn't support file parameters in pipeline jobs) so I have to escape it and send it as part of a json parameter list, hence this painful rigmarole on the receiving end.</p>