use of groovy.lang.StringWriterIOException in project groovy by apache.
the class EncodingGroovyMethods method encodeBase64.
private static Writable encodeBase64(final byte[] data, final boolean chunked, final boolean urlSafe, final boolean pad) {
return new Writable() {
public Writer writeTo(final Writer writer) throws IOException {
int charCount = 0;
final int dLimit = (data.length / 3) * 3;
final char[] table = urlSafe ? T_TABLE_URLSAFE : T_TABLE;
for (int dIndex = 0; dIndex != dLimit; dIndex += 3) {
int d = ((data[dIndex] & 0XFF) << 16) | ((data[dIndex + 1] & 0XFF) << 8) | (data[dIndex + 2] & 0XFF);
writer.write(table[d >> 18]);
writer.write(table[(d >> 12) & 0X3F]);
writer.write(table[(d >> 6) & 0X3F]);
writer.write(table[d & 0X3F]);
if (chunked && ++charCount == 19) {
writer.write(CHUNK_SEPARATOR);
charCount = 0;
}
}
if (dLimit != data.length) {
int d = (data[dLimit] & 0XFF) << 16;
if (dLimit + 1 != data.length) {
d |= (data[dLimit + 1] & 0XFF) << 8;
}
writer.write(table[d >> 18]);
writer.write(table[(d >> 12) & 0X3F]);
if (pad) {
writer.write((dLimit + 1 < data.length) ? table[(d >> 6) & 0X3F] : '=');
writer.write('=');
} else {
if (dLimit + 1 < data.length) {
writer.write(table[(d >> 6) & 0X3F]);
}
}
if (chunked && charCount != 0) {
writer.write(CHUNK_SEPARATOR);
}
}
return writer;
}
public String toString() {
StringWriter buffer = new StringWriter();
try {
writeTo(buffer);
} catch (IOException e) {
throw new StringWriterIOException(e);
}
return buffer.toString();
}
};
}
Aggregations