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:net.sf.vntconverter.VntConverter.java

/**
 * Liefert den gesamten Inhalt der bergebenen UTF-8-Textdatei als String zurck.
 *//*from   www . java2 s .c  o m*/
private String getFileContent(File in) {
    FileInputStream inStream = null;
    try {
        inStream = new FileInputStream(in);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
        StringBuffer fileData = new StringBuffer();
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    } catch (Exception e) {
        throw new RuntimeException("Exception caught in VntConverter.getFileContent(in):", e);
    } finally {
        if (inStream != null) {
            try {
                inStream.close();
            } catch (IOException e) {
                // Einfach ignorieren ist hier ok
            }
        }
    }
}

From source file:com.everis.storage.web.Controller.java

private String getBody(HttpServletRequest request) throws IOException {
    String body = null;//from  ww w. j a  v  a  2s  .co m
    StringBuilder stringBuilder = new StringBuilder();
    BufferedReader bufferedReader = null;

    try {
        InputStream inputStream = request.getInputStream();
        if (inputStream != null) {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            char[] charBuffer = new char[128];
            int bytesRead = -1;
            while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                stringBuilder.append(charBuffer, 0, bytesRead);
            }
        } else {
            stringBuilder.append("");
        }
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException ex) {
                throw ex;
            }
        }
    }

    body = stringBuilder.toString();
    return body;
}

From source file:com.github.punkboy.ampdroid.ui.AMPDroidBaseActivity.java

final protected String exec(String cmd) {

    try {/*  w w w  . j ava  2  s . c  om*/
        // Executes the command.
        Process process = Runtime.getRuntime().exec(cmd);

        // NOTE: You can write to stdin of the command using
        // process.getOutputStream().
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        int read;
        char[] buffer = new char[4096 * 12];
        StringBuffer output = new StringBuffer();
        while ((read = reader.read(buffer)) > 0) {
            output.append(buffer, 0, read);
        }
        reader.close();

        // Waits for the command to finish.
        process.waitFor();

        return output.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.escidoc.core.test.examples.RetrieveCompressedExamplesIT.java

@Test
public void testRetrieveCompressedExampleOrganizationalUnits() throws Exception {

    for (int i = 0; i < EXAMPLE_OU_IDS.length; ++i) {

        String ou = handleXmlResult(ouClient.retrieve(EXAMPLE_OU_IDS[i]));

        String response = null;/*  w  w  w . j av  a2 s  . c  o  m*/

        String url = "http://localhost:8080" + Constants.ORGANIZATIONAL_UNIT_BASE_URI + "/" + EXAMPLE_OU_IDS[i];

        HttpClient client = new HttpClient();

        try {
            if (PropertiesProvider.getInstance().getProperty("http.proxyHost") != null
                    && PropertiesProvider.getInstance().getProperty("http.proxyPort") != null) {
                ProxyHost proxyHost = new ProxyHost(
                        PropertiesProvider.getInstance().getProperty("http.proxyHost"),
                        Integer.parseInt(PropertiesProvider.getInstance().getProperty("http.proxyPort")));

                client.getHostConfiguration().setProxyHost(proxyHost);
            }
        } catch (final Exception e) {
            throw new RuntimeException("[ClientBase] Error occured loading properties! " + e.getMessage(), e);
        }

        GetMethod getMethod = new GetMethod(url);
        getMethod.setRequestHeader("Accept-Encoding", "gzip");

        InputStream responseBody = null;
        try {

            int statusCode = client.executeMethod(getMethod);

            if (statusCode != HttpStatus.SC_OK) {
                throw new RuntimeException("getMethod failed:" + getMethod.getStatusLine());
            }
            responseBody = new GZIPInputStream(getMethod.getResponseBodyAsStream());

            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(responseBody, getMethod.getResponseCharSet()));
            StringBuffer result = new StringBuffer();

            char[] buffer = new char[4 * 1024];
            int charsRead;
            while ((charsRead = bufferedReader.read(buffer)) != -1) {
                result.append(buffer, 0, charsRead);
            }

            response = result.toString();
        } catch (Exception e) {
            throw new RuntimeException("Error occured in retrieving compressed example! " + e.getMessage(), e);
        }

        assertXmlEquals("Compressed document not the same like the uncompressed one", ou, response);
    }
}

From source file:com.tealeaf.ResourceDownloaderTask.java

private String fileToString(final File file) {
    StringBuilder result = new StringBuilder();
    BufferedReader reader = null;
    try {//  w  w  w  .  j av  a 2  s.c om
        reader = new BufferedReader(new FileReader(file));
        char[] buf = new char[1024];
        int r = 0;
        while ((r = reader.read(buf)) != -1) {
            result.append(buf, 0, r);
        }
    } catch (Exception e) {
        logger.log(e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                logger.log(e);
            }
        }
    }

    return result.toString();
}

