下面列出了java.io.Reader#ready ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
/**
* 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);
}
}
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);
}
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);
}
@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();
}
/** 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);
}
}
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;
}
}
@Override
public boolean ready() throws IOException {
Reader rdr = getReader();
return (rdr != null && rdr.ready());
}
/**
* 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;
}
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;
}
}