Search in sources :

Example 6 with StringWriterIOException

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();
        }
    };
}
Also used : StringWriter(java.io.StringWriter) StringWriterIOException(groovy.lang.StringWriterIOException) Writable(groovy.lang.Writable) StringWriterIOException(groovy.lang.StringWriterIOException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

StringWriterIOException (groovy.lang.StringWriterIOException)6 Writable (groovy.lang.Writable)6 IOException (java.io.IOException)6 StringWriter (java.io.StringWriter)6 Writer (java.io.Writer)6 GroovyPrintWriter (groovy.io.GroovyPrintWriter)2 FromString (groovy.transform.stc.FromString)2 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 BooleanClosureWrapper (org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper)2