Example usage for java.io Reader skip

List of usage examples for java.io Reader skip

Introduction

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

Prototype

public long skip(long n) throws IOException 

Source Link

Document

Skips characters.

Usage

From source file:Main.java

public static void main(String[] args) {
    String s = "tutorial from java2s.com";

    Reader reader = new StringReader(s);

    try {//ww w  .  ja v a  2s.  c  om
        // read the first five chars
        for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();

            // skip a char every time
            reader.skip(1);

            System.out.println(c);
        }
        reader.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:com.urswolfer.gerrit.client.rest.http.GerritRestClient.java

private JsonElement parseResponse(InputStream response) throws IOException {
    Reader reader = new InputStreamReader(response, Consts.UTF_8);
    reader.skip(5);
    try {/* w  w  w .ja va 2 s. c  o m*/
        return new JsonParser().parse(reader);
    } catch (JsonSyntaxException jse) {
        throw new IOException(String.format("Couldn't parse response: %n%s", CharStreams.toString(reader)),
                jse);
    } finally {
        reader.close();
    }
}

From source file:ConcatReader.java

/**
 * Skip characters. This method will block until some characters are
 * available, an I/O error occurs, or the end of the stream is reached.
 * <p>/*from w ww. j  av  a 2  s.c om*/
 * If this class in not done accepting readers and the end of the last known
 * stream is reached, this method will block forever unless another thread
 * adds a reader or interrupts.
 *
 * @param n the number of characters to skip
 * @return The number of characters actually skipped
 *
 * @throws IllegalArgumentException If n is negative.
 * @throws IOException If an I/O error occurs
 *
 * @since ostermillerutils 1.04.00
 */
@Override
public long skip(long n) throws IOException {
    if (closed)
        throw new IOException("Reader closed");
    if (n <= 0)
        return 0;
    long s = -1;
    while (s <= 0) {
        Reader in = getCurrentReader();
        if (in == null) {
            if (doneAddingReaders)
                return 0;
            try {
                Thread.sleep(100);
            } catch (InterruptedException iox) {
                throw new IOException("Interrupted");
            }
        } else {
            s = in.skip(n);
            // When nothing was skipped it is a bit of a puzzle.
            // The most common cause is that the end of the underlying
            // stream was reached.  In which case calling skip on it
            // will always return zero.  If somebody were calling skip
            // until it skipped everything they needed, there would
            // be an infinite loop if we were to return zero here.
            // If we get zero, let us try to read one character so
            // we can see if we are at the end of the stream.  If so,
            // we will move to the next.
            if (s <= 0) {
                // read() will advance to the next stream for us, so don't do it again
                s = ((read() == -1) ? -1 : 1);
            }
        }

    }
    return s;
}

From source file:org.apache.catalina.servlets.DefaultServlet.java

/**
 * Copy the contents of the specified input stream to the specified
 * output stream, and ensure that both streams are closed before returning
 * (even in the face of an exception).//www .  j  a va 2s.c  om
 *
 * @param reader The reader to read from
 * @param writer The writer to write to
 * @param start  Start of the range which will be copied
 * @param end    End of the range which will be copied
 * @return Exception which occurred during processing
 */
private IOException copyRange(Reader reader, PrintWriter writer, long start, long end) {

    try {
        reader.skip(start);
    } catch (IOException e) {
        return e;
    }

    IOException exception = null;
    long bytesToRead = end - start + 1;

    char buffer[] = new char[input];
    int len = buffer.length;
    while ((bytesToRead > 0) && (len >= buffer.length)) {
        try {
            len = reader.read(buffer);
            if (bytesToRead >= len) {
                writer.write(buffer, 0, len);
                bytesToRead -= len;
            } else {
                writer.write(buffer, 0, (int) bytesToRead);
                bytesToRead = 0;
            }
        } catch (IOException e) {
            exception = e;
            len = -1;
        }
        if (len < buffer.length) {
            break;
        }
    }

    return exception;
}

From source file:org.gss_project.gss.server.rest.Webdav.java

/**
 * Copy the contents of the specified input stream to the specified output
 * stream, and ensure that both streams are closed before returning (even in
 * the face of an exception).//from w ww . j  a  v  a 2 s .com
 *
 * @param reader The reader to read from
 * @param writer The writer to write to
 * @param start Start of the range which will be copied
 * @param end End of the range which will be copied
 * @return Exception which occurred during processing
 */
private IOException copyRange(Reader reader, PrintWriter writer, long start, long end) {
    try {
        reader.skip(start);
    } catch (IOException e) {
        return e;
    }
    IOException exception = null;
    long bytesToRead = end - start + 1;
    char buffer[] = new char[input];
    int len = buffer.length;
    while (bytesToRead > 0 && len >= buffer.length) {
        try {
            len = reader.read(buffer);
            if (bytesToRead >= len) {
                writer.write(buffer, 0, len);
                bytesToRead -= len;
            } else {
                writer.write(buffer, 0, (int) bytesToRead);
                bytesToRead = 0;
            }
        } catch (IOException e) {
            exception = e;
            len = -1;
        }
        if (len < buffer.length)
            break;
    }
    return exception;
}

From source file:org.opencms.webdav.CmsWebdavServlet.java

/**
 * Copy the contents of the specified input stream to the specified
 * output stream, and ensure that both streams are closed before returning
 * (even in the face of an exception).<p>
 *
 * @param reader the reader to read from
 * @param writer the writer to write to//from   www.j a v a 2s .  c  o  m
 * @param start the start of the range which will be copied
 * @param end the end of the range which will be copied
 * 
 * @return the exception which occurred during processing
 */
protected IOException copyRange(Reader reader, PrintWriter writer, long start, long end) {

    try {
        reader.skip(start);
    } catch (IOException e) {
        return e;
    }

    IOException exception = null;
    long bytesToRead = (end - start) + 1;

    char[] buffer = new char[m_input];
    int len = buffer.length;
    while ((bytesToRead > 0) && (len >= buffer.length)) {
        try {
            len = reader.read(buffer);
            if (bytesToRead >= len) {
                writer.write(buffer, 0, len);
                bytesToRead -= len;
            } else {
                writer.write(buffer, 0, (int) bytesToRead);
                bytesToRead = 0;
            }
        } catch (IOException e) {
            exception = e;
            len = -1;
        }

        if (len < buffer.length) {
            break;
        }
    }

    return exception;
}

From source file:org.openhab.binding.samsungtv.internal.protocol.RemoteControllerLegacy.java

private void readInitialInfo(Reader reader) throws RemoteControllerException {
    try {// www . j  a va  2  s.co  m
        /* @formatter:off
        *
        * offset value and description
        * ------ ---------------------
        * 0x00   don't know, it it always 0x00 or 0x02
        * 0x01   0x000c - string length (little endian)
        * 0x03   "iapp.samsung" - string content
        * 0x0f   0x0006 - payload size (little endian)
        * 0x11   payload
        *
        * @formatter:on
        */

        reader.skip(1);
        readString(reader);
        char[] result = readCharArray(reader);

        if (Arrays.equals(result, ACCESS_GRANTED_RESP)) {
            logger.debug("Access granted");
        } else if (Arrays.equals(result, ACCESS_DENIED_RESP)) {
            throw new RemoteControllerException("Access denied");
        } else if (Arrays.equals(result, ACCESS_TIMEOUT_RESP)) {
            throw new RemoteControllerException("Registration timed out");
        } else if (Arrays.equals(result, WAITING_USER_GRANT_RESP)) {
            throw new RemoteControllerException("Waiting for user to grant access");
        } else {
            throw new RemoteControllerException("Unknown response received for access query");
        }
    } catch (IOException e) {
        throw new RemoteControllerException(e);
    }
}

From source file:org.openhab.binding.samsungtv.internal.protocol.RemoteControllerLegacy.java

private void sendKeyData(KeyCode key) throws RemoteControllerException {
    logger.debug("Sending key code {}", key.getValue());

    Writer localwriter = writer;//from   w w w .j a  va  2 s .  c  om
    Reader localreader = reader;
    if (localwriter == null || localreader == null) {
        return;
    }
    /* @formatter:off
     *
     * offset value and description
     * ------ ---------------------
     * 0x00   always 0x00
     * 0x01   0x0013 - string length (little endian)
     * 0x03   "iphone.iapp.samsung" - string content
     * 0x16   0x0011 - payload size (little endian)
     * 0x18   payload
     *
     * @formatter:on
     */
    try {
        localwriter.append((char) 0x00);
        writeString(localwriter, APP_STRING);
        writeString(localwriter, createKeyDataPayload(key));
        localwriter.flush();

        /*
         * Read response. Response is pretty useless, because TV seems to
         * send same response in both ok and error situation.
         */
        localreader.skip(1);
        readString(localreader);
        readCharArray(localreader);
    } catch (IOException e) {
        throw new RemoteControllerException(e);
    }
}

From source file:org.rhq.core.pluginapi.event.log.LogFileEventPoller.java

private Set<Event> processNewLines(FileInfo fileInfo) {
    Set<Event> events = null;
    Reader reader = null;
    try {/*from   w ww .j a va 2 s .co m*/
        reader = new FileReader(this.logFile);

        long offset = getOffset(fileInfo);

        if (offset > 0) {
            reader.skip(offset);
        }
        BufferedReader bufferedReader = new BufferedReader(reader);
        events = this.entryProcessor.processLines(bufferedReader);
    } catch (IOException e) {
        log.error("Failed to read log file being tailed: " + this.logFile, e);
    } finally {
        if (reader != null) {
            //noinspection EmptyCatchBlock
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }
    return events;
}