title: Failure encoding files in base64 java
body: <p>I have this class to encode and decode a file. When I run the class with .txt files the result is successfully. But when I run the code with .jpg or .doc I can not open the file or it is not equals to original. I don’t know why this is happening. I have modified this class
  <a href="http://myjeeva.com/convert-image-to-string-and-string-to-image-in-java.html" rel="noreferrer">http://myjeeva.com/convert-image-to-string-and-string-to-image-in-java.html</a>. But i want change this line</p>
  
  <pre><code>byte imageData[] = new byte[(int) file.length()];
  </code></pre>
  
  <p>for </p>
  
  <pre><code>byte example[] = new byte[1024];
  </code></pre>
  
  <p>and read the file so many times how we need. Thanks.</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
    public class Encode {
  </code></pre>
  
  <p>Input = Input file root - Output = Output file root - imageDataString =String encoded </p>
  
  <pre><code>  String input;
    String output;
    String imageDataString;
  
  
    public void setFileInput(String input){
      this.input=input;
    }
  
    public void setFileOutput(String output){
      this.output=output;
    }
  
    public String getFileInput(){
      return input;
    }
  
    public String getFileOutput(){
      return output;
    }
  
    public String getEncodeString(){
      return  imageDataString;
    }
  
    public String processCode(){
      StringBuilder sb= new StringBuilder();
  
      try{
          File fileInput= new File( getFileInput() );
          FileInputStream imageInFile = new FileInputStream(fileInput);
  </code></pre>
  
  <p>i have seen in examples that people create a byte[] with the same length than the file. I don´t want this because i will not know what  length will have the file. </p>
  
  <pre><code>        byte buff[] = new byte[1024];
  
          int r = 0;
  
          while ( ( r = imageInFile.read( buff)) &gt; 0 ) {
  
            String imageData = encodeImage(buff);
  
            sb.append( imageData);
  
            if ( imageInFile.available() &lt;= 0 ) {
              break;
            }
          }
  
  
  
         } catch (FileNotFoundException e) {
          System.out.println("File not found" + e);
        } catch (IOException ioe) {
          System.out.println("Exception while reading the file " + ioe);
  
      } 
  
          imageDataString = sb.toString();
  
         return imageDataString;
  }  
  
  
    public  void processDecode(String str) throws IOException{
  
        byte[] imageByteArray = decodeImage(str);
        File fileOutput= new File( getFileOutput());
        FileOutputStream imageOutFile = new FileOutputStream( fileOutput);
  
        imageOutFile.write(imageByteArray);
        imageOutFile.close();
  
  }
  
   public static String encodeImage(byte[] imageByteArray) {
  
        return  Base64.getEncoder().withoutPadding().encodeToString( imageByteArray);
  
      }
  
      public static byte[] decodeImage(String imageDataString) {
        return  Base64.getDecoder().decode(  imageDataString);  
  
      }
  
  
    public static void main(String[] args) throws IOException {
  
      Encode a = new Encode();
  
      a.setFileInput( "C://Users//xxx//Desktop//original.doc");
      a.setFileOutput("C://Users//xxx//Desktop//original-copied.doc");
  
      a.processCode( );
  
      a.processDecode( a.getEncodeString());
  
      System.out.println("C O P I E D");
    }
  }
  </code></pre>
  
  <p>I tried changing</p>
  
  <pre><code>String imageData = encodeImage(buff);
  </code></pre>
  
  <p>for </p>
  
  <pre><code>String imageData = encodeImage(buff,r);
  </code></pre>
  
  <p>and the method encodeImage</p>
  
  <pre><code>public static String encodeImage(byte[] imageByteArray, int r) {
  
       byte[] aux = new byte[r];
  
       for ( int i = 0; i &lt; aux.length; i++) {
         aux[i] = imageByteArray[i];
  
         if ( aux[i] &lt;= 0 ) {
           break;
         }
       }
  return  Base64.getDecoder().decode(  aux);
  }
  </code></pre>
  
  <p>But i have the error:</p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalArgumentException: Last unit does not have enough valid bits   
  </code></pre>