Example usage for java.io InputStream skip

List of usage examples for java.io InputStream skip

Introduction

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

Prototype

public long skip(long n) throws IOException 

Source Link

Document

Skips over and discards n bytes of data from this input stream.

Usage

From source file:org.eclipse.skalli.core.storage.Historian.java

private static void skip(InputStream in, long n) throws IOException {
    while (n > 0) {
        n -= in.skip(n);
    }/*from ww  w. j  a  v a  2  s . co  m*/
}

From source file:com.ery.ertc.estorm.util.IOUtils.java

/**
 * Similar to readFully(). Skips bytes in a loop.
 * /*w  w  w .j a va2 s.co  m*/
 * @param in
 *            The InputStream to skip bytes from
 * @param len
 *            number of bytes to skip.
 * @throws IOException
 *             if it could not skip requested number of bytes for any reason
 *             (including EOF)
 */
public static void skipFully(InputStream in, long len) throws IOException {
    while (len > 0) {
        long ret = in.skip(len);
        if (ret < 0) {
            throw new IOException("Premature EOF from inputStream");
        }
        len -= ret;
    }
}

From source file:org.apache.hadoop.util.IOUtils.java

/**
 * Similar to readFully(). Skips bytes in a loop.
 * @param in The InputStream to skip bytes from
 * @param len number of bytes to skip./*from w  w  w  .  java 2  s. c o  m*/
 * @throws IOException if it could not skip requested number of bytes 
 * for any reason (including EOF)
 */
public static void skipFully(InputStream in, long len) throws IOException {
    long amt = len;
    while (amt > 0) {
        long ret = in.skip(amt);
        if (ret == 0) {
            // skip may return 0 even if we're not at EOF.  Luckily, we can 
            // use the read() method to figure out if we're at the end.
            int b = in.read();
            if (b == -1) {
                throw new EOFException(
                        "Premature EOF from inputStream after " + "skipping " + (len - amt) + " byte(s).");
            }
            ret = 1;
        }
        amt -= ret;
    }
}

From source file:com.redhat.rhn.common.util.FileUtils.java

/**
 * Read a file off disk into a byte array with specified range
 *
 * This can use lots of memory if you read a large file
 *
 * @param fileToRead File to read part of into byte array
 * @param start index of read/*from w  w  w. j  a va  2 s  .c o m*/
 * @param end index of read
 * @return byte[] array from file.
 */
