MCPcopy Create free account
hub / github.com/baumgarr/nixnote2 / encodeFromFile

Method encodeFromFile

java/src/cx/fbn/encrypt/Base64.java:1524–1559  ·  view source on GitHub ↗

Convenience method for reading a binary file and base64-encoding it. As of v 2.3, if there is a error, the method will throw an java.io.IOException. This is new to v2.3! In earlier versions, it just returned false, but in retrospect that's a pretty poor way to handle it. @param filen

( String filename )

Source from the content-addressed store, hash-verified

1522 * @since 2.1
1523 */
1524 public static String encodeFromFile( String filename )
1525 throws java.io.IOException {
1526
1527 String encodedData = null;
1528 Base64.InputStream bis = null;
1529 try
1530 {
1531 // Set up some useful variables
1532 java.io.File file = new java.io.File( filename );
1533 byte[] buffer = new byte[ Math.max((int)(file.length() * 1.4),40) ]; // Need max() for math on small files (v2.2.1)
1534 int length = 0;
1535 int numBytes = 0;
1536
1537 // Open a stream
1538 bis = new Base64.InputStream(
1539 new java.io.BufferedInputStream(
1540 new java.io.FileInputStream( file ) ), Base64.ENCODE );
1541
1542 // Read until done
1543 while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 ) {
1544 length += numBytes;
1545 } // end while
1546
1547 // Save in a variable to return
1548 encodedData = new String( buffer, 0, length, Base64.PREFERRED_ENCODING );
1549
1550 } // end try
1551 catch( java.io.IOException e ) {
1552 throw e; // Catch and release to execute finally{}
1553 } // end catch: java.io.IOException
1554 finally {
1555 try{ bis.close(); } catch( Exception e) {}
1556 } // end finally
1557
1558 return encodedData;
1559 } // end encodeFromFile
1560
1561 /**
1562 * Reads <tt>infile</tt> and encodes it to <tt>outfile</tt>.

Callers 1

encodeFileToFileMethod · 0.95

Calls 2

readMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected