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.spark.io.NioBufferedFileInputStreamSuite.java

@Test
public void testBytesSkippedAfterEOF() throws IOException {
    InputStream inputStream = new NioBufferedFileInputStream(inputFile);
    assertEquals(randomBytes.length, inputStream.skip(randomBytes.length + 1));
    assertEquals(-1, inputStream.read());
}

From source file:uk.ac.ox.webauth.crypto.DesCbcCrc.java

/**
 * From RFC 3961:// w ww . j a va 2 s .  c  o  m
 * <pre>
 *   +-----------+----------+---------+-----+
 *   |confounder | checksum | msg-seq | pad |
 *   +-----------+----------+---------+-----+
 * </pre>
 */
@Override
public ASN1Encodable decrypt(byte[] cipherData) throws IOException, GeneralSecurityException {
    // decrypt the data
    Cipher cipher = Cipher.getInstance("DES/CBC/NoPadding");
    IvParameterSpec iv = new IvParameterSpec(key.getEncoded());
    cipher.init(DECRYPT_MODE, key, iv);
    byte[] data = cipher.doFinal(cipherData);

    // split out the CRC checksum (4 bytes) and check it
    byte[] checksum = new byte[4];
    System.arraycopy(data, cipher.getBlockSize(), checksum, 0, checksum.length);
    Arrays.fill(data, cipher.getBlockSize(), cipher.getBlockSize() + checksum.length, (byte) 0);
    if (!Arrays.equals(checksum, modifiedCRC32(data))) {
        throw new GeneralSecurityException("Checksum failure.");
    }

    // return an ASN.1 object
    InputStream is = new ByteArrayInputStream(data);
    is.skip(cipher.getBlockSize() + checksum.length);
    ASN1InputStream ais = new ASN1InputStream(is);

    return (ASN1Encodable) ais.readObject();
}

From source file:org.apache.jackrabbit.core.value.BLOBFileValue.java

/**
 * {@inheritDoc}//  w  w w .ja  v  a2  s  .co  m
 */
public int read(byte[] b, long position) throws IOException, RepositoryException {
    InputStream in = getStream();
    try {
        in.skip(position);
        return in.read(b);
    } finally {
        in.close();
    }
}

From source file:samples.mediators.BinaryExtractMediator.java

public boolean mediate(MessageContext msgCtx) {
    try {/* w w w .  j a  va 2 s  . c  om*/
        log.debug("BinaryExtractMediator Process, with offset: " + offset + " ,length " + length);
        SOAPBody soapBody = msgCtx.getEnvelope().getBody();
        OMElement firstElement = soapBody.getFirstElement();
        log.debug("First Element : " + firstElement.getLocalName());
        log.debug("First Element Text : " + firstElement.getText());
        OMText binaryNode = (OMText) firstElement.getFirstOMChild();
        log.debug("First Element Node Text : " + binaryNode.getText());
        DataHandler dataHandler = (DataHandler) binaryNode.getDataHandler();
        InputStream inputStream = dataHandler.getInputStream();
        byte[] searchByte = new byte[length];
        inputStream.skip(offset - 1);
        int readBytes = inputStream.read(searchByte, 0, length);
        String outString = new String(searchByte, binaryEncoding);
        msgCtx.setProperty(variableName, outString);
        log.debug("Set property to MsgCtx, " + variableName + " = " + outString);
        inputStream.close();
    } catch (IOException e) {
        log.error("Excepton on mediation : " + e.getMessage());
    }
    return true;
}

From source file:org.olat.core.gui.media.ServletUtil.java

private static void pseudoStreamFlashResource(HttpServletRequest httpReq, HttpServletResponse httpResp,
        MediaResource mr) {/*w  w  w.j  av  a  2  s. c o  m*/
    Long range = getRange(httpReq);
    long seekPos = range == null ? 0l : range.longValue();
    long fileSize = mr.getSize() - ((seekPos > 0) ? seekPos + 1 : 0);

    InputStream s = null;
    OutputStream out = null;

    try {
        s = new BufferedInputStream(mr.getInputStream());
        out = httpResp.getOutputStream();

        if (seekPos == 0) {
            httpResp.addHeader("Content-Length", Long.toString(fileSize));
        } else {
            httpResp.addHeader("Content-Length", Long.toString(fileSize + 13));
            byte[] flvHeader = new byte[] { 70, 76, 86, 1, 1, 0, 0, 0, 9, 0, 0, 0, 9 };
            out.write(flvHeader);
        }

        s.skip(seekPos);

        final int bufferSize = 1024 * 10;
        long left = fileSize;
        while (left > 0) {
            int howMuch = bufferSize;
            if (howMuch > left) {
                howMuch = (int) left;
            }

            byte[] buf = new byte[howMuch];
            int numRead = s.read(buf);

            out.write(buf, 0, numRead);
            httpResp.flushBuffer();

            if (numRead == -1) {
                break;
            }

            left -= numRead;
        }
    } catch (Exception e) {
        log.error("", e);
        if (e.getClass().getName().contains("Eof")) {
            //ignore
        } else {
            throw new RuntimeException(e);
        }
    } finally {
        FileUtils.closeSafely(s);
    }
}

