/** * The number of bytes that have passed through this stream. * <p> * NOTE: From v1.3 this method throws an ArithmeticException if the * count is greater than can be expressed by an <code>int</code>. * See {@link #getByteCount()} for a method using a <code>long</code>. * * @return the number of bytes accumulated * @throws ArithmeticException if the byte count is too large */ public int getCount() { final long result = getByteCount(); if (result > Integer.MAX_VALUE) { throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); } return (int) result; }
/** * The number of bytes that have passed through this stream. * <p> * NOTE: From v1.3 this method throws an ArithmeticException if the * count is greater than can be expressed by an <code>int</code>. * See {@link #getByteCount()} for a method using a <code>long</code>. * * @return the number of bytes accumulated * @throws ArithmeticException if the byte count is too large */ public synchronized int getCount() { long result = getByteCount(); if (result > Integer.MAX_VALUE) { throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); } return (int) result; }
@Override public Void invoke(File dir, VirtualChannel channel) throws IOException, InterruptedException { try (InputStream in = archive.openStream()) { CountingInputStream cis = new CountingInputStream(in); try { if (archive.toExternalForm().endsWith(".zip")) { unzip(dir, cis); } else { readFromTar("input stream", dir, GZIP.extract(cis)); } } catch (IOException x) { throw new IOException(String.format("Failed to unpack %s (%d bytes read)", archive, cis.getByteCount()), x); } } return null; } }
long start = cis.getByteCount(); LOGGER.log(Level.FINEST, "handling frame with {0} of length {1}", new Object[] {op, framelen}); boolean handled = handle(op, framelen); if (handled) { long actuallyRead = cis.getByteCount() - start; if (actuallyRead != framelen) { throw new IOException("corrupt stream: expected to read " + framelen + " bytes from " + op + " but read " + actuallyRead);
} catch (IOException e) { throw new IOException(String.format("Failed to unpack %s (%d bytes read of total %d)", archive,cis.getByteCount(),con.getContentLength()),e);
@Test public void testLargeFiles_IO84() throws Exception { final long size = (long)Integer.MAX_VALUE + (long)1; final NullInputStream mock = new NullInputStream(size); final CountingInputStream cis = new CountingInputStream(mock); final OutputStream out = new NullOutputStream(); // Test integer methods IOUtils.copyLarge(cis, out); try { cis.getCount(); fail("Expected getCount() to throw an ArithmeticException"); } catch (final ArithmeticException ae) { // expected result } try { cis.resetCount(); fail("Expected resetCount() to throw an ArithmeticException"); } catch (final ArithmeticException ae) { // expected result } mock.close(); // Test long methods IOUtils.copyLarge(cis, out); assertEquals("getByteCount()", size, cis.getByteCount()); assertEquals("resetByteCount()", size, cis.resetByteCount()); }
private InputStream dataInputStream(long offset, long size) throws IOException { InputStream is; // use LZMA stream if the bundle is compressed if (bundle.header().compressed()) { // create initial input stream if required if (lzma == null) { lzma = lzmaInputStream(); } // recreate stream if the offset is behind long lzmaOffset = lzma.getByteCount(); if (lzmaOffset > offset) { lzma.close(); lzma = lzmaInputStream(); } // skip forward if required if (lzmaOffset < offset) { lzma.skip(offset - lzmaOffset); } is = lzma; } else { in.position(bundle.header().headerSize() + offset); is = in.stream(); } return new BoundedInputStream(is, size); }
long getByteCount() { return countingInputStream.getByteCount(); }
/** * The number of bytes that have passed through this stream. * <p> * NOTE: From v1.3 this method throws an ArithmeticException if the * count is greater than can be expressed by an <code>int</code>. * See {@link #getByteCount()} for a method using a <code>long</code>. * * @return the number of bytes accumulated * @throws ArithmeticException if the byte count is too large */ public synchronized int getCount() { long result = getByteCount(); if (result > Integer.MAX_VALUE) { throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); } return (int) result; }
/** * The number of bytes that have passed through this stream. * <p> * NOTE: From v1.3 this method throws an ArithmeticException if the * count is greater than can be expressed by an <code>int</code>. * See {@link #getByteCount()} for a method using a <code>long</code>. * * @return the number of bytes accumulated * @throws ArithmeticException if the byte count is too large */ public synchronized int getCount() { long result = getByteCount(); if (result > Integer.MAX_VALUE) { throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); } return (int) result; }
/** * The number of bytes that have passed through this stream. * <p> * NOTE: From v1.3 this method throws an ArithmeticException if the * count is greater than can be expressed by an <code>int</code>. * See {@link #getByteCount()} for a method using a <code>long</code>. * * @return the number of bytes accumulated * @throws ArithmeticException if the byte count is too large */ public int getCount() { final long result = getByteCount(); if (result > Integer.MAX_VALUE) { throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); } return (int) result; }
/** * The number of bytes that have passed through this stream. * <p> * NOTE: From v1.3 this method throws an ArithmeticException if the * count is greater than can be expressed by an <code>int</code>. * See {@link #getByteCount()} for a method using a <code>long</code>. * * @return the number of bytes accumulated * @throws ArithmeticException if the byte count is too large */ public int getCount() { final long result = getByteCount(); if (result > Integer.MAX_VALUE) { throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); } return (int) result; }
/** * The number of bytes that have passed through this stream. * <p> * NOTE: From v1.3 this method throws an ArithmeticException if the * count is greater than can be expressed by an <code>int</code>. * See {@link #getByteCount()} for a method using a <code>long</code>. * * @return the number of bytes accumulated * @throws ArithmeticException if the byte count is too large */ public int getCount() { long result = getByteCount(); if (result > Integer.MAX_VALUE) { throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); } return (int) result; }
/** * The number of bytes that have passed through this stream. * <p> * NOTE: From v1.3 this method throws an ArithmeticException if the * count is greater than can be expressed by an <code>int</code>. * See {@link #getByteCount()} for a method using a <code>long</code>. * * @return the number of bytes accumulated * @throws ArithmeticException if the byte count is too large */ public int getCount() { long result = getByteCount(); if (result > Integer.MAX_VALUE) { throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); } return (int) result; }
CountingInputStream counter = new CountingInputStream( dataInputStream ); dataInputStream = new DataInputStream( counter ); ... counter.getByteCount() //this returns num of bytes read from dataInputStream
@Override public Void invoke(File dir, VirtualChannel channel) throws IOException, InterruptedException { try (InputStream in = archive.openStream()) { CountingInputStream cis = new CountingInputStream(in); try { if (archive.toExternalForm().endsWith(".zip")) { unzip(dir, cis); } else { readFromTar("input stream", dir, GZIP.extract(cis)); } } catch (IOException x) { throw new IOException(String.format("Failed to unpack %s (%d bytes read)", archive, cis.getByteCount()), x); } } return null; } }
/** * Deserializes a <code>NodePropBundle</code> from a data input stream. * * @param id the node id for the new bundle * @return the bundle * @throws IOException if an I/O error occurs. */ public NodePropBundle readBundle(NodeId id) throws IOException { long start = cin.getByteCount(); NodePropBundle bundle = new NodePropBundle(id); if (version >= BundleBinding.VERSION_3) { readBundleNew(bundle); } else { readBundleOld(bundle); } bundle.setSize(cin.getByteCount() - start); return bundle; }
/** * Deserializes a <code>NodePropBundle</code> from a data input stream. * * @param id the node id for the new bundle * @return the bundle * @throws IOException if an I/O error occurs. */ public NodePropBundle readBundle(NodeId id) throws IOException { long start = cin.getByteCount(); NodePropBundle bundle = new NodePropBundle(id); if (version >= BundleBinding.VERSION_3) { readBundleNew(bundle); } else { readBundleOld(bundle); } bundle.setSize(cin.getByteCount() - start); return bundle; }
private Pair<String, Long> calculateFileHashAndSize(InputStream in) { try (CountingInputStream countingStream = new CountingInputStream(in)) { MessageDigest md = MessageDigest.getInstance("MD5"); try (InputStream digestInputStream = new DigestInputStream( countingStream, md)) { byte[] buffer = new byte[256]; while (digestInputStream.read(buffer) > 0) { // continue } } String hash = new String(Hex.encodeHex(md.digest())); return new ImmutablePair<>(hash, countingStream.getByteCount()); } catch (NoSuchAlgorithmException | IOException e) { throw new RuntimeException(e); } }
@Override public void run( ) { String output = ""; if ( totalBytes > 0 ) { final ProgressInfo progressInfo = new ProgressInfo( startTime, totalBytes, countingInputStream.getByteCount() ); output += progressInfo.debugOutput(); } else { output += "recordsImported=" + importLineCounter; } output += ", avgTransactionSize=" + transactionCalculator.getTransactionSize() + ", recordsPerMinute=" + eventRateMeter.readEventRate().setScale( 2, BigDecimal.ROUND_DOWN ); writeStringToOut( out, output ); } }, 30 * 1000, 30 * 1000 );