Example usage for java.io Writer write

List of usage examples for java.io Writer write

Introduction

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

Prototype

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

Source Link

Document

Writes a portion of a string.

Usage

From source file:Main.java

public static String streamToString(InputStream is) throws IOException {
    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {/* w  w w  .j av  a 2  s  .  c om*/
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}

From source file:Main.java

public static String convertStreamToString(InputStream inputStream) throws IOException {
    if (inputStream != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {//from  www  .jav a 2 s .com
            Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 1024);
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            inputStream.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}

From source file:Streams.java

public static void drain(Reader r, Writer w) throws IOException {
    char[] bytes = new char[BLOCK_SIZE];
    try {/*from w ww .  ja  v  a  2 s  . c  om*/
        int length = r.read(bytes);
        while (length != -1) {
            if (length != 0) {
                w.write(bytes, 0, length);
            }
            length = r.read(bytes);
        }
    } finally {
        bytes = null;
    }

}

From source file:Main.java

public static void write(Reader input, Writer output) throws IOException {
    int len;/*from  w  ww  .  j a  va2  s.  co  m*/
    char[] buffer = new char[4096];
    while (-1 != (len = input.read(buffer)))
        output.write(buffer, 0, len);
}

From source file:ca.mudar.parkcatcher.io.LocalExecutor.java

/**
 * Loads the JSON text resource with the given ID and returns the JSON
 * content./*from ww w.j a  v  a  2  s.  co  m*/
 */
public static String loadResourceJson(Context context, int resource) throws IOException {
    InputStream is = context.getResources().openRawResource(resource);
    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    try {
        Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        is.close();
    }

    return writer.toString();
}

From source file:org.carewebframework.ui.test.CommonTest.java

/**
 * Reads text from the specified resource on the classpath.
 * //from  w  w w .  j a  va2s. co  m
 * @param resourceName Name of the resource.
 * @return Text read from the resource.
 * @throws IOException IO exception.
 */
public static String getTextFromResource(String resourceName) throws IOException {
    Resource resource = desktopContext.getResource("classpath:" + resourceName);
    InputStream is = resource.getInputStream();
    Writer writer = new StringWriter();
    char[] buffer = new char[1024];

    try {
        Reader reader = new BufferedReader(new InputStreamReader(is, StrUtil.CHARSET));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        is.close();
    }
    return writer.toString();
}

From source file:function.Functions.java

private static int copy(Reader input, Writer output) throws IOException {
    char[] buffer = new char[1024];
    int count = 0;
    int n = 0;// w ww  .j a  v  a  2  s . com
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:com.swordlord.gozer.file.GozerFileLoader.java

public static String convertInputStreamToString(InputStream is) {
    String strReturn = "";

    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {/* w  w  w .j  av a2s  .  c o  m*/
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }

            reader.close();
            strReturn = writer.toString();
            writer.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }

    return strReturn;
}

From source file:llc.rockford.webcast.StringUtils.java

public static String convertStreamToString(InputStream is) throws IOException {
    /*//from   www . j a v a 2  s. co  m
     * To convert the InputStream to String we use the Reader.read(char[]
     * buffer) method. We iterate until the Reader return -1 which means
     * there's no more data to read. We use the StringWriter class to
     * produce the string.
     */
    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}

From source file:Main.java

/**
 * Write the entire contents of the supplied string to the given writer. This method always flushes and closes the writer when
 * finished./*from   w w  w. ja v  a 2s.  co m*/
 * 
 * @param input the content to write to the writer; may be null
 * @param writer the writer to which the content is to be written
 * @throws IOException
 * @throws IllegalArgumentException if the writer is null
 */
public static void write(Reader input, Writer writer) throws IOException {
    boolean error = false;
    try {
        if (input != null) {
            char[] buffer = new char[1024];
            try {
                int numRead = 0;
                while ((numRead = input.read(buffer)) > -1) {
                    writer.write(buffer, 0, numRead);
                }
            } finally {
                input.close();
            }
        }
    } catch (IOException e) {
        error = true; // this error should be thrown, even if there is an error flushing/closing writer
        throw e;
    } catch (RuntimeException e) {
        error = true; // this error should be thrown, even if there is an error flushing/closing writer
        throw e;
    } finally {
        try {
            writer.flush();
        } catch (IOException e) {
            if (!error)
                throw e;
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                if (!error)
                    throw e;
            }
        }
    }
}