From source file:org.apache.spark.io.GenericFileInputStreamSuite.java

@Test
public void testBytesSkippedAfterRead() throws IOException {
    for (InputStream inputStream : inputStreams) {
        for (int i = 0; i < 1024; i++) {
            assertEquals(randomBytes[i], (byte) inputStream.read());
        }//from  w  ww. j  av  a2s  . c  o  m
        assertEquals(1024, inputStream.skip(1024));
        for (int i = 2048; i < randomBytes.length; i++) {
            assertEquals(randomBytes[i], (byte) inputStream.read());
        }
    }
}

From source file:org.apache.spark.io.NioBufferedFileInputStreamSuite.java

@Test
public void testBytesSkippedAfterRead() throws IOException {
    InputStream inputStream = new NioBufferedFileInputStream(inputFile);
    for (int i = 0; i < 1024; i++) {
        assertEquals(randomBytes[i], (byte) inputStream.read());
    }/*from   w  w w  .j a va2 s .  com*/
    assertEquals(1024, inputStream.skip(1024));
    for (int i = 2048; i < randomBytes.length; i++) {
        assertEquals(randomBytes[i], (byte) inputStream.read());
    }
}

From source file:org.apache.spark.io.GenericFileInputStreamSuite.java

@Test
public void testSkipFromFileChannel() throws IOException {
    for (InputStream inputStream : inputStreams) {
        // Since the buffer is smaller than the skipped bytes, this will guarantee
        // we skip from underlying file channel.
        assertEquals(1024, inputStream.skip(1024));
        for (int i = 1024; i < 2048; i++) {
            assertEquals(randomBytes[i], (byte) inputStream.read());
        }//from   www. ja  v a  2 s.c  o m
        assertEquals(256, inputStream.skip(256));
        assertEquals(256, inputStream.skip(256));
        assertEquals(512, inputStream.skip(512));
        for (int i = 3072; i < randomBytes.length; i++) {
            assertEquals(randomBytes[i], (byte) inputStream.read());
        }
    }
}

From source file:org.apache.spark.io.NioBufferedFileInputStreamSuite.java

@Test
public void testSkipFromFileChannel() throws IOException {
    InputStream inputStream = new NioBufferedFileInputStream(inputFile, 10);
    // Since the buffer is smaller than the skipped bytes, this will guarantee
    // we skip from underlying file channel.
    assertEquals(1024, inputStream.skip(1024));
    for (int i = 1024; i < 2048; i++) {
        assertEquals(randomBytes[i], (byte) inputStream.read());
    }//  w  w  w .  j ava2s.c o m
    assertEquals(256, inputStream.skip(256));
    assertEquals(256, inputStream.skip(256));
    assertEquals(512, inputStream.skip(512));
    for (int i = 3072; i < randomBytes.length; i++) {
        assertEquals(randomBytes[i], (byte) inputStream.read());
    }
}

From source file:com.zimbra.cs.octosync.PatchInputStream.java

private InputStream nextInputStream() throws IOException, ServiceException {
    if (!patchReader.hasMoreRecordInfos()) {
        return null;
    }// ww w .j a  va2  s .co  m

    PatchReader.RecordInfo ri = patchReader.getNextRecordInfo();

    InputStream nextStream = null;

    if (ri.type == PatchReader.RecordType.DATA) {

        log.debug("Patch data, length: " + ri.length);
        nextStream = patchReader.popData();

    } else if (ri.type == PatchReader.RecordType.REF) {

        PatchRef patchRef = patchReader.popRef();
        log.debug("Patch reference " + patchRef);

        if (patchRef.length > MAX_REF_LENGTH) {
            throw new InvalidPatchReferenceException(
                    "referenced data too large: " + patchRef.length + " > " + MAX_REF_LENGTH);
        }

        if (manifest != null) {
            int[] actualRef = blobAccess.getActualReference(patchRef.fileId, patchRef.fileVersion);
            manifest.addReference(actualRef[0], actualRef[1], patchRef.length);
        }

        try {
            InputStream blobIs = blobAccess.getBlobInputStream(patchRef.fileId, patchRef.fileVersion);

            blobIs.skip(patchRef.offset);

            byte[] chunkBuf = new byte[patchRef.length];

            DataInputStream dis = new DataInputStream(blobIs);
            dis.readFully(chunkBuf);
            dis.close();

            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(chunkBuf);
            byte[] calcHash = md.digest();

            if (!Arrays.equals(patchRef.hashKey, calcHash)) {
                throw new InvalidPatchReferenceException("refrenced data hash mismatch, actual hash: "
                        + new String(Hex.encodeHex(calcHash)) + "; " + patchRef);
            }

            nextStream = new ByteArrayInputStream(chunkBuf);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            assert false : "SHA-256 must be supported";
        }
    } else {
        assert false : "Invalid record type: " + ri.type;
    }

    assert nextStream != null : "Stream returned here must be non-null";
    return nextStream;
}