Example usage for java.io CharArrayWriter write

List of usage examples for java.io CharArrayWriter write

Introduction

In this page you can find the example usage for java.io CharArrayWriter write.

Prototype

public void write(int c) 

Source Link

Document

Writes a character to the buffer.

Usage

From source file:net.violet.platform.util.SafeBase64.java

/**
 * Encodes a byte array into Base64 format. No blanks or line breaks are
 * inserted./*from  w  ww  .  j ava  2  s.c om*/
 * 
 * @param in an array containing the data bytes to be encoded.
 * @param in an array containing the data bytes to be encoded.
 * @throws IOException
 */
public static void encodeTo(byte[] in, boolean applyUrlSafeMapping, CharArrayWriter out) {

    final char[] charMap = applyUrlSafeMapping ? SafeBase64.urlSafeCharMap : SafeBase64.rfc1521CharMap;

    final int iLen = in.length;
    int ip = 0;
    final char[] output4 = new char[4];

    while (ip < iLen) {
        final int i0 = in[ip++] & 0xff;
        final int i1 = ip < iLen ? in[ip++] & 0xff : 0;
        final int i2 = ip < iLen ? in[ip++] & 0xff : 0;

        final int o0 = i0 >>> 2;
        final int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
        final int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
        final int o3 = i2 & 0x3F;

        output4[0] = charMap[o0];
        output4[1] = charMap[o1];
        output4[2] = (o2 == 0) ? SafeBase64.PADDING_CHAR : charMap[o2];
        output4[3] = (o3 == 0) ? SafeBase64.PADDING_CHAR : charMap[o3];

        try {
            out.write(output4);
        } catch (final IOException ignore) {
        }
    }

}

From source file:org.topazproject.ambra.auth.web.UsernameReplacementWithGuidFilter.java

private String dumpResponse(final CharResponseWrapper wrapper) throws IOException {
    log.debug("Response ContentType:" + wrapper.getContentType());
    CharArrayWriter caw = new CharArrayWriter();
    caw.write(wrapper.toString());
    final String response = caw.toString();
    log.debug("Response generated:");
    log.debug(response);//from www.j a v a2  s. c om
    return response;
}

From source file:importer.filters.Filter.java

protected void writeCurrent(CharArrayWriter txt, char[] current) throws IOException {
    txt.write(current);
    written += current.length;//from ww  w .  ja v  a 2s.  c  o m
}

From source file:org.apache.jasper.compiler.JspUtil.java

public static char[] removeQuotes(char[] chars) {
    CharArrayWriter caw = new CharArrayWriter();
    for (int i = 0; i < chars.length; i++) {
        if (chars[i] == '%' && chars[i + 1] == '\\' && chars[i + 2] == '>') {
            caw.write('%');
            caw.write('>');
            i = i + 2;/*from w  ww. j  av  a  2  s .c o m*/
        } else {
            caw.write(chars[i]);
        }
    }
    return caw.toCharArray();
}

From source file:org.bsc.maven.plugin.confluence.PegdownTest.java

private char[] loadResource(String name) throws IOException {

    final ClassLoader cl = PegdownTest.class.getClassLoader();

    final java.io.InputStream is = cl.getResourceAsStream(name);
    try {/*from   www . ja v a  2s. c  o  m*/

        java.io.CharArrayWriter caw = new java.io.CharArrayWriter();

        for (int c = is.read(); c != -1; c = is.read()) {
            caw.write(c);
        }

        return caw.toCharArray();

    } finally {
        IOUtils.closeQuietly(is);
    }

}

From source file:org.bsc.maven.plugin.confluence.PegdownParse.java

protected char[] loadResource(String name) throws IOException {

    final ClassLoader cl = PegdownParse.class.getClassLoader();

    final java.io.InputStream is = cl.getResourceAsStream(name);
    try {/*from w w  w  .  ja va2  s  .  c  om*/

        java.io.CharArrayWriter caw = new java.io.CharArrayWriter();

        for (int c = is.read(); c != -1; c = is.read()) {
            caw.write(c);
        }

        return caw.toCharArray();

    } finally {
        IOUtils.closeQuietly(is);
    }

}

