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:io.uploader.drive.drive.largefile.GDriveUpload.java

private String uploadFile(DriveResumableUpload upload, BasicFileAttributes attr) throws IOException {

    long currentBytePosition = upload.getCurrentByte();
    File file = new File(filename);
    if (currentBytePosition > -1 && currentBytePosition < attr.size()) {
        byte[] chunk;
        int retries = 0;
        while (retries < 5) {
            InputStream stream = io.uploader.drive.util.FileUtils
                    .getInputStreamWithProgressFilter(progressCallback, attr.size(), new FileInputStream(file));
            if (currentBytePosition > 0) {
                stream.skip(currentBytePosition);
            }// w w w . j a  va2s .c  om
            chunk = new byte[chunkSize];
            int bytes_read = stream.read(chunk, 0, chunkSize);
            stream.close();
            if (bytes_read > 0) {
                int status = upload.uploadChunk(chunk, currentBytePosition, bytes_read);
                if (status == 308) {
                    // If Status is 308 RESUME INCOMPLETE there's no retry done.
                    retries = 0;
                } else if (status >= 500 && status < 600) {
                    // Good practice: Exponential backoff
                    try {
                        long seconds = Math.round(Math.pow(2, retries + 1));
                        logger.info("Exponential backoff. Waiting " + seconds + " seconds.");
                        Thread.sleep(seconds * 1000);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                } else if (status == 401) {
                    logger.info("Tokan has experied, need to be refreshed...");
                    upload.updateAccessToken();
                } else if (status == 200 || status == 201) {

                    boolean success = upload.checkMD5(md5);
                    logger.info("local md5sum: " + md5);
                    logger.info("File upload complete.");
                    if (!success) {
                        throw new TransferException(false, "The md5 values do not macth");
                    }
                    break;
                } else if (status == 404) {
                    // this can be due to a remaining temporary file with an out-dated link
                    // we throw that exception with no recovery option (not resumable) in order to 
                    // delete this file (if any)
                    throw new TransferException(false, "The file cannot be found");
                } else {
                    logger.info("Status: " + String.valueOf(status));
                }
            }
            ++retries;
            currentBytePosition = upload.getCurrentByte();
        }
    } else if (currentBytePosition == attr.size()) {
        boolean success = upload.checkMD5(md5);
        logger.info("local md5sum: " + md5);
        logger.info("File upload complete.");

        if (!success) {
            throw new IOException("The md5 values do not macth");
        }
    } else {
        // Some BUG occured. lastbyte = -1.
        throw new TransferException(false, "Some anomalies have been observed");
    }
    // get file id
    return upload.getFileId();
}

From source file:com.wegas.core.jcr.content.ContentConnector.java

protected InputStream getData(String absolutePath, long from, int len) throws RepositoryException, IOException {
    InputStream data = this.getData(absolutePath);
    byte[] bytes = new byte[len];
    data.skip(from);
    data.read(bytes, 0, len);/*w  w w.j ava 2  s.  com*/

    return new ByteArrayInputStream(bytes);
}

From source file:gov.nasa.ensemble.core.jscience.csvxml.ProfileLoader.java

private void loadData() throws ProfileLoadingException {
    if (sourceStream != null) {
        loadDataFromThisPointInStream(sourceStream);
        dataLoaded = true;/*w ww  . ja  v  a2 s .c  om*/
    } else {
        try {
            ExtensibleURIConverterImpl converter = new ExtensibleURIConverterImpl();
            InputStream stream = converter.createInputStream(sourceURI);
            stream.skip(nCharactersPrecedingData);
            loadDataFromThisPointInStream(stream);
            dataLoaded = true;
        } catch (IOException e) {
            throw new ProfileLoadingException(
                    "Error opening " + sourceURI + " to position " + nCharactersPrecedingData + ": " + e);
        }
    }
}

From source file:ch.cyberduck.core.dav.DAVPath.java

@Override
public void upload(final BandwidthThrottle throttle, final StreamListener listener,
        final TransferStatus status) {
    try {//from w  w w  .jav  a2  s.  c om
        InputStream in = null;
        ResponseOutputStream<Void> out = null;
        try {
            in = this.getLocal().getInputStream();
            if (status.isResume()) {
                long skipped = in.skip(status.getCurrent());
                log.info(String.format("Skipping %d bytes", skipped));
                if (skipped < status.getCurrent()) {
                    throw new IOResumeException(
                            String.format("Skipped %d bytes instead of %d", skipped, status.getCurrent()));
                }
            }
            out = this.write(status);
            this.upload(out, in, throttle, listener, status);
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
        if (null != out) {
            out.getResponse();
        }
    } catch (IOException e) {
        this.error("Upload failed", e);
    }
}

From source file:com.alibaba.otter.shared.common.utils.NioUtils.java

/**
 * ??copy/*from www.  j ava 2  s . c o  m*/
 */
public static long copy(InputStream input, OutputStream output, long offset, long count) throws IOException {
    long rcount = 0;
    long n = 0;
    if (input instanceof FileInputStream) {
        FileChannel inChannel = ((FileInputStream) input).getChannel();
        WritableByteChannel outChannel = Channels.newChannel(output);
        rcount = inChannel.transferTo(offset, count, outChannel);
    } else if (output instanceof FileOutputStream) {
        FileChannel outChannel = ((FileOutputStream) output).getChannel();
        ReadableByteChannel inChannel = Channels.newChannel(input);
        do {
            if (count < DEFAULT_BUFFER_SIZE) {
                n = outChannel.transferFrom(inChannel, offset + rcount, count);
            } else {
                n = outChannel.transferFrom(inChannel, offset + rcount, DEFAULT_BUFFER_SIZE);
            }
            count -= n;
            rcount += n;
        } while (n > 0);
    } else {
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

        input.skip(offset);
        while (count > 0) {
            if (count < DEFAULT_BUFFER_SIZE) {
                n = input.read(buffer, 0, (int) count);
            } else {
                n = input.read(buffer, 0, DEFAULT_BUFFER_SIZE);
            }

            output.write(buffer, 0, (int) n);
            count -= n;
            rcount += n;
        }
        // ReadableByteChannel inChannel = Channels.newChannel(input);
        // WritableByteChannel outChannel = Channels.newChannel(output);
        //            
        // //ByteBuffer buffer = new ByteBuffer(DEFAULT_BUFFER_SIZE);
        // ByteBuffer buffer = ByteBuffer.allocateDirect(DEFAULT_BUFFER_SIZE);
        // while (-1 != (n = inChannel.read(buffer))) {
        // outChannel.write(buffer);
        // count += n;
        // }
    }
    return rcount;
}

From source file:org.apache.jackrabbit.spi.commons.value.AbstractQValue.java

/**
 * This implementation creates a binary instance that uses
 * {@link #getStream()} and skipping on the given stream as its underlying
 * mechanism to provide random access defined on {@link Binary}.
 *
 * @see QValue#getBinary()/* w  ww.j a  va2  s.  c o m*/
 */
public Binary getBinary() throws RepositoryException {
    return new Binary() {
        public InputStream getStream() throws RepositoryException {
            return AbstractQValue.this.getStream();
        }

        public int read(byte[] b, long position) throws IOException, RepositoryException {
            InputStream in = getStream();
            try {
                long skip = position;
                while (skip > 0) {
                    long skipped = in.skip(skip);
                    if (skipped <= 0) {
                        return -1;
                    }
                    skip -= skipped;
                }
                return in.read(b);
            } finally {
                in.close();
            }
        }

        public long getSize() throws RepositoryException {
            return getLength();
        }

        public void dispose() {
        }
    };
}

From source file:mitm.common.util.SizeLimitedInputStreamTest.java

@Test
public void testLimitNoException() throws IOException, CertificateException, NoSuchProviderException {
    InputStream input = new FileInputStream(new File(testBase, "certificates/random-self-signed-1000.p7b"));

    final int maxBytes = 1000;

    InputStream limitedInput = new SizeLimitedInputStream(input, maxBytes, false);

    byte[] data = IOUtils.toByteArray(limitedInput);

    /*/*  w ww .  j  a v  a2s .co m*/
     * Actual read is 4096 because the buffer size is 4096
     */
    assertEquals(4096, data.length);

    assertEquals(0, limitedInput.available());
    assertEquals(-1, limitedInput.read());
    assertEquals(-1, limitedInput.read(data));
    assertEquals(-1, limitedInput.read(data, 0, 100));
    assertEquals(0, limitedInput.skip(10));
}

From source file:ddf.catalog.resource.impl.URLResourceReader.java

private void skipBytes(InputStream is, String bytesToSkip) throws IOException {
    if (bytesToSkip != null) {
        LOGGER.debug("Skipping {} bytes", bytesToSkip);
        long bytesSkipped = is.skip(Long.parseLong(bytesToSkip));
        if (Long.parseLong(bytesToSkip) != bytesSkipped) {
            LOGGER.debug("Did not skip specified bytes while retrieving resource."
                    + " Bytes to skip: {} -- Skipped Bytes: {}", bytesToSkip, bytesSkipped);
        }//from  w  w  w.  j  a  v a 2  s  .c o m
    }
}

From source file:org.opendatakit.common.android.utilities.WebUtils.java

/**
 * Utility to ensure that the entity stream of a response is drained of bytes.
 *
 * @param response/*  w  w w.j  a v  a  2s.c  om*/
 */
public void discardEntityBytes(HttpResponse response) {
    // may be a server that does not handle
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        try {
            // have to read the stream in order to reuse the connection
            InputStream is = response.getEntity().getContent();
            // read to end of stream...
            final long count = 1024L;
            while (is.skip(count) == count)
                ;
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}