Java Code Examples for com.google.common.io.ByteStreams#skipFully()

The following examples show how to use com.google.common.io.ByteStreams#skipFully() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: emodb   File: BlobStoreClient.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
public InputStream getInputStream() throws IOException {
    if (!claimInputStream()) {
        // The input stream has already been consumed
        return null;
    }

    if (_rangeApplied) {
        // Either the entire blob was requested or the requested range was applied by the server
        return _stream;
    }

    // The client requested a sub-range of the blob but the server ignored it.  Manipulate the input stream
    // to return only the client requested range.
    ByteStreams.skipFully(_stream, _range.getOffset());
    return ByteStreams.limit(_stream, _range.getLength());
}
 
Example 2
Source Project: bboxdb   File: RoutingHeaderParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Decode a routed package header
 * @param inputStream
 * @return
 * @throws IOException
 */
private static RoutingHeader decodeRoutedPackage(final InputStream inputStream) throws IOException {
	
	// Hop
	final byte[] hopBuffer = new byte[2];
	ByteStreams.readFully(inputStream, hopBuffer, 0, hopBuffer.length);
	final short hop = DataEncoderHelper.readShortFromByte(hopBuffer);
	
	// Skip one unused byte
	ByteStreams.skipFully(inputStream, 1);

	// Routing list list length
	final byte[] routingListLengthBuffer = new byte[2];
	ByteStreams.readFully(inputStream, routingListLengthBuffer, 0, routingListLengthBuffer.length);
	final short routingListLength = DataEncoderHelper.readShortFromByte(routingListLengthBuffer);

	final byte[] routingListBuffer = new byte[routingListLength];
	ByteStreams.readFully(inputStream, routingListBuffer, 0, routingListBuffer.length);
	final String routingList = new String(routingListBuffer);
	
	return new RoutingHeader(hop, routingList);
}
 
Example 3
Source Project: h2o-2   File: PersistTachyon.java    License: Apache License 2.0 6 votes vote down vote up
@Override public byte[] load(Value v) {
  Key k = v._key; // key for value
  if (k._kb[0] != Key.DVEC) throw H2O.unimpl(); // Load only from values stored in vector
  long skip = FileVec.chunkOffset(k); // Compute skip for this value
  long start_io_ms = System.currentTimeMillis();
  final byte[] b = MemoryManager.malloc1(v._max);
  String[] keyComp = decodeKey(k);
  String clientUri = keyComp[0];
  String fpath = keyComp[1];
  TachyonFS tfs = null;
  InputStream is = null;
  try {
    tfs = (TachyonFS) (Persist.I[Value.TACHYON].createClient(clientUri));
    long start_ns = System.nanoTime(); // Blocking i/o call timing - without counting repeats
    is = tfs.getFile(fpath).getInStream(ReadType.NO_CACHE);
    ByteStreams.skipFully(is, skip);
    ByteStreams.readFully(is, b);
    TimeLine.record_IOclose(start_ns, start_io_ms, 1/* read */, v._max, Value.TACHYON);
    return b;
  } catch (IOException e) {
    throw new RuntimeException(Log.err("File load failed: ", e));
  } finally {
    if (is!=null) Utils.close(is);
  }
}
 
Example 4
Source Project: ZjDroid   File: DexBackedOdexFile.java    License: Apache License 2.0 5 votes vote down vote up
public static DexBackedOdexFile fromInputStream(@Nonnull Opcodes opcodes,
		@Nonnull InputStream is) throws IOException {
	if (!is.markSupported()) {
		throw new IllegalArgumentException("InputStream must support mark");
	}
	is.mark(8);
	byte[] partialHeader = new byte[8];
	try {
		ByteStreams.readFully(is, partialHeader);
	} catch (EOFException ex) {
		throw new NotADexFile("File is too short");
	} finally {
		is.reset();
	}

	verifyMagic(partialHeader);

	is.reset();
	byte[] odexBuf = new byte[OdexHeaderItem.ITEM_SIZE];
	ByteStreams.readFully(is, odexBuf);
	int dexOffset = OdexHeaderItem.getDexOffset(odexBuf);
	if (dexOffset > OdexHeaderItem.ITEM_SIZE) {
		ByteStreams.skipFully(is, dexOffset - OdexHeaderItem.ITEM_SIZE);
	}

	byte[] dexBuf = ByteStreams.toByteArray(is);

	return new DexBackedOdexFile(opcodes, odexBuf, dexBuf);
}
 
