Java Code Examples for java.io.Reader#ready()

The following examples show how to use java.io.Reader#ready() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private static void gobble(InputStream is, Reader rd,
        int ec, boolean force)
                throws Exception
                {
    int a = is.available();
    boolean r = rd.ready();
    log.print("" + a + " bytes available, "
            + "reader " + (r ? "" : "not ") + "ready");
    if (!r && !force) {
        log.println();
        return;
    }
    int c;
    try {
        c = rd.read();
    } catch (InterruptedIOException x) {
        log.println();
        throw x;
    }
    log.println(", read() ==> "
            + (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));
    if (c != ec)
        throw new Exception("Incorrect value read: Expected "
                + ec + ", read " + (char)c);
}
 
Example 2
private static void gobble(InputStream is, Reader rd,
        int ec, boolean force)
                throws Exception
                {
    int a = is.available();
    boolean r = rd.ready();
    log.print("" + a + " bytes available, "
            + "reader " + (r ? "" : "not ") + "ready");
    if (!r && !force) {
        log.println();
        return;
    }
    int c;
    try {
        c = rd.read();
    } catch (InterruptedIOException x) {
        log.println();
        throw x;
    }
    log.println(", read() ==> "
            + (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));
    if (c != ec)
        throw new Exception("Incorrect value read: Expected "
                + ec + ", read " + (char)c);
}
 
Example 3
private static void gobble(InputStream is, Reader rd,
        int ec, boolean force)
                throws Exception
                {
    int a = is.available();
    boolean r = rd.ready();
    log.print("" + a + " bytes available, "
            + "reader " + (r ? "" : "not ") + "ready");
    if (!r && !force) {
        log.println();
        return;
    }
    int c;
    try {
        c = rd.read();
    } catch (InterruptedIOException x) {
        log.println();
        throw x;
    }
    log.println(", read() ==> "
            + (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));
    if (c != ec)
        throw new Exception("Incorrect value read: Expected "
                + ec + ", read " + (char)c);
}
 
Example 4
private static void gobble(InputStream is, Reader rd,
        int ec, boolean force)
                throws Exception
                {
    int a = is.available();
    boolean r = rd.ready();
    log.print("" + a + " bytes available, "
            + "reader " + (r ? "" : "not ") + "ready");
    if (!r && !force) {
        log.println();
        return;
    }
    int c;
    try {
        c = rd.read();
    } catch (InterruptedIOException x) {
        log.println();
        throw x;
    }
    log.println(", read() ==> "
            + (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));
    if (c != ec)
        throw new Exception("Incorrect value read: Expected "
                + ec + ", read " + (char)c);
}
 
Example 5
private static void gobble(InputStream is, Reader rd,
        int ec, boolean force)
                throws Exception
                {
    int a = is.available();
    boolean r = rd.ready();
    log.print("" + a + " bytes available, "
            + "reader " + (r ? "" : "not ") + "ready");
    if (!r && !force) {
        log.println();
        return;
    }
    int c;
    try {
        c = rd.read();
    } catch (InterruptedIOException x) {
        log.println();
        throw x;
    }
    log.println(", read() ==> "
            + (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));
    if (c != ec)
        throw new Exception("Incorrect value read: Expected "
                + ec + ", read " + (char)c);
}
 
Example 6
private static void gobble(InputStream is, Reader rd,
        int ec, boolean force)
                throws Exception
                {
    int a = is.available();
    boolean r = rd.ready();
    log.print("" + a + " bytes available, "
            + "reader " + (r ? "" : "not ") + "ready");
    if (!r && !force) {
        log.println();
        return;
    }
    int c;
    try {
        c = rd.read();
    } catch (InterruptedIOException x) {
        log.println();
        throw x;
    }
    log.println(", read() ==> "
            + (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));
    if (c != ec)
        throw new Exception("Incorrect value read: Expected "
                + ec + ", read " + (char)c);
}
 
