title: Who is adding "\n" in Base64 encoded image when I write it in a file? Java
body: <p><strong>WHAT I'M DOING</strong></p>
  
  <p>I need to send via HTTPS request a JsonArray with some data and images in Base64 encoded strings. This works well if data is stored in memory.</p>
  
  <p>Now, I need to avoid load all data in memory and I'm creating a temporally file in android device with all data that I need to send.
  To create the file, I'm writting lots of JsonObjects inside him. Some of this JsonObjects have a field that represents the image. When I detect one, I get the image path and I encode it with Base64 as a String.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>First of all, I inicialize the file and I get the bufferedWriter</p>
  
  <pre><code>            File f = new File(privateSincronizePath+File.separator+"upload_"+timefile+".json");
              f.createNewFile();
              FileWriter fw = new FileWriter(f.getAbsoluteFile(), true);
              BufferedWriter bw = new BufferedWriter(fw);
  </code></pre>
  
  <p>Here is the code that create the image when exists:</p>
  
  <pre><code>            JSONObject jobInf = new JSONObject();
              jobInf.put("xx", "INSERT");
              jobInf.put("xx", c.getString(c.getColumnIndex("xx")));
              jobInf.put("xx", ""+c.getInt(c.getColumnIndex("xx")));
              jobInf.put("xx", c.getString(c.getColumnIndex("xx")));
  
              JSONObject jsonObject = new JSONObject(docu.filtre(dades, docu.getXx()));
              Iterator&lt;?&gt; keys = jsonObject.keys();
              boolean updated = false;
              while(keys.hasNext() &amp;&amp; !updated){
                  String key = (String)keys.next();
                  if (key != null &amp;&amp; key.equals("path") &amp;&amp; key.length() &gt; 0){
                      jsonObject.put(key, getBase64EncodeFromImage(jsonObject.getString(key)));
                      updated = true;
                  }
              }
  
              jobInf.put("object", jsonObject);
  
              escriure(bw, ","+jobInf.toString());
  </code></pre>
  
  <p><strong>Method escriure():</strong></p>
  
  <p><strong>UPDATE:</strong> This method is called every time I complete the creation of the JsonObject. Only append JsonObject as String to the file.</p>
  
  <pre><code>    private void escriure(BufferedWriter bw, String s) throws IOException {
          uploadLength += s.length();
          bw.append(s);
      }
  </code></pre>
  
  <p>Finally, when file is created, I'm reading it and, using OutputStream, I'm sending the data as Post parameters to the server:</p>
  
  <pre><code>        this.Https_url = new URL(url+"/sync.php?");
          HttpsURLConnection con = (HttpsURLConnection) Https_url.openConnection();
          con.setReadTimeout(60000);
          con.setConnectTimeout(60000);
          con.setRequestMethod("POST");
          con.setDoInput(true);
          con.setDoOutput(true);
          con.setFixedLengthStreamingMode(uploadLength);
  
          OutputStream os = con.getOutputStream();
          InputStream inp = new FileInputStream(new File(privateSincronizePath+File.separator+"upload_"+timefile+".json"));
          byte[] buffer = new byte[1024];
  
          int len;
          while ((len = inp.read(buffer)) != -1) {
              os.write(buffer, 0, len);
          }
          inp.close();
          os.close();         
  
          // Establim la connexió:
          con.connect();
  </code></pre>
  
  <p><strong>WHAT IS THE PROBLEM</strong></p>
  
  <p>The problem is simple. When i open the image in the server, the file is corrupted and doesn't show the image.</p>
  
  <p><strong>WHAT I NOTICED</strong></p>
  
  <p>If I capture the image Base64 string encoded, before write in the file, and uploads it in the server, the image is Ok! Then, after write in the file, the image seems to be corrupted.</p>
  
  <p>After investigate, I noticed that the Base64 encoded string after be written in the file, it have a lot of "\n" every x characters.
  If I delete all of this "\n" or breaklines, the image can be opened by the server correctly.</p>
  
  <p><strong>THE QUESTION</strong>  </p>
  
  <p>Who is putting this breaklines? How can I write the Base64 encoded as String "as is" ?
  Thanks all for your help in advance!</p>
  
  <p><strong>THE ANSWER</strong></p>
  
  <p>As Schoenobates answer, the solution was use the flag NO_WRAP. 
  To add more information, we put in the server side this function to read the encoded Base64 string with flag URL_SAFE</p>
  
  <p>The function, obtained in on <a href="http://php.net/manual/en/function.base64-decode.php" rel="noreferrer">coment of TOM in php.net</a> is:</p>
  
  <pre><code>&lt;?php
      function base64url_decode($base64url)
      {
          $base64 = strtr($base64url, '-_', '+/');
          $plainText = base64_decode($base64);
          return ($plainText);
      }
      ?&gt;
  </code></pre>
  
  <p>Thanks to StackOverflow, Axel and Schoenobates for your time!</p>