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:info.magnolia.cms.filters.MultipartRequestFilterTempFileDeletionTest.java

private void checkMultipartForm(MultipartForm form, String expectedDocumentType) throws IOException {
    assertNotNull("MultipartForm request attribute expected", form);
    assertEquals(3, form.getParameters().size());
    assertEquals("value1", form.getParameter("param1"));
    assertEquals("", form.getParameter("param2"));

    String[] value3 = form.getParameterValues("param3");
    assertNotNull("multi-value parameter has not been parsed", value3);
    assertEquals(2, value3.length);//from w  w  w . j  a v a 2  s  . co m

    assertEquals(1, form.getDocuments().size());

    Document document = form.getDocument("document");
    assertNotNull("expected non-null Document", document);
    assertEquals("document", document.getAtomName());
    assertEquals("xml", document.getExtension());
    assertEquals("pom", document.getFileName());
    assertEquals("pom.xml", document.getFileNameWithExtension());
    assertEquals(testFile.length(), document.getLength());

    assertEquals(expectedDocumentType, document.getType());

    assertTrue(document.getType().startsWith("text/xml"));

    File documentFile = document.getFile();
    assertTrue(documentFile.exists());
    assertTrue(documentFile.canRead());
    InputStream stream1 = document.getStream();
    assertEquals(testFile.length(), stream1.available());
    assertEquals(testFile.length(), stream1.skip(testFile.length()));
    assertEquals(0, stream1.available());
    documentFile.deleteOnExit();
}

From source file:net.lightbody.bmp.proxy.jetty.util.Resource.java

/** 
 * @param out /*  w w w. j  a  v a 2s  .c  o m*/
 * @param start First byte to write
 * @param count Bytes to write or -1 for all of them.
 */
public void writeTo(OutputStream out, long start, long count) throws IOException {
    InputStream in = getInputStream();
    try {
        in.skip(start);
        if (count < 0)
            IO.copy(in, out);
        else
            IO.copy(in, out, (int) count);
    } finally {
        in.close();
    }
}

From source file:com.adaptris.core.lms.StreamWrapperCase.java

@Test
public void testInputStream_Skip() throws Exception {
    StreamWrapper wrapper = createWrapper(false);
    File file = writeFile(TempFileUtils.createTrackedFile(wrapper));
    InputStream in = wrapper.openInputStream(file, NO_OP);
    try (InputStream closeable = in) {
        final long toSkip = BYTES_TEXT.length - 1;
        tryQuietly(() -> {// w w  w. ja  va  2  s.  co m
            long skipped = in.skip(toSkip);
            assertEquals(toSkip, skipped);
        });
    }
}

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

@Test
public void testNegativeBytesSkippedAfterRead() 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  v  a 2 s  . c  o m
    // Skipping negative bytes should essential be a no-op
    assertEquals(0, inputStream.skip(-1));
    assertEquals(0, inputStream.skip(-1024));
    assertEquals(0, inputStream.skip(Long.MIN_VALUE));
    assertEquals(1024, inputStream.skip(1024));
    for (int i = 2048; i < randomBytes.length; i++) {
        assertEquals(randomBytes[i], (byte) inputStream.read());
    }
}

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

/**
 * ??copy//  w w w.jav  a2  s  .co  m
 */
public static long copy(InputStream input, OutputStream output, long offset) throws IOException {
    long count = 0;
    long n = 0;
    if (input instanceof FileInputStream) {
        FileChannel inChannel = ((FileInputStream) input).getChannel();
        WritableByteChannel outChannel = Channels.newChannel(output);
        count = inChannel.transferTo(offset, inChannel.size() - offset, outChannel);
    } else if (output instanceof FileOutputStream) {
        FileChannel outChannel = ((FileOutputStream) output).getChannel();
        ReadableByteChannel inChannel = Channels.newChannel(input);
        do {
            n = outChannel.transferFrom(inChannel, offset + count, DEFAULT_BUFFER_SIZE);
            count += n;
        } while (n > 0);
    } else {
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

        input.skip(offset);
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, (int) n);
            count += 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 count;
}

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