Example 7
private static void gobble(InputStream is, Reader rd,
        int ec, boolean force)
                throws Exception
                {
    int a = is.available();
    boolean r = rd.ready();
    log.print("" + a + " bytes available, "
            + "reader " + (r ? "" : "not ") + "ready");
    if (!r && !force) {
        log.println();
        return;
    }
    int c;
    try {
        c = rd.read();
    } catch (InterruptedIOException x) {
        log.println();
        throw x;
    }
    log.println(", read() ==> "
            + (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));
    if (c != ec)
        throw new Exception("Incorrect value read: Expected "
                + ec + ", read " + (char)c);
}
 
Example 8
private static void gobble(InputStream is, Reader rd,
        int ec, boolean force)
                throws Exception
                {
    int a = is.available();
    boolean r = rd.ready();
    log.print("" + a + " bytes available, "
            + "reader " + (r ? "" : "not ") + "ready");
    if (!r && !force) {
        log.println();
        return;
    }
    int c;
    try {
        c = rd.read();
    } catch (InterruptedIOException x) {
        log.println();
        throw x;
    }
    log.println(", read() ==> "
            + (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));
    if (c != ec)
        throw new Exception("Incorrect value read: Expected "
                + ec + ", read " + (char)c);
}
 
Example 9
private static void gobble(InputStream is, Reader rd,
        int ec, boolean force)
                throws Exception
                {
    int a = is.available();
    boolean r = rd.ready();
    log.print("" + a + " bytes available, "
            + "reader " + (r ? "" : "not ") + "ready");
    if (!r && !force) {
        log.println();
        return;
    }
    int c;
    try {
        c = rd.read();
    } catch (InterruptedIOException x) {
        log.println();
        throw x;
    }
    log.println(", read() ==> "
            + (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));
    if (c != ec)
        throw new Exception("Incorrect value read: Expected "
                + ec + ", read " + (char)c);
}
 
Example 10
private static void gobble(InputStream is, Reader rd,
        int ec, boolean force)
                throws Exception
                {
    int a = is.available();
    boolean r = rd.ready();
    log.print("" + a + " bytes available, "
            + "reader " + (r ? "" : "not ") + "ready");
    if (!r && !force) {
        log.println();
        return;
    }
    int c;
    try {
        c = rd.read();
    } catch (InterruptedIOException x) {
        log.println();
        throw x;
    }
    log.println(", read() ==> "
            + (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));
    if (c != ec)
        throw new Exception("Incorrect value read: Expected "
                + ec + ", read " + (char)c);
}
 
Example 11
Source Project: netbeans   File: ExecSupport.java    License: Apache License 2.0 5 votes vote down vote up
private int read(Reader is, char[] buff, int start, int count) throws InterruptedException, IOException {

            while (!is.ready() && !done) {
                sleep(100);
            }
            return is.read(buff, start, count);
        }
 
Example 12
Source Project: samsung-tv-control   File: SamsungRemote.java    License: MIT License 5 votes vote down vote up
/**
 * Reads all messages which are left in the buffer and therefore empties it.
 *
 * @param reader the reader.
 * @throws IOException if an I/O error occurs.
 */
private void emptyReaderBuffer(Reader reader) throws IOException {
    log("Emptying reader buffer.");
    while (reader.ready()) {
        readMessage(reader);
    }
}
 
Example 13
Source Project: netbeans   File: ExecSupport.java    License: Apache License 2.0 5 votes vote down vote up
private int read(Reader is, char[] buff, int start, int count) throws InterruptedException, IOException {

            while (!is.ready() && !done) {
                sleep(100);
            }
            return is.read(buff, start, count);
        }
 
Example 14
Source Project: netbeans   File: ExecSupport.java    License: Apache License 2.0 5 votes vote down vote up
private int read(Reader is, char[] buff, int start, int count) 
        throws InterruptedException, IOException {
    
    while (!is.ready() && !done) {
        sleep(100);
    }
    
    return is.read(buff, start, count);
}
 
