Berkeley DB Java Edition
version 5.0.34

com.sleepycat.je.util
Class LogVerificationInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.sleepycat.je.util.LogVerificationInputStream
All Implemented Interfaces:
Closeable

public class LogVerificationInputStream
extends InputStream

Verifies the checksums in an InputStream for a log file in a JE Environment.

This InputStream reads input from some other given InputStream, and verifies checksums while reading. Its primary intended use is to verify log files that are being copied as part of a programmatic backup. It is critical that invalid files are not added to a backup set, since then both the live environment and the backup will be invalid.

The following example verifies log files as they are being copied. The DbBackup class should normally be used to obtain the array of files to be copied.

  void copyFiles(final Environment env,
                 final String[] fileNames,
                 final File destDir,
                 final int bufSize)
      throws IOException, DatabaseException {

      final File srcDir = env.getHome();

      for (final String fileName : fileNames) {

          final File destFile = new File(destDir, fileName);
          final FileOutputStream fos = new FileOutputStream(destFile);

          final File srcFile = new File(srcDir, fileName);
          final FileInputStream fis = new FileInputStream(srcFile);
          final LogVerificationInputStream vis =
              new LogVerificationInputStream(env, fis, fileName);

          final byte[] buf = new byte[bufSize];

          try {
              while (true) {
                  final int len = vis.read(buf);
                  if (len < 0) {
                      break;
                  }
                  fos.write(buf, 0, len);
              }
          } finally {
              fos.close();
              vis.close();
          }
      }
  }
 

It is important to call the close() method of the LogVerificationInputStream to detect incomplete entries at the end of the log file.

Note that mark and reset are not supported and markSupported returns false. The default InputStream implementation of these methods is used.

See Also:
DbBackup, DbVerifyLog

Constructor Summary
LogVerificationInputStream(Environment env, InputStream in, String fileName)
          Creates a verification input stream.
 
Method Summary
 int available()
          
 void close()
          
 int read()
          
 int read(byte[] b)
          
 int read(byte[] b, int off, int len)
          
 long skip(long bytesToSkip)
          
 
Methods inherited from class java.io.InputStream
mark, markSupported, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LogVerificationInputStream

public LogVerificationInputStream(Environment env,
                                  InputStream in,
                                  String fileName)
Creates a verification input stream.

Parameters:
env - the Environment associated with the log.
in - the underlying InputStream for the log to be read.
fileName - the file name of the input stream, for reporting in the LogVerificationException. This should be a simple file name of the form NNNNNNNN.jdb, where NNNNNNNN is the file number in hexidecimal format.
Throws:
EnvironmentFailureException - if an unexpected, internal or environment-wide failure occurs.
Method Detail

read

public int read()
         throws IOException

This method reads the underlying InputStream and verifies the contents of the stream.

Specified by:
read in class InputStream
Throws:
LogVerificationException - if a checksum cannot be verified or a log entry is determined to be invalid by examining its contents.
EnvironmentFailureException - if an unexpected, internal or environment-wide failure occurs.
IOException

read

public int read(byte[] b)
         throws IOException

This method reads the underlying InputStream and verifies the contents of the stream.

Overrides:
read in class InputStream
Throws:
LogVerificationException - if a checksum cannot be verified or a log entry is determined to be invalid by examining its contents.
EnvironmentFailureException - if an unexpected, internal or environment-wide failure occurs.
IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException

This method reads the underlying InputStream and verifies the contents of the stream.

Overrides:
read in class InputStream
Throws:
LogVerificationException - if a checksum cannot be verified or a log entry is determined to be invalid by examining its contents.
EnvironmentFailureException - if an unexpected, internal or environment-wide failure occurs.
IOException

skip

public long skip(long bytesToSkip)
          throws IOException

This method reads the underlying InputStream in order to skip the required number of bytes and verifies the contents of the stream. A temporary buffer is allocated lazily for reading.

Overrides:
skip in class InputStream
Throws:
LogVerificationException - if a checksum cannot be verified or a log entry is determined to be invalid by examining its contents.
EnvironmentFailureException - if an unexpected, internal or environment-wide failure occurs.
IOException

available

public int available()
              throws IOException

This method simply performs in.available().

Overrides:
available in class InputStream
Throws:
IOException

close

public void close()
           throws IOException

This method closes the underlying InputStream and verifies that the stream ends with a complete log entry.

Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
LogVerificationException - if the stream does not end with a complete log entry.
EnvironmentFailureException - if an unexpected, internal or environment-wide failure occurs.
IOException

Berkeley DB Java Edition
version 5.0.34

Copyright (c) 2004-2011 Oracle. All rights reserved.