Example 5
Source Project: zjdroid   File: DexBackedOdexFile.java    License: Apache License 2.0 5 votes vote down vote up
public static DexBackedOdexFile fromInputStream(@Nonnull Opcodes opcodes,
		@Nonnull InputStream is) throws IOException {
	if (!is.markSupported()) {
		throw new IllegalArgumentException("InputStream must support mark");
	}
	is.mark(8);
	byte[] partialHeader = new byte[8];
	try {
		ByteStreams.readFully(is, partialHeader);
	} catch (EOFException ex) {
		throw new NotADexFile("File is too short");
	} finally {
		is.reset();
	}

	verifyMagic(partialHeader);

	is.reset();
	byte[] odexBuf = new byte[OdexHeaderItem.ITEM_SIZE];
	ByteStreams.readFully(is, odexBuf);
	int dexOffset = OdexHeaderItem.getDexOffset(odexBuf);
	if (dexOffset > OdexHeaderItem.ITEM_SIZE) {
		ByteStreams.skipFully(is, dexOffset - OdexHeaderItem.ITEM_SIZE);
	}

	byte[] dexBuf = ByteStreams.toByteArray(is);

	return new DexBackedOdexFile(opcodes, odexBuf, dexBuf);
}
 
Example 6
Source Project: AppTroy   File: DexBackedOdexFile.java    License: Apache License 2.0 5 votes vote down vote up
public static DexBackedOdexFile fromInputStream(@Nonnull Opcodes opcodes, @Nonnull InputStream is)
        throws IOException {
    if (!is.markSupported()) {
        throw new IllegalArgumentException("InputStream must support mark");
    }
    is.mark(8);
    byte[] partialHeader = new byte[8];
    try {
        ByteStreams.readFully(is, partialHeader);
    } catch (EOFException ex) {
        throw new NotADexFile("File is too short");
    } finally {
        is.reset();
    }

    verifyMagic(partialHeader);

    is.reset();
    byte[] odexBuf = new byte[OdexHeaderItem.ITEM_SIZE];
    ByteStreams.readFully(is, odexBuf);
    int dexOffset = OdexHeaderItem.getDexOffset(odexBuf);
    if (dexOffset > OdexHeaderItem.ITEM_SIZE) {
        ByteStreams.skipFully(is, dexOffset - OdexHeaderItem.ITEM_SIZE);
    }

    byte[] dexBuf = ByteStreams.toByteArray(is);

    return new DexBackedOdexFile(opcodes, odexBuf, dexBuf);
}
 
Example 7
Source Project: HeyGirl   File: DexBackedOdexFile.java    License: Apache License 2.0 5 votes vote down vote up
public static DexBackedOdexFile fromInputStream(@Nonnull Opcodes opcodes,
		@Nonnull InputStream is) throws IOException {
	if (!is.markSupported()) {
		throw new IllegalArgumentException("InputStream must support mark");
	}
	is.mark(8);
	byte[] partialHeader = new byte[8];
	try {
		ByteStreams.readFully(is, partialHeader);
	} catch (EOFException ex) {
		throw new NotADexFile("File is too short");
	} finally {
		is.reset();
	}

	verifyMagic(partialHeader);

	is.reset();
	byte[] odexBuf = new byte[OdexHeaderItem.ITEM_SIZE];
	ByteStreams.readFully(is, odexBuf);
	int dexOffset = OdexHeaderItem.getDexOffset(odexBuf);
	if (dexOffset > OdexHeaderItem.ITEM_SIZE) {
		ByteStreams.skipFully(is, dexOffset - OdexHeaderItem.ITEM_SIZE);
	}

	byte[] dexBuf = ByteStreams.toByteArray(is);

	return new DexBackedOdexFile(opcodes, odexBuf, dexBuf);
}
 
Example 8
Source Project: bazel   File: Chunker.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Seek to an offset, if necessary resetting or initializing
 *
 * <p>May close open resources in order to seek to an earlier offset.
 */
public void seek(long toOffset) throws IOException {
  if (toOffset < offset) {
    reset();
  }
  maybeInitialize();
  ByteStreams.skipFully(data, toOffset - offset);
  offset = toOffset;
}
 
