Example usage for java.io BufferedReader read

List of usage examples for java.io BufferedReader read

Introduction

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

Prototype

public int read(java.nio.CharBuffer target) throws IOException 

Source Link

Document

Attempts to read characters into the specified character buffer.

Usage

From source file:org.apache.camel.converter.IOConverter.java

@Converter
public static String toString(BufferedReader reader) throws IOException {
    if (reader == null) {
        return null;
    }/*from w w  w.ja va2  s .  c  om*/

    StringBuilder sb = new StringBuilder(1024);
    char[] buf = new char[1024];
    try {
        int len = 0;
        // read until we reach then end which is the -1 marker
        while (len != -1) {
            len = reader.read(buf);
            if (len != -1) {
                sb.append(buf, 0, len);
            }
        }
    } finally {
        IOHelper.close(reader, "reader", LOG);
    }

    return sb.toString();
}

From source file:io.seldon.importer.articles.GeneralItemAttributesImporter.java

private static String readFileAsString(String fpath) throws IOException {
    StringBuilder sb = new StringBuilder();
    BufferedReader reader = new BufferedReader(new FileReader(new File(fpath)));
    char[] buff = new char[1024];
    int numRead = 0;
    while ((numRead = reader.read(buff)) != -1) {
        String readData = String.valueOf(buff, 0, numRead);
        sb.append(readData);//from   w ww  .  j a  va 2  s. c om
    }
    return sb.toString();
}

From source file:net.xy.jcms.controller.configurations.Configuration.java

/**
 * initializes an configuration with an input stream resource. only for
 * certain configuration available.//w  w w. j  a  v  a  2s .  c om
 * 
 * @param type
 * @param stream
 * @param mount
 *            specifies the mountpoint on which relative component pathes should be resolved
 * @return value
 */
public static Configuration<?> initByStream(final ConfigurationType type, final InputStream stream,
        final ClassLoader loader, final String mount) {
    final StringBuilder writer = new StringBuilder();
    BufferedReader reader;
    try {
        reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"), 1024);
    } catch (final UnsupportedEncodingException e) {
        throw new UnsupportedOperationException(
                "Mendatory charachterset UTF-8 is not supported on this system.", e);
    }
    final char[] buffer = new char[1024];
    try {
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.append(buffer, 0, n);
        }
    } catch (final IOException e) {
    }
    return initByString(type, writer.toString(), loader, mount);
}

From source file:com.blockwithme.longdb.test.util.JSONUtil.java

/** Reads file as string.
 * //from   w  w w .  j  av a 2  s  .  c  o  m
 * @param theFilePath
 *        the file path
 * @return the the file as a string
 * @throws IOException
 *         Signals that an I/O exception has occurred. */
private static String readFileAsString(final String theFilePath) throws java.io.IOException {
    final StringBuilder fileData = new StringBuilder();
    final InputStream inputStream = JSONUtil.class.getResourceAsStream(theFilePath);
    final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Charsets.UTF_8.name()));
    try {
        char[] buf = new char[ONE_K];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            final String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[ONE_K];
        }
    } finally {
        reader.close();
    }
    return fileData.toString();
}

From source file:com.amalto.workbench.compare.Utilities.java

public static String readString(InputStream is, String encoding) throws IOException {
    if (is == null)
        return null;
    BufferedReader reader = null;
    try {/*from   w  ww  .  ja  va2s  .  com*/
        StringBuffer buffer = new StringBuffer();
        char[] part = new char[2048];
        int read = 0;
        reader = new BufferedReader(new InputStreamReader(is, encoding));
        while ((read = reader.read(part)) != -1)
            buffer.append(part, 0, read);

        return buffer.toString();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ex) {
                // silently ignored
            }
        }
    }
}

From source file:org.ebayopensource.turmeric.tools.codegen.CodeGenInfoFinder.java

private static String readContent(InputStream input) throws IOException {
    Charset defaultCharset = Charset.defaultCharset();
    InputStreamReader isr = new InputStreamReader(input, defaultCharset);
    BufferedReader reader = null;

    StringBuilder strBuff = new StringBuilder();
    try {/*from  ww w.j a  v a2 s .com*/
        reader = new BufferedReader(isr);
        char[] charBuff = new char[512];
        int charsRead = -1;
        while ((charsRead = reader.read(charBuff)) > -1) {
            strBuff.append(charBuff, 0, charsRead);
        }
    } finally {
        CodeGenUtil.closeQuietly(reader);
    }
    return strBuff.toString();
}

From source file:guiTool.Helper.java

public static String readUrl(String urlString, final String userid, final String password) throws Exception {

    BufferedReader reader = null;
    StringBuilder buffer = null;//from   w  w w  .ja  v  a2  s. c om
    try {
        URL url = new URL(urlString);

        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userid, password.toCharArray());
            }
        });
        InputStreamReader s = new InputStreamReader(url.openStream());
        //       System.out.println("getEncoding " + s.getEncoding());
        reader = new BufferedReader(s);

        buffer = new StringBuilder();
        int read;
        char[] chars = new char[1024];
        while ((read = reader.read(chars)) != -1) {
            buffer.append(chars, 0, read);
        }

    } catch (Exception e) {
        System.out.println("Exception       " + e.getMessage());
        //  JOptionPane.showMessageDialog(null, "The server or your internet connection could be down.", "connection test", JOptionPane.ERROR_MESSAGE);

    } finally {
        if (reader != null) {
            reader.close();
        }
    }
    return buffer.toString();
}

From source file:marytts.util.io.FileUtils.java

public static String getReaderAsString(Reader reader) throws IOException {
    StringWriter sw = new StringWriter();
    BufferedReader in = new BufferedReader(reader);
    char[] buf = new char[8192];
    int n;/*w w w .  j  a  v a2 s.co m*/
    while ((n = in.read(buf)) > 0) {
        sw.write(buf, 0, n);
    }
    return sw.toString();

}

From source file:net.naijatek.myalumni.util.utilities.FileUtil.java

public static String readFile(final String fileName, final String srcEncoding)
        throws FileNotFoundException, IOException {

    File file = null;/*  w w w  .j  ava 2  s. co  m*/
    try {
        file = new File(fileName);
        if (file.isFile() == false) {
            throw new IOException("'" + fileName + "' is not a file.");
        }
    } finally {
        // we dont have to close File here
    }

    BufferedReader reader = null;
    try {
        StringBuffer result = new StringBuffer(1024);
        FileInputStream fis = new FileInputStream(fileName);
        reader = new BufferedReader(new InputStreamReader(fis, srcEncoding));

        char[] block = new char[512];
        while (true) {
            int readLength = reader.read(block);
            if (readLength == -1) {
                break;// end of file
            }
            result.append(block, 0, readLength);
        }
        return result.toString();
    } catch (FileNotFoundException fe) {
        logger.error("Error", fe);
        throw fe;
    } catch (IOException e) {
        logger.error("Error", e);
        throw e;
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException ex) {
        }
    }
}

From source file:webtest.Test1.java

private static String readUrl(String urlString) throws Exception {
    BufferedReader reader = null;
    try {// w w w  .j  ava  2s .  c o m
        URL url = new URL(urlString);
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
        StringBuffer buffer = new StringBuffer();
        int read;
        char[] chars = new char[1024];
        while ((read = reader.read(chars)) != -1) {
            buffer.append(chars, 0, read);
        }

        return buffer.toString();
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}