public void validateIntegrity() throws ByteBufferStorageCompromisedException {
synchronized (this) {
    if (!_initialized)     return;
  }
  try {
    SegmentCursor reader=null;
    int segmentIndex=0;
    int packetCount=0;
    long unindexedLength=0;
    int packetInSegment=0;
    long position=0;
    try {
      StorageSegment currentSegment=getFirstSegment();
      reader=currentSegment.getCursorForReading();
      reader.setPosition(0);
      boolean firstIteration=true;
      while (true) {
        position=reader.getPosition();
        if (_traceableLogger.isLoggable(Level.FINEST))         _traceableLogger.finest("start index integrity iteration [" + packetCount + "], position="+ position+ " segment index ["+ segmentIndex+ "] in segment packet index ["+ packetInSegment+ "] unindexed length ["+ unindexedLength+ "]");
        if (!firstIteration && unindexedLength >= _maxScanLength) {
          long indexPos=currentSegment.getIndexPosition(packetInSegment);
          if (_traceableLogger.isLoggable(Level.FINEST))           _traceableLogger.finest("index position=" + indexPos + " reader position="+ position);
          if (indexPos != reader.getPosition())           throw new ByteBufferStorageCompromisedException("Error while checking index integrity, segment index [" + segmentIndex + "]"+ " packet index ["+ packetCount+ "] in segment packet index ["+ packetInSegment+ "] expected index ["+ reader.getPosition()+ "] actual ["+ indexPos+ "]");
          unindexedLength=0;
        }
        firstIteration=false;
        int length=reader.readInt();
        if (_traceableLogger.isLoggable(Level.FINEST))         _traceableLogger.finest("next packet length=" + length);
        if (length == END_OF_PACKETS_MARKER) {
          if (_traceableLogger.isLoggable(Level.FINEST))           _traceableLogger.finest("reached end of packets marker");
          break;
        }
        if (length == END_OF_SEGMENT_MARKER) {
          if (_traceableLogger.isLoggable(Level.FINEST))           _traceableLogger.finest("reached end of segment marker");
          SegmentCursor tempReader=reader;
          reader=null;
          if (_traceableLogger.isLoggable(Level.FINEST))           _traceableLogger.finest("releasing reader");
          tempReader.release();
          currentSegment=_segments.get(++segmentIndex);
          reader=currentSegment.getCursorForReading();
          reader.setPosition(0);
          position=0;
          packetInSegment=0;
          unindexedLength=0;
          if (_traceableLogger.isLoggable(Level.FINEST))           _traceableLogger.finest("moved to next segment");
          continue;
        }
        byte[] packetBuffer=new byte[length];
        reader.readBytes(packetBuffer,0,length);
        if (_traceableLogger.isLoggable(Level.FINEST))         _traceableLogger.finest("deserializing packet");
        T deserializePacket=_packetSerializer.deserializePacket(packetBuffer);
        if (deserializePacket == null)         throw new ByteBufferStorageCompromisedException("Error while checking data integrity, packet at index " + packetCount + " is null");
        if (packetInSegment >= currentSegment.getNumOfDeletedPackets())         packetCount++;
        packetInSegment++;
        unindexedLength+=reader.getPosition() - position;
      }
      if (_traceableLogger.isLoggable(Level.FINEST))       _traceableLogger.finest("done index integrity check, releasing last reader");
      SegmentCursor tempReader=reader;
      reader=null;
      if (tempReader != null)       tempReader.release();
    }
 catch (    Exception e) {
      if (reader != null)       try {
        reader.release();
      }
 catch (      Exception ei) {
        throw new ByteBufferStorageCompromisedException("Error while checking data integrity, reached packet index " + packetCount + " additionally exception occurred when releasing the reader "+ ei.toString(),e);
      }
      if (e instanceof ByteBufferStorageCompromisedException)       throw (ByteBufferStorageCompromisedException)e;
      throw new ByteBufferStorageCompromisedException("Error while checking data integrity, reached packet index " + packetCount,e);
    }
    if (_traceableLogger.isLoggable(Level.FINEST))     _traceableLogger.finest("validating size integrity, expected size=" + packetCount + " known size="+ _size);
    if (packetCount != _size)     throw new ByteBufferStorageCompromisedException("Calculated size [" + packetCount + "] does not match kept size ["+ _size+ "]");
    if (_traceableLogger.isLoggable(Level.FINEST))     _traceableLogger.finest("perform one entire scan using iterator");
    StorageReadOnlyIterator<T> iterator=null;
    int count=0;
    try {
      iterator=readOnlyIterator();
      while (iterator.hasNext()) {
        if (iterator.next() == null)         throw new ByteBufferStorageCompromisedException("Error while checking full iteration indexes, at packet index " + count + " iterator returned null packet");
        if (_traceableLogger.isLoggable(Level.FINEST))         _traceableLogger.finest("iteration count " + count);
        count++;
      }
      if (_traceableLogger.isLoggable(Level.FINEST))       _traceableLogger.finest("done iterator integrity check, closing iterator");
      StorageReadOnlyIterator<T> tempIterator=iterator;
      iterator=null;
      tempIterator.close();
      if (_traceableLogger.isLoggable(Level.FINEST))       _traceableLogger.finest("validating iterator size integrity, expected size=" + count + " known size="+ _size);
      if (count != _size)       throw new ByteBufferStorageCompromisedException("Full iteration size [" + packetCount + "] does not match kept size ["+ _size+ "]");
    }
 catch (    Exception e) {
      if (iterator != null)       try {
        iterator.close();
      }
 catch (      StorageException ei) {
        throw new ByteBufferStorageCompromisedException("Error while checking full iteration integrity, at packet index " + count + " additionally an error occurred while closing iterator "+ ei.toString(),e);
      }
      if (e instanceof ByteBufferStorageCompromisedException)       throw (ByteBufferStorageCompromisedException)e;
      throw new ByteBufferStorageCompromisedException("Error while checking full iteration integrity, at packet index " + count,e);
    }
    if (_traceableLogger.isLoggable(Level.FINEST))     _traceableLogger.finest("validating fetch iterator by index integrity");
    iterator=null;
    int i=0;
    try {
      for (; i < _size; ++i) {
        iterator=readOnlyIterator(i);
        if (!iterator.hasNext())         throw new ByteBufferStorageCompromisedException("Error while validating indexes, at packet index " + i + " iterator has no next");
        T packet=iterator.next();
        if (packet == null)         throw new ByteBufferStorageCompromisedException("Error while validating indexes, at packet index " + i + " iterator returned null packet");
        if (_traceableLogger.isLoggable(Level.FINEST))         _traceableLogger.finest("validated fetch iterator by index [" + i + "] integrity done, closing iterator");
        StorageReadOnlyIterator<T> tempIterator=iterator;
        iterator=null;
        tempIterator.close();
      }
    }
 catch (    Exception e) {
      if (iterator != null)       try {
        iterator.close();
      }
 catch (      StorageException ei) {
        throw new ByteBufferStorageCompromisedException("Error while validating indexes, at packet index " + i + " additionally an error occurred while closing iterator "+ ei.toString(),e);
      }
      if (e instanceof ByteBufferStorageCompromisedException)       throw (ByteBufferStorageCompromisedException)e;
      throw new ByteBufferStorageCompromisedException("Error while validating indexes, at packet index " + i,e);
    }
  }
 catch (  ByteBufferStorageCompromisedException e) {
    logGeneralStatusOnError();
    if (_traceableLogger.isLoggable(Level.FINEST))     _traceableLogger.finest(e.getMessage());
    _traceableLogger.showThreadTrace();
    throw e;
  }
catch (  Exception e) {
    logGeneralStatusOnError();
    if (_traceableLogger.isLoggable(Level.FINEST))     _traceableLogger.finest(e.getMessage());
    _traceableLogger.showThreadTrace();
    throw new ByteBufferStorageCompromisedException(e);
  }
 finally {
    _traceableLogger.clearThreadTrace();
  }
}