Example 15
Source Project: googleads-java-lib   File: CsvFilesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetCsvDataArray_fromReader() throws IOException {
  Reader reader = Files.newReader(csvStringFile, StandardCharsets.UTF_8);
  List<String[]> actualDataArray = CsvFiles.getCsvDataArray(reader, headerPresent);
  assertEquals(dataList.size(), actualDataArray.size());
  for (int i = 0; i < actualDataArray.size(); i++) {
    assertArrayEquals("Row " + i + " does not match", dataList.get(i), actualDataArray.get(i));
  }

  // Reader should be closed (not ready).
  thrown.expect(IOException.class);
  thrown.expectMessage("Stream closed");
  reader.ready();
}
 
Example 16
Source Project: angelix   File: SolverProcess.java    License: MIT License 5 votes vote down vote up
/** Reads the given Reader until the given String is read (or end of input is reached);
 * may block until input is available; the stopping string must occur at the end of the
 * input.  This is typically used to read through a prompt on standard output; when the stopping
 * string (the prompt) is read, one knows that the output from the program is complete and not
 * to wait for any more.
 * 
 * @param r the Reader to read characters from
 * @param end a stopping String
 * @return the String read
 * @throws IOException if an IO failure occurs
 */
static public /*@NonNull*/String listenThru(/*@NonNull*/Reader r, /*@Nullable*/ String end) throws IOException {
	char[] buf = getBuffer();
	try {
		int len = end != null ? end.length() : 0;
		int p = 0; // Number of characters read
		int parens = 0;
		while (end != null || r.ready()) {
			//System.out.println("ABOUT TO READ " + p);
			int i = r.read(buf,p,buf.length-p);
			if (i == -1) break; // End of Input
			for (int ii=0; ii<i; ++ii) {
				if (buf[p+ii] == '(') ++parens; 
				else if (buf[p+ii] == ')') --parens;
			}
			p += i;
			//System.out.println("HEARD: " + new String(buf,0,p));
			if (end != null && p >= len) {
				// Need to compare a String to a part of a char array - we iterate by
				// hand to avoid creating a new String or CharBuffer object
				boolean match = true;
				int k = p-len;
				for (int j=0; j<len; j++) {
					if (end.charAt(j) != buf[k++]) { match = false; break; }
				}
				if (match && (!"\n".equals(end) || parens == 0)) break; // stopping string matched
			}
			if (p == buf.length) { // expand the buffer
				char[] nbuf = new char[2*buf.length];
				System.arraycopy(buf,0,nbuf,0,p);
				buf = nbuf;
			}
		}
		return new String(buf,0,p);
	} finally {
		putBuffer(buf);
	}
}
 
Example 17
Source Project: james-project   File: BayesianAnalyzer.java    License: Apache License 2.0 4 votes vote down vote up
private String nextToken(Reader reader) throws java.io.IOException {
    StringBuilder token = new StringBuilder();
    int i;
    char ch;
    char ch2;
    boolean previousWasDigit = false;
    boolean tokenCharFound = false;

    if (!reader.ready()) {
        return null;
    }

    while ((i = reader.read()) != -1) {

        ch = (char) i;

        if (ch == ':') {
            String tokenString = token.toString() + ':';
            if (tokenString.equals("From:") || tokenString.equals("Return-Path:") || tokenString.equals("Subject:") || tokenString.equals("To:")) {
                return tokenString;
            }
        }

        if (Character.isLetter(ch) || ch == '-' || ch == '$' || ch == '€'
                || ch == '!' || ch == '\'') {
            tokenCharFound = true;
            previousWasDigit = false;
            token.append(ch);
        } else if (Character.isDigit(ch)) {
            tokenCharFound = true;
            previousWasDigit = true;
            token.append(ch);
        } else if (previousWasDigit && (ch == '.' || ch == ',')) {
            reader.mark(1);
            previousWasDigit = false;
            i = reader.read();
            if (i == -1) {
                break;
            }
            ch2 = (char) i;
            if (Character.isDigit(ch2)) {
                tokenCharFound = true;
                previousWasDigit = true;
                token.append(ch);
                token.append(ch2);
            } else {
                reader.reset();
                break;
            }
        } else if (ch == '\r') {
            // cr found, ignore
        } else if (ch == '\n') {
            // eol found
            tokenCharFound = true;
            previousWasDigit = false;
            token.append(ch);
            break;
        } else if (tokenCharFound) {
            break;
        }
    }

    if (tokenCharFound) {
        // System.out.println("Token read: " + token);
        return token.toString();
    } else {
        return null;
    }
}
 