From source file:org.apache.jmeter.save.CSVSaveService.java

/**
 * Reads from file and splits input into strings according to the delimiter,
 * taking note of quoted strings.//w ww  .  j  a  va  2s.c  o m
 * <p>
 * Handles DOS (CRLF), Unix (LF), and Mac (CR) line-endings equally.
 * <p>
 * A blank line - or a quoted blank line - both return an array containing
 * a single empty String.
 * @param infile
 *            input file - must support mark(1)
 * @param delim
 *            delimiter (e.g. comma)
 * @return array of strings, will be empty if there is no data, i.e. if the input is at EOF.
 * @throws IOException
 *             also for unexpected quote characters
 */
public static String[] csvReadFile(BufferedReader infile, char delim) throws IOException {
    int ch;
    ParserState state = ParserState.INITIAL;
    List<String> list = new ArrayList<>();
    CharArrayWriter baos = new CharArrayWriter(200);
    boolean push = false;
    while (-1 != (ch = infile.read())) {
        push = false;
        switch (state) {
        case INITIAL:
            if (ch == QUOTING_CHAR) {
                state = ParserState.QUOTED;
            } else if (isDelimOrEOL(delim, ch)) {
                push = true;
            } else {
                baos.write(ch);
                state = ParserState.PLAIN;
            }
            break;
        case PLAIN:
            if (ch == QUOTING_CHAR) {
                baos.write(ch);
                throw new IOException("Cannot have quote-char in plain field:[" + baos.toString() + "]");
            } else if (isDelimOrEOL(delim, ch)) {
                push = true;
                state = ParserState.INITIAL;
            } else {
                baos.write(ch);
            }
            break;
        case QUOTED:
            if (ch == QUOTING_CHAR) {
                state = ParserState.EMBEDDEDQUOTE;
            } else {
                baos.write(ch);
            }
            break;
        case EMBEDDEDQUOTE:
            if (ch == QUOTING_CHAR) {
                baos.write(QUOTING_CHAR); // doubled quote => quote
                state = ParserState.QUOTED;
            } else if (isDelimOrEOL(delim, ch)) {
                push = true;
                state = ParserState.INITIAL;
            } else {
                baos.write(QUOTING_CHAR);
                throw new IOException(
                        "Cannot have single quote-char in quoted field:[" + baos.toString() + "]");
            }
            break;
        default:
            throw new IllegalStateException("Unexpected state " + state);
        } // switch(state)
        if (push) {
            if (ch == '\r') {// Remove following \n if present
                infile.mark(1);
                if (infile.read() != '\n') {
                    infile.reset(); // did not find \n, put the character
                                    // back
                }
            }
            String s = baos.toString();
            list.add(s);
            baos.reset();
        }
        if ((ch == '\n' || ch == '\r') && state != ParserState.QUOTED) {
            break;
        }
    } // while not EOF
    if (ch == -1) {// EOF (or end of string) so collect any remaining data
        if (state == ParserState.QUOTED) {
            throw new IOException("Missing trailing quote-char in quoted field:[\"" + baos.toString() + "]");
        }
        // Do we have some data, or a trailing empty field?
        if (baos.size() > 0 // we have some data
                || push // we've started a field
                || state == ParserState.EMBEDDEDQUOTE // Just seen ""
        ) {
            list.add(baos.toString());
        }
    }
    return list.toArray(new String[list.size()]);
}

From source file:org.apache.jasper.compiler.JspReader.java

String getText(Mark start, Mark stop) throws JasperException {
    Mark oldstart = mark();/*w  w w  .  j a v  a2 s .  co  m*/
    reset(start);
    CharArrayWriter caw = new CharArrayWriter();
    while (!stop.equals(mark()))
        caw.write(nextChar());
    caw.close();
    reset(oldstart);
    return caw.toString();
}

