Example usage for com.google.common.io ByteStreams skipFully

List of usage examples for com.google.common.io ByteStreams skipFully

Introduction

In this page you can find the example usage for com.google.common.io ByteStreams skipFully.

Prototype

public static void skipFully(InputStream in, long n) throws IOException 

Source Link

Document

Discards n bytes of data from the input stream.

Usage

From source file:org.b1.pack.cli.FsBuilderFile.java

@Override
public void writeTo(OutputStream stream, long start, long end) throws IOException {
    Preconditions.checkArgument(start < end);
    InputStream inputStream = new FileInputStream(fsObject.getFile());
    try {//from  ww w . ja v  a  2 s . c  o  m
        ByteStreams.skipFully(inputStream, start);
        byte[] buffer = new byte[0x10000];
        while (start < end) {
            int count = inputStream.read(buffer, 0, (int) Math.min(buffer.length, end - start));
            if (count <= 0)
                throw new EOFException();
            stream.write(buffer, 0, count);
            start += count;
        }
        Preconditions.checkState(start == end);
    } finally {
        inputStream.close();
    }
}

From source file:org.b1.pack.standard.reader.PackInputStream.java

public void seek(RecordPointer pointer) throws IOException {
    BlockPointer blockPointer = chunkCursor.getBlockPointer();
    if (blockPointer != null && blockPointer.volumeNumber == pointer.volumeNumber
            && blockPointer.blockOffset == pointer.blockOffset) {
        long skipCount = pointer.recordOffset - chunkCursor.getRecordOffset();
        if (skipCount >= 0) {
            ByteStreams.skipFully(this, skipCount);
            return;
        }//from  w  ww  .j a  v  a  2 s.co  m
    }
    chunkCursor.seek(new BlockPointer(pointer.volumeNumber, pointer.blockOffset));
    ByteStreams.skipFully(this, pointer.recordOffset);
}

From source file:org.queeg.hadoop.tar.TarExtractor.java

public void extract(ByteSource source) throws IOException {
    TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(source.openStream());

    TarArchiveEntry entry;//from w w w .j a  v  a  2s .c o  m
    while ((entry = archiveInputStream.getNextTarEntry()) != null) {
        if (entry.isFile()) {
            BoundedInputStream entryInputStream = new BoundedInputStream(archiveInputStream, entry.getSize());
            ByteSink sink = new PathByteSink(conf, new Path(destination, entry.getName()));
            sink.writeFrom(entryInputStream);
        } else if (entry.isDirectory()) {
            ByteStreams.skipFully(archiveInputStream, entry.getSize());
            fs.mkdirs(new Path(destination, entry.getName()));
        }
    }

    archiveInputStream.close();
}

From source file:hihex.cs.GrantPermission.java

private static void readAdbPacket(final InputStream stream, final byte[] header) throws IOException {
    ByteStreams.readFully(stream, header);
    long length = ByteBuffer.wrap(header).order(ByteOrder.LITTLE_ENDIAN).get(12);
    ByteStreams.skipFully(stream, length);
}

From source file:org.jclouds.io.internal.BasePayloadSlicer.java

protected Payload doSlice(InputStream content, long offset, long length) {
    try {//from ww  w . j  a v a  2  s .  com
        ByteStreams.skipFully(content, offset);
    } catch (IOException ioe) {
        throw Throwables.propagate(ioe);
    }
    return new InputStreamPayload(ByteStreams.limit(content, length));
}

From source file:org.b1.pack.standard.reader.VolumeCursor.java

public void seek(BlockPointer pointer) throws IOException {
    if (pointer.volumeNumber == volumeNumber) {
        long skipCount = pointer.blockOffset - inputStream.getCount();
        if (skipCount >= 0) {
            ByteStreams.skipFully(inputStream, skipCount);
            return;
        }//  ww w .  j a va2 s  . c  om
    }
    openVolume(pointer.volumeNumber);
    long skipCount = pointer.blockOffset - inputStream.getCount();
    Preconditions.checkState(skipCount >= 0);
    ByteStreams.skipFully(inputStream, skipCount);
}

From source file:garmintools.main.NavDataTool.java

private void printTableOfContents(File garminDataFile) throws IOException {
    InputStream inputStream = new FileInputStream(garminDataFile);
    ByteStreams.skipFully(inputStream, TableOfContentsGarminAdapter.TABLE_OF_CONTENTS_OFFSET);
    SectionManager.GarminBuilder sectionManagerBuilder = new SectionManager.GarminBuilder();
    sectionManagerBuilder.readTableOfContents(inputStream, Ints.checkedCast(garminDataFile.length()));
    SectionManager sectionManager = sectionManagerBuilder.build();
    TableOfContentsSection tocSection = sectionManager.getTableOfContentsSection();
    for (TableOfContentsEntry entry : tocSection.getEntryMap().values()) {
        System.out.println(entry.toString());
    }// w  w w.  ja  v  a2 s.c o m
    inputStream.close();
}

From source file:org.scache.network.buffer.FileSegmentManagedBuffer.java

@Override
public InputStream createInputStream() throws IOException {
    FileInputStream is = null;//from w ww.ja va2s .co  m
    try {
        is = new FileInputStream(file);
        ByteStreams.skipFully(is, offset);
        return new LimitedInputStream(is, length);
    } catch (IOException e) {
        try {
            if (is != null) {
                long size = file.length();
                throw new IOException("Error in reading " + this + " (actual file length " + size + ")", e);
            }
        } catch (IOException ignored) {
            // ignore
        } finally {
            JavaUtils.closeQuietly(is);
        }
        throw new IOException("Error in opening " + this, e);
    } catch (RuntimeException e) {
        JavaUtils.closeQuietly(is);
        throw e;
    }
}

From source file:hudson.plugins.timestamper.io.TimestampsReader.java

/**
 * Read up to {@code count} time-stamps and add them to the given list.
 * //from   ww  w .j a v  a 2  s .  c  o m
 * @param count
 *          the number of time-stamps to read
 * @param timestamps
 *          the list that will contain the time-stamps (optional)
 * @throws IOException
 */
private void read(int count, Optional<List<Timestamp>> timestamps) throws IOException {
    if (count < 1 || !timestampsFile.isFile()) {
        return;
    }
    InputStream inputStream = new FileInputStream(timestampsFile);
    boolean threw = true;
    try {
        ByteStreams.skipFully(inputStream, filePointer);
        inputStream = new BufferedInputStream(inputStream);
        int i = 0;
        while (i < count && filePointer < timestampsFile.length()) {
            Timestamp timestamp = readNext(inputStream);
            if (timestamps.isPresent()) {
                timestamps.get().add(timestamp);
            }
            i++;
        }
        threw = false;
    } finally {
        Closeables.close(inputStream, threw);
    }
}

From source file:io.crate.blob.DigestBlob.java

private void calculateDigest() {
    assert headSize.get() == headLength : "Head hasn't catched up, can't calculate digest";
    try (FileInputStream stream = new FileInputStream(file)) {
        ByteStreams.skipFully(stream, headLength);
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = stream.read(buffer, 0, 4096)) > 0) {
            md.update(buffer, 0, bytesRead);
        }/*from  w w  w  . j  a  va 2s .co m*/
    } catch (IOException ex) {
        logger.error("error accessing file to calculate digest", ex);
    }
}