Package com.google.common.io

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


   * @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

  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

  }

  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

    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

    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

      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

    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

  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

        for (int i = 0; i < columnHandles.size(); i++) {
            ExampleColumnHandle columnHandle = columnHandles.get(i);
            fieldToColumnIndex[i] = columnHandle.getOrdinalPosition();
        }

        try (CountingInputStream input = new CountingInputStream(inputStreamSupplier.getInput())) {
            lines = asByteSource(inputStreamSupplier).asCharSource(StandardCharsets.UTF_8).readLines().iterator();
            totalBytes = input.getCount();
        }
        catch (IOException e) {
            throw Throwables.propagate(e);
        }
    }
View Full Code Here

TOP

Related Classes of com.google.common.io.CountingInputStream

Copyright © 2018 www.massapicom. 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.