Created
June 20, 2013 21:10
Groovy script for calculating how much space a file will consume when Base64 encoded.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Script for calculating how much space a file will consume when Base64 encoded. | |
*/ | |
def showUsage = { -> | |
println ''' | |
Usages: calcBase64.groovy [options] file | |
calcBase64.groovy [options] number | |
Options: provide as single string, e.g. 'gp' or 'pq' (with optional prefix '-') | |
g: use geocar's calculation formula instead of adzm's | |
q: quiet output--just shows the base64 number (or percentage increase if 'p' is set) | |
p: show percentage increase of size | |
Examples: | |
> calcBase64.groovy C:\\temp\\myfile.txt | |
# describes the calculated base64 length of myfile.txt | |
> calcBase64.groovy q C:\\temp\\myfile.txt | |
# shows the calculate base64 length of myfile.txt alone | |
> calcBase64.groovy p 1234567 | |
# describes the calculated base64 length and % increase of 1234567 bytes | |
> calcBase64.groovy pq 1234567 | |
# shows the % increase of the calculated base64 length of 1234567 bytes | |
''' | |
System.exit(1) | |
} | |
/* | |
* Process input args | |
*/ | |
if (args.length < 1) { | |
println 'Please specify input param(s)' | |
showUsage() | |
} | |
def inputSize = ({ input -> | |
def inputFile = new File(input) | |
if (inputFile.exists()) { | |
inputFile.length() | |
} else { | |
try { | |
new BigInteger(input) | |
} catch (Exception e) { | |
println 'Please specify a file name or length' | |
showUsage() | |
} | |
} | |
})(args.length > 1 ? args[1] : args[0]) | |
def opts = args.length > 1 ? args[0] : '' | |
opts = opts - '-' | |
/* | |
* Algorithms ripped off from http://stackoverflow.com/questions/1533113/calculate-the-size-to-a-base-64-encoded-message | |
* admz's was the chosen answer, so it is the default | |
*/ | |
def adzm64 = { input_size -> | |
adjustment = ( (input_size % 3) ? (3 - (input_size % 3)) : 0); | |
code_padded_size = ( (input_size + adjustment) / 3) * 4; | |
newline_size = ((code_padded_size) / 72) * 2; | |
code_padded_size + newline_size; | |
} | |
def geocar64 = { input_size -> | |
code_size = ((input_size * 4) / 3); | |
padding_size = (input_size % 3) ? (3 - (input_size % 3)) : 0; | |
crlfs_size = 2 + (2 * (code_size + padding_size) / 72); | |
total_size = code_size + padding_size + crlfs_size; | |
} | |
def calc = opts.contains('g') ? geocar64 : adzm64 | |
def totalSize = Math.ceil(calc(inputSize)).longValue() | |
def pct = (totalSize / inputSize * 100).round(new java.math.MathContext(5)).toPlainString() | |
/* | |
* Display the output | |
*/ | |
if (opts.contains('q')) { | |
println opts.contains('p') ? "$pct%" : totalSize | |
} else { | |
def pctDesc = opts.contains('p') ? "(+${pct}%)" : "" | |
println "$inputSize raw bytes = $totalSize base64-encoded bytes $pctDesc" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment