Search in sources :

Example 1 with StringWriterIOException

use of groovy.lang.StringWriterIOException in project groovy by apache.

the class EncodingGroovyMethods method encodeHex.

/**
     * Produces a Writable that writes the hex encoding of the byte[]. Calling
     * toString() on this Writable returns the hex encoding as a String. The hex
     * encoding includes two characters for each byte and all letters are lower case.
     *
     * @param data byte array to be encoded
     * @return object which will write the hex encoding of the byte array
     * @see Integer#toHexString(int)
     */
public static Writable encodeHex(final byte[] data) {
    return new Writable() {

        public Writer writeTo(Writer out) throws IOException {
            for (int i = 0; i < data.length; i++) {
                // convert byte into unsigned hex string
                String hexString = Integer.toHexString(data[i] & 0xFF);
                // add leading zero if the length of the string is one
                if (hexString.length() < 2) {
                    out.write("0");
                }
                // write hex string to writer
                out.write(hexString);
            }
            return out;
        }

        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)

Example 2 with StringWriterIOException

use of groovy.lang.StringWriterIOException in project groovy-core by groovy.

the class IOGroovyMethods method filterLine.

/**
     * Filter the lines from this Reader, and return a Writable which can be
     * used to stream the filtered lines to a destination.  The closure should
     * return <code>true</code> if the line should be passed to the writer.
     *
     * @param reader  this reader
     * @param closure a closure used for filtering
     * @return a Writable which will use the closure to filter each line
     *         from the reader when the Writable#writeTo(Writer) is called.
     * @since 1.0
     */
public static Writable filterLine(Reader reader, @ClosureParams(value = SimpleType.class, options = "java.lang.String") final Closure closure) {
    final BufferedReader br = new BufferedReader(reader);
    return new Writable() {

        public Writer writeTo(Writer out) throws IOException {
            BufferedWriter bw = new BufferedWriter(out);
            String line;
            BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
            while ((line = br.readLine()) != null) {
                if (bcw.call(line)) {
                    bw.write(line);
                    bw.newLine();
                }
            }
            bw.flush();
            return out;
        }

        public String toString() {
            StringWriter buffer = new StringWriter();
            try {
                writeTo(buffer);
            } catch (IOException e) {
                throw new StringWriterIOException(e);
            }
            return buffer.toString();
        }
    };
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper) StringWriter(java.io.StringWriter) StringWriterIOException(groovy.lang.StringWriterIOException) BufferedReader(java.io.BufferedReader) Writable(groovy.lang.Writable) FromString(groovy.transform.stc.FromString) StringWriterIOException(groovy.lang.StringWriterIOException) IOException(java.io.IOException) GroovyPrintWriter(groovy.io.GroovyPrintWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

Example 3 with StringWriterIOException

use of groovy.lang.StringWriterIOException in project groovy-core by groovy.

the class EncodingGroovyMethods method encodeHex.

/**
     * Produces a Writable that writes the hex encoding of the byte[]. Calling
     * toString() on this Writable returns the hex encoding as a String. The hex
     * encoding includes two characters for each byte and all letters are lower case.
     *
     * @param data byte array to be encoded
     * @return object which will write the hex encoding of the byte array
     * @see Integer#toHexString(int)
     */
public static Writable encodeHex(final byte[] data) {
    return new Writable() {

        public Writer writeTo(Writer out) throws IOException {
            for (int i = 0; i < data.length; i++) {
                // convert byte into unsigned hex string
                String hexString = Integer.toHexString(data[i] & 0xFF);
                // add leading zero if the length of the string is one
                if (hexString.length() < 2) {
                    out.write("0");
                }
                // write hex string to writer
                out.write(hexString);
            }
            return out;
        }

        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)

Example 4 with StringWriterIOException

use of groovy.lang.StringWriterIOException in project groovy-core by groovy.

the class EncodingGroovyMethods method encodeBase64.

/**
     * Produce a Writable object which writes the Base64 encoding of the byte array.
     * Calling toString() on the result returns the encoding as a String. For more
     * information on Base64 encoding and chunking see <code>RFC 4648</code>.
     *
     * @param data byte array to be encoded
     * @param chunked whether or not the Base64 encoded data should be MIME chunked
     * @return object which will write the Base64 encoding of the byte array
     * @since 1.5.7
     */
public static Writable encodeBase64(final byte[] data, final boolean chunked) {
    return new Writable() {

        public Writer writeTo(final Writer writer) throws IOException {
            int charCount = 0;
            final int dLimit = (data.length / 3) * 3;
            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(T_TABLE[d >> 18]);
                writer.write(T_TABLE[(d >> 12) & 0X3F]);
                writer.write(T_TABLE[(d >> 6) & 0X3F]);
                writer.write(T_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(T_TABLE[d >> 18]);
                writer.write(T_TABLE[(d >> 12) & 0X3F]);
                writer.write((dLimit + 1 < data.length) ? T_TABLE[(d >> 6) & 0X3F] : '=');
                writer.write('=');
                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)

Example 5 with StringWriterIOException

use of groovy.lang.StringWriterIOException in project groovy by apache.

the class IOGroovyMethods method filterLine.

/**
     * Filter the lines from this Reader, and return a Writable which can be
     * used to stream the filtered lines to a destination.  The closure should
     * return <code>true</code> if the line should be passed to the writer.
     *
     * @param reader  this reader
     * @param closure a closure used for filtering
     * @return a Writable which will use the closure to filter each line
     *         from the reader when the Writable#writeTo(Writer) is called.
     * @since 1.0
     */
public static Writable filterLine(Reader reader, @ClosureParams(value = SimpleType.class, options = "java.lang.String") final Closure closure) {
    final BufferedReader br = new BufferedReader(reader);
    return new Writable() {

        public Writer writeTo(Writer out) throws IOException {
            BufferedWriter bw = new BufferedWriter(out);
            String line;
            BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
            while ((line = br.readLine()) != null) {
                if (bcw.call(line)) {
                    bw.write(line);
                    bw.newLine();
                }
            }
            bw.flush();
            return out;
        }

        public String toString() {
            StringWriter buffer = new StringWriter();
            try {
                writeTo(buffer);
            } catch (IOException e) {
                throw new StringWriterIOException(e);
            }
            return buffer.toString();
        }
    };
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper) StringWriter(java.io.StringWriter) StringWriterIOException(groovy.lang.StringWriterIOException) BufferedReader(java.io.BufferedReader) Writable(groovy.lang.Writable) FromString(groovy.transform.stc.FromString) StringWriterIOException(groovy.lang.StringWriterIOException) IOException(java.io.IOException) GroovyPrintWriter(groovy.io.GroovyPrintWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

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