Java Code Examples for com.google.common.io.CountingInputStream#getCount()

The following examples show how to use com.google.common.io.CountingInputStream#getCount() . 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 File: WebpAutoFix.java    From size-analyzer with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the provided bytes into a new file path, based on the initial image's filepath, but with
 * a .webp extension.
 */
public void apply() {
  Path newFilePath =
      filePath.resolveSibling(MoreFiles.getNameWithoutExtension(filePath) + ".webp");
  try (InputStream inputStream = new FileInputStream(new File(filePath.toString()))) {
    CountingInputStream countingStream = new CountingInputStream(inputStream);
    BufferedImage bufferedImage = WebpSuggester.safelyParseImage(countingStream);

    long oldSize = countingStream.getCount();
    byte[] webpBytes = webpConverter.encodeLosslessWebp(bufferedImage);
    Files.write(newFilePath, webpBytes);
    Files.delete(filePath);
  } catch (IOException | ImageReadException e) {
    throw new RuntimeException(e);
  }
}
 
Example 2
Source File: CStoreSender.java    From healthcare-dicom-dicomweb-adapter with Apache License 2.0 6 votes vote down vote up
@Override
public long cstore(AetDictionary.Aet target,
    String studyUid,
    String seriesUid,
    String sopInstanceUid,
    String sopClassUid)
    throws IDicomWebClient.DicomWebException, IOException, InterruptedException {
  String wadoUri =
      String.format("studies/%s/series/%s/instances/%s", studyUid, seriesUid, sopInstanceUid);
  log.info("CStore wadoUri : " + wadoUri);

  InputStream responseStream = dicomWebClient.wadoRs(wadoUri);

  CountingInputStream countingStream = new CountingInputStream(responseStream);
  DicomClient.connectAndCstore(sopClassUid, sopInstanceUid, countingStream,
      applicationEntity, target.getName(), target.getHost(), target.getPort());
  return countingStream.getCount();
}
 
Example 3
Source File: DataBlockHeaderImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static DataBlockHeader fromStream(InputStream stream) throws IOException {
    CountingInputStream countingStream = new CountingInputStream(stream);
    DataInputStream dis = new DataInputStream(countingStream);
    int magic = dis.readInt();
    if (magic != MAGIC_WORD) {
        throw new IOException("Data block header magic word not match. read: " + magic + " expected: " + MAGIC_WORD);
    }

    long headerLen = dis.readLong();
    long blockLen = dis.readLong();
    long firstEntryId = dis.readLong();
    long toSkip = headerLen - countingStream.getCount();
    if (dis.skip(toSkip) != toSkip) {
        throw new EOFException("Header was too small");
    }

    return new DataBlockHeaderImpl(headerLen, blockLen, firstEntryId);
}
 
Example 4
Source File: BlockCompressedRecordFile.java    From lsmtree with Apache License 2.0 4 votes vote down vote up
@Override
public Either<IOException, Option<BlockCacheEntry>> load(final Long blockAddress) {
    final Either<IOException, ? extends RandomAccessDataInput> input = inputSupplier.get();
    RandomAccessDataInput in = null;
    try {
        in = input.get();
        in.seek(blockAddress);
        final int blockLength = in.readInt();
        if (blockLength == Integer.MAX_VALUE) return Either.Right.of(Option.<BlockCacheEntry>none());
        if (blockLength < 4) throw new IOException("block length for block at address "+blockAddress+" in file "+file+" is "+blockLength+" which is less than 4. this is not possible. this address is probably no good.");
        final long blockEnd = (((blockAddress+8+blockLength-1)>>>padBits)+1)<<padBits;
        final long maxAddress = in.length() - 12;
        if (blockLength < 0 || blockEnd > maxAddress) throw new IOException("block address "+blockAddress+" in file "+file+" is no good, length is "+blockLength+" and end of data is "+maxAddress);
        final int checksum = in.readInt();
        if (maxChunkSize > 0) {
            final int chunkLength = in.readInt();
            if (chunkLength > maxChunkSize) throw new IOException("first chunk length ("+chunkLength+") for block at address "+blockAddress+" in file "+file+" is greater than "+maxChunkSize+". while this may be correct it is extremely unlikely and this is probably a bad address.");
            in.seek(blockAddress + 8);
        }
        final byte[] compressedBytes = new byte[blockLength];
        in.readFully(compressedBytes);
        final int padLength = (int)(pad-in.position()%pad);
        if (padLength != pad) {
            in.seek(in.position()+padLength);
        }

        final CheckedInputStream checksumStream = new CheckedInputStream(new ByteArrayInputStream(compressedBytes), new Adler32());
        Decompressor decompressor = decompressorPool.poll();
        if (decompressor == null) {
            decompressor = codec.createDecompressor();
        }
        decompressor.reset();
        final InputStream decompressed = new BlockDecompressorStream(checksumStream, decompressor, blockSize*2);
        final ByteArrayOutputStream decompressedByteStream = new ByteArrayOutputStream(blockSize*2);
        ByteStreams.copy(decompressed, decompressedByteStream);
        decompressed.close();
        decompressedByteStream.close();
        decompressorPool.offer(decompressor);
        if (((int)checksumStream.getChecksum().getValue()) != checksum) throw new IOException("checksum for chunk at block address "+blockAddress+" does not match data");
        final byte[] decompressedBytes = decompressedByteStream.toByteArray();
        final CountingInputStream counter = new CountingInputStream(new ByteArrayInputStream(decompressedBytes));
        final DataInputStream dataInput = new DataInputStream(counter);
        final int numRecords = dataInput.readInt();
        final int[] recordOffsets = new int[numRecords+1];
        int sum = 0;
        for (int i = 0; i < numRecords; i++) {
            recordOffsets[i] = sum;
            final int delta = VIntUtils.readVInt((InputStream)dataInput);
            sum+=delta;
        }
        recordOffsets[numRecords] = sum;
        final int count = (int)counter.getCount();
        dataInput.close();
        final byte[] block = new byte[decompressedBytes.length-count];
        System.arraycopy(decompressedBytes, count, block, 0, block.length);
        return Either.Right.of(Option.some(new BlockCacheEntry(recordOffsets, new HeapMemory(block, ByteOrder.BIG_ENDIAN), in.position())));
    } catch (IOException e) {
        log.info("error reading block at address "+blockAddress+" in file "+file, e);
        return Either.Left.of(e);
    } finally {
        Closeables2.closeQuietly(in, log);
    }
}