Example usage for java.io StringWriter write

List of usage examples for java.io StringWriter write

Introduction

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

Prototype

public void write(String str, int off, int len) 

Source Link

Document

Write a portion of a string.

Usage

From source file:org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData.java

private static String toString(final InputStream input, final String encoding) throws IOException {
    final int DEFAULT_BUFFER_SIZE = 1024 * 4;
    StringWriter sw = new StringWriter();
    InputStreamReader in = new InputStreamReader(input, encoding);
    char[] buffer = new char[DEFAULT_BUFFER_SIZE];
    int n = 0;//from   w  w  w . jav  a 2s  .co  m
    while (-1 != (n = in.read(buffer))) {
        sw.write(buffer, 0, n);
    }
    return sw.toString();
}

From source file:com.taveloper.http.test.ParseBenchmark.java

private static String resourceToString(String path) throws Exception {
    InputStream in = ParseBenchmark.class.getResourceAsStream(path);
    if (in == null) {
        throw new IllegalArgumentException("No such file: " + path);
    }// w  w w.  j av  a  2 s  . c  o m

    Reader reader = new InputStreamReader(in, "UTF-8");
    char[] buffer = new char[8192];
    StringWriter writer = new StringWriter();
    int count;
    while ((count = reader.read(buffer)) != -1) {
        writer.write(buffer, 0, count);
    }
    reader.close();
    return writer.toString();
}

From source file:com.android.volley.misc.Utils.java

public static String readFully(Reader reader) throws IOException {
    try {/* w w w  . j  av a 2 s  .com*/
        StringWriter 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 {
        reader.close();
    }
}

From source file:io.fabric8.insight.log.support.LogQuerySupport.java

protected static String loadString(URL url) throws IOException {
    InputStream is = url.openStream();
    if (is == null) {
        return null;
    }/*from  www.  j a  v  a 2 s.  com*/
    try {
        InputStreamReader reader = new InputStreamReader(is);
        StringWriter writer = new StringWriter();
        final char[] buffer = new char[4096];
        int n;
        while (-1 != (n = reader.read(buffer))) {
            writer.write(buffer, 0, n);
        }
        writer.flush();
        return writer.toString();
    } finally {
        is.close();
    }
}

From source file:com.github.gekoh.yagen.hst.CreateEntities.java

private static String readContents(InputStream is) {
    StringWriter wr = new StringWriter();

    try {/* w  w  w .j  ava2  s.com*/
        Reader rd = new InputStreamReader(is, "UTF-8");

        char[] buf = new char[1024];
        int read;
        while ((read = rd.read(buf)) > 0) {
            wr.write(buf, 0, read);
        }

    } catch (Exception e) {
        LOG.error("An error occurred while reading the template file.", e);
    }

    return wr.toString();
}

From source file:com.googlecode.talkingrssreader.talkingrss.ReaderHttp.java

private static String readAll(InputStream stream) throws NetworkException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream), 16 * 1024);
    StringWriter sw = new StringWriter();
    char[] buf = new char[32 * 1024];
    try {/* w w  w.j  ava2s. co m*/
        while (true) {
            int len = reader.read(buf);
            if (len == -1)
                break;
            sw.write(buf, 0, len);
        }
    } catch (IOException e) {
        throw new NetworkException(e);
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
        }
    }
    return sw.toString();
}

From source file:com.github.gekoh.yagen.ddl.DDLGenerator.java

public static String read(Reader reader) {
    StringWriter wr = new StringWriter();
    char[] buf = new char[1024];
    int read;/*from www . j  a  va 2 s.c o  m*/
    try {

        while ((read = reader.read(buf)) > -1) {
            wr.write(buf, 0, read);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return wr.toString();
}

From source file:org.openadaptor.util.FileUtils.java

/**
 * @param fstream/*  w ww.ja  v  a  2 s  . c  o m*/
 *          the stream used to access the file contents
 * 
 * @return the contents of the file referenced by the input stream supplied. The file is processed in 1K chunks.
 * 
 * @throws IOException
 *           if the file doe not exist
 */
public static String getFileContents(InputStream fstream) throws IOException {
    InputStreamReader in = new InputStreamReader(fstream);

    StringWriter writer = new StringWriter();
    char[] buf = new char[1024];
    int count;

    while ((count = in.read(buf)) != -1)
        writer.write(buf, 0, count);

    in.close();
    fstream.close();

    return writer.toString();
}

From source file:se.jguru.nazgul.tools.visualization.model.jaxb.PlainJaxbContextRule.java

/**
 * Convenience method to read the content of a resource fully.
 *
 * @param resourcePath The resource path.
 * @return The resource, converted to a String.
 *///  www  .  jav a 2 s.co m
public static String readFully(final String resourcePath) {

    final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    final String effectiveResourcePath = resourcePath.charAt(0) == '/' ? resourcePath.substring(1)
            : resourcePath;

    final BufferedReader in = new BufferedReader(
            new InputStreamReader(contextClassLoader.getResourceAsStream(effectiveResourcePath)));
    final StringWriter out = new StringWriter();

    // Perform a Buffered read.
    final char[] buffer = new char[1024 * 4];
    int n = 0;
    try {
        while (-1 != (n = in.read(buffer))) {
            out.write(buffer, 0, n);
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("Could not read resource [" + resourcePath + "]", e);
    }

    // All Done.
    return out.toString();
}

From source file:com.icesoft.faces.webapp.parser.JspPageToDocument.java

static String getInputAsString(Reader in) {
    char[] buf = new char[1024];
    StringWriter out = new StringWriter();

    try {/*w  w  w  .  j  a v a2  s. co  m*/
        int l = 1;
        while (l > 0) {
            l = in.read(buf);
            if (l > 0) {
                out.write(buf, 0, l);
            }
        }
    } catch (IOException e) {
        if (log.isWarnEnabled()) {
            log.warn(e.getMessage(), e);
        }
    }
    return out.toString();

}