VerifyingFile.java

Index Score
com.limegroup.gnutella.downloader
FrostWire

View: Reasons, Metrics, Source Code

These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.

MetricDescription
JAVA0034JAVA0034 Missing braces in if statement
JAVA0143JAVA0143 Synchronized method
DECL_COMMENTSComments in declarations
COMPARISONSNumber of comparison operators
RETURNSNumber of return points from functions
JAVA0108JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
SIZESize of the file in bytes
CYCLOMATICCyclomatic complexity
DOC_COMMENTNumber of javadoc comment lines
FUNCTIONSNumber of function declarations
JAVA0115JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
INTERFACE_COMPLEXITYInterface complexity
JAVA0110JAVA0110 Incorrect javadoc: no @return tag
LINESNumber of lines in the source file
COMMENTSComment lines
OPERATORSNumber of operators
ELOCEffective lines of code
EXEC_COMMENTSComments in executable code
PROGRAM_LENGTHHalstead program length
BLOCKSNumber of blocks
LINE_COMMENTNumber of line comments
EXITSProcedure exits
LOCLines of code
LOGICAL_LINESNumber of statements
JAVA0126JAVA0126 Method declares unchecked exception in throws
OPERANDSNumber of operands
PROGRAM_VOCABHalstead program vocabulary
JAVA0109JAVA0109 Incorrect javadoc: no parameter 'parameter'
UNIQUE_OPERANDSNumber of unique operands
JAVA0117JAVA0117 Missing javadoc: method 'method'
UNIQUE_OPERATORSNumber of unique operators
WHITESPACENumber of whitespace lines
PARAMSNumber of formal parameter declarations
JAVA0116JAVA0116 Missing javadoc: field 'field'
JAVA0096JAVA0096 Field in nested class hides outer field
JAVA0075JAVA0075 Method parameter hides field
JAVA0008JAVA0008 Empty catch block
JAVA0128JAVA0128 Public constructor in non-public class
NEST_DEPTHMaximum nesting depth
PROGRAM_VOLUMEHalstead program volume
JAVA0136JAVA0136 N methods defined in class (maximum: M)
JAVA0100JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0145JAVA0145 Tab character used in source file
package com.limegroup.gnutella.downloader; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.util.ArrayList; import java.util.List; import java.util.NoSuchElementException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.limewire.collection.IntervalSet; import org.limewire.collection.MultiIterable; import org.limewire.collection.Range; import org.limewire.io.DiskException; import org.limewire.util.FileUtils; import com.google.inject.Provider; import com.limegroup.gnutella.tigertree.HashTree; /** * A control point for all access to the file being downloaded to, also does * on-the-fly verification. * * Every region of the file can be in one of five states, and can move from one * state to another only in the following order: * * 1. available for download * 2. currently being downloaded * 3. waiting to be written. * 4. written (and immediately into, if possible..) * 5. verified, or if it doesn't verify back to * 1. available for download * * In order to maintain these constraints, the only possible operations are: * Lease a block - find an area which is available for download and claim it * Write a block - report that the specified block has been read from the network. * Release a block - report that the specified block will not be downloaded. */ public class VerifyingFile { static final Log LOG = LogFactory.getLog(VerifyingFile.class); /** * If the number of corrupted data gets over this, assume the file will not be recovered */ static final float MAX_CORRUPTION = 0.9f; /** The default chunk size - if we don't have a tree we request chunks this big. * * This is a power of two in order to minimize the number of small partial chunk * downloads that will be required after we learn the chunk size from the TigerTree, * since the chunk size will always be a power of two. */ static final int DEFAULT_CHUNK_SIZE = 131072; //128 KB = 128 * 1024 B = 131072 bytes /** How much to verify at a time */ private static final int VERIFYABLE_CHUNK = 64 * 1024; // 64k /** * The file we're writing to / reading from. */ private volatile RandomAccessFile fos; /** * Whether this file is open for writing */ private volatile boolean isOpen; /** * The eventual completed size of the file we're writing. */ private final long completedSize; /** * How much data did we lose due to corruption */ private long lostSize; /** * The VerifyingFile uses an IntervalSet to keep track of the blocks written * to disk and find out which blocks to check before writing to disk */ private final IntervalSet verifiedBlocks; /** * Ranges that are currently being written by the ManagedDownloader. * * Replaces the IntervalSet of needed ranges previously stored in the * ManagedDownloader but which could get out of sync with the verifiedBlocks * IntervalSet and is therefore replaced by a more failsafe implementation. */ private IntervalSet leasedBlocks; /** * Ranges that are currently written to disk, but do not form complete chunks * so cannot be verified by the HashTree. */ private IntervalSet partialBlocks; /** * Ranges that are discarded (but verification was attempted) */ private IntervalSet savedCorruptBlocks; /** * Ranges which are pending writing & verification. */ private IntervalSet pendingBlocks; /** * Decides which blocks to start downloading next. */ private SelectionStrategy blockChooser = null; /** * The hashtree we use to verify chunks, if any */ private HashTree hashTree; /** * The expected TigerTree root (null if we'll accept any). */ private String expectedHashRoot; /** * Whether someone is currently requesting the hash tree */ private boolean hashTreeRequested; /** * Whether we are actually verifying chunks */ private boolean discardBad = true; /** * The IOException, if any, we got while writing. */ private IOException storedException; /** * The size of the file on disk if we're going to scan for completed * blocks. Otherwise -1. */ private long existingFileSize = -1; /** * Additional counter to keep track of scheduled chunks in each single file. * Needed to prevent premature closing of underlying RandomAccessFile. */ private int chunksScheduledPerFile = 0; /** * Holds the iterable for all blocks, is lazily instantiated when * needed for the first time. */ private MultiIterable<Range> allBlocksIterable = null; /** The controller for doing disk reads/writes. */ private final Provider<DiskController> diskController; /** * Constructs a new VerifyingFile for the specified size. * If checkOverlap is true, will scan for overlap corruption. */ VerifyingFile(long completedSize, Provider<DiskController> diskController) { this.completedSize = completedSize; verifiedBlocks = new IntervalSet(); leasedBlocks = new IntervalSet(); pendingBlocks = new IntervalSet(); partialBlocks = new IntervalSet(); savedCorruptBlocks = new IntervalSet(); this.diskController = diskController; } /** * Opens this VerifyingFile for writing. * MUST be called before anything else. * * If there is no completion size, this fails. */ public void open(File file) throws IOException { if(completedSize == -1) throw new IllegalStateException("cannot open for unknown size."); // Ensure that the directory this file is in exists & is writeable. File parentFile = file.getParentFile(); if( parentFile != null ) { parentFile.mkdirs(); if(!parentFile.exists()) throw new IOException("permission denied"); FileUtils.setWriteable(parentFile); } FileUtils.setWriteable(file); this.fos = new RandomAccessFile(file,"rw"); SelectionStrategy myStrategy = SelectionStrategyFactory.getStrategyFor( FileUtils.getFileExtension(file), completedSize); synchronized(this) { storedException = null; // Figure out which SelectionStrategy to use blockChooser = myStrategy; isOpen = true; } } /** * used to add blocks direcly. Blocks added this way are marked * partial. */ public synchronized void addInterval(Range interval) { //delegates to underlying IntervalSet partialBlocks.add(interval); } public void registerWriteCallback(WriteRequest request, WriteCallback callback) { request.startScheduling(); if (writeBlockImpl(request)) { callback.writeScheduled(); } else { diskController.get().addDelayedWrite(new VerifyingFileDelayedWrite(request, callback, this)); } } /** * Writes bytes to the underlying file. * * @param currPos the position in the file to write to * @param start the start position in the buffer to read from * @param length the length of data in the buffer to use * @param buf the buffer of data * @return null if this scheduled a write or wasn't open, otherwise Object * that can be used to schedule a write. */ public boolean writeBlock(WriteRequest request) { if(!validateState(request)) return true; request.startProcessing(); updateState(request.in); boolean canWrite = diskController.get().canWriteNow(); if(canWrite) return writeBlockImpl(request); else // do not try to write if something else is waiting. return false; } /** * Writes bytes to the underlying file. * * @param currPos the position in the file to write to * @param start the start position in the buffer to read from * @param length the length of data in the buffer to use * @param buf the buffer of data * @return true if this scheduled a write or wasn't open, false if it couldn't. */ private boolean writeBlockImpl(WriteRequest request) { if (LOG.isTraceEnabled()) LOG.trace("trying to write block at offset " + request.currPos + " with size " + request.length); if (!validateState(request)) return true; byte[] temp = diskController.get().getWriteChunk(); if(temp == null) return false; request.setDone(); assert temp.length >= request.length : "bad length: " + request.length + ", needed <= " + temp.length; System.arraycopy(request.buf, request.start, temp, 0, request.length); synchronized (this) { chunksScheduledPerFile++; } diskController.get().addDiskJob(new ChunkHandler(temp, request.in)); return true; } private synchronized void updateState(Range intvl) { /// some stuff to help debugging /// assert leasedBlocks.contains(intvl) : "trying to write an interval "+intvl+" that wasn't leased.\n"+dumpState(); assert !(partialBlocks.contains(intvl) || savedCorruptBlocks.contains(intvl) || pendingBlocks.contains(intvl)) : "trying to write an interval "+intvl+ " that was already written"+dumpState(); leasedBlocks.delete(intvl); // add only the ranges that aren't already verified into pending. // this is necessary because full-scanning may have added unforeseen // blocks into verified. if(verifiedBlocks.containsAny(intvl)) { // technically the code in this if block would work for all cases, // but it's kind of inefficient to do lots of work all the time, // when the if is only necessary after a full-scan. IntervalSet remaining = new IntervalSet(); remaining.add(intvl); remaining.delete(verifiedBlocks); pendingBlocks.add(remaining); } else { pendingBlocks.add(intvl); } } /** * @return false if this request should return immediately */ private boolean validateState(WriteRequest request) { if(request.length == 0) //nothing to write? return return false; if(fos == null) throw new IllegalStateException("no fos!"); if (!isOpen()) return false; return true; } /** * Set whether or not we're going to do a one-time full scan * on this file for verified blocks once we find a * hash tree. * * @param scan * @param length */ public void setScanForExistingBlocks(boolean scan, long length) throws IOException { if(scan && length != 0) { if (length > completedSize) throw new IOException("invalid completed size or length"); existingFileSize = length; } else { existingFileSize = -1; } } public synchronized String dumpState() { return "verified:"+verifiedBlocks+"\npartial:"+partialBlocks+ "\ndiscarded:"+savedCorruptBlocks+ "\npending:"+pendingBlocks+"\nleased:"+leasedBlocks; } /** * Returns a block of data that needs to be written. * * This method will not break up contiguous chunks into smaller chunks. */ public Range leaseWhite() throws NoSuchElementException { return leaseWhiteHelper(null, completedSize); } /** * Returns a block of data that needs to be written. * The returned block will NEVER be larger than chunkSize. */ public Range leaseWhite(long chunkSize) throws NoSuchElementException { return leaseWhiteHelper(null, chunkSize); } /** * Returns a block of data that needs to be written * and is within the specified set of ranges. * The parameter IntervalSet is modified */ public Range leaseWhite(IntervalSet ranges) throws NoSuchElementException { return leaseWhiteHelper(ranges, DEFAULT_CHUNK_SIZE); } /** * Returns a block of data that needs to be written * and is within the specified set of ranges. * The returned block will NEVER be larger than chunkSize. */ public Range leaseWhite(IntervalSet ranges, long chunkSize) throws NoSuchElementException { return leaseWhiteHelper(ranges, chunkSize); } /** * Removes the specified internal from the set of leased intervals. */ public synchronized void releaseBlock(Range in) { assert leasedBlocks.contains(in) : "trying to release an interval " + in + " that wasn't leased " + dumpState(); if(LOG.isInfoEnabled()) LOG.info("Releasing interval: " + in+" state "+dumpState()); leasedBlocks.delete(in); } /** * Returns all verified blocks with an Iterator. */ public synchronized Iterable<Range> getVerifiedBlocks() { return verifiedBlocks; } /** * @return the verified IntervalSet. */ public synchronized IntervalSet getVerifiedIntervalSet() { return verifiedBlocks; } /** * @return the partial IntervalSet. */ public synchronized IntervalSet getPartialIntervalSet() { return partialBlocks; } /** * @return byte-packed representation of the verified blocks. */ public synchronized IntervalSet.ByteIntervals toBytes() { return verifiedBlocks.toBytes(); } public String toString() { return dumpState(); } /** * @return List of Intervals that should be serialized. Excludes pending intervals. */ public synchronized List<Range> getSerializableBlocks() { IntervalSet ret = new IntervalSet(); for(Range next : new MultiIterable<Range>(verifiedBlocks, partialBlocks, savedCorruptBlocks)) ret.add(next); return ret.getAllIntervalsAsList(); } /** * While iterating over the result a lock to the verifying file should * be held to ensure the interval lists are not modified elsewhere. * @return all downloaded blocks as list */ public synchronized Iterable<Range> getBlocks() { if (allBlocksIterable == null) { allBlocksIterable = new MultiIterable<Range>(verifiedBlocks, partialBlocks, savedCorruptBlocks, pendingBlocks); } return allBlocksIterable; } /** * @return the offset of the first contiguous downloaded * region of the file */ public synchronized long getOffsetForPreview() { // if we have any savedCorrupt intervals, we need to lump everything // together to get the longest continuous interval if (!savedCorruptBlocks.isEmpty()) { IntervalSet lump = new IntervalSet(); lump.add(savedCorruptBlocks); lump.add(verifiedBlocks); lump.add(partialBlocks); if (lump.getFirst().getLow() != 0) return 0; return lump.getFirst().getHigh(); } /* * we know that the intervals are contiguous, mutually exclusive * and sorted. Also, we know that a partial interval must be smaller * than a full chunk. So all we need to look at are the * first verified & first partial intervals. */ IntervalSet firsts = new IntervalSet(); if (!verifiedBlocks.isEmpty()) firsts.add(verifiedBlocks.getFirst()); if (!partialBlocks.isEmpty()) firsts.add(partialBlocks.getFirst()); if (firsts.isEmpty() || firsts.getFirst().getLow() != 0) return 0; return firsts.getFirst().getHigh(); } /** * Returns all verified blocks as a List. */ public synchronized List<Range> getVerifiedBlocksAsList() { return verifiedBlocks.getAllIntervalsAsList(); } /** * Returns the total number of bytes written to disk. */ public synchronized long getBlockSize() { return verifiedBlocks.getSize() + partialBlocks.getSize() + savedCorruptBlocks.getSize() + pendingBlocks.getSize(); } public synchronized long getPendingSize() { return pendingBlocks.getSize(); } /** * Returns the total number of verified bytes written to disk. */ public synchronized long getVerifiedBlockSize() { return verifiedBlocks.getSize(); } /** * @return how much data was lost due to corruption */ public synchronized long getAmountLost() { return lostSize; } /** * Determines if all blocks have been written to disk and verified */ public synchronized boolean isComplete() { if (hashTree != null) return verifiedBlocks.getSize() + savedCorruptBlocks.getSize() == completedSize; else { return verifiedBlocks.getSize() + savedCorruptBlocks.getSize() + partialBlocks.getSize()== completedSize; } } /** Returns all missing pieces. */ public synchronized String listMissingPieces() { IntervalSet all = new IntervalSet(); all.add(Range.createRange(0, completedSize-1)); all.delete(verifiedBlocks); all.delete(savedCorruptBlocks); if(hashTree == null) all.delete(partialBlocks); return all.toString() + ", pending: " + pendingBlocks.toString() + ", has tree? " + (hashTree != null) + ", verified: " + verifiedBlocks + ", savedCorrupt: " + savedCorruptBlocks + ", partial: " + partialBlocks; } /** * If the last remaining chunks of the file are currently pending writing & verification, * wait until it finishes. */ public synchronized void waitForPendingIfNeeded() throws InterruptedException, DiskException { if(storedException != null) throw new DiskException(storedException); while (!isComplete() && getBlockSize() == completedSize) { if(storedException != null) throw new DiskException(storedException); if (LOG.isInfoEnabled()) LOG.info("waiting for a pending chunk to verify or write.."); wait(); } } /** * Waits until all pending write requests have been completed. */ public synchronized void waitForPending(int timeout) throws InterruptedException, DiskException { if(storedException != null) throw new DiskException(storedException); synchronized (this) { while (chunksScheduledPerFile > 0) { this.wait(timeout); } } } /** * @return whether we think we will not be able to complete this file */ public synchronized boolean isHopeless() { return lostSize >= MAX_CORRUPTION * completedSize; } public boolean isOpen() { return isOpen; } /** * Determines if there are any blocks that are not assigned * or written. */ public synchronized long hasFreeBlocksToAssign() { return completedSize - (verifiedBlocks.getSize() + leasedBlocks.getSize() + partialBlocks.getSize() + savedCorruptBlocks.getSize() + pendingBlocks.getSize()); } /** * Closes the file output stream. */ public void close() { isOpen = false; if(fos==null) return; try { synchronized (this) { while (chunksScheduledPerFile > 0) { try { wait(); } catch (InterruptedException ie) { } } } fos.close(); } catch (IOException ioe) {} } /////////////////////////private helpers////////////////////////////// /** * Determines which interval should be assigned next, leases that interval, * and returns that interval. * * @param availableRanges if ranges is non-null, the return value will be a chosen * from within availableRanges * @param chunkSize if greater than zero, the return value will end one byte before * a chunkSize boundary and will be at most chunkSize bytes large. * @return the leased interval */ private synchronized Range leaseWhiteHelper(IntervalSet availableBytes, long chunkSize) throws NoSuchElementException { if (LOG.isDebugEnabled()) LOG.debug("leasing white, state:\n"+dumpState()); // If ranges is null, make ranges represent the entire file if (availableBytes == null) availableBytes = IntervalSet.createSingletonSet(0, completedSize-1); // Figure out which blocks we still need to assign IntervalSet neededBytes = IntervalSet.createSingletonSet(0, completedSize-1); neededBytes.delete(verifiedBlocks); neededBytes.delete(leasedBlocks); neededBytes.delete(partialBlocks); neededBytes.delete(savedCorruptBlocks); neededBytes.delete(pendingBlocks); if (LOG.isDebugEnabled()) LOG.debug("needed bytes: "+neededBytes); // Calculate the intersection of neededBytes and availableBytes availableBytes.delete(neededBytes.invert(completedSize)); Range ret = blockChooser.pickAssignment(availableBytes, neededBytes, chunkSize); leaseBlock(ret); if (LOG.isDebugEnabled()) LOG.debug("leasing white interval "+ret+"\nof available intervals "+ neededBytes); return ret; } /** * Leases the specified interval. */ private synchronized void leaseBlock(Range in) { //if(LOG.isDebugEnabled()) //LOG.debug("Obtaining interval: " + in); leasedBlocks.add(in); } /** * Sets the expected hash tree root. If non-null, we'll only accept * hash trees whose root hash matches this. */ public synchronized void setExpectedHashTreeRoot(String root) { expectedHashRoot = root; } public synchronized HashTree getHashTree() { return hashTree; } /** * sets the HashTree the current download will use. That affects whether * we do overlap checking. * @return true if the new tree was accepted. */ public synchronized boolean setHashTree(HashTree tree) { // doesn't match our expected tree, bail. if (expectedHashRoot != null && tree != null && !tree.getRootHash().equalsIgnoreCase(expectedHashRoot)) return false; // if the tree is of incorrect size, ignore it if (tree != null && tree.getFileSize() != completedSize) return false; HashTree previous = hashTree; // if the roots are different if (previous != null && tree != null && !previous.getRootHash().equals(tree.getRootHash())){ // and we have verified at least two default chunks, don't change if (verifiedBlocks.getSize() > 2 * DEFAULT_CHUNK_SIZE) return false; // else trigger verification if (verifiedBlocks.getSize() > 0) { partialBlocks.add(verifiedBlocks); verifiedBlocks.clear(); diskController.get().addDiskJobWithoutChunk(new EmptyVerifier(existingFileSize)); } } hashTree = tree; // if we did not have a tree previously // and we do have a hash tree now // and either we want to scan the whole file once // or we don't have pending blocks but do have partial blocks, // trigger verification. if (previous == null && tree != null && (existingFileSize != -1 || (pendingBlocks.getSize() == 0 && partialBlocks.getSize() > 0)) ) { diskController.get().addDiskJobWithoutChunk(new EmptyVerifier(existingFileSize)); existingFileSize = -1; } return true; } /** * flags that someone is currently requesting the tree */ public synchronized void setHashTreeRequested(boolean yes) { hashTreeRequested = yes; } public synchronized boolean isHashTreeRequested() { return hashTreeRequested; } public synchronized void setDiscardUnverified(boolean yes) { discardBad = yes; } public synchronized int getChunkSize() { return hashTree == null ? DEFAULT_CHUNK_SIZE : hashTree.getNodeSize(); } /** * Stub for calling verifyChunks(-1). */ private void verifyChunks() { verifyChunks(-1); } /** * Schedules those chunks that can be verified against the hash tree * for verification. */ private void verifyChunks(long existingFileSize) { boolean fullScan = existingFileSize != -1; HashTree tree = getHashTree(); // capture the tree. // if we have a tree, see if there is a completed chunk in the partial list if(tree != null) { for(Range i : findVerifyableBlocks(existingFileSize)) { byte[] tmp = diskController.get().getPowerOf2Chunk(Math.min(VERIFYABLE_CHUNK,tree.getNodeSize())); boolean good = !tree.isCorrupt(i, fos, tmp); synchronized (this) { partialBlocks.delete(i); if (good) verifiedBlocks.add(i); else { if (!fullScan) { if (!discardBad) savedCorruptBlocks.add(i); lostSize += (i.getHigh() - i.getLow() + 1); } } } } } } /** * iterates through the pending blocks and checks if the recent write has created * some (verifiable) full chunks. Its not possible to verify more than two chunks * per method call unless the downloader is being deserialized from disk */ private synchronized List<Range> findVerifyableBlocks(long existingFileSize) { if (LOG.isTraceEnabled()) LOG.trace("trying to find verifyable blocks out of "+partialBlocks); boolean fullScan = existingFileSize != -1; List<Range> verifyable = new ArrayList<Range>(2); List<Range> partial; int chunkSize = getChunkSize(); if(fullScan) { IntervalSet temp = partialBlocks.clone(); temp.add(Range.createRange(0, existingFileSize)); partial = temp.getAllIntervalsAsList(); } else { partial = partialBlocks.getAllIntervalsAsList(); } for (int i = 0; i < partial.size() ; i++) { Range current = partial.get(i); // find the beginning of the first chunk offset long lowChunkOffset = current.getLow() - current.getLow() % chunkSize; if (current.getLow() % chunkSize != 0) lowChunkOffset += chunkSize; while (current.getHigh() >= lowChunkOffset+chunkSize-1) { Range complete = Range.createRange(lowChunkOffset, lowChunkOffset+chunkSize -1); verifyable.add(complete); lowChunkOffset += chunkSize; } } // special case for the last chunk if (!partial.isEmpty()) { long lastChunkOffset = completedSize - (completedSize % chunkSize); if (lastChunkOffset == completedSize) lastChunkOffset-=chunkSize; Range last = partial.get(partial.size() - 1); if (last.getHigh() == completedSize-1 && last.getLow() <= lastChunkOffset ) { if(LOG.isDebugEnabled()) LOG.debug("adding the last chunk for verification"); verifyable.add(Range.createRange(lastChunkOffset, last.getHigh())); } } return verifyable; } /** * Runnable that writes chunks to disk & verifies partial blocks. */ private class ChunkHandler extends ChunkDiskJob { /** The interval that we are about to write */ private final Range intvl; /** Whether or not running the job freed a pending block. */ private boolean freedPending = false; public ChunkHandler(byte[] buf, Range intvl) { super(buf); this.intvl = intvl; long length = intvl.getHigh() - intvl.getLow() + 1; assert length <= buf.length : "invalid length "+length+ " vs buf "+buf.length; } public void runChunkJob(byte[] buf) { try { if(LOG.isTraceEnabled()) LOG.trace("Writing intvl: " + intvl); synchronized(fos) { fos.seek(intvl.getLow()); fos.write(buf, 0, (int)(intvl.getHigh() - intvl.getLow() + 1)); } synchronized(VerifyingFile.this) { pendingBlocks.delete(intvl); partialBlocks.add(intvl); freedPending = true; } verifyChunks(); } catch(IOException diskIO) { synchronized(VerifyingFile.this) { pendingBlocks.delete(intvl); storedException = diskIO; } } } public void finish() { synchronized(VerifyingFile.this) { try { if (!freedPending) pendingBlocks.delete(intvl); } finally { --chunksScheduledPerFile; VerifyingFile.this.notifyAll(); } } } } /** A simple Runnable that schedules a verification of the file. */ private class EmptyVerifier implements Runnable { private final long existingFileSize; EmptyVerifier(long existingFileSize) { this.existingFileSize = existingFileSize; } public void run() { verifyChunks(existingFileSize); synchronized(VerifyingFile.this) { VerifyingFile.this.notify(); } } } static interface WriteCallback { public void writeScheduled(); } private static class VerifyingFileDelayedWrite implements DelayedWrite { private final WriteRequest request; private final WriteCallback callback; private final VerifyingFile vf; VerifyingFileDelayedWrite(WriteRequest request, WriteCallback callback, VerifyingFile vf) { this.request = request; this.callback = callback; this.vf = vf; } public boolean write() { if(vf.writeBlockImpl(request)) { callback.writeScheduled(); return true; } else { return false; } } } public static class WriteRequest { public final long currPos; public final int start; public final int length; public final byte[] buf; public final Range in; private boolean processed, done, scheduled; WriteRequest(long currPos, int start, int length, byte [] buf) { this.currPos = currPos; this.start = start; this.length = length; this.buf = buf; in = Range.createRange(currPos, currPos + length - 1); } private synchronized void startProcessing() { if (isInvalidForWriting()) throw new IllegalStateException("invalid request state"); processed = true; } private synchronized void startScheduling() { if (isInvalidForCallback()) throw new IllegalStateException("invalid request state"); scheduled = true; } private synchronized void setDone() { if (done) throw new IllegalStateException("invalid request state"); done = true; } public synchronized boolean isInvalidForCallback(){ return !processed || done || scheduled; } public synchronized boolean isInvalidForWriting() { return done || processed; } } }

The table below shows all metrics for VerifyingFile.java.

MetricValueDescription
BLOCKS107.00Number of blocks
BLOCK_COMMENT 6.00Number of block comment lines
COMMENTS296.00Comment lines
COMMENT_DENSITY 0.67Comment density
COMPARISONS110.00Number of comparison operators
CYCLOMATIC143.00Cyclomatic complexity
DECL_COMMENTS66.00Comments in declarations
DOC_COMMENT259.00Number of javadoc comment lines
ELOC441.00Effective lines of code
EXEC_COMMENTS21.00Comments in executable code
EXITS79.00Procedure exits
FUNCTIONS62.00Number of function declarations
HALSTEAD_DIFFICULTY107.54Halstead difficulty
HALSTEAD_EFFORT 0.00Halstead effort
INTERFACE_COMPLEXITY138.00Interface complexity
JAVA0001 0.00JAVA0001 Package name does not contain only lower case letters
JAVA0002 0.00JAVA0002 Package name does not begin with a top level domain name or country code
JAVA0003 0.00JAVA0003 Minimize use of on-demand (.*) imports
JAVA0004 0.00JAVA0004 Unnecessary import from java.lang
JAVA0005 0.00JAVA0005 Imports not in specified order
JAVA0006 0.00JAVA0006 Empty finally block
JAVA0007 0.00JAVA0007 Should not declare public field
JAVA0008 2.00JAVA0008 Empty catch block
JAVA0009 0.00JAVA0009 Protected member in final class
JAVA0010 0.00JAVA0010 Non-instantiable class does not contain a non-private static member
JAVA0011 0.00JAVA0011 Abstract class does not contain an abstract method
JAVA0012 0.00JAVA0012 Non-constructor method with same name as declaring class
JAVA0013 0.00JAVA0013 Non-blank final field is not static
JAVA0014 0.00JAVA0014 Class with only static members has non-private constructor
JAVA0015 0.00JAVA0015 Package class contains public nested type
JAVA0016 0.00JAVA0016 Abstract class contains public constructor
JAVA0017 0.00JAVA0017 Class name does not have required form
JAVA0018 0.00JAVA0018 Method name does not have required form
JAVA0019 0.00JAVA0019 Interface name does not have required form
JAVA0020 0.00JAVA0020 Field name does not have required form
JAVA0021 0.00JAVA0021 Interface method name does not have required form
JAVA0022 0.00JAVA0022 Static final field name does not have required form
JAVA0023 0.00JAVA0023 Empty finalize method
JAVA0024 0.00JAVA0024 Empty class
JAVA0025 0.00JAVA0025 Method override is empty
JAVA0026 0.00JAVA0026 Finalize method with parameters
JAVA0029 0.00JAVA0029 Private method not used
JAVA0030 0.00JAVA0030 Private field not used
JAVA0031 0.00JAVA0031 Case statement not properly closed
JAVA0032 0.00JAVA0032 Switch statement missing default
JAVA0033 0.00JAVA0033 default: not last case in switch statement
JAVA003442.00JAVA0034 Missing braces in if statement
JAVA0035 0.00JAVA0035 Missing braces in for statement
JAVA0036 0.00JAVA0036 Missing braces in while statement
JAVA0038 0.00JAVA0038 Non-case label in switch statement
JAVA0039 0.00JAVA0039 Break statement with label
JAVA0040 0.00JAVA0040 Switch statement contains N cases (maximum: M)
JAVA0041 0.00JAVA0041 Nested synchronized block
JAVA0042 0.00JAVA0042 Empty synchronized statement
JAVA0043 0.00JAVA0043 Inner class does not use outer class
JAVA0044 0.00JAVA0044 Serializable class with no instance variables
JAVA0045 0.00JAVA0045 Serializable class with only transient fields
JAVA0046 0.00JAVA0046 Name of class not derived from Exception ends with 'Exception'
JAVA0047 0.00JAVA0047 Serializable class derives from invalid base class
JAVA0048 0.00JAVA0048 Name of class derived from Exception does not end with 'Exception'
JAVA0049 1.00JAVA0049 Nested block at depth N (maximum: M)
JAVA0050 0.00JAVA0050 Class derives from java.lang.Error
JAVA0051 0.00JAVA0051 Class derives from java.lang.RuntimeException
JAVA0052 0.00JAVA0052 Class derives from java.lang.Throwable
JAVA0053 0.00JAVA0053 Unused label
JAVA0054 0.00JAVA0054 Inheritance depth N exceeds maximum M
JAVA0055 0.00JAVA0055 Class should be interface
JAVA0056 0.00JAVA0056 Unnecessary abstract modifier for interface or annotation
JAVA0057 0.00JAVA0057 Unnecessary default constructor
JAVA0058 0.00JAVA0058 Constructor calls super()
JAVA0059 0.00JAVA0059 Method override only calls super()
JAVA0061 0.00JAVA0061 Inaccessible member in anonymous class
JAVA0062 0.00JAVA0062 Public class missing public member or protected constructor
JAVA0063 0.00JAVA0063 Identifier name should not contain '$'
JAVA0064 0.00JAVA0064 N variations of identifier name (maximum: M)
JAVA0065 0.00JAVA0065 Unnecessary final modifier for method in final class
JAVA0066 0.00JAVA0066 Unnecessary modifier for interface nested type
JAVA0067 0.00JAVA0067 Array descriptor on identifier name
JAVA0068 0.00JAVA0068 Modifiers not declared in recommended order
JAVA0071 0.00JAVA0071 Strings compared with ==
JAVA0073 0.00JAVA0073 Integer division in floating-point context
JAVA0074 1.00JAVA0074 Use of Object.notify()
JAVA0075 2.00JAVA0075 Method parameter hides field
JAVA0076 0.00JAVA0076 Use of magic number
JAVA0077 0.00JAVA0077 Private field not used in declaring class
JAVA0078 0.00JAVA0078 Floating point values compared with ==
JAVA0079 0.00JAVA0079 Use of instance to reference static member
JAVA0080 0.00JAVA0080 Import declaration not used
JAVA0081 0.00JAVA0081 Boolean literal in comparison
JAVA0082 0.00JAVA0082 Unnecessary widening cast
JAVA0083 0.00JAVA0083 Unnecessary instanceof test
JAVA0084 0.00JAVA0084 Should use compound assignment operator
JAVA0085 0.00JAVA0085 Use of sun.* class
JAVA0087 0.00JAVA0087 Use of Thread.sleep()
JAVA0089 0.00JAVA0089 Use of restricted package
JAVA0092 0.00JAVA0092 Use of restricted type
JAVA0093 0.00JAVA0093 Redundant assignment
JAVA0094 0.00JAVA0094 Field hides a superclass field
JAVA0095 0.00JAVA0095 Uninitialized private field
JAVA0096 1.00JAVA0096 Field in nested class hides outer field
JAVA0098 0.00JAVA0098 Minimize use of implicit field initializers
JAVA0100 1.00JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0101 0.00JAVA0101 Unnecessary modifier for field in interface
JAVA0102 0.00JAVA0102 Last statement in finalize() not super.finalize()
JAVA0103 0.00JAVA0103 Explicit call to finalize()
JAVA0104 0.00JAVA0104 finalize() only calls super.finalize()
JAVA0105 0.00JAVA0105 Duplicate import declaration
JAVA0106 0.00JAVA0106 Unnecessary import from current package
JAVA010820.00JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0109 9.00JAVA0109 Incorrect javadoc: no parameter 'parameter'
JAVA011012.00JAVA0110 Incorrect javadoc: no @return tag
JAVA0111 0.00JAVA0111 Incorrect javadoc: @return tag for void method
JAVA0112 0.00JAVA0112 Incorrect javadoc: no exception 'exception' in throws
JAVA0113 1.00JAVA0113 Incorrect javadoc: no @author tag
JAVA0114 1.00JAVA0114 Incorrect javadoc: no @version tag
JAVA011511.00JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
JAVA0116 5.00JAVA0116 Missing javadoc: field 'field'
JAVA011712.00JAVA0117 Missing javadoc: method 'method'
JAVA0118 1.00JAVA0118 Missing javadoc: type 'type'
JAVA0119 0.00JAVA0119 Control variable changed within body of for loop
JAVA0123 0.00JAVA0123 Use all three components of for loop
JAVA0125 0.00JAVA0125 Continue statement with label
JAVA0126 5.00JAVA0126 Method declares unchecked exception in throws
JAVA0128 1.00JAVA0128 Public constructor in non-public class
JAVA0130 0.00JAVA0130 Non-static method does not use instance fields
JAVA0131 0.00JAVA0131 Compatible method does not override base
JAVA0132 0.00JAVA0132 Method overload with compatible signature
JAVA0133 0.00JAVA0133 Non-synchronized method overrides synchronized method
JAVA0135 0.00JAVA0135 Only one of Object.equals and Object.hashCode defined: missing 'method'
JAVA0136 1.00JAVA0136 N methods defined in class (maximum: M)
JAVA0137 0.00JAVA0137 Non-abstract class missing constructor
JAVA0138 0.00JAVA0138 N parameters defined for method (maximum: M)
JAVA0139 0.00JAVA0139 Definition of main other than public static void main(java.lang.String[])
JAVA0141 1.00JAVA0141 Unnecessary modifier for method in interface
JAVA014337.00JAVA0143 Synchronized method
JAVA0144 1.00JAVA0144 Line exceeds maximum M characters
JAVA0145153.00JAVA0145 Tab character used in source file
JAVA0150 0.00JAVA0150 java.lang.Error (or subclass) thrown
JAVA0153 0.00JAVA0153 Inefficient conversion of integer to string
JAVA0159 0.00JAVA0159 Inefficient conversion of string to integer
JAVA0160 0.00JAVA0160 Method does not throw specified exception
JAVA0161 0.00JAVA0161 Conditional wait() not in loop
JAVA0163 0.00JAVA0163 Empty statement
JAVA0165 0.00JAVA0165 Conflicting return statement in finally block
JAVA0166 0.00JAVA0166 Generic exception caught
JAVA0167 0.00JAVA0167 ThreadDeath not rethrown
JAVA0169 0.00JAVA0169 Unnecessary catch block: exception 'exception'
JAVA0170 0.00JAVA0170 Caught exception not derived from java.lang.Exception
JAVA0171 0.00JAVA0171 Unused local variable
JAVA0173 0.00JAVA0173 Unused method parameter
JAVA0174 0.00JAVA0174 Assigned local variable never used
JAVA0175 0.00JAVA0175 Successive assignment to variable
JAVA0176 0.00JAVA0176 Local variable name does not have required form
JAVA0177 1.00JAVA0177 Variable declaration missing initializer
JAVA0179 0.00JAVA0179 Local variable hides visible field
JAVA0233 0.00JAVA0233 Definition of serialVersionUID other than 'private static final long serialVersionUID'
JAVA0234 0.00JAVA0234 Class is Serializable but does not define serialVersionUID
JAVA0235 0.00JAVA0235 Class defines serialVersionUID but does not implement Serializable
JAVA0236 0.00JAVA0236 Attempt to clone an object which does not implement Cloneable
JAVA0237 0.00JAVA0237 Class implements Cloneable but does not have public clone method
JAVA0238 0.00JAVA0238 Clone method does not call super.clone()
JAVA0239 0.00JAVA0239 Class declares 'readObject' or 'writeObject' but does not implement Serializable
JAVA0240 0.00JAVA0240 Serializable class which declares readObject or writeObject but not both
JAVA0241 0.00JAVA0241 'readObject' or 'writeObject' should be declared private in Serializable class
JAVA0242 0.00JAVA0242 Transient field in non-Serializable class
JAVA0243 0.00JAVA0243 'readResolve' or 'writeReplace' should be declared private or protected
JAVA0244 0.00JAVA0244 Field or method name in subclass differs only by case from inherited field or method
JAVA0245 0.00JAVA0245 JUnit TestCase with non-trivial constructor
JAVA0246 0.00JAVA0246 JUnit assertXXX statement missing message parameter
JAVA0247 0.00JAVA0247 JUnit 'setUp()' and 'tearDown()' should call super method
JAVA0248 0.00JAVA0248 JUnit method 'setUp' or 'tearDown' with incorrect signature
JAVA0249 0.00JAVA0249 JUnit TestCase 'suite()' should be declared static
JAVA0250 0.00JAVA0250 JUnit TestCase declares testXXX method with incorrect signature
JAVA0251 0.00JAVA0251 Use '%n' for line breaks in printf/format for platform independence
JAVA0252 0.00JAVA0252 'enum' is a Java 1.5 reserved word
JAVA0253 0.00JAVA0253 Not all enum constants consumed in switch statement
JAVA0254 0.00JAVA0254 Use enhanced for loop construct instead of Iterator
JAVA0255 0.00JAVA0255 Result of method invocation not used
JAVA0256 1.00JAVA0256 Assignment of external collection/array to field
JAVA0257 0.00JAVA0257 Use of 'Constant Interface' anti-pattern
JAVA0258 0.00JAVA0258 Implement Iterable for foreach compatibility
JAVA0259 0.00JAVA0259 Return of collection/array field
JAVA0260 0.00JAVA0260 Use 'enum' instead of Enumerated Type pattern
JAVA0261 0.00JAVA0261 Use specialized Enum collection types
JAVA0262 0.00JAVA0262 Use of char in integer context
JAVA0263 0.00JAVA0263 Long literal ends with 'l' instead of 'L'
JAVA0264 0.00JAVA0264 Integer math in long context - check for overflow
JAVA0265 0.00JAVA0265 Use of Throwable.printStackTrace()
JAVA0266 0.00JAVA0266 Use of System.out
JAVA0267 0.00JAVA0267 Use of System.err
JAVA0269 0.00JAVA0269 Contents of StringBuffer never used
JAVA0270 0.00JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
JAVA0271 0.00JAVA0271 Minimize use of on-demand (.*) static imports
JAVA0272 0.00JAVA0272 Thread.run() called
JAVA0273 0.00JAVA0273 Non-final derivative of Thread calls start() in constructor
JAVA0274 0.00JAVA0274 Serializable class has a synchronized readObject()
JAVA0275 0.00JAVA0275 Serializable class has a synchronized writeObject() and no other synchronized methods
JAVA0276 0.00JAVA0276 Unnecessary use of String constructor
JAVA0277 0.00JAVA0277 Iterator.next() implementation does not throw NoSuchElementException
JAVA0278 0.00JAVA0278 Unnecessary use of Boolean constructor
JAVA0279 0.00JAVA0279 Serialization method readObject or readObjectNoData calls an overridable method
JAVA0280 0.00JAVA0280 IllegalMonitorStateException caught
JAVA0281 0.00JAVA0281 Iterator.next() not called in loop
JAVA0282 0.00JAVA0282 Call to Iterator.next() in loop which does not test Iterator.hasNext()
JAVA0283 0.00JAVA0283 Control variable not updated in loop body
JAVA0284 0.00JAVA0284 Explicit garbage collection
JAVA0285 0.00JAVA0285 Dereference of potentially null variable
JAVA0286 0.00JAVA0286 Dereference of null variable
JAVA0287 0.00JAVA0287 Unnecessary null check
JAVA0288 0.00JAVA0288 Inconsistent null check
LINES986.00Number of lines in the source file
LINE_COMMENT31.00Number of line comments
LOC543.00Lines of code
LOGICAL_LINES262.00Number of statements
LOOPS 5.00Number of loops
NEST_DEPTH 6.00Maximum nesting depth
OPERANDS1109.00Number of operands
OPERATORS2389.00Number of operators
PARAMS38.00Number of formal parameter declarations
PROGRAM_LENGTH3498.00Halstead program length
PROGRAM_VOCAB394.00Halstead program vocabulary
PROGRAM_VOLUME 0.00Halstead program volume
RETURNS100.00Number of return points from functions
SIZE33192.00Size of the file in bytes
UNIQUE_OPERANDS330.00Number of unique operands
UNIQUE_OPERATORS64.00Number of unique operators
WHITESPACE147.00Number of whitespace lines