Example 18
@Override
public boolean ready() throws IOException {
	Reader rdr = getReader();
	return (rdr != null && rdr.ready());
}
 
Example 19
Source Project: Repeat   File: IPCProtocol.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Retrieve a list of messages from the reader.
 * This blocks until there is a message to be read from
 * the reader.
 */
public static List<String> getMessages(Reader reader) throws IOException, InterruptedException {
	/**
	 * Create a blocking read waiting for the next communication.
	 */
	int retryCount = 0;
	int firstCharacter = reader.read();
	while (firstCharacter == -1) {
		retryCount++;
		if (retryCount < MAX_MESSAGE_RETRY) {
			Thread.sleep(READ_LOOP_SLEEP_DURATION_MS);
		} else {
			LOGGER.log(Level.WARNING, "Max retry reached.");
			return null;
		}
		firstCharacter = reader.read();
	}

	/**
	 * Build the request.
	 */
	List<String> output = new LinkedList<>();
	ByteArrayOutputStream buffer = new ByteArrayOutputStream();

	if (firstCharacter != MESSAGE_DELIMITER) {
		buffer.write(firstCharacter);
	}

	while (reader.ready() || output.isEmpty()) {
		int readValue = reader.read();
		if (readValue != -1) {
			if (readValue == MESSAGE_DELIMITER) {
				if (buffer.size() != 0) {
					output.add(IPCCodec.decode(buffer.toByteArray()));
					buffer.reset();
				}
			} else if (readValue != NULL_CHARACTER) {
				buffer.write(readValue);
			}
		} else {
			if (buffer.size() != 0) {
				output.add(IPCCodec.decode(buffer.toByteArray()));
				buffer.reset();
			}
			break;
		}
	}
	return output;
}
 
Example 20
Source Project: james-project   File: Tokenizer.java    License: Apache License 2.0 4 votes vote down vote up
private String nextToken(Reader reader) throws java.io.IOException {
    StringBuilder token = new StringBuilder();
    int i;
    char ch;
    char ch2;
    boolean previousWasDigit = false;
    boolean tokenCharFound = false;

    if (!reader.ready()) {
        return null;
    }

    while ((i = reader.read()) != -1) {

        ch = (char) i;

        if (ch == ':') {
            String tokenString = token.toString() + ':';
            if (tokenString.equals("From:") || tokenString.equals("Return-Path:") || tokenString.equals("Subject:") || tokenString.equals("To:")) {
                return tokenString;
            }
        }

        if (Character.isLetter(ch) || ch == '-' || ch == '$' || ch == '€'
                || ch == '!' || ch == '\'') {
            tokenCharFound = true;
            previousWasDigit = false;
            token.append(ch);
        } else if (Character.isDigit(ch)) {
            tokenCharFound = true;
            previousWasDigit = true;
            token.append(ch);
        } else if (previousWasDigit && (ch == '.' || ch == ',')) {
            reader.mark(1);
            previousWasDigit = false;
            i = reader.read();
            if (i == -1) {
                break;
            }
            ch2 = (char) i;
            if (Character.isDigit(ch2)) {
                tokenCharFound = true;
                previousWasDigit = true;
                token.append(ch);
                token.append(ch2);
            } else {
                reader.reset();
                break;
            }
        } else if (ch == '\r') { //NOPMD
            // cr found, ignore
        } else if (ch == '\n') {
            // eol found
            tokenCharFound = true;
            previousWasDigit = false;
            token.append(ch);
            break;
        } else if (tokenCharFound) {
            break;
        }
    }

    if (tokenCharFound) {
        // System.out.println("Token read: " + token);
        return token.toString();
    } else {
        return null;
    }
}