@Test
public void testNegativeBytesSkippedAfterRead() throws IOException {
    for (InputStream inputStream : inputStreams) {
        for (int i = 0; i < 1024; i++) {
            assertEquals(randomBytes[i], (byte) inputStream.read());
        }/*from w  w w .  jav  a2 s  .c  o m*/
        // Skipping negative bytes should essential be a no-op
        assertEquals(0, inputStream.skip(-1));
        assertEquals(0, inputStream.skip(-1024));
        assertEquals(0, inputStream.skip(Long.MIN_VALUE));
        assertEquals(1024, inputStream.skip(1024));
        for (int i = 2048; i < randomBytes.length; i++) {
            assertEquals(randomBytes[i], (byte) inputStream.read());
        }
    }
}

From source file:net.seedboxer.common.ftp.FtpUploaderCommons.java

private void uploadFile(File fileToUpload, Map<String, Long> filesListInServer, FtpUploaderListener listener)
        throws IOException {

    String fileName = fileToUpload.getName();
    Long size = filesListInServer.get(fileName);
    if (size != null && size != 0L) {
        // Tell listener that already exist and transfer (part) of the file
        listener.bytesTransferred(size);
        if (size == fileToUpload.length()) {
            LOGGER.debug("File already exists {}", fileName);
        } else {/*from   w w w.ja  va2 s . c o m*/
            LOGGER.trace("Resuming file {} from {} MB", fileName, (size / (1024 * 1024)));
            // Set the offset
            ftpClient.setRestartOffset(size);
            // Create stream and skip first SIZE bytes
            InputStream ins = new FileInputStream(fileToUpload);
            ins.skip(size);
            // Upload file
            storeFile(fileName, ins, fileToUpload.length() - size, listener);

            LOGGER.debug("File {} successfully uploaded", fileName);
        }
    } else {
        storeFile(fileName, new FileInputStream(fileToUpload), fileToUpload.length(), listener);
        LOGGER.debug("File {} successfully uploaded", fileName);
    }
}

From source file:slina.mb.smb.SmbSessionImpl.java

public void read(String source, OutputStream os, long skipLimit) throws IOException {

    try {//from   ww  w  . ja v a 2 s  .  com
        SmbFile file = new SmbFile(source, ntlmAuth);
        InputStream is = file.getInputStream();

        long remoteFileSize = file.length();

        if (remoteFileSize > skipLimit) {

            long skip = remoteFileSize - skipLimit;
            is.skip(skip);
        }
        FileCopyUtils.copy(is, os);
    } catch (SmbAuthException ex) {
        // handle authentication related issue here
        isOpen = false;
        throw new NestedIOException("Failed to read resource " + source, ex);
    } catch (SmbException ex) {
        // any special SMB related exception handling
        throw new NestedIOException("Failed to read resource " + source, ex);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Successfully read resource " + source);
    }

}

From source file:slina.mb.smb.SmbSessionImpl.java

@Override
public void read(String source, OutputStream os) throws IOException {
    try {//from   w w  w  .ja  v a 2s.c o m
        SmbFile file = new SmbFile(source, ntlmAuth);
        InputStream is = file.getInputStream();

        long remoteFileSize = file.length();

        if (remoteFileSize > this.fileSizeLmit) {

            long skip = remoteFileSize - this.fileSizeLmit;
            is.skip(skip);

        }
        FileCopyUtils.copy(is, os);
    } catch (SmbAuthException ex) {
        // handle authentication related issue here
        isOpen = false;
        throw new NestedIOException("Failed to read resource " + source, ex);
    } catch (SmbException ex) {
        // any special SMB related exception handling
        throw new NestedIOException("Failed to read resource " + source, ex);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Successfully read resource " + source);
    }
}

From source file:com.nesscomputing.hbase.spill.SpilledFile.java

public List<Put> load() throws IOException {
    InputStream is = null;

    final ImmutableList.Builder<Put> builder = ImmutableList.builder();

    try {/*from   w ww .  j a v  a 2s  .co  m*/
        is = new BufferedInputStream(new FileInputStream(file));

        final int skippedBytes = 4 + 4 + 8; // int, int, long
        Preconditions.checkState(skippedBytes == is.skip(skippedBytes),
                "skipped byte mismatch (you are in trouble...)");

        Put put = null;

        while ((put = BinaryConverter.PUT_READ_FUNCTION.apply(is)) != null) {
            builder.add(put);
        }

        final List<Put> result = builder.build();
        Preconditions.checkState(result.size() == elements,
                "The preamble reported %d elements, but %d were found!", elements, result.size());
        return result;
    } finally {
        Closeables.closeQuietly(is);
    }
}