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:alluxio.rest.TestCase.java

/**
 * @param connection the HttpURLConnection
 * @return the String from the InputStream of HttpURLConnection
 * @throws Exception/*from w ww. j  a  v  a  2s.c  om*/
 */
public String getResponse(HttpURLConnection connection) throws Exception {
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    char[] buffer = new char[1024];
    int len;

    while ((len = br.read(buffer)) > 0) {
        sb.append(buffer, 0, len);

    }
    br.close();

    return sb.toString();
}

From source file:com.moss.posixfifosockets.testharness.JavaTestClient.java

@Override
String send(String message) throws Exception {

    log("Connecting to server");
    socket = PosixFifoSocket.newClientConnection(address, 1000);
    log("Using socket " + socket);
    log("Opening output ");
    Writer w = new OutputStreamWriter(socket.out());
    log("Writing message ");
    w.write(message);//from   ww w.  java  2s.  co m
    log("Closing output");
    w.close();

    log("Opening response");
    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.in()));

    char[] b = new char[1024];

    log("Reading response");

    StringBuilder text = new StringBuilder();
    for (int x = reader.read(b); x != -1; x = reader.read(b)) {
        text.append(b, 0, x);
    }
    log("Closing response");
    reader.close();

    log("Done (received " + message + ")");

    //         String response = reader.readLine();
    //         reader.close();
    socket.close();
    //         System.out.println("Response " + response);
    return text.toString();
}

From source file:ape.NetworkDisconnectCommand.java

/**
 * This method writes to the Stand output
 *//* w ww.j a  va  2s.c o m*/
private boolean writeSTDOut(Process p) {
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    int count;
    CharBuffer cbuf = CharBuffer.allocate(99999);
    try {
        count = stdInput.read(cbuf);
        if (count != -1)
            cbuf.array()[count] = '\0';
        else if (cbuf.array()[0] != '\0')
            count = cbuf.array().length;
        else
            count = 0;
        for (int i = 0; i < count; i++)
            System.out.print(cbuf.get(i));
    } catch (IOException e) {
        System.err.println(
                "Writing Stdout in NetworkDisconnectCommand catches an exception, Turn on the VERBOSE flag to see the stack trace");
        e.printStackTrace();
        return false;
    }

    try {
        stdInput.close();
    } catch (IOException e) {
        System.err.println("Unable to close the IOStream");
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:com.cubusmail.server.services.CubusmailServlet.java

@Override
public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String fileName = request.getSession().getServletContext().getRealPath(request.getServletPath());
    BufferedReader reader = new BufferedReader(new FileReader(fileName));

    OutputStream outputStream = response.getOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(outputStream);
    char[] inBuf = new char[1024];
    int len = 0;//  www .  j  a  va 2s. c om
    try {
        while ((len = reader.read(inBuf)) > 0) {
            writer.write(inBuf, 0, len);
        }
    } catch (Throwable e) {
        log.error(e.getMessage(), e);
    }

    writer.flush();
    writer.close();
    outputStream.flush();
    outputStream.close();
}

From source file:de.dan_nrw.web.StringResponseHandler.java

@Override
protected String handleResponseInternal(HttpResponse response) throws ClientProtocolException, IOException {
    BufferedReader reader = null;

    try {/*from  ww w .  ja  va2 s  .  c  o m*/
        reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        int bytesRead = 0;
        char[] buffer = new char[2048];
        StringBuilder stringBuilder = new StringBuilder();

        while ((bytesRead = reader.read(buffer)) > 0) {
            stringBuilder.append(buffer, 0, bytesRead);
        }

        return stringBuilder.toString();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ex) {
                // would hide exception thrown in outer try block
                ex.printStackTrace(System.err);
            }
        }
    }
}

From source file:alluxio.client.rest.TestCase.java

/**
 * @param connection the HttpURLConnection
 * @return the String from the InputStream of HttpURLConnection
 *//*from  ww  w.  j a v a 2s .  c  om*/
public String getResponse(HttpURLConnection connection) throws Exception {
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    char[] buffer = new char[1024];
    int len;

    while ((len = br.read(buffer)) > 0) {
        sb.append(buffer, 0, len);
    }
    br.close();

    return sb.toString();
}

From source file:fr.dudie.acrachilisync.utils.IssueDescriptionReaderTest.java

/**
 * Constructor.//from w w w.j  av  a  2 s.  c  o  m
 * 
 * @param pFile
 *            the file containing the description to parse
 * @param pExpectParseException
 *            true if a parse exception is expected during parsing the description the given
 *            file contains
 * @throws IOException
 *             unable to read test file
 */
public IssueDescriptionReaderTest(final File pFile, final boolean pExpectParseException) throws IOException {

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Initialize test for file {}", pFile.getAbsolutePath());
    }
    expectParseException = pExpectParseException;

    final StringBuilder descriptionBuf = new StringBuilder();
    final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(pFile)));

    final char[] cbuf = new char[512];
    int len;
    while ((len = reader.read(cbuf)) != -1) {
        descriptionBuf.append(cbuf, 0, len);
    }

    description = descriptionBuf.toString();
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Description to parse: \n{}", description);
    }

    stackMD5 = pFile.getName().replaceAll("^descriptionreader_[ok]{2}_", "").replaceAll("(_\\w+)?\\.txt$", "");
    filename = pFile.getName();
}

From source file:br.msf.maven.utils.FileCopy.java

public StringBuilder readContents(final Charset encoding) throws IOException {
    BufferedReader reader = null;
    try {/*  w  ww  .  j  av  a 2  s . c  o m*/
        final StringBuilder fileData = new StringBuilder(1000);
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(getInput()), encoding));
        char[] buffer = new char[16384]; // 16k chars buffer
        int nRead;
        while ((nRead = reader.read(buffer)) != -1) {
            String readData = String.valueOf(buffer, 0, nRead);
            fileData.append(readData);
            buffer = new char[16384];
        }
        return fileData;
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:com.wind_now.statistics_api.latest.Builder.java

protected String readUrl(String urlString) throws Exception {
    BufferedReader reader = null;
    try {/*from   ww  w  .  j  a  v  a 2  s  .  c o m*/
        URL url = new URL(urlString);
        reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
        StringBuilder buffer = new StringBuilder();
        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();
        }
    }
}

From source file:forplay.android.AndroidAssetManager.java

@Override
protected void doGetText(final String path, final ResourceCallback<String> callback) {
    try {// w  w  w.j a  va2  s .c o  m
        InputStream is = openResource(path);
        try {
            StringBuffer fileData = new StringBuffer(1000);
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            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();
            String text = fileData.toString();
            callback.done(text);
        } finally {
            is.close();
        }
    } catch (IOException e) {
        callback.error(e);
    }
}