/** * Creates a new CounterInputStream using the specified InputStream. A new {@link ByteCounter} will be created. * * @param in the underlying InputStream the data will be read from */ public CounterInputStream(InputStream in) { this.in = in; this.counter = new ByteCounter(); }
/** * Increases the byte counter by the number of bytes contained in the specified counter (as returned by its * {@link #getByteCount()} method) and resets its byte counter after (if specified). * * @param counter the Bytecounter to add to this one, and reset after (if specified). * @param resetAfter if true, the specified counter will be reset after its byte count has been added to this ByteCounter */ public synchronized void add(ByteCounter counter, boolean resetAfter) { // Hold a lock on the provided counter to make sure that it is not modified or accessed // while this operation is carried out synchronized(counter) { add(counter.getByteCount()); if(resetAfter) counter.reset(); } }
@Override public void write(byte b[]) throws IOException { out.write(b); counter.add(b.length); }
/** * Returns the total number of bytes that have been skipped by this job so far. * Bytes are skipped when file transfers are resumed. * * @return the total number of bytes that have been skipped by this job so far */ public long getTotalSkippedByteCount() { return totalSkippedByteCounter.getByteCount(); }
/** * Resets the number of bytes that have been processed in the current file. */ public void resetCurrentFileByteCounter() { currentFileByteCounter.reset(); }
@Override public void write(byte b[], int off, int len) throws IOException { out.write(b, off, len); counter.add(len); }
/** * Returns the number of bytes that have been processed in the current file. * * @return the number of bytes that have been processed in the current file */ public long getCurrentFileByteCount() { return currentFileByteCounter.getByteCount(); }
private String calculateChecksum(AbstractFile file) throws IOException, NoSuchAlgorithmException { currentFileByteCounter.reset(); InputStream in = setCurrentInputStream(file.getInputStream()); try { return AbstractFile.calculateChecksum(in, MessageDigest.getInstance(CHECKSUM_VERIFICATION_ALGORITHM)); } finally { closeCurrentInputStream(); } }
@Override public void write(int b) throws IOException { out.write(b); counter.add(1); }
/** * Returns the number of bytes that have been skipped in the current file. Bytes are skipped when file transfers * are resumed. * * @return the number of bytes that have been skipped in the current file */ public long getCurrentFileSkippedByteCount() { return currentFileSkippedByteCounter.getByteCount(); }
/** * Creates a new TransferFileJob. */ public TransferFileJob(ProgressDialog progressDialog, MainFrame mainFrame, FileSet files) { super(progressDialog, mainFrame, files); this.currentFileByteCounter = new ByteCounter(); this.currentFileSkippedByteCounter = new ByteCounter(); // Account the current file's byte counter in the total byte counter this.totalByteCounter = new ByteCounter(currentFileByteCounter); this.totalSkippedByteCounter = new ByteCounter(currentFileSkippedByteCounter); }
@Override public int read(byte b[], int off, int len) throws IOException { int nbRead = in.read(b, off, len); if(nbRead>0) counter.add(nbRead); return nbRead; }
/** * Returns the total number of bytes that have been processed by this job so far. * * @return the total number of bytes that have been processed by this job so far */ public long getTotalByteCount() { return totalByteCounter.getByteCount(); }
/** * Creates a new CounterOutputStream using the specified OutputStream. A new {@link ByteCounter} will be created. * * @param out the underlying OutputStream the data will be written to */ public CounterOutputStream(OutputStream out) { this.out = out; this.counter = new ByteCounter(); }
@Override public int read(byte b[]) throws IOException { int nbRead = in.read(b); if(nbRead>0) counter.add(nbRead); return nbRead; }
/** * Return the number of bytes which have been accounted for. */ public synchronized long getByteCount() { if(addedCounter!=null) return count + addedCounter.getByteCount(); return this.count; }
@Override public long skip(long n) throws IOException { long nbSkipped = in.skip(n); // Count skipped bytes only if this has been enabled if(countSkippedBytes && nbSkipped>0) counter.add(nbSkipped); return nbSkipped; }
@Override public int read() throws IOException { int i = in.read(); if(i>0) counter.add(1); return i; }
/** * Advances file index and resets current file's byte counters. This method should be called by subclasses * whenever the job starts processing a new file. */ @Override protected void nextFile(AbstractFile file) { totalByteCounter.add(currentFileByteCounter, true); totalSkippedByteCounter.add(currentFileSkippedByteCounter, true); // Reset some fields that need it currentFileSkipped = false; super.nextFile(file); }