From source file:org.clothocad.phagebook.controllers.PersonController.java

private static String getValue(Part part) throws IOException {
    System.out.println("Reached get Value");
    BufferedReader reader = new BufferedReader(new InputStreamReader(part.getInputStream(), "UTF-8"));
    StringBuilder value = new StringBuilder();
    char[] buffer = new char[1024];
    for (int length = 0; (length = reader.read(buffer)) > 0;) {
        System.out.print(".");
        value.append(buffer, 0, length);
    }/*w  ww  . j a  v  a2  s. c o  m*/
    System.out.println("Value :: " + value.toString());
    return value.toString();
}

From source file:org.apache.wookie.proxy.ProxyClient.java

/**
 * This is supposed to be the correct way to read the response instead of using getResponseBody() - which gives a runtime warning;
 *
 * See - http://mail-archives.apache.org/mod_mbox/jakarta-httpclient-user/200411.mbox/%3c1101558111.4070.22.camel@localhost.localdomain%3e
 * @param input/*ww w  .  ja  v a 2  s  .c  o  m*/
 * @return
 * @throws IOException
 */
private String readFully(Reader input) throws IOException {
    BufferedReader bufferedReader = input instanceof BufferedReader ? (BufferedReader) input
            : new BufferedReader(input);
    StringBuffer result = new StringBuffer();
    char[] buffer = new char[4 * 1024];
    int charsRead;
    while ((charsRead = bufferedReader.read(buffer)) != -1) {
        result.append(buffer, 0, charsRead);
    }
    return result.toString();
}

From source file:AddPost.java

private String getPostString() throws IOException, ServletException {
    String textPost = "";
    Collection<Part> p = mReq.getParts();
    for (Part part : p) {
        if (part.getName().equals("post")) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(part.getInputStream(), "UTF-8"));
            StringBuilder value = new StringBuilder();
            char[] buffer = new char[1024];
            for (int length = 0; (length = reader.read(buffer)) > 0;) {
                value.append(buffer, 0, length);
            }//from   w ww .  j ava  2s .  com
            textPost = value.toString();
        }
    }
    return textPost;
}

From source file:net.collegeman.phpinjava.PHP.java

private void initByInputStream(InputStream stream) {
    try {/*from   w w w . j a va  2 s.  com*/
        StringBuilder sourceCode = new StringBuilder(1024);
        BufferedReader in = new BufferedReader(new InputStreamReader(stream));
        char[] buffer = new char[1024];
        int read = 0;
        while ((read = in.read(buffer)) != -1)
            sourceCode.append(buffer, 0, read);
        in.close();

        snippet(sourceCode.toString());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.murati.oszk.audiobook.model.OfflineJSONSource.java

private JSONObject fetchJSON(String urlString) throws JSONException {
    BufferedReader reader = null;
    try {//from w  ww. j  av  a2 s .  co m
        InputStream is = getContext().getResources().openRawResource(R.raw.offline_catalog);
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            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 new JSONObject(writer.toString());
    } catch (JSONException e) {
        throw e;
    } catch (Exception e) {
        LogHelper.e(TAG, "Failed to parse the json for media list", e);
        return null;
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}