LogVerificationReadableByteChannel (Oracle - Berkeley DB Java Edition API)
Date: 2014-12-16

com.sleepycat.je.util

## Class LogVerificationReadableByteChannel

- java.lang.Object
- - com.sleepycat.je.util.LogVerificationReadableByteChannel

- All Implemented Interfaces:
    Closeable, AutoCloseable, Channel, ReadableByteChannel

      public class LogVerificationReadableByteChannel
      extends Object
      implements ReadableByteChannel

  Verifies the checksums in a `ReadableByteChannel` for a log file in a JE `Environment`. This class is similar to the `LogVerificationInputStream` class, but permits using NIO channels and direct buffers to provide better copying performance.

  This `ReadableByteChannel` reads input from some other given `ReadableByteChannel`, 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 copyFilesNIO(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 FileChannel foc = fos.getChannel();

                final File srcFile = new File(srcDir, fileName);
                final FileInputStream fis = new FileInputStream(srcFile);
                final FileChannel fic = fis.getChannel();
                final LogVerificationReadableByteChannel vic =
                    new LogVerificationReadableByteChannel(env, fic, fileName);

                final ByteBuffer buf = ByteBuffer.allocateDirect(bufSize);

                try {
                    while (true) {
                        final int len = vic.read(buf);
                        if (len < 0) {
                            break;
                        }
                        buf.flip();
                        foc.write(buf);
                        buf.clear();
                    }
                } finally {
                    fos.close();
                    vic.close();
                }
            }
        }

  It is important to read the entire underlying input stream until the end-of-file is reached to detect incomplete entries at the end of the log file.

  See Also:
    `DbBackup`, `DbVerifyLog`, `LogVerificationInputStream`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `LogVerificationReadableByteChannel(Environment env, ReadableByteChannel channel, String fileName)` Creates a verification input stream.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()`
    `boolean` | `isOpen()`
    `int` | `read(ByteBuffer buffer)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### LogVerificationReadableByteChannel

          public LogVerificationReadableByteChannel(Environment env,
                                            ReadableByteChannel channel,
                                            String fileName)

      Creates a verification input stream.
      Parameters:
        `env` - the `Environment` associated with the log
        `channel` - the underlying `ReadableByteChannel` 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 hexadecimal format.
      Throws:
        `EnvironmentFailureException` - if an unexpected, internal or environment-wide failure occurs

  - ### Method Detail

    - #### read

          public int read(ByteBuffer buffer)
                   throws IOException

      This method reads the underlying `ReadableByteChannel` and verifies the contents of the stream.

      Specified by:
        `read` in interface `ReadableByteChannel`
      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`

    - #### close

          public void close()
                     throws IOException

      This method calls `close` on the underlying channel.

      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Specified by:
        `close` in interface `Channel`
      Throws:
        `IOException`

    - #### isOpen

          public boolean isOpen()

      This method calls `isOpen` on the underlying channel.

      Specified by:
        `isOpen` in interface `Channel`

Copyright (c) 2004, 2014 Oracle and/or its affiliates. All rights reserved.