Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @param finPath where to read binary data from
- * @param foutPath where to write encoded data. if null, please create and use temporary file.
- * @return file to read encoded data from
- * @throws IOException is case of input/output errors
- */
- @NotNull
- public File encodeFile(@NotNull String finPath, @Nullable String foutPath) throws IOException {
- final File fin = new File(finPath);
- final File fout;
- if (foutPath != null) {
- fout = new File(foutPath);
- } else {
- fout = File.createTempFile("based_file_", ".txt");
- fout.deleteOnExit();
- }
- try (
- final FileInputStream fis = new FileInputStream(fin);
- final FileOutputStream fos = new FileOutputStream(fout);
- ) {
- while(true) {
- int b1 = fis.read();
- int b2 = fis.read();
- int b3 = fis.read();
- if (b1 == -1) {
- break;
- }
- fos.write(toBase64[b1 >> 2]);
- if (b2 != -1) {
- fos.write(toBase64[((b1 & 3) << 4) | (b2 >> 4)]);
- } else {
- fos.write(toBase64[((b1 & 3) << 4)]);
- fos.write("==".getBytes());
- break;
- }
- if (b3 != -1) {
- fos.write(toBase64[((b2 & 15) << 2) | (b3 >> 6)]);
- } else {
- fos.write(toBase64[((b2 & 15) << 2)]);
- fos.write('=');
- break;
- }
- fos.write(toBase64[b3 & 63]);
- }
- }
- return fout;
- }
Advertisement
Add Comment
Please, Sign In to add comment