public final class CodecUtil extends Object
Writing codec headers is useful to ensure that a file is in the format you think it is.
Modifier and Type | Field and Description |
---|---|
static int |
CODEC_MAGIC
Constant to identify the start of a codec header.
|
static int |
FOOTER_MAGIC
Constant to identify the start of a codec footer.
|
Modifier and Type | Method and Description |
---|---|
static void |
checkEOF(IndexInput in)
Deprecated.
Use
checkFooter(org.apache.lucene.store.ChecksumIndexInput) instead, this should only used for files without checksums |
static long |
checkFooter(ChecksumIndexInput in)
Validates the codec footer previously written by
writeFooter(org.apache.lucene.store.IndexOutput) . |
static int |
checkHeader(DataInput in,
String codec,
int minVersion,
int maxVersion)
Reads and validates a header previously written with
writeHeader(DataOutput, String, int) . |
static int |
checkHeaderNoMagic(DataInput in,
String codec,
int minVersion,
int maxVersion)
Like
checkHeader(DataInput,String,int,int) except this
version assumes the first int has already been read
and validated from the input. |
static long |
checksumEntireFile(IndexInput input)
Clones the provided input, reads all bytes from the file, and calls
checkFooter(org.apache.lucene.store.ChecksumIndexInput) |
static int |
footerLength()
Computes the length of a codec footer.
|
static int |
headerLength(String codec)
Computes the length of a codec header.
|
static long |
retrieveChecksum(IndexInput in)
Returns (but does not validate) the checksum previously written by
checkFooter(org.apache.lucene.store.ChecksumIndexInput) . |
static void |
writeFooter(IndexOutput out)
Writes a codec footer, which records both a checksum
algorithm ID and a checksum.
|
static void |
writeHeader(DataOutput out,
String codec,
int version)
Writes a codec header, which records both a string to
identify the file and a version number.
|
public static final int CODEC_MAGIC
public static final int FOOTER_MAGIC
public static void writeHeader(DataOutput out, String codec, int version) throws IOException
checkHeader()
.
CodecHeader --> Magic,CodecName,Version
Uint32
. This
identifies the start of the header. It is always 1071082519.
String
. This
is a string to identify this file.
Uint32
. Records
the version of the file.
Note that the length of a codec header depends only upon the
name of the codec, so this length can be computed at any time
with headerLength(String)
.
out
- Output streamcodec
- String to identify this file. It should be simple ASCII,
less than 128 characters in length.version
- Version numberIOException
- If there is an I/O error writing to the underlying medium.public static int headerLength(String codec)
codec
- Codec name.writeHeader(DataOutput, String, int)
public static int checkHeader(DataInput in, String codec, int minVersion, int maxVersion) throws IOException
writeHeader(DataOutput, String, int)
.
When reading a file, supply the expected codec
and
an expected version range (minVersion to maxVersion
).
in
- Input stream, positioned at the point where the
header was previously written. Typically this is located
at the beginning of the file.codec
- The expected codec name.minVersion
- The minimum supported expected version number.maxVersion
- The maximum supported expected version number.codec
, with an actual version
where minVersion <= actual <= maxVersion
.
Otherwise an exception is thrown.CorruptIndexException
- If the first four bytes are not
CODEC_MAGIC
, or if the actual codec found is
not codec
.IndexFormatTooOldException
- If the actual version is less
than minVersion
.IndexFormatTooNewException
- If the actual version is greater
than maxVersion
.IOException
- If there is an I/O error reading from the underlying medium.writeHeader(DataOutput, String, int)
public static int checkHeaderNoMagic(DataInput in, String codec, int minVersion, int maxVersion) throws IOException
checkHeader(DataInput,String,int,int)
except this
version assumes the first int has already been read
and validated from the input.IOException
public static void writeFooter(IndexOutput out) throws IOException
checkFooter()
.
CodecFooter --> Magic,AlgorithmID,Checksum
Uint32
. This
identifies the start of the footer. It is always -1071082520.
Uint32
. This
indicates the checksum algorithm used. Currently this is always 0,
for zlib-crc32.
Uint32
. The
actual checksum value for all previous bytes in the stream, including
the bytes from Magic and AlgorithmID.
out
- Output streamIOException
- If there is an I/O error writing to the underlying medium.public static int footerLength()
writeFooter(IndexOutput)
public static long checkFooter(ChecksumIndexInput in) throws IOException
writeFooter(org.apache.lucene.store.IndexOutput)
.IOException
- if the footer is invalid, if the checksum does not match,
or if in
is not properly positioned before the footer
at the end of the stream.public static long retrieveChecksum(IndexInput in) throws IOException
checkFooter(org.apache.lucene.store.ChecksumIndexInput)
.IOException
- if the footer is invalid@Deprecated public static void checkEOF(IndexInput in) throws IOException
checkFooter(org.apache.lucene.store.ChecksumIndexInput)
instead, this should only used for files without checksumsIOException
public static long checksumEntireFile(IndexInput input) throws IOException
checkFooter(org.apache.lucene.store.ChecksumIndexInput)
Note that this method may be slow, as it must process the entire file.
If you just need to extract the checksum value, call retrieveChecksum(org.apache.lucene.store.IndexInput)
.
IOException
Copyright © 2000-2014 Apache Software Foundation. All Rights Reserved.