Guest User

Untitled

a guest
Jun 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. import java.io.*;
  2. import java.nio.channels.*;
  3. import javax.xml.bind.DatatypeConverter;
  4.  
  5. public class EncodeDecode {    
  6.   public static void main(String[] args) throws Exception {
  7.     File file = new File("/bin/ls");
  8.     byte[] bytes = loadFile(file, new ByteArrayOutputStream()).toByteArray();
  9.     String encoded = DatatypeConverter.printBase64Binary(bytes);
  10.     System.out.println(encoded);
  11.     byte[] decoded = DatatypeConverter.parseBase64Binary(encoded);
  12.     // check
  13.     for (int i = 0; i < bytes.length; i++) {
  14.       assert bytes[i] == decoded[i];
  15.     }
  16.   }
  17.  
  18.   private static <T extends OutputStream> T loadFile(File file, T out)
  19.                                                        throws IOException {
  20.     FileChannel in = new FileInputStream(file).getChannel();
  21.     try {
  22.       assert in.size() == in.transferTo(0, in.size(), Channels.newChannel(out));
  23.       return out;
  24.     } finally {
  25.       in.close();
  26.     }
  27.   }
  28. }
Add Comment
Please, Sign In to add comment