Examples of CountingInputStream


Examples of com.fasterxml.storemate.store.util.CountingInputStream

       
    protected void _readAllWriteStreamingCompressed3(InputStream in0, OutputStream out,
            byte[] copyBuffer, StreamyBytesMemBuffer offHeap)
        throws IOException
    {
        final CountingInputStream counter = new CountingInputStream(in0);
       
        InputStream in = Compressors.uncompressingStream(counter, _compression);

        // First: anything to skip (only the case for range requests)?
        if (_dataOffset > 0L) {
            long skipped = 0L;
            long toSkip = _dataOffset;

            final long start = (_diagnostics == null) ? 0L : _timeMaster.nanosForDiagnostics();
           
            while (toSkip > 0) {
                long count = in.skip(toSkip);
                if (count <= 0L) { // should not occur really...
                    throw new IOException("Failed to skip more than "+skipped+" bytes (needed to skip "+_dataOffset+")");
                }
                skipped += count;
                toSkip -= count;
            }
            if (_diagnostics != null) {
                // assume here skipping is "free" (i.e. no bytes read)
                _diagnostics.addFileReadAccess(start, _timeMaster, 0L);
            }
        }
        // Second: output the whole thing, or just subset?
        // TODO: buffer
        if (_dataLength < 0) { // all of it
            long prevCount = 0L;
            while (true) {
                final long start = _timeMaster.nanosForDiagnostics();
                int count = in.read(copyBuffer);
                if (_diagnostics != null) {
                    long newCount = counter.readCount();
                    _diagnostics.addFileReadAccess(start, _timeMaster, newCount-prevCount);
                    prevCount = newCount;
                }
               
                if (count <= 0) {
                    break;
                }
                final long outputStart = _timeMaster.nanosForDiagnostics();
                out.write(copyBuffer, 0, count);
                if (_diagnostics != null) {
                    _diagnostics.addResponseWriteTime(outputStart, _timeMaster);
                }
            }
            return;
        }
        // Just some of it
        long left = _dataLength;

        // TODO: buffer
        long prevCount = 0L;
        while (left > 0) {
            final long start = (_diagnostics == null) ? 0L : _timeMaster.nanosForDiagnostics();
            int count = in.read(copyBuffer, 0, (int) Math.min(copyBuffer.length, left));
            if (_diagnostics != null) {
                long newCount = counter.readCount();
                _diagnostics.addFileReadAccess(start, _timeMaster, newCount-prevCount);
                prevCount = newCount;
            }
            if (count <= 0) {
                break;
View Full Code Here

Examples of com.google.common.io.CountingInputStream

    @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
    public InputStreamSliceInput(InputStream inputStream)
    {
        pushbackInputStream = new PushbackInputStream(inputStream);
        countingInputStream = new CountingInputStream(pushbackInputStream);
        dataInputStream = new LittleEndianDataInputStream(countingInputStream);
    }
View Full Code Here

Examples of com.google.common.io.CountingInputStream

   * @return Size of the data
   * @throws IOException
   */
  public static long getInputStreamSize(InputStream in) throws IOException
  {
    CountingInputStream cin = new CountingInputStream(in);
    ByteStreams.copy(cin, new NullOutputStream());
    return cin.getCount();
  }
View Full Code Here

Examples of com.google.common.io.CountingInputStream

  public void testExceptionHandling() throws Exception {
    // Create an input stream than has 2 records in the first 9 bytes and exception while the 3rd
    // record is read
    byte[] content = new byte[] {1, 2, 3, 4, 0, 6, 7, 8, 0, 10, 11, 12};
    try (CountingInputStream countingInputStream =
        new CountingInputStream(new ExceptionThrowingInputStream(
            new BufferedInputStream(new NonResetableByteArrayInputStream(content)), 11))) {
      LineInputStream iterator =
          new TestLineInputReader(countingInputStream, content.length, false, (byte) 0);
      byte[] next = iterator.next();
      assertNotNull(next);
View Full Code Here

Examples of com.google.common.io.CountingInputStream

  }

  private void test(long start, long end, boolean skipFirstTerminator, int expectedIndexStart,
      int expectedIndexEnd) throws IOException {
    input.skip(start);
    CountingInputStream countingInputStream = new CountingInputStream(input);
    LineInputStream iterator =
        new TestLineInputReader(countingInputStream, end - start, skipFirstTerminator, (byte) -1);
    int totalCount = 0;
    try {
      while (true) {
        byte[] record = iterator.next();
        assertEquals(content.get(totalCount + expectedIndexStart), new String(record));
        assertEquals(byteContentOffsets.get(totalCount + expectedIndexStart).longValue(),
            countingInputStream.getCount() - record.length - 1 + start);
        totalCount++;
      }
    } catch (NoSuchElementException e) {
      // Used as break
    }
View Full Code Here

Examples of com.google.common.io.CountingInputStream

    private boolean skipFirstTerminator;

    TestLineInputReader(InputStream input, long length, boolean skipFirstTerminator,
        byte separator) {
      super(new CountingInputStream(input), length, separator);
      this.skipFirstTerminator = skipFirstTerminator;
    }
View Full Code Here

Examples of com.google.common.io.CountingInputStream

    Codec.Encoder encoder = codec.getEncoder(dos);
    encoder.flush();
    dos.close();
    long offset = cos.getCount();
    assertEquals(0, offset);
    CountingInputStream cis =
      new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
    DataInputStream dis = new DataInputStream(cis);
    Codec.Decoder decoder = codec.getDecoder(dis);
    assertFalse(decoder.advance());
    dis.close();
    assertEquals(0, cis.getCount());
  }
View Full Code Here

Examples of com.google.common.io.CountingInputStream

      new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("q"), Bytes.toBytes("v"));
    encoder.write(kv);
    encoder.flush();
    dos.close();
    long offset = cos.getCount();
    CountingInputStream cis =
      new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
    DataInputStream dis = new DataInputStream(cis);
    Codec.Decoder decoder = codec.getDecoder(dis);
    assertTrue(decoder.advance()); // First read should pull in the KV
    // Second read should trip over the end-of-stream marker and return false
    assertFalse(decoder.advance());
    dis.close();
    assertEquals(offset, cis.getCount());
  }
View Full Code Here

Examples of com.google.common.io.CountingInputStream

    encoder.write(kv2);
    encoder.write(kv3);
    encoder.flush();
    dos.close();
    long offset = cos.getCount();
    CountingInputStream cis =
      new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
    DataInputStream dis = new DataInputStream(cis);
    Codec.Decoder decoder = codec.getDecoder(dis);
    assertTrue(decoder.advance());
    Cell c = decoder.current();
    assertTrue(CellComparator.equals(c, kv1));
    assertTrue(decoder.advance());
    c = decoder.current();
    assertTrue(CellComparator.equals(c, kv2));
    assertTrue(decoder.advance());
    c = decoder.current();
    assertTrue(CellComparator.equals(c, kv3));
    assertFalse(decoder.advance());
    dis.close();
    assertEquals(offset, cis.getCount());
  }
View Full Code Here

Examples of com.google.common.io.CountingInputStream

  private final int separator;
  private final CountingInputStream in;
  private final long lengthToRead;

  LineInputStream(InputStream in, long lengthToRead, byte separator) {
    this.in = new CountingInputStream(in);
    this.lengthToRead = lengthToRead;
    this.separator = (separator & 0xff);
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.