Example usage for java.io Reader ready

List of usage examples for java.io Reader ready

Introduction

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

Prototype

public boolean ready() throws IOException 

Source Link

Document

Tells whether this stream is ready to be read.

Usage

From source file:Main.java

public static void main(String[] args) {

    String s = "tutorial from java2s.com";

    Reader reader = new StringReader(s);

    try {/*from  w  ww  . j  a v a  2  s .  co  m*/
        // check if reader is ready
        System.out.println(reader.ready());

        // read the first five chars
        for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }
        reader.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:com.discursive.jccook.slide.GetExample.java

public static void main(String[] args) throws HttpException, IOException {
    HttpClient client = new HttpClient();

    String url = "http://www.discursive.com/jccook/dav/test.html";
    Credentials credentials = new UsernamePasswordCredentials("davuser", "davpass");

    // List resources in top directory
    WebdavResource resource = new WebdavResource(url, credentials);

    System.out.println("The three ways to Read resources.");

    // Read to a temporary file
    File tempFile = new File(resource.getName());
    resource.getMethod(tempFile);//from w  w  w  .j a  va  2  s  .  c  om
    System.out.println("1. " + resource.getName() + " saved in file: " + tempFile.toString());

    // Read as a String
    String resourceData = resource.getMethodDataAsString();
    System.out.println("2. Contents of " + resource.getName() + " as String.");
    System.out.println(resourceData);

    // Read with a stream
    InputStream resourceStream = resource.getMethodData();
    Reader reader = new InputStreamReader(resourceStream);
    StringWriter writer = new StringWriter();
    while (reader.ready()) {
        writer.write(reader.read());
    }
    System.out.println("3. Contents of " + resource.getName() + " from InputStream.");
    System.out.println(writer.toString());

    resource.close();

}

From source file:Main.java

/**
 * Reads all text up to next XML tag and returns it as a String.
 *
 * @return the String of the text read, which may be empty.
 *///from  w ww  .  j a  v  a2 s . c o m
public static String readUntilTag(Reader r) throws IOException {
    if (!r.ready()) {
        return "";
    }
    StringBuilder b = new StringBuilder();
    int c = r.read();
    while (c >= 0 && c != '<') {
        b.append((char) c);
        c = r.read();
    }
    return b.toString();
}

From source file:Main.java

/**
 * Reads all text of the XML tag and returns it as a String.
 * Assumes that a '<' character has already been read.
 *
 * @param r The reader to read from/*from w  w  w  . j a  va  2  s . c  o  m*/
 * @return The String representing the tag, or null if one couldn't be read
 *         (i.e., EOF).  The returned item is a complete tag including angle
 *         brackets, such as <code>&lt;TXT&gt;</code>
 */
public static String readTag(Reader r) throws IOException {
    if (!r.ready()) {
        return null;
    }
    StringBuilder b = new StringBuilder("<");
    int c = r.read();
    while (c >= 0) {
        b.append((char) c);
        if (c == '>') {
            break;
        }
        c = r.read();
    }
    if (b.length() == 1) {
        return null;
    }
    return b.toString();
}

From source file:com.tussle.main.Utility.java

public static String readAll(Reader reader) throws IOException {
    StringBuilder toReturn = new StringBuilder();
    while (reader.ready()) {
        char[] holder = new char[1024];
        int charsRead = reader.read(holder);
        toReturn.append(holder, 0, charsRead);
    }/*from   ww w .j  a va  2 s .c  om*/
    return toReturn.toString();
}

From source file:au.com.gridstone.rxstore.converters.JacksonConverter.java

@Override
public <T> T read(File file, Type type) throws ConverterException {
    JavaType javaType = objectMapper.getTypeFactory().constructType(type);

    try {//ww w. j  a v  a 2 s  .c o  m
        Reader reader = new FileReader(file);
        T value;

        if (!reader.ready()) {
            value = null;
        } else {
            value = objectMapper.readValue(reader, javaType);
        }

        reader.close();
        return value;
    } catch (Exception e) {
        throw new ConverterException(e);
    }
}

From source file:net.krotscheck.dfr.text.AbstractTextDecoderTest.java

/**
 * Test that the reader is closeable.// w  w w  .  j a v  a 2  s  .  co m
 *
 * @throws Exception Should not throw an exception.
 */
@Test
public void testCloseWithReader() throws Exception {
    ITextDecoder decoder = new TestTextDecoder(testData);

    Assert.assertNull(decoder.getReader());
    Reader reader = new FileReader(ResourceUtil.getFileForResource("test.csv"));
    decoder.setReader(reader);
    Assert.assertEquals(reader, decoder.getReader());

    decoder.close();

    Assert.assertNull(decoder.getReader());
    try {
        Assert.assertFalse(reader.ready());
    } catch (IOException ioe) {
        Assert.assertFalse(false);
    }
}

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

/**
 * Reads a line from the process output.
 * @param r reader.//from  www .  ja  v a 2  s. c  om
 * @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:ConcatReader.java

/**
 * Tell whether this stream is ready to be read.
 *
 * @return True if the next read() is guaranteed not to block for input,
 *    false otherwise. Note that returning false does not guarantee that the next
 *    read will block.//from ww  w  .  j  a  v  a  2s.co  m
 *
 * @throws IOException If an I/O error occurs
 *
 * @since ostermillerutils 1.04.00
 */
@Override
public boolean ready() throws IOException {
    if (closed)
        throw new IOException("Reader closed");
    Reader in = getCurrentReader();
    if (in == null)
        return false;
    return in.ready();
}

From source file:BayesianAnalyzer.java

private String nextToken(Reader reader) throws java.io.IOException {
    StringBuffer token = new StringBuffer();
    int i;/*  w  w w.j  ava 2  s.com*/
    char ch, 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 == '\u20AC' // the EURO symbol
                || 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;
    }
}