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:com.cognitect.transit.TransitMPTest.java

public void testReadSymbol() throws IOException {

    Reader r = readerOf("~$foo");
    Object v = r.read();
    assertEquals("foo", v.toString());
}

From source file:com.cognitect.transit.TransitMPTest.java

public void testReadInteger() throws IOException {

    Reader r = readerOf("~i42");
    assertEquals(42L, r.read());

    r = readerOf("~n4256768765123454321897654321234567");
    assertEquals(0, (new BigInteger("4256768765123454321897654321234567")).compareTo((BigInteger) r.read()));
}

From source file:net.fenyo.gnetwatch.actions.ExternalCommand.java

/**
 * Reads a line from the process output.
 * @param r reader.//from   ww  w.  jav  a  2 s.c  o m
 * @return line read.
 * @throws IOException IO exception.
 * @throws InterruptedException exception.
 */
// data read is lost when interrupted
// returns null if EOF
// major feature: it never blocks the current thread while reading a stream
// On peut amliorer les perfs en gardant dans sb ce qui est lu et donc en lisant plusieurs caractres  la fois
// et en ne retournant que jusqu'au retour chariot.
// this private method must be called from synchronized methods
// any thread
private String readLine(Reader r) throws IOException, InterruptedException {
    sb.setLength(0);
    while (!Thread.currentThread().isInterrupted()) {
        if (r.ready()) {
            final int ret = r.read();
            if (ret == -1)
                return sb.length() != 0 ? sb.toString() : null;
            if (ret == '\n')
                return sb.toString();
            sb.append((char) ret);
        } else {
            try {
                process.exitValue();
                return sb.length() != 0 ? sb.toString() : null;
            } catch (final IllegalThreadStateException ex) {
            }
            Thread.sleep(100);
        }
    }
    log.info("readLine(): was interrupted");
    throw new InterruptedException("readLine()");
}

From source file:net.krautchan.parser.KCPageParser.java

public List<KCThread> filterThreads(Reader reader, KCThreadStreamParser parser) throws Exception {
    parser.setBasePath(resolverPath);//from w w  w.ja va 2  s .  c o m
    List<KCThread> threads = new ArrayList<KCThread>();
    char[] filter = parser.getFilterMarker();
    ThreadState state = new ThreadState();
    int curChar;
    int pos = 0;
    curChar = reader.read();
    while (-1 != curChar) {
        if ((state.curState == StateEnum.START) || (state.curState == StateEnum.READ_THREAD)) {
            if (curChar == filter[pos]) {
                pos++;
                if (pos == filter.length) {
                    KCThread t = parser.parse(reader);
                    t.board_id = boardDbId;
                    threads.add(t);
                    state.curState = StateEnum.START;
                    pos = 0;
                }
            } else {
                pos = 0;
            }
        }
        curChar = reader.read();
    }
    parser.notifyDone();
    return threads;
}

From source file:fr.paris.lutece.plugins.helpdesk.modules.solr.search.SolrHelpdeskIndexer.java

/**
 * Builds a {@link SolrItem} element which will be used by Solr during the indexing of the subject list
 *
 * @param subject the {@link Subject} to index
 * @param strUrl the url of the subject/*  w w  w .  j a  v  a 2 s. co  m*/
 * @param strRoleKey The role key
 * @param plugin The {@link Plugin}
 * @return The Solr {@link SolrItem} containing Subject data
 * @throws IOException The IO Exception
 */
private SolrItem getDocument(Subject subject, String strRoleKey, String strUrl, Plugin plugin)
        throws IOException {
    // make a new, empty document
    SolrItem item = new SolrItem();

    // Setting the URL field
    item.setUrl(strUrl);

    // Setting the Uid field
    String strIdSubject = String.valueOf(subject.getId());
    item.setUid(getResourceUid(strIdSubject, HelpdeskIndexerUtils.CONSTANT_SUBJECT_TYPE_RESOURCE));

    //Setting the Content field
    String strContentToIndex = subject.getText();
    StringReader readerPage = new StringReader(strContentToIndex);
    HTMLParser parser = new HTMLParser(readerPage);

    Reader reader = parser.getReader();
    int c;
    StringBuffer sb = new StringBuffer();

    while ((c = reader.read()) != -1) {
        sb.append(String.valueOf((char) c));
    }

    reader.close();
    item.setContent(sb.toString());

    // Setting the Title field
    item.setTitle(subject.getText());

    // Setting the Site field
    item.setSite(SolrIndexerService.getWebAppName());

    // Setting the Type field
    item.setType(HelpdeskPlugin.PLUGIN_NAME);

    // Setting the Role field
    item.setRole(strRoleKey);

    // return the document
    return item;
}

