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:Main.java

public static int skip(final InputStream is, final int n, final int bufferSize) throws IOException {
    int rd = 0;/*from www  .  j  a va2  s .co m*/
    long ch = 0;
    while (rd < n && ch >= 0) {
        final long cn = (n - rd > bufferSize) ? bufferSize : (n - rd);
        ch = is.skip(cn);

        if (ch > 0) {
            rd += ch;
        }
    }
    return rd;
}

From source file:org.apache.solr.common.util.Utils.java

/**
 * Make sure the InputStream is fully read.
 * //from w ww.jav a2  s  .c  o m
 * @param is to read
 * @throws IOException on problem with IO
 */
private static void readFully(InputStream is) throws IOException {
    is.skip(is.available());
    while (is.read() != -1) {
    }
}

From source file:io.milton.common.RangeUtils.java

public static void writeRanges(InputStream in, List<Range> ranges, OutputStream responseOut)
        throws IOException {
    try {//from   w w w. j  av a 2s . co m
        InputStream bufIn = in; //new BufferedInputStream(in);
        long pos = 0;
        for (Range r : ranges) {
            long skip = r.getStart() - pos;
            bufIn.skip(skip);
            Long length = r.getLength();
            if (length == null) { // will return null if cant calculate
                throw new IOException(
                        "Unable to write range because either start or finish index are not provided: " + r);
            }
            sendBytes(bufIn, responseOut, length);
            pos = r.getFinish();
        }
    } finally {
        StreamUtils.close(in);
    }
}

From source file:Main.java

public static byte[] readBytes(InputStream in, long skip, long size) throws IOException {
    ByteArrayOutputStream out = null;
    try {//from  w w  w  . j av a2 s  .c  o m
        if (skip > 0) {
            long skipSize = 0;
            while (skip > 0 && (skipSize = in.skip(skip)) > 0) {
                skip -= skipSize;
            }
        }
        out = new ByteArrayOutputStream();
        for (int i = 0; i < size; i++) {
            out.write(in.read());
        }
    } finally {
        closeQuietly(out);
    }
    return out.toByteArray();
}

From source file:org.apache.jxtadoop.io.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 www . ja va2 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 {
    while (len > 0) {
        long ret = in.skip(len);
        if (ret < 0) {
            throw new IOException("Premeture EOF from inputStream");
        }
        len -= ret;
    }
}

From source file:io.milton.common.RangeUtils.java

public static void writeRange(InputStream in, Range r, OutputStream responseOut) throws IOException {
    if (r != null) {
        if (r.getStart() != null) {
            long skip = r.getStart();
            in.skip(skip);
        }/*w ww  . j  a v a 2 s .  c  o m*/
        if (r.getFinish() != null) {
            long length = r.getFinish() - r.getStart() + 1;
            sendBytes(in, responseOut, length);
        } else {
            IOUtils.copy(in, responseOut);
        }
    } else {
        IOUtils.copy(in, responseOut);
    }
}

From source file:org.apache.sling.testing.clients.util.InputStreamBodyWithLength.java

/**
 * Returns the length of a resource (which is needed for the InputStreamBody
 * to work. Can't currently think of a better solution than going through
 * the resource stream and count./*ww  w  .ja v a2  s .c  om*/
 *
 * @param resourcePath path to the file
 * @return the size of the resource
 */
private static long getResourceStreamLength(String resourcePath) throws ClientException {
    int streamLength = 0;
    InputStream stream = ResourceUtil.getResourceAsStream(resourcePath);
    try {
        for (int avail = stream.available(); avail > 0; avail = stream.available()) {
            streamLength += avail;
            stream.skip(avail);
        }
    } catch (IOException e) {
        throw new ClientException("Could not read " + resourcePath + "!", e);
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            throw new ClientException("Could not close Inputstream for " + resourcePath + "!", e);
        }
    }
    return streamLength;
}

From source file:eu.stratosphere.nephele.util.IOUtils.java

/**
 * Similar to readFully(). Skips bytes in a loop.
 * //w  ww .j av a 2s  . com
 * @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(final InputStream in, long len) throws IOException {
    while (len > 0) {
        final long ret = in.skip(len);
        if (ret < 0) {
            throw new IOException("Premeture EOF from inputStream");
        }
        len -= ret;
    }
}

From source file:org.andstatus.app.util.FileUtils.java

/** Reads up to 'size' bytes, starting from 'offset' */
public static byte[] getBytes(File file, int offset, int size) throws IOException {
    if (file != null) {
        InputStream is = new FileInputStream(file);
        byte[] readBuffer = new byte[size];
        try {//ww  w. j  a va2 s . c o  m
            long bytesSkipped = is.skip(offset);
            if (bytesSkipped < offset) {
                throw new FileNotFoundException("Skiiped only " + bytesSkipped + " of " + offset
                        + " bytes in file='" + file.getAbsolutePath() + "'");
            }
            int bytesRead = is.read(readBuffer, 0, size);
            if (bytesRead == readBuffer.length) {
                return readBuffer;
            } else if (bytesRead > 0) {
                return Arrays.copyOf(readBuffer, bytesRead);
            }
        } finally {
            DbUtils.closeSilently(is);
        }
    }
    return new byte[0];
}

From source file:org.trellisldp.file.FileUtils.java

/**
 * Get a bounded inputstream.//from   w  w w  .j  a  v  a2  s . co m
 * @param stream the input stream
 * @param from the byte from which to start
 * @param to the byte to which to read
 * @throws IOException if an error occurs when skipping forward
 * @return the bounded inputstream
 */
public static InputStream getBoundedStream(final InputStream stream, final int from, final int to)
        throws IOException {
    final long skipped = stream.skip(from);
    LOGGER.debug("Skipped {} bytes", skipped);
    return new BoundedInputStream(stream, to - from);
}