From source file:com.aliyun.odps.rodps.ROdps.java

public boolean setLogPath(String log_path) throws IOException {
    String fileName = ROdps.class.getClassLoader().getResource("log4j.properties").getPath();
    String mode = "loghome";
    File file = new File(fileName);
    BufferedReader reader = null;
    try {/*from www.  j  ava 2s  .  c o m*/
        reader = new BufferedReader(new FileReader(file));
        CharArrayWriter tempStream = new CharArrayWriter();
        String tempString = null;
        int line = 1;
        while ((tempString = reader.readLine()) != null) {
            if (tempString.contains(mode) && (!tempString.contains("${" + mode + "}"))) {
                tempString = tempString.substring(0, tempString.indexOf('=') + 1) + log_path;
            }
            tempStream.write(tempString);
            tempStream.append(System.getProperty("line.separator"));
        }
        reader.close();
        FileWriter out = new FileWriter(fileName);
        tempStream.writeTo(out);
        out.close();

    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e1) {
            }
        }
    }
    return true;
}

From source file:org.commoncrawl.util.ArcFileWriter.java

private String escapeURI(String uri, String charsetEncoding) throws IOException {

    boolean needToChange = false;

    StringBuffer out = new StringBuffer(uri.length());

    Charset charset;/*  ww w  . ja va 2s.com*/

    CharArrayWriter charArrayWriter = new CharArrayWriter();

    if (charsetEncoding == null)
        throw new NullPointerException("charsetName");

    try {
        charset = Charset.forName(charsetEncoding);
    } catch (IllegalCharsetNameException e) {
        throw new UnsupportedEncodingException(charsetEncoding);
    } catch (UnsupportedCharsetException e) {
        throw new UnsupportedEncodingException(charsetEncoding);
    }

    for (int i = 0; i < uri.length();) {
        int c = (int) uri.charAt(i);
        // System.out.println("Examining character: " + c);
        if (dontNeedEncoding.get(c)) {
            out.append((char) c);
            i++;
        } else {
            // convert to external encoding before hex conversion
            do {
                charArrayWriter.write(c);
                /*
                 * If this character represents the start of a Unicode surrogate pair,
                 * then pass in two characters. It's not clear what should be done if
                 * a bytes reserved in the surrogate pairs range occurs outside of a
                 * legal surrogate pair. For now, just treat it as if it were any
                 * other character.
                 */
                if (c >= 0xD800 && c <= 0xDBFF) {
                    /*
                     * System.out.println(Integer.toHexString(c) +
                     * " is high surrogate");
                     */
                    if ((i + 1) < uri.length()) {
                        int d = (int) uri.charAt(i + 1);
                        /*
                         * System.out.println("\tExamining " + Integer.toHexString(d));
                         */
                        if (d >= 0xDC00 && d <= 0xDFFF) {
                            /*
                             * System.out.println("\t" + Integer.toHexString(d) +
                             * " is low surrogate");
                             */
                            charArrayWriter.write(d);
                            i++;
                        }
                    }
                }
                i++;
            } while (i < uri.length() && !dontNeedEncoding.get((c = (int) uri.charAt(i))));

            charArrayWriter.flush();
            String str = new String(charArrayWriter.toCharArray());
            byte[] ba = str.getBytes(charsetEncoding);
            for (int j = 0; j < ba.length; j++) {
                out.append('%');
                char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
                // converting to use uppercase letter as part of
                // the hex value if ch is a letter.
                if (Character.isLetter(ch)) {
                    ch -= caseDiff;
                }
                out.append(ch);
                ch = Character.forDigit(ba[j] & 0xF, 16);
                if (Character.isLetter(ch)) {
                    ch -= caseDiff;
                }
                out.append(ch);
            }
            charArrayWriter.reset();
            needToChange = true;
        }
    }

    return (needToChange ? out.toString() : uri);
}