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

The following examples show how to use java.io.Reader#ready() . 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
Source File: StreamTimeout.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: StreamTimeout.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: StreamTimeout.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: StreamTimeout.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: StreamTimeout.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: StreamTimeout.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: StreamTimeout.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: StreamTimeout.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: StreamTimeout.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: StreamTimeout.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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 File: ExecSupport.java    From netbeans with 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 File: CsvFilesTest.java    From googleads-java-lib with 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 13
Source File: ExecSupport.java    From netbeans with 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 File: SamsungRemote.java    From samsung-tv-control with 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 15
Source File: ExecSupport.java    From netbeans with 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 16
Source File: SolverProcess.java    From angelix with 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 File: BayesianAnalyzer.java    From james-project with 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
Source File: XmlBeansMarshaller.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public boolean ready() throws IOException {
	Reader rdr = getReader();
	return (rdr != null && rdr.ready());
}
 
Example 19
Source File: IPCProtocol.java    From Repeat with 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 File: Tokenizer.java    From james-project with 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;
    }
}