public void validateIntegrity ( ) throws ByteBufferStorageCompromisedException { synchronized ( this ) { if ( ! _initialized ) return ; } try { SegmentCursor reader = null ; int segmentIndex = 0 ; int packetCount = 0 ; long unindexedLength = 0 ; int packetInSegment = 0 ; long position = 0 ; try { StorageSegment currentSegment = getFirstSegment ( ) ; reader = currentSegment . getCursorForReading ( ) ; reader . setPosition ( 0 ) ; boolean firstIteration = true ; while ( true ) { position = reader . getPosition ( ) ; if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "start index integrity iteration [" + packetCount + "], position=" + position + " segment index [" + segmentIndex + "] in segment packet index [" + packetInSegment + "] unindexed length [" + unindexedLength + "]" ) ; if ( ! firstIteration && unindexedLength >= _maxScanLength ) { long indexPos = currentSegment . getIndexPosition ( packetInSegment ) ; if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "index position=" + indexPos + " reader position=" + position ) ; if ( indexPos != reader . getPosition ( ) ) throw new ByteBufferStorageCompromisedException ( "Error while checking index integrity, segment index [" + segmentIndex + "]" + " packet index [" + packetCount + "] in segment packet index [" + packetInSegment + "] expected index [" + reader . getPosition ( ) + "] actual [" + indexPos + "]" ) ; unindexedLength = 0 ; } firstIteration = false ; int length = reader . readInt ( ) ; if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "next packet length=" + length ) ; if ( length == END_OF_PACKETS_MARKER ) { if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "reached end of packets marker" ) ; break ; } if ( length == END_OF_SEGMENT_MARKER ) { if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "reached end of segment marker" ) ; SegmentCursor tempReader = reader ; reader = null ; if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "releasing reader" ) ; tempReader . release ( ) ; currentSegment = _segments . get ( ++ segmentIndex ) ; reader = currentSegment . getCursorForReading ( ) ; reader . setPosition ( 0 ) ; position = 0 ; packetInSegment = 0 ; unindexedLength = 0 ; if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "moved to next segment" ) ; continue ; } byte [ ] packetBuffer = new byte [ length ] ; reader . readBytes ( packetBuffer , 0 , length ) ; if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "deserializing packet" ) ; T deserializePacket = _packetSerializer . deserializePacket ( packetBuffer ) ; if ( deserializePacket == null ) throw new ByteBufferStorageCompromisedException ( "Error while checking data integrity, packet at index " + packetCount + " is null" ) ; if ( packetInSegment >= currentSegment . getNumOfDeletedPackets ( ) ) packetCount ++ ; packetInSegment ++ ; unindexedLength += reader . getPosition ( ) - position ; } if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "done index integrity check, releasing last reader" ) ; SegmentCursor tempReader = reader ; reader = null ; if ( tempReader != null ) tempReader . release ( ) ; } catch ( Exception e ) { if ( reader != null ) try { reader . release ( ) ; } catch ( Exception ei ) { throw new ByteBufferStorageCompromisedException ( "Error while checking data integrity, reached packet index " + packetCount + " additionally exception occurred when releasing the reader " + ei . toString ( ) , e ) ; } if ( e instanceof ByteBufferStorageCompromisedException ) throw ( ByteBufferStorageCompromisedException ) e ; throw new ByteBufferStorageCompromisedException ( "Error while checking data integrity, reached packet index " + packetCount , e ) ; } if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "validating size integrity, expected size=" + packetCount + " known size=" + _size ) ; if ( packetCount != _size ) throw new ByteBufferStorageCompromisedException ( "Calculated size [" + packetCount + "] does not match kept size [" + _size + "]" ) ; if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "perform one entire scan using iterator" ) ; StorageReadOnlyIterator < T > iterator = null ; int count = 0 ; try { iterator = readOnlyIterator ( ) ; while ( iterator . hasNext ( ) ) { if ( iterator . next ( ) == null ) throw new ByteBufferStorageCompromisedException ( "Error while checking full iteration indexes, at packet index " + count + " iterator returned null packet" ) ; if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "iteration count " + count ) ; count ++ ; } if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "done iterator integrity check, closing iterator" ) ; StorageReadOnlyIterator < T > tempIterator = iterator ; iterator = null ; tempIterator . close ( ) ; if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "validating iterator size integrity, expected size=" + count + " known size=" + _size ) ; if ( count != _size ) throw new ByteBufferStorageCompromisedException ( "Full iteration size [" + packetCount + "] does not match kept size [" + _size + "]" ) ; } catch ( Exception e ) { if ( iterator != null ) try { iterator . close ( ) ; } catch ( StorageException ei ) { throw new ByteBufferStorageCompromisedException ( "Error while checking full iteration integrity, at packet index " + count + " additionally an error occurred while closing iterator " + ei . toString ( ) , e ) ; } if ( e instanceof ByteBufferStorageCompromisedException ) throw ( ByteBufferStorageCompromisedException ) e ; throw new ByteBufferStorageCompromisedException ( "Error while checking full iteration integrity, at packet index " + count , e ) ; } if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "validating fetch iterator by index integrity" ) ; iterator = null ; int i = 0 ; try { for ( ; i < _size ; ++ i ) { iterator = readOnlyIterator ( i ) ; if ( ! iterator . hasNext ( ) ) throw new ByteBufferStorageCompromisedException ( "Error while validating indexes, at packet index " + i + " iterator has no next" ) ; T packet = iterator . next ( ) ; if ( packet == null ) throw new ByteBufferStorageCompromisedException ( "Error while validating indexes, at packet index " + i + " iterator returned null packet" ) ; if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( "validated fetch iterator by index [" + i + "] integrity done, closing iterator" ) ; StorageReadOnlyIterator < T > tempIterator = iterator ; iterator = null ; tempIterator . close ( ) ; } } catch ( Exception e ) { if ( iterator != null ) try { iterator . close ( ) ; } catch ( StorageException ei ) { throw new ByteBufferStorageCompromisedException ( "Error while validating indexes, at packet index " + i + " additionally an error occurred while closing iterator " + ei . toString ( ) , e ) ; } if ( e instanceof ByteBufferStorageCompromisedException ) throw ( ByteBufferStorageCompromisedException ) e ; throw new ByteBufferStorageCompromisedException ( "Error while validating indexes, at packet index " + i , e ) ; } } catch ( ByteBufferStorageCompromisedException e ) { logGeneralStatusOnError ( ) ; if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( e . getMessage ( ) ) ; _traceableLogger . showThreadTrace ( ) ; throw e ; } catch ( Exception e ) { logGeneralStatusOnError ( ) ; if ( _traceableLogger . isLoggable ( Level . FINEST ) ) _traceableLogger . finest ( e . getMessage ( ) ) ; _traceableLogger . showThreadTrace ( ) ; throw new ByteBufferStorageCompromisedException ( e ) ; } finally { _traceableLogger . clearThreadTrace ( ) ; } }
peforms an integrity check that verifies the storage state and data is not compromised
