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.eclipse.jubula.client.archive.XmlStorage.java

/**
 * read data from a file using a specified character encoding
 * @param projectURL the project xml URL
 * @param encoding character encoding. Must be a supported encoding or an
 * UnsupportedEncodingException will be thrown. Null is allowed and means
 * system default encoding.// w w  w.ja  v  a 2s.c o m
 * @return a Stringbuilder holding the characters from the file
 * @throws IOException  in  case of io problems
 */
public static StringBuilder readFileContent(URL projectURL, String encoding) throws IOException {
    StringBuilder result = new StringBuilder();

    BufferedReader in = null;
    try {
        if (encoding != null) {
            in = new BufferedReader(new InputStreamReader(projectURL.openStream(), encoding));
        } else {
            in = new BufferedReader(new InputStreamReader(projectURL.openStream()));
        }

        char buff[] = new char[10240];
        int transfer;
        while ((transfer = in.read(buff)) != -1) {
            result.append(buff, 0, transfer);
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
    return result;
}

From source file:net.i2cat.netconf.test.ClassLoaderResourceTest.java

/**
 * Simple parser. It was used for proves with xml files
 * /* ww  w  . j  ava  2s.  c  o  m*/
 * @param stream
 * @return
 */
private String readStringFromFile(String pathFile) throws Exception {
    String answer = null;
    InputStream inputFile = getClass().getResourceAsStream(pathFile);
    InputStreamReader streamReader = new InputStreamReader(inputFile);
    StringBuffer fileData = new StringBuffer(1000);
    BufferedReader reader = new BufferedReader(streamReader);
    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();
    answer = fileData.toString();

    return answer;
}

From source file:com.github.util.HttpHandler.java

private String streamToString(InputStream is) throws IOException {
    StringWriter writer = new StringWriter();
    char[] buffer = new char[1024];
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    int n;// w w  w.  ja  v  a 2s  .com
    while ((n = reader.read(buffer)) != -1) {
        writer.write(buffer, 0, n);
    }
    is.close();
    return writer.toString();
}

From source file:com.sparkplatform.api.core.HttpClientTest.java

private String readString(InputStream is) throws IOException {
    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);/*from  w  w w . j ava2 s.  co  m*/
        buf = new char[1024];
    }
    reader.close();
    return fileData.toString();
}

From source file:org.onebusaway.nyc.vehicle_tracking.webapp.servlets.VehicleLocationCollectionServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    BufferedReader reader = req.getReader();
    StringBuffer out = new StringBuffer();
    char[] buf = new char[4096];
    while (true) {
        int n = reader.read(buf);
        if (n == -1) {
            break;
        }/*  w  ww .j  a  v a2s .c  o m*/
        out.append(new String(buf, 0, n));
    }
    String body = out.toString();

    Siri siri = (Siri) _xstream.fromXML(body);
    _vehicleLocationService.handleVehicleLocation(siri, body);

    resp.setStatus(200);
    resp.setContentType("text/html;charset=UTF-8");
    ServletOutputStream writer = resp.getOutputStream();
    writer.print("ok");
    writer.close();
}

From source file:org.appverse.web.framework.backend.frontfacade.rest.authentication.servlet.JWSHttpServletRequestWrapperTest.java

private String obtainContent(InputStream is) throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
    char[] charBuffer = new char[128];
    int bytesRead = -1;

    while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
        stringBuilder.append(charBuffer, 0, bytesRead);
    }//from ww w  .  j ava 2 s  .  c  om
    return stringBuilder.toString();
}

From source file:com.tealeaf.Downloader.java

protected String read(File f) {
    StringBuffer contents = new StringBuffer(1000);
    try {//from w w w. j  a  v a  2  s  .co  m
        BufferedReader reader = new BufferedReader(new FileReader(f));
        char[] buf = new char[1024];
        int read = 0;
        while ((read = reader.read(buf)) != -1) {
            contents.append(buf, 0, read);
        }
        reader.close();
    } catch (IOException e) {
        logger.log(e);
        contents = new StringBuffer();
    }
    return contents.toString();
}

From source file:RGSCommonUtils.UniversalConnectionInterfaceImp.java

protected String responceToString(CloseableHttpResponse response) throws IOException {
    String result = "";
    Header[] headers = response.getAllHeaders();
    char[] cbuf = new char[10000];
    try {/*from w  w w. ja  v  a  2s  . com*/
        HttpEntity resEntity = response.getEntity();
        if (resEntity != null) {
            InputStreamReader inStream = new InputStreamReader(resEntity.getContent(), "windows-1251");
            BufferedReader br = new BufferedReader(inStream);
            int r = 0;
            while ((r = br.read(cbuf)) != -1) {
                result += String.valueOf(cbuf, 0, r);
            }
        }
    } finally {
        response.close();
    }
    return result;
}

From source file:foam.blob.RestBlobService.java

@Override
public Blob put_(X x, Blob blob) {
    if (blob instanceof IdentifiedBlob) {
        return blob;
    }/*w  ww  .ja va  2  s  .com*/

    HttpURLConnection connection = null;
    OutputStream os = null;
    InputStream is = null;

    try {
        URL url = new URL(address_);
        connection = (HttpURLConnection) url.openConnection();

        //configure HttpURLConnection
        connection.setConnectTimeout(5 * 1000);
        connection.setReadTimeout(5 * 1000);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        //set request method
        connection.setRequestMethod("PUT");

        //configure http header
        connection.setRequestProperty("Accept", "*/*");
        connection.setRequestProperty("Connection", "keep-alive");
        connection.setRequestProperty("Content-Type", "application/octet-stream");

        // get connection ouput stream
        os = connection.getOutputStream();

        //output blob into connection
        blob.read(os, 0, blob.getSize());

        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new RuntimeException("Upload failed");
        }

        is = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        CharBuffer cb = CharBuffer.allocate(65535);
        reader.read(cb);
        cb.rewind();

        return (Blob) getX().create(JSONParser.class).parseString(cb.toString(), IdentifiedBlob.class);
    } catch (Throwable t) {
        t.printStackTrace();
        throw new RuntimeException(t);
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
        IOUtils.close(connection);
    }
}

From source file:kr.ac.kaist.swrc.jhannanum.share.JSONReader.java

/**
 * Constructor./*from w  ww  .  j  ava2s .  c o  m*/
 * @param filePath - the file path of the plug-in configuration file
 * @throws JSONException
 * @throws IOException
 */
public JSONReader(String filePath) throws JSONException, IOException {
    //      FileReader reader = new FileReader(filePath);
    BufferedReader reader = BufferedReaderBuilder.fromRelativePath(filePath);
    StringBuffer buf = new StringBuffer();
    char[] cbuf = new char[4096];
    int idx = 0;
    while ((idx = reader.read(cbuf)) != -1) {
        buf.append(cbuf, 0, idx);
    }
    json = new JSONObject(buf.toString());

    reader.close();
}