From source file:com.cognitect.transit.TransitMPTest.java

public void testReadMany() throws IOException {

    Reader r = readerOf(true, null, false, "foo", 42.2, 42);
    assertTrue((Boolean) r.read());
    assertNull(r.read());//from  ww w.  ja v a 2  s.  c o  m
    assertFalse((Boolean) r.read());
    assertEquals("foo", r.read());
    assertEquals(42.2, r.read());
    assertEquals(42L, r.read());
}

From source file:fr.paris.lutece.plugins.helpdesk.modules.solr.search.SolrHelpdeskIndexer.java

/**
 * Builds a {@link SolrItem} which will be used by Solr during the indexing of the question/answer list
 *
 * @param nIdFaq The {@link Faq} Id/*from  w w w  . j av  a  2 s .  c  o  m*/
 * @param questionAnswer the {@link QuestionAnswer} to index
 * @param strUrl the url of the subject
 * @param strRoleKey The role key
 * @param plugin The {@link Plugin}
 * @return A Solr {@link SolrItem} containing QuestionAnswer Data
 * @throws IOException The IO Exception
 */
private SolrItem getDocument(int nIdFaq, QuestionAnswer questionAnswer, String strUrl, String strRoleKey,
        Plugin plugin) throws IOException {
    // make a new, empty document
    SolrItem item = new SolrItem();

    // Setting the Id faq field
    item.addDynamicField(HelpdeskSearchItem.FIELD_FAQ_ID, String.valueOf(nIdFaq));

    // Setting the Role field
    item.setRole(strRoleKey);

    // Setting the URL field
    item.setUrl(strUrl);

    // Setting the subject field
    item.addDynamicField(HelpdeskSearchItem.FIELD_SUBJECT, String.valueOf(questionAnswer.getIdSubject()));

    // Setting the Uid field
    String strIdQuestionAnswer = String.valueOf(questionAnswer.getIdQuestionAnswer());
    item.setUid(
            getResourceUid(strIdQuestionAnswer, HelpdeskIndexerUtils.CONSTANT_QUESTION_ANSWER_TYPE_RESOURCE));

    // Setting the Date field
    // Add the last modified date of the file a field named "modified".
    item.setDate(questionAnswer.getCreationDate());

    //Setting the Content field
    String strContentToIndex = getContentToIndex(questionAnswer, plugin);
    StringReader readerPage = new StringReader(strContentToIndex);
    HTMLParser parser = new HTMLParser(readerPage);

    Reader reader = parser.getReader();
    int c;
    StringBuffer sb = new StringBuffer();

    while ((c = reader.read()) != -1) {
        sb.append(String.valueOf((char) c));
    }

    reader.close();
    item.setContent(sb.toString());

    // Setting the Title field
    item.setTitle(questionAnswer.getQuestion());

    // Setting the Site field
    item.setSite(SolrIndexerService.getWebAppName());

    // Setting the Type field
    item.setType(HelpdeskPlugin.PLUGIN_NAME);

    // return the document
    return item;
}

From source file:com.cognitect.transit.TransitMPTest.java

public void testWriteReadTime() throws Exception {

    final Date da[] = { new Date(-6106017600000l), new Date(0), new Date(946728000000l),
            new Date(1396909037000l) };

    List l = Arrays.asList(da);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Writer w = TransitFactory.writer(TransitFactory.Format.MSGPACK, out);
    w.write(l);/*w w  w  . j  av a2 s . com*/
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    Reader r = TransitFactory.reader(TransitFactory.Format.MSGPACK, in);
    Object o = r.read();
}

From source file:com.cognitect.transit.TransitMPTest.java

public void testWriteReadSpecialNumbers() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Writer w = TransitFactory.writer(TransitFactory.Format.MSGPACK, out);
    w.write(Double.NaN);//  w  ww  . java 2s .c  o  m
    w.write(Float.NaN);
    w.write(Double.POSITIVE_INFINITY);
    w.write(Float.POSITIVE_INFINITY);
    w.write(Double.NEGATIVE_INFINITY);
    w.write(Float.NEGATIVE_INFINITY);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    Reader r = TransitFactory.reader(TransitFactory.Format.MSGPACK, in);
    assert ((Double) r.read()).isNaN();
    assert ((Double) r.read()).isNaN();
    assertEquals(Double.POSITIVE_INFINITY, (Double) r.read());
    assertEquals(Double.POSITIVE_INFINITY, (Double) r.read());
    assertEquals(Double.NEGATIVE_INFINITY, (Double) r.read());
    assertEquals(Double.NEGATIVE_INFINITY, (Double) r.read());
}