Example usage for java.io Reader read

List of usage examples for java.io Reader read

Introduction

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

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:net.officefloor.packaging.officefloor.OfficeFloorIT.java

/**
 * Undertakes the request./*from   ww w.  j  a  va2s  .  c  o  m*/
 * 
 * @param client
 *            {@link HttpClient}.
 * @param uri
 *            URI.
 * @return String content of response entity.
 */
private String doRequest(HttpClient client, String uri) throws Exception {
    HttpResponse response = client.execute(new HttpGet("http://localhost:7878/" + uri));
    assertEquals("Should be successful", 200, response.getStatusLine().getStatusCode());
    StringWriter buffer = new StringWriter();
    Reader reader = new InputStreamReader(response.getEntity().getContent());
    for (int character = reader.read(); character != -1; character = reader.read()) {
        buffer.write(character);
    }
    return buffer.toString();
}

From source file:org.n52.car.io.RESTConnectionTest.java

private void assertReader(Reader data) throws IOException {
    Assert.assertThat(data, is(notNullValue()));
    Assert.assertThat(data.read(), is(not(-1)));

    data.close();/*from w w  w  .j  av  a 2  s .c  o  m*/
}

From source file:edu.asu.msse.sgowdru.moviemediaplayerrpc.MovieGetInfoAsync.java

String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;/*from  w  w w . ja va  2s  .com*/
    while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
    }
    return sb.toString();
}

From source file:com.deltachi.videotex.utils.JSONReader.java

private String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;// ww  w.java2  s  . co  m
    while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
    }
    return sb.toString();
}

From source file:ru.gelin.android.weather.google.GoogleWeatherSourceTest.java

public void ignoretestRawQueryRu() throws Exception {
    String apiUrl = "http://www.google.com/ig/api?weather=%s&hl=%s";
    String fullUrl = String.format(apiUrl, URLEncoder.encode("?", "UTF-8"),
            URLEncoder.encode("ru", "UTF-8"));

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(fullUrl);
    request.setHeader("User-Agent", "Google Weather/1.0 (Linux; Android)");
    HttpResponse response = client.execute(request);

    StatusLine status = response.getStatusLine();
    if (status.getStatusCode() != 200) {
        assertTrue(false);/*from   www  .  j av a2s  .  com*/
    }

    HttpEntity entity = response.getEntity();
    String charset = HttpUtils.getCharset(entity);
    System.out.println(response.getAllHeaders());
    Reader in = new InputStreamReader(entity.getContent(), charset);
    int c;
    while ((c = in.read()) >= 0) {
        System.out.print((char) c);
    }
    assertTrue(true);
}

From source file:com.seajas.search.contender.service.modifier.ModifierFilterProcessor.java

/**
 * Process the given reader using this filter.
 * /* ww  w  .  j  ava  2 s .co m*/
 * @param filter
 * @param reader
 * @return Reader
 * @throws IOException
 */
public Reader process(final ModifierFilter filter, final Reader reader) throws IOException {
    StringBuffer stringBuffer = new StringBuffer();

    for (int c; (c = reader.read()) != -1;)
        stringBuffer.append((char) c);

    reader.close();

    if (!filter.getIsExpression()) {
        Pattern pattern = Pattern.compile(
                Pattern.quote(filter.getFragmentStart()) + ".*" + Pattern.quote(filter.getFragmentEnd()),
                Pattern.DOTALL | Pattern.CASE_INSENSITIVE);

        return new StringReader(
                pattern.matcher(stringBuffer).replaceAll(filter.getFragmentStart() + filter.getFragmentEnd()));
    } else {
        Matcher startMatcher = Pattern.compile(filter.getFragmentStart(), Pattern.CASE_INSENSITIVE)
                .matcher(stringBuffer),
                endMatcher = Pattern.compile(filter.getFragmentEnd(), Pattern.CASE_INSENSITIVE)
                        .matcher(stringBuffer);

        while (startMatcher.find() && endMatcher.find(startMatcher.end()))
            if (startMatcher.end() != endMatcher.start()) {
                stringBuffer.delete(startMatcher.end(), endMatcher.start());

                startMatcher.reset();
                endMatcher.reset();
            }

        // Store the result

        return new StringReader(stringBuffer.toString());
    }
}

From source file:ReaderUtil.java

/**
 * Dumps the contents of the {@link Reader} to a String, closing the {@link Reader} when done.
 *///from  www .java2  s .  c o  m
