public class LogVerificationReadableByteChannel
extends java.lang.Object
implements java.nio.channels.ReadableByteChannel
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.
DbBackup,
DbVerifyLog,
LogVerificationInputStream| Constructor and Description |
|---|
LogVerificationReadableByteChannel(Environment env,
java.nio.channels.ReadableByteChannel channel,
java.lang.String fileName)
Creates a verification input stream.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
boolean |
isOpen() |
int |
read(java.nio.ByteBuffer buffer) |
public LogVerificationReadableByteChannel(Environment env, java.nio.channels.ReadableByteChannel channel, java.lang.String fileName)
env - the Environment associated with the logchannel - the underlying ReadableByteChannel for the log to
be readfileName - 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.EnvironmentFailureException - if an unexpected, internal or
environment-wide failure occurspublic int read(java.nio.ByteBuffer buffer)
throws java.io.IOException
This method reads the underlying ReadableByteChannel and
verifies the contents of the stream.
read in interface java.nio.channels.ReadableByteChannelLogVerificationException - if a checksum cannot be verified or a
log entry is determined to be invalid by examining its contentsEnvironmentFailureException - if an unexpected, internal or
environment-wide failure occursjava.io.IOExceptionpublic void close()
throws java.io.IOException
This method calls close on the underlying channel.
close in interface java.io.Closeableclose in interface java.lang.AutoCloseableclose in interface java.nio.channels.Channeljava.io.IOExceptionpublic boolean isOpen()
This method calls isOpen on the underlying channel.
isOpen in interface java.nio.channels.ChannelCopyright (c) 2002, 2017 Oracle and/or its affiliates. All rights reserved.