Example 9
private static byte[] readBytesAndSkipPadding(LittleEndianDataInputStream input, int size)
    throws IOException {
  byte[] result = new byte[size];
  input.readFully(result);

  long overage = size % AAPT_FLAT_FILE_ALIGNMENT;
  if (overage != 0) {
    ByteStreams.skipFully(input, AAPT_FLAT_FILE_ALIGNMENT - overage);
  }
  return result;
}
 
Example 10
Source Project: ZjDroid   File: DexBackedOdexFile.java    License: Apache License 2.0 5 votes vote down vote up
public static DexBackedOdexFile fromInputStream(@Nonnull Opcodes opcodes,
		@Nonnull InputStream is) throws IOException {
	if (!is.markSupported()) {
		throw new IllegalArgumentException("InputStream must support mark");
	}
	is.mark(8);
	byte[] partialHeader = new byte[8];
	try {
		ByteStreams.readFully(is, partialHeader);
	} catch (EOFException ex) {
		throw new NotADexFile("File is too short");
	} finally {
		is.reset();
	}

	verifyMagic(partialHeader);

	is.reset();
	byte[] odexBuf = new byte[OdexHeaderItem.ITEM_SIZE];
	ByteStreams.readFully(is, odexBuf);
	int dexOffset = OdexHeaderItem.getDexOffset(odexBuf);
	if (dexOffset > OdexHeaderItem.ITEM_SIZE) {
		ByteStreams.skipFully(is, dexOffset - OdexHeaderItem.ITEM_SIZE);
	}

	byte[] dexBuf = ByteStreams.toByteArray(is);

	return new DexBackedOdexFile(opcodes, odexBuf, dexBuf);
}
 
Example 11
Source Project: slice   File: AbstractSliceInputTest.java    License: Apache License 2.0 5 votes vote down vote up
protected void testReadOffEnd(SliceInputTester tester, Slice slice)
{
    SliceInput input = createSliceInput(slice);
    try {
        ByteStreams.skipFully(input, slice.length() - tester.valueSize() + 1);
    }
    catch (IOException e) {
        throw Throwables.propagate(e);
    }
    tester.verifyReadOffEnd(input);
}
 
Example 12
private static long copyStreams(final InputStream from, final OutputStream to,
        final FileStreamingProgressListener progressListener, final long start, final long length,
        final String filename) throws IOException {

    final long startMillis = System.currentTimeMillis();
    LOG.trace("Start of copy-streams of file {} from {} to {}", filename, start, length);

    Preconditions.checkNotNull(from);
    Preconditions.checkNotNull(to);
    final byte[] buf = new byte[BUFFER_SIZE];
    long total = 0;
    int progressPercent = 1;

    ByteStreams.skipFully(from, start);

    long toRead = length;
    boolean toContinue = true;
    long shippedSinceLastEvent = 0;

    while (toContinue) {
        final int r = from.read(buf);
        if (r == -1) {
            break;
        }

        toRead -= r;
        if (toRead > 0) {
            to.write(buf, 0, r);
            total += r;
            shippedSinceLastEvent += r;
        } else {
            to.write(buf, 0, (int) toRead + r);
            total += toRead + r;
            shippedSinceLastEvent += toRead + r;
            toContinue = false;
        }

        if (progressListener != null) {
            final int newPercent = DoubleMath.roundToInt(total * 100.0 / length, RoundingMode.DOWN);

            // every 10 percent an event
            if (newPercent == 100 || newPercent > progressPercent + 10) {
                progressPercent = newPercent;
                progressListener.progress(length, shippedSinceLastEvent, total);
                shippedSinceLastEvent = 0;
            }
        }
    }

    final long totalTime = System.currentTimeMillis() - startMillis;

    if (total < length) {
        throw new FileStreamingFailedException(filename + ": " + (length - total)
                + " bytes could not be written to client, total time on write: !" + totalTime + " ms");
    }

    LOG.trace("Finished copy-stream of file {} with length {} in {} ms", filename, length, totalTime);

    return total;
}
 
Example 13
private static void method() throws Exception {
    ByteStreams.skipFully(null, 0L);
}
 