public static byte[] readByteArrayFromFile(File fileToRead, long start, long end) {
    log.debug("readByteArrayFromFile: " + fileToRead.getAbsolutePath() + " start: " + start + " end: " + end);

    int size = (int) (end - start);
    log.debug("size of array: " + size);
    // Create the byte array to hold the data
    byte[] bytes = new byte[size];
    InputStream is = null;
    try {
        is = new FileInputStream(fileToRead);
        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        // Skip ahead
        is.skip(start);
        // start reading
        while (offset < bytes.length && (numRead) >= 0) {
            numRead = is.read(bytes, offset, bytes.length - offset);
            offset += numRead;
        }
    } catch (FileNotFoundException fnf) {
        log.error("Could not read from: " + fileToRead.getAbsolutePath());
        throw new RuntimeException(fnf);
    } catch (IOException e) {
        log.error("Could not read from: " + fileToRead.getAbsolutePath());
        throw new RuntimeException(e);

    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    return bytes;
}

From source file:in.goahead.apps.util.URLUtils.java

public static void AppendStream(InputStream inputStream, String outputFileName, long skip) throws IOException {
    long actualSkip = 0;
    Logger.debug("Skip length: " + skip + " Actual skip: " + actualSkip);
    actualSkip = inputStream.skip(skip);
    Logger.debug("Skip length: " + skip + " Actual skip: " + actualSkip);
    if (actualSkip == skip) {
        Logger.debug("Skip length: " + skip + " Actual skip: " + actualSkip);
        OutputStream os = new FileOutputStream(outputFileName, true);
        DownloadStream(inputStream, os);
        os.flush();/*from  www. j a va2  s.  c o m*/
        os.close();
    } else {
        Logger.debug("Skip length: " + skip + " Actual skip: " + actualSkip);
    }

}

From source file:org.jaxygen.invoker.FileDeliveryHandler.java

private static long sendPart(long pos, HttpRangeUtil.Range range, InputStream is, OutputStream os)
        throws IOException {
    long initPos = pos;
    if (range.getStart() > pos) {
        pos = range.getStart();/* w ww .  jav  a2 s.co m*/
    }
    long toSkip = pos - initPos;
    long toSend = range.getEnd() - pos;
    int b;
    is.skip((int) toSkip);
    // TODO: performance improovement needed.
    for (int i = 0; i <= toSend; i++) {
        b = is.read();
        if (b > -1) {
            os.write(b);
        }
        pos++;
    }
    return pos;
}

From source file:com.buaa.cfs.utils.IOUtils.java

/**
 * Similar to readFully(). Skips bytes in a loop.
 *
 * @param in  The InputStream to skip bytes from
 * @param len number of bytes to skip.//from  w w  w  .j  ava 2s  . co  m
 *
 * @throws IOException if it could not skip requested number of bytes for any reason (including EOF)
 */
public static void skipFully(InputStream in, long len) throws IOException {
    long amt = len;
    while (amt > 0) {
        long ret = in.skip(amt);
        if (ret == 0) {
            // skip may return 0 even if we're not at EOF.  Luckily, we can
            // use the read() method to figure out if we're at the end.
            int b = in.read();
            if (b == -1) {
                throw new EOFException(
                        "Premature EOF from inputStream after " + "skipping " + (len - amt) + " byte(s).");
            }
            ret = 1;
        }
        amt -= ret;
    }
}

From source file:UTF8Util.java

/**
 * Tries harder to skip the requested number of bytes.
 * <p>//from w w  w .ja  va2s .c  o  m
 * Note that even if the method fails to skip the requested number of bytes,
 * it will not throw an exception. If this happens, the caller can be sure
 * that end-of-stream has been reached.
 *
 * @param in byte stream
 * @param bytesToSkip the number of bytes to skip
 * @return The number of bytes skipped.
 * @throws IOException if reading from the stream fails
 */
public static final long skipPersistent(InputStream in, long bytesToSkip) throws IOException {
    long skipped = 0;
    while (skipped < bytesToSkip) {
        long skippedNow = in.skip(bytesToSkip - skipped);
        if (skippedNow == 0) {
            if (in.read() == -1) {
                // EOF, return what we have and leave it up to caller to
                // decide what to do about it.
                break;
            } else {
                skippedNow = 1; // Added to count below.
            }
        }
        skipped += skippedNow;
    }
    return skipped;
}

From source file:org.nuxeo.ecm.platform.ui.web.download.BlobDownloadServlet.java

public static void writeStream(InputStream in, OutputStream out, ByteRange range) throws IOException {
    BufferingServletOutputStream.stopBuffering(out);
    byte[] buffer = new byte[BUFFER_SIZE];
    long read;/*from w ww. j ava  2s.  c o m*/
    long offset = range.getStart();
    in.skip(offset);
    while (offset <= range.getEnd() && (read = in.read(buffer)) != -1) {
        read = Math.min(read, range.getEnd() - offset + 1);
        out.write(buffer, 0, (int) read);
        out.flush();
        offset += read;
    }

}

From source file:com.exadel.flamingo.flex.messaging.amf.io.AMF0Deserializer.java

public static Document convertToDOM(InputStream is) throws IOException {
    Document document = null;// w w  w. ja  v  a2  s.c  om
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    is.skip(4); // skip length
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(new InputSource(is));
    } catch (Exception e) {
        log.error(e, e);
        throw new IOException("Error while parsing xml: " + e.getMessage());
    }
    return document;
}