public String readToString(Reader in) throws IOException {
    StringBuffer buf = null;
    try {
        buf = pool.borrowObject();
        for (int c = in.read(); c != -1; c = in.read()) {
            buf.append((char) c);
        }
        return buf.toString();
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException("Unable to borrow buffer from pool" + e.toString());
    } finally {
        try {
            in.close();
        } catch (Exception e) {
            // ignored
        }
        try {
            if (null != buf) {
                pool.returnObject(buf);
            }
        } catch (Exception e) {
            // ignored
        }
    }
}

From source file:cz.vse.fis.keg.entityclassifier.core.entitylinking.SFISearch.java

public LinkedEntity findWikipediaArticle(String mention, String lang) {

    LinkedEntity linkedEntity = null;/*from w  w  w.  jav  a2s. c  o m*/
    OutputStream os = null;
    try {
        String url = Settings.SEMITAGS_LINKING_ENDPOINT;
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        String data = "lang=" + lang + "&surfaceForm=" + mention;
        con.setRequestMethod("POST");
        con.setRequestProperty("Accept", "application/json");
        con.setDoOutput(true);
        os = con.getOutputStream();
        os.write(data.getBytes("UTF-8"));
        os.flush();
        os.close();

        int responseCode = con.getResponseCode();

        int code = con.getResponseCode();

        if (code == 500) {
            return linkedEntity;
        }
        StringBuffer buffer = new StringBuffer();
        InputStream is = con.getInputStream();
        Reader isr = new InputStreamReader(is, "UTF-8");
        Reader in = new BufferedReader(isr);
        int ch;
        while ((ch = in.read()) > -1) {
            buffer.append((char) ch);
        }
        in.close();
        isr.close();
        String result = buffer.toString();

        Object linkObj = null;
        Object scoreObj = null;
        String link = "";
        double score;
        JSONObject jsonObj = new JSONObject(result);

        linkObj = jsonObj.get("link");
        scoreObj = jsonObj.get("socre");
        if (!linkObj.toString().equals("null")) {
            link = jsonObj.getString("link");
            score = jsonObj.getDouble("socre");
            linkedEntity = new LinkedEntity();
            linkedEntity.setPageTitle(link.split("/")[link.split("/").length - 1].replace("_", " "));
            linkedEntity.setConfidence(score);
            return linkedEntity;
        } else {
            return null;
        }
    } catch (MalformedURLException ex) {
        Logger.getLogger(SFISearch.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(SFISearch.class.getName()).log(Level.SEVERE, null, ex);
    }

    return linkedEntity;
}

From source file:com.yahoo.platform.yui.coverage.report.CoverageReport.java

/**
 * Creates a new report object from a reader.
 * @param in The reader containing JSON information.
 * @throws java.io.IOException/*from  w  w w . ja  v a2  s.  co  m*/
 * @throws org.json.JSONException
 */
public CoverageReport(Reader in) throws IOException, JSONException {
    StringBuilder builder = new StringBuilder();
    int c;

    while ((c = in.read()) != -1) {
        builder.append((char) c);
    }

    this.data = new JSONObject(builder.toString());
    generateFileReports();
}

From source file:net.officefloor.tutorial.inherithttpserver.InheritHttpServerTest.java

/**
 * Undertakes the {@link HttpRequest} and ensures the responding page is as
 * expected.//from ww  w.j  a v a  2  s .  com
 * 
 * @param url
 *            URL.
 * @param fileNameContainingExpectedContent
 *            Name of file containing the expected content.
 */
private void doTest(String url, String fileNameContainingExpectedContent) throws IOException {

    // Undertake the request
    HttpResponse response = this.client.execute(new HttpGet("http://localhost:7878/" + url));
    assertEquals("Incorrect response status for URL " + url, 200, response.getStatusLine().getStatusCode());
    String content = EntityUtils.toString(response.getEntity());

    // Obtain the expected content
    InputStream contentInputStream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream(fileNameContainingExpectedContent);
    assertNotNull("Can not find file " + fileNameContainingExpectedContent, contentInputStream);
    Reader reader = new InputStreamReader(contentInputStream);
    StringWriter expected = new StringWriter();
    for (int character = reader.read(); character != -1; character = reader.read()) {
        expected.append((char) character);
    }
    reader.close();

    // Ensure the context is as expected
    assertEquals("Incorrect content for URL " + url, expected.toString(), content);
}