Example 14
private static ResourceContainer readResourceContainer(
    ZipFile zipFile, boolean includeFileContentsForValidation) throws IOException {
  List<ResourceTable> resourceTables = new ArrayList<>();
  List<CompiledFileWithData> compiledFiles = new ArrayList<>();

  Enumeration<? extends ZipEntry> resourceFiles = zipFile.entries();
  while (resourceFiles.hasMoreElements()) {
    ZipEntry resourceFile = resourceFiles.nextElement();
    String fileZipPath = resourceFile.getName();
    if (fileZipPath.endsWith(CompiledResources.ATTRIBUTES_FILE_EXTENSION)) {
      continue;
    }

    try (LittleEndianDataInputStream dataInputStream =
        new LittleEndianDataInputStream(zipFile.getInputStream(resourceFile))) {
      int magic = dataInputStream.readInt();
      verify(
          magic == AAPT_CONTAINER_MAGIC,
          "Unexpected magic number in %s: %s",
          resourceFile,
          magic);
      int version = dataInputStream.readInt();
      verify(
          version == AAPT_CONTAINER_VERSION,
          "Unexpected version number in %s: %s",
          resourceFile,
          version);

      int numberOfEntries = dataInputStream.readInt();
      for (int i = 0; i < numberOfEntries; i++) {
        int entryType = dataInputStream.readInt();
        verify(
            entryType == AAPT_CONTAINER_ENTRY_RES_TABLE
                || entryType == AAPT_CONTAINER_ENTRY_RES_FILE,
            "Unexpected entry type in %s: %s",
            resourceFile,
            entryType);

        if (entryType == AAPT_CONTAINER_ENTRY_RES_TABLE) {
          long size = dataInputStream.readLong();
          verify(size <= Integer.MAX_VALUE);
          byte[] tableBytes = readBytesAndSkipPadding(dataInputStream, (int) size);

          resourceTables.add(
              ResourceTable.parseFrom(tableBytes, ExtensionRegistry.getEmptyRegistry()));
        } else {
          // useless and wrong "size" data; see
          // https://android-review.googlesource.com/c/platform/frameworks/base/+/1161789
          ByteStreams.skipFully(dataInputStream, 8);
          int headerSize = dataInputStream.readInt();
          long payloadSize = dataInputStream.readLong();
          byte[] headerBytes = readBytesAndSkipPadding(dataInputStream, headerSize);

          CompiledFile compiledFile =
              CompiledFile.parseFrom(headerBytes, ExtensionRegistry.getEmptyRegistry());

          HashCode fingerprint;
          XmlNode rootXmlNode;
          if (includeFileContentsForValidation) {
            byte[] payloadBytes = readBytesAndSkipPadding(dataInputStream, (int) payloadSize);
            fingerprint = Hashing.goodFastHash(/*minimumBits=*/ 64).hashBytes(payloadBytes);
            rootXmlNode =
                compiledFile.getType() == Resources.FileReference.Type.PROTO_XML
                    ? XmlNode.parseFrom(payloadBytes, ExtensionRegistry.getEmptyRegistry())
                    : null;
          } else {
            ByteStreams.skipFully(
                dataInputStream, roundUpToNearest(payloadSize, AAPT_FLAT_FILE_ALIGNMENT));
            fingerprint = null;
            rootXmlNode = null;
          }

          compiledFiles.add(CompiledFileWithData.create(compiledFile, fingerprint, rootXmlNode));
        }
      }
    }
  }
  return ResourceContainer.create(resourceTables, compiledFiles);
}
 
Example 15
/**
 * Skip forward the given number of bytes in the compressed stream. Note
 * that after any seek/skip the memberNumbers may not reflect a member's
 * true ordinal position from the beginning of the stream. 
 * 
 * @param offset bytes to skip
 * @throws IOException
 * @throws EOFException 
 */
public void compressedSkip(long offset) throws IOException {
    ByteStreams.skipFully(in, offset);
    updateInnerMark();
    currentMemberStart = ((CountingInputStream)in).getCount(); 
    currentMemberEnd = -1; 
    startNewMember();
}
 
Example 16
Source Project: bboxdb   File: RoutingHeaderParser.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Decode a direct package header
 * @param inputStream
 * @return
 * @throws IOException
 */
private static RoutingHeader decodeDirectPackage(final InputStream inputStream) throws IOException {
	// Skip 5 unused bytes
	ByteStreams.skipFully(inputStream, 5);
	return new RoutingHeader(false);
}