Convenience method for reading a base64-encoded file and decoding 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 file
( String filename )
| 1461 | * @since 2.1 |
| 1462 | */ |
| 1463 | public static byte[] decodeFromFile( String filename ) |
| 1464 | throws java.io.IOException { |
| 1465 | |
| 1466 | byte[] decodedData = null; |
| 1467 | Base64.InputStream bis = null; |
| 1468 | try |
| 1469 | { |
| 1470 | // Set up some useful variables |
| 1471 | java.io.File file = new java.io.File( filename ); |
| 1472 | byte[] buffer = null; |
| 1473 | int length = 0; |
| 1474 | int numBytes = 0; |
| 1475 | |
| 1476 | // Check for size of file |
| 1477 | if( file.length() > Integer.MAX_VALUE ) |
| 1478 | { |
| 1479 | throw new java.io.IOException( "File is too big for this convenience method (" + file.length() + " bytes)." ); |
| 1480 | } // end if: file too big for int index |
| 1481 | buffer = new byte[ (int)file.length() ]; |
| 1482 | |
| 1483 | // Open a stream |
| 1484 | bis = new Base64.InputStream( |
| 1485 | new java.io.BufferedInputStream( |
| 1486 | new java.io.FileInputStream( file ) ), Base64.DECODE ); |
| 1487 | |
| 1488 | // Read until done |
| 1489 | while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 ) { |
| 1490 | length += numBytes; |
| 1491 | } // end while |
| 1492 | |
| 1493 | // Save in a variable to return |
| 1494 | decodedData = new byte[ length ]; |
| 1495 | System.arraycopy( buffer, 0, decodedData, 0, length ); |
| 1496 | |
| 1497 | } // end try |
| 1498 | catch( java.io.IOException e ) { |
| 1499 | throw e; // Catch and release to execute finally{} |
| 1500 | } // end catch: java.io.IOException |
| 1501 | finally { |
| 1502 | try{ bis.close(); } catch( Exception e) {} |
| 1503 | } // end finally |
| 1504 | |
| 1505 | return decodedData; |
| 1506 | } // end decodeFromFile |
| 1507 | |
| 1508 | |
| 1509 |
no test coverage detected