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.
|
Modifier and Type | Method and Description |
---|---|
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 int |
headerLength(String codec)
Computes the length of a codec header.
|
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 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
Copyright © 2000-2013 Apache Software Foundation. All Rights Reserved.