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.apache.padaf.preflight.helpers.StreamValidationHelper.java

protected void checkStreamLength(DocumentHandler handler, COSObject cObj, List<ValidationError> result)
        throws ValidationException {
    COSStream streamObj = (COSStream) cObj.getObject();
    int length = streamObj.getInt(COSName.getPDFName(STREAM_DICTIONARY_KEY_LENGHT));
    InputStream ra = null;
    try {// w  ww. j a  va  2 s .co  m
        ra = handler.getSource().getInputStream();
        Long offset = handler.getDocument().getDocument().getXrefTable().get(new COSObjectKey(cObj));

        // ---- go to the beginning of the object
        long skipped = 0;
        if (offset != null) {
            while (skipped != offset) {
                long curSkip = ra.skip(offset - skipped);
                if (curSkip < 0) {
                    throw new ValidationException("Unable to skip bytes in the PDFFile to check stream length");
                }
                skipped += curSkip;
            }

            // ---- go to the stream key word
            if (readUntilStream(ra)) {
                int c = ra.read();
                if (c == '\r') {
                    ra.read();
                } // else c is '\n' no more character to read

                // ---- Here is the true beginning of the Stream Content.
                // ---- Read the given length of bytes and check the 10 next bytes
                // ---- to see if there are endstream.
                byte[] buffer = new byte[1024];
                int nbBytesToRead = length;

                do {
                    int cr = 0;
                    if (nbBytesToRead > 1024) {
                        cr = ra.read(buffer, 0, 1024);
                    } else {
                        cr = ra.read(buffer, 0, nbBytesToRead);
                    }
                    if (cr == -1) {
                        result.add(new ValidationResult.ValidationError(
                                ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
                                "Stream length is invalide"));
                        return;
                    } else {
                        nbBytesToRead = nbBytesToRead - cr;
                    }
                } while (nbBytesToRead > 0);

                int len = "endstream".length() + 2;
                byte[] buffer2 = new byte[len];
                for (int i = 0; i < len; ++i) {
                    buffer2[i] = (byte) ra.read();
                }

                // ---- check the content of 10 last characters
                String endStream = new String(buffer2);
                if (buffer2[0] == '\r' && buffer2[1] == '\n') {
                    if (!endStream.contains("endstream")) {
                        result.add(new ValidationResult.ValidationError(
                                ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
                                "Stream length is invalide"));
                    }
                } else if (buffer2[0] == '\r' && buffer2[1] == 'e') {
                    if (!endStream.contains("endstream")) {
                        result.add(new ValidationResult.ValidationError(
                                ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
                                "Stream length is invalide"));
                    }
                } else if (buffer2[0] == '\n' && buffer2[1] == 'e') {
                    if (!endStream.contains("endstream")) {
                        result.add(new ValidationResult.ValidationError(
                                ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
                                "Stream length is invalide"));
                    }
                } else {
                    result.add(new ValidationResult.ValidationError(
                            ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
                            "Stream length is invalide"));
                }

            } else {
                result.add(new ValidationResult.ValidationError(
                        ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide"));
            }
        } else {
            /*
             * 
             * Offset is null. The stream isn't used, check is useless.
             * 
             * TODO : Is it the truth? 
             */
        }
    } catch (IOException e) {
        throw new ValidationException("Unable to read a stream to validate it due to : " + e.getMessage(), e);
    } finally {
        if (ra != null) {
            IOUtils.closeQuietly(ra);
        }
    }
}

From source file:com.github.horrorho.inflatabledonkey.chunk.engine.ChunkListDecrypter.java

Optional<InputStream> positionedStream(InputStream inputStream, int position, int chunkOffset,
        int chunkLength) {
    // Align stream offset with chunk offset, although we cannot back track nor should we need to.
    try {/*from   ww w . ja  v  a  2s. c  o m*/
        if (chunkOffset < position) {
            logger.warn("-- positionedStream() - bad stream position: {} chunk offset: {}", position,
                    chunkOffset);
            return Optional.empty();
        }
        if (chunkOffset > position) {
            int bytes = chunkOffset - position;
            logger.debug("-- positionedStream() - skipping: {}", bytes);
            inputStream.skip(bytes);
        }
        return Optional.of(inputStream);

    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
}

From source file:ConcatInputStream.java

/**
 * Skips over and discards n bytes of data from this input stream. The skip method
 * may, for a variety of reasons, end up skipping over some smaller number of bytes,
 * possibly 0. This may result from any of a number of conditions; reaching end of
 * file before n bytes have been skipped is only one possibility. The actual number
 * of bytes skipped is returned. If n is negative, no bytes are skipped.
 * <p>//from  www . j ava2  s.  c  om
 * If this class in not done accepting inputstreams and the end of the last known
 * stream is reached, this method will block forever unless another thread
 * adds an inputstream or interrupts.
 *
 * @param n he number of characters to skip
 * @return The number of characters actually skipped
 *
 * @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("InputStream closed");
    if (n <= 0)
        return 0;
    long s = -1;
    while (s <= 0) {
        InputStream in = getCurrentInputStream();
        if (in == null) {
            if (doneAddingInputStreams)
                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:com.aliyun.fs.oss.utils.OSSClientAgent.java

@SuppressWarnings("unchecked")
private UploadPartResult uploadPart(String uploadId, String bucket, String key, Long partSize, Long beginIndex,
        int partNumber, File file, Configuration conf, boolean retry)
        throws IOException, ServiceException, ClientException {
    InputStream instream = null;
    try {//from ww  w  .  j a  v a 2s  .c o m
        instream = new FileInputStream(file);
        instream.skip(beginIndex);

        Class UploadPartRequestClz = ResourceLoader.getInstance().getUrlClassLoader(conf)
                .loadClass("com.aliyun.oss.model.UploadPartRequest");
        Constructor cons = UploadPartRequestClz.getConstructor();
        Object uploadPartRequest = cons.newInstance();
        Method method0 = UploadPartRequestClz.getMethod("setBucketName", String.class);
        method0.invoke(uploadPartRequest, bucket);
        Method method1 = UploadPartRequestClz.getMethod("setKey", String.class);
        method1.invoke(uploadPartRequest, key);
        Method method2 = UploadPartRequestClz.getMethod("setUploadId", String.class);
        method2.invoke(uploadPartRequest, uploadId);
        Method method3 = UploadPartRequestClz.getMethod("setInputStream", InputStream.class);
        method3.invoke(uploadPartRequest, instream);
        Method method4 = UploadPartRequestClz.getMethod("setPartSize", Long.TYPE);
        method4.invoke(uploadPartRequest, partSize);
        Method method5 = UploadPartRequestClz.getMethod("setPartNumber", Integer.TYPE);
        method5.invoke(uploadPartRequest, partNumber);

        Method method = this.ossClientClz.getMethod("uploadPart", UploadPartRequestClz);
        Object ret = method.invoke(this.ossClient, uploadPartRequest);
        return gson.fromJson(gson.toJson(ret), UploadPartResult.class);
    } catch (Exception e) {
        if (retry && updateOSSClient(e)) {
            return uploadPart(uploadId, bucket, key, partSize, beginIndex, partNumber, file, conf, false);
        } else {
            handleException(e);
            return null;
        }
    } finally {
        if (instream != null) {
            try {
                instream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.wyona.yanel.impl.resources.node.NodeResourceV101.java

/**
 * @see org.wyona.yanel.core.api.attributes.ViewableV2#getView(String)
 *///from www .jav a  2s  .co  m
public View getView(String viewId) throws Exception {
    if (!exists()) {
        throw new ResourceNotFoundException("No such repository node: " + getRepoPath());
    }

    View view = new View();
    view.setMimeType(getMimeType());
    view.setEncoding(getResourceConfigProperty("encoding"));

    String range = getEnvironment().getRequest().getHeader("Range");
    if (range != null) { // INFO: Also see http://stackoverflow.com/questions/12768812/video-streaming-to-ipad-does-not-work-with-tapestry5, http://balusc.blogspot.ch/2009/02/fileservlet-supporting-resume-and.html
        if (!range.equals("bytes=0-")) {
            log.warn("DEBUG: Specific range requested for node '" + getRepoPath() + "': " + range);
            String[] ranges = range.split("=")[1].split("-");
            int from = Integer.parseInt(ranges[0]);
            int to = Integer.parseInt(ranges[1]);
            int len = to - from + 1;
            view.setResponse(false); // INFO: In this case we write directly into the response...
            HttpServletResponse response = getEnvironment().getResponse();
            response.setStatus(206);
            response.setHeader("Accept-Ranges", "bytes");
            String responseRange = String.format("bytes %d-%d/%d", from, to, getSize());
            response.setHeader("Connection", "close");
            response.setHeader("Content-Range", responseRange);
            log.debug("Content-Range:" + responseRange);
            response.setDateHeader("Last-Modified", new Date().getTime());
            response.setContentLength(len);
            log.debug("Content length: " + len);

            OutputStream os = response.getOutputStream();
            InputStream is = new java.io.BufferedInputStream(getNode().getInputStream());

            try {
                byte[] buf = new byte[4096];
                is.skip(from);
                while (len != 0) {
                    int read = is.read(buf, 0, len >= buf.length ? buf.length : len);
                    if (read != -1) {
                        os.write(buf, 0, read);
                        len -= read;
                    }
                }
            } catch (Exception e) {
                log.error("Exception '" + e.getMessage() + "' while handling request '" + getRepoPath()
                        + "' (Range: " + range + "), whereas see stack trace for details...");
                log.error(e, e);
            } finally {
                if (is != null) {
                    is.close();
                    log.warn("DEBUG: Making sure to close input stream of '" + getNode().getPath()
                            + "' (Range: " + range + ").");
                }
            }
            return view;
        } else {
            //log.debug("Range requested for node '" + getRepoPath()+ "': " + range);
        }
    } else {
        //log.debug("No range requested for node: " + getRepoPath());
    }

    view.setInputStream(getNode().getInputStream());

    return view;
}

From source file:ch.cyberduck.core.sftp.SFTPPath.java

@Override
public InputStream read(final TransferStatus status) throws IOException {
    InputStream in = null;
    if (Preferences.instance().getProperty("ssh.transfer").equals(Protocol.SFTP.getIdentifier())) {
        final SFTPv3FileHandle handle = this.getSession().sftp().openFileRO(this.getAbsolute());
        in = new SFTPInputStream(handle);
        if (status.isResume()) {
            log.info(String.format("Skipping %d bytes", status.getCurrent()));
            final long skipped = in.skip(status.getCurrent());
            if (skipped < status.getCurrent()) {
                throw new IOResumeException(
                        String.format("Skipped %d bytes instead of %d", skipped, status.getCurrent()));
            }/*from w ww  .  j a v a 2 s  .c  o m*/
        }
        // No parallel requests if the file size is smaller than the buffer.
        this.getSession().sftp().setRequestParallelism(
                (int) (status.getLength() / Preferences.instance().getInteger("connection.chunksize")) + 1);
    } else if (Preferences.instance().getProperty("ssh.transfer").equals(Protocol.SCP.getIdentifier())) {
        SCPClient scp = this.getSession().openScp();
        scp.setCharset(this.getSession().getEncoding());
        in = scp.get(this.getAbsolute());
    }
    return in;
}

From source file:com.healthmarketscience.rmiio.RemoteStreamServerTest.java

public static void copy(InputStream in, OutputStream out, boolean doPartial, boolean doSkip)
        throws IOException {
    final int BUF_SIZE = 1024;
    byte[] tmp = new byte[BUF_SIZE];

    int initAvail = in.available();

    int skipAt = 0;
    if (doSkip) {
        skipAt = (FILE_SIZE % BUF_SIZE) - 1;
    }/*w ww  .j av  a  2 s . c  o  m*/

    int numRead = 0;
    int totRead = 0;
    int iteration = 0;
    while ((numRead = cycleRead(in, tmp, iteration)) != -1) {
        cycleWrite(out, tmp, numRead, iteration);
        totRead += numRead;

        if (doPartial && (totRead >= PARTIAL_SIZE)) {
            // just return
            return;
        }
        if (doSkip && (totRead >= skipAt)) {
            long toSkip = FILE_SIZE - totRead;
            long skipped = in.skip(toSkip);
            if (skipped != toSkip) {
                throw new IOException("skipped wrong?");
            }
        }

        ++iteration;
    }

    if (totRead > 0) {
        if (initAvail < 0) {
            throw new IOException("no bytes?");
        }
    }

    out.flush();
    if (in.available() != 0) {
        throw new IOException("more bytes?");
    }
}

From source file:org.alfresco.repo.web.util.HttpRangeProcessor.java

/**
 * Stream a range of bytes from the given InputStream to the ServletOutputStream
 * //from w  w w.j a v  a2  s.  c om
 * @param r       Byte Range to process
 * @param is      InputStream
 * @param os      ServletOutputStream
 * @param offset  Assumed InputStream position - to calculate skip bytes from
 * 
 */
private void streamRangeBytes(final Range r, final InputStream is, final OutputStream os, long offset)
        throws IOException {
    final Log logger = getLogger();
    final boolean trace = logger.isTraceEnabled();

    // TODO: investigate using getFileChannel() on ContentReader

    if (r.start != 0L && r.start > offset) {
        long skipped = offset + is.skip(r.start - offset);
        if (skipped < r.start) {
            // Nothing left to download!
            return;
        }
    }
    long span = (r.end - r.start) + 1L;
    long bytesLeft = span;
    int read = 0;

    // Check that bytesLeft isn't greater than int can hold
    int bufSize;
    if (bytesLeft >= Integer.MAX_VALUE - 8) {
        bufSize = CHUNKSIZE;
    } else {
        bufSize = ((int) bytesLeft) < CHUNKSIZE ? (int) bytesLeft : CHUNKSIZE;
    }
    byte[] buf = new byte[bufSize];

    while ((read = is.read(buf)) > 0 && bytesLeft != 0L) {
        os.write(buf, 0, read);

        bytesLeft -= (long) read;

        if (bytesLeft != 0L) {
            int resize;
            if (bytesLeft >= Integer.MAX_VALUE - 8) {
                resize = CHUNKSIZE;
            } else {
                resize = ((int) bytesLeft) < CHUNKSIZE ? (int) bytesLeft : CHUNKSIZE;
            }
            if (resize != buf.length) {
                buf = new byte[resize];
            }
        }
        if (trace)
            logger.trace("...wrote " + read + " bytes, with " + bytesLeft + " to go...");
    }
}

From source file:com.cloud.storage.template.VhdProcessor.java

protected long getTemplateVirtualSize(File file) throws IOException {
    byte[] currentSize = new byte[8];
    byte[] cookie = new byte[8];
    byte[] creatorApp = new byte[4];

    BufferedInputStream fileStream = new BufferedInputStream(new FileInputStream(file));
    InputStream strm = fileStream;

    boolean isCompressed = checkCompressed(file.getAbsolutePath());

    if (isCompressed) {
        try {/*from ww w . j  av  a 2 s.  c om*/
            strm = new CompressorStreamFactory().createCompressorInputStream(fileStream);
        } catch (CompressorException e) {
            s_logger.info("error opening compressed VHD file " + file.getName());
            return file.length();
        }
    }
    try {

        //read the backup footer present at the top of the VHD file
        strm.read(cookie);
        if (!new String(cookie).equals(vhdIdentifierCookie)) {
            strm.close();
            return file.length();
        }

        long skipped = strm.skip(vhdFooterCreatorAppOffset - vhdCookieOffset);
        if (skipped == -1) {
            throw new IOException("Unexpected end-of-file");
        }
        long read = strm.read(creatorApp);
        if (read == -1) {
            throw new IOException("Unexpected end-of-file");
        }
        skipped = strm.skip(vhdFooterCurrentSizeOffset - vhdFooterCreatorVerOffset - vhdCookieOffset);
        if (skipped == -1) {
            throw new IOException("Unexpected end-of-file");
        }
        read = strm.read(currentSize);
        if (read == -1) {
            throw new IOException("Unexpected end-of-file");
        }
    } catch (IOException e) {
        s_logger.warn("Error reading virtual size from VHD file " + e.getMessage() + " VHD: " + file.getName());
        return file.length();
    } finally {
        if (strm != null) {
            strm.close();
        }
    }

    return NumbersUtil.bytesToLong(currentSize);
}

From source file:org.apache.hadoop.crypto.CryptoStreamsTestBase.java

/** Test skip. */
@Test(timeout = 120000)//w ww . j a  v a 2  s  .  c o  m
public void testSkip() throws Exception {
    OutputStream out = getOutputStream(defaultBufferSize);
    writeData(out);

    // Default buffer size
    InputStream in = getInputStream(defaultBufferSize);
    byte[] result = new byte[dataLen];
    int n1 = readAll(in, result, 0, dataLen / 3);
    Assert.assertEquals(n1, ((Seekable) in).getPos());

    long skipped = in.skip(dataLen / 3);
    int n2 = readAll(in, result, 0, dataLen);

    Assert.assertEquals(dataLen, n1 + skipped + n2);
    byte[] readData = new byte[n2];
    System.arraycopy(result, 0, readData, 0, n2);
    byte[] expectedData = new byte[n2];
    System.arraycopy(data, dataLen - n2, expectedData, 0, n2);
    Assert.assertArrayEquals(readData, expectedData);

    try {
        skipped = in.skip(-3);
        Assert.fail("Skip Negative length should fail.");
    } catch (IllegalArgumentException e) {
        GenericTestUtils.assertExceptionContains("Negative skip length", e);
    }

    // Skip after EOF
    skipped = in.skip(3);
    Assert.assertEquals(skipped, 0);

    in.close();
}