Example usage for java.io Reader read

List of usage examples for java.io Reader read

Introduction

In this page you can find the example usage for java.io Reader read.

Prototype

public int read(char cbuf[]) throws IOException 

Source Link

Document

Reads characters into an array.

Usage

From source file:cognition.common.service.DocumentConversionService.java

/**
 * Starts a thread that reads the contents of the standard output or error
 * stream of the given process to not block the process. The stream is
 * closed once fully processed./*from  w w  w.java2 s .c om*/
 */
private void logStream(final InputStream stream) {
    new Thread() {
        public void run() {
            Reader reader = new InputStreamReader(stream, IOUtils.UTF_8);
            StringBuilder out = new StringBuilder();
            char[] buffer = new char[1024];
            try {
                for (int n = reader.read(buffer); n != -1; n = reader.read(buffer)) {
                    out.append(buffer, 0, n);
                }
            } catch (Exception e) {
                logger.error(e.getMessage());
            } finally {
                IOUtils.closeQuietly(stream);
                IOUtils.closeQuietly(reader);
            }
            logger.debug(out.toString());
        }
    }.start();
}

From source file:Main.java

/**
* Copies the contents of the Reader into the Writer, until the end of the 
* stream has been reached.//  ww w . j a va  2s  .  c  om
*
* @param in  the reader from which to read.
* @param out  the writer where the data is written to.
* @param buffersize  the buffer size.
*
* @throws IOException if a IOError occurs.
*/
public void copyWriter(final Reader in, final Writer out, final int buffersize) throws IOException {
    // create a 4kbyte buffer to read the file
    final char[] bytes = new char[buffersize];

    // the input stream does not supply accurate available() data
    // the zip entry does not know the size of the data
    int bytesRead = in.read(bytes);
    while (bytesRead > -1) {
        out.write(bytes, 0, bytesRead);
        bytesRead = in.read(bytes);
    }
}

From source file:org.lanark.jsr303js.taglib.JSR303JSCodebaseTag.java

/**
 * Copies the chars from in to out and then closes in but
 * leaves out open.//  ww  w .ja v a2 s  . com
 *
 * @param in the input reader
 * @param out the output writer
 * @throws IOException if there is an io exception
 */
private void copy(Reader in, Writer out) throws IOException {
    Assert.notNull(in, "No Reader specified");
    Assert.notNull(out, "No Writer specified");
    try {
        char[] buffer = new char[1024];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            logger.warn("Could not close Reader", ex);
        }
    }
}

From source file:org.springmodules.validation.valang.javascript.taglib.ValangCodebaseTag.java

/**
 * Copies the chars from in to out and then closes in but leaves out open.
 *//*from  w  w w. j a v a2s  .  co m*/
private void copy(Reader in, Writer out) throws IOException {
    Assert.notNull(in, "No Reader specified");
    Assert.notNull(out, "No Writer specified");
    try {
        char[] buffer = new char[1024];
        int bytesRead;

        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            logger.warn("Could not close Reader", ex);
        }
    }
}

From source file:com.seajas.search.contender.service.modifier.AbstractModifierService.java

/**
 * Create a StringBuffer out of the given reader content.
 * /*from   w  w w .  jav a  2s  .c  o  m*/
 * @param buffer
 * @param reader
 * @param cleanFromProlog
 * @return Reader
 */
protected Reader readerToBuffer(final StringBuffer buffer, final Reader reader, final boolean cleanFromProlog) {
    Integer lastProlog = -1;

    try {
        char[] characterBuffer = new char[8192];

        for (int count; (count = reader.read(characterBuffer)) > 0;) {
            buffer.append(characterBuffer, 0, count);

            // When cleaning by prolog, we stop writing out the buffer after the second prolog

            if (cleanFromProlog) {
                if (lastProlog == -1)
                    lastProlog = buffer.indexOf("<?xml");
                if (lastProlog != -1 && (lastProlog = buffer.indexOf("<?xml", lastProlog + 1)) != -1) {
                    logger.warn("Input document contains more than one XML prolog. Stripping any extra ones.");

                    buffer.replace(lastProlog, buffer.length(), "");

                    break;
                }
            }
        }

        reader.close();

        return new StringReader(buffer.toString().trim());
    } catch (IOException e) {
        logger.error("Could not convert the given reader to a buffer", e);

        return null;
    }
}

From source file:kr.co.skysoft.framework.taglib.JSR303JSCodebaseTag.java

/**
 * Copies the chars from in to out and then closes in but
 * leaves out open./*from   ww w .  j  av a 2s .  com*/
 *
 * @param in the input reader
 * @param out the output writer
 * @throws IOException if there is an io exception
 */
private void copy(Reader in, Writer out) throws IOException {

    Assert.notNull(in, "No Reader specified");
    Assert.notNull(out, "No Writer specified");
    try {
        char[] buffer = new char[1024];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            logger.warn("Could not close Reader", ex);
        }
    }
}

From source file:net.ausgstecktis.DAL.RestClient.java

/**
 * Converts a Inputstream to String/*  www . ja  v a2  s.com*/
 * 
 * @author naikon
 * @param is the inputstream which is to convert
 * @throws IOException if an input or output exception occurred
 * @return the converted string
 */
public String convertStreamToString(final InputStream is) throws IOException {
    if (is != null) {
        final Writer writer = new StringWriter();
        final char[] buffer = new char[1024];
        try {
            final Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1)
                writer.write(buffer, 0, n);
        } catch (final UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {
            is.close();
        }
        return writer.toString();
    }
    return "";
}

From source file:com.cheesmo.nzb.client.NNTPConnection.java

public boolean downloadSegment(String group, String string, String downloadName) {
    try {/*from   w  w  w.j a  v  a2 s .c o  m*/
        tryConnect();
        client.selectNewsgroup(group);
        Reader reader = client.retrieveArticleBody(string);

        if (reader != null) {
            FileOutputStream fos = new FileOutputStream(
                    config.getCacheDir() + java.io.File.separator + downloadName);
            char[] buffer = new char[512];
            int charsRead = reader.read(buffer);
            while (charsRead > -1 && client.isConnected()) {
                fos.write(Charset.forName("ISO-8859-1").encode(CharBuffer.wrap(buffer, 0, charsRead)).array());
                charsRead = reader.read(buffer);
            }

            if (!client.isConnected()) {
                System.out.println("Client disconnected.");
            }
            reader.close();
            fos.flush();
            fos.close();
            return true;
        } else {
            //System.out.println("Problem retrieving " + string);
        }

    } catch (SocketException e) {
        System.out.println("Client disconnected.");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}

From source file:cn.isif.util_plus.cache.LruDiskCache.java

private static String readFully(Reader reader) throws IOException {
    StringWriter writer = null;/*from   w w w .j  a  va 2  s  .  c  o  m*/
    try {
        writer = new StringWriter();
        char[] buffer = new char[1024];
        int count;
        while ((count = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, count);
        }
        return writer.toString();
    } finally {
        IOUtils.closeQuietly(reader);
        IOUtils.closeQuietly(writer);
    }
}

From source file:net.metanotion.json.StreamingParser.java

private Lexeme maybeTrue(final Reader in) throws IOException {
    final char[] cbuf = new char[BUFFER_LEN3];
    final int ct = in.read(cbuf);
    if ((ct == BUFFER_LEN3) && ("rue".equals(new String(cbuf)))) {
        return new Lexeme(Token.BOOL, true);
    } else {//from w ww .  ja v a 2  s .  c o  m
        throw new ParserException("Expected 'true', instead found: t" + new String(cbuf));
    }
}