Merge "add readHeader." into jb-mr1-dev
This commit is contained in:
commit
ded498d4a4
1 changed files with 45 additions and 20 deletions
|
@ -1380,6 +1380,48 @@ public class BinaryDictInputOutput {
|
||||||
return NOT_A_VERSION_NUMBER;
|
return NOT_A_VERSION_NUMBER;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to get and validate the binary format version.
|
||||||
|
* @throws UnsupportedFormatException
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private static int checkFormatVersion(final FusionDictionaryBufferInterface buffer)
|
||||||
|
throws IOException, UnsupportedFormatException {
|
||||||
|
final int version = getFormatVersion(buffer);
|
||||||
|
if (version < MINIMUM_SUPPORTED_VERSION || version > MAXIMUM_SUPPORTED_VERSION) {
|
||||||
|
throw new UnsupportedFormatException("This file has version " + version
|
||||||
|
+ ", but this implementation does not support versions above "
|
||||||
|
+ MAXIMUM_SUPPORTED_VERSION);
|
||||||
|
}
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads a header from a buffer.
|
||||||
|
* @throws IOException
|
||||||
|
* @throws UnsupportedFormatException
|
||||||
|
*/
|
||||||
|
private static int readHeader(final FusionDictionaryBufferInterface buffer,
|
||||||
|
final HashMap<String, String> options,
|
||||||
|
final int version)
|
||||||
|
throws IOException, UnsupportedFormatException {
|
||||||
|
|
||||||
|
final int headerSize;
|
||||||
|
if (version < FIRST_VERSION_WITH_HEADER_SIZE) {
|
||||||
|
headerSize = buffer.position();
|
||||||
|
} else {
|
||||||
|
headerSize = buffer.readInt();
|
||||||
|
populateOptions(buffer, headerSize, options);
|
||||||
|
buffer.position(headerSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (headerSize < 0) {
|
||||||
|
throw new UnsupportedFormatException("header size can't be negative.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return headerSize;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads options from a buffer and populate a map with their contents.
|
* Reads options from a buffer and populate a map with their contents.
|
||||||
*
|
*
|
||||||
|
@ -1414,33 +1456,16 @@ public class BinaryDictInputOutput {
|
||||||
public static FusionDictionary readDictionaryBinary(
|
public static FusionDictionary readDictionaryBinary(
|
||||||
final FusionDictionaryBufferInterface buffer, final FusionDictionary dict)
|
final FusionDictionaryBufferInterface buffer, final FusionDictionary dict)
|
||||||
throws IOException, UnsupportedFormatException {
|
throws IOException, UnsupportedFormatException {
|
||||||
// Check file version
|
|
||||||
final int version = getFormatVersion(buffer);
|
|
||||||
if (version < MINIMUM_SUPPORTED_VERSION || version > MAXIMUM_SUPPORTED_VERSION) {
|
|
||||||
throw new UnsupportedFormatException("This file has version " + version
|
|
||||||
+ ", but this implementation does not support versions above "
|
|
||||||
+ MAXIMUM_SUPPORTED_VERSION);
|
|
||||||
}
|
|
||||||
|
|
||||||
// clear cache
|
// clear cache
|
||||||
wordCache.clear();
|
wordCache.clear();
|
||||||
|
|
||||||
// Read options
|
// Read header
|
||||||
|
final int version = checkFormatVersion(buffer);
|
||||||
final int optionsFlags = buffer.readUnsignedShort();
|
final int optionsFlags = buffer.readUnsignedShort();
|
||||||
|
|
||||||
final int headerSize;
|
|
||||||
final HashMap<String, String> options = new HashMap<String, String>();
|
final HashMap<String, String> options = new HashMap<String, String>();
|
||||||
if (version < FIRST_VERSION_WITH_HEADER_SIZE) {
|
final int headerSize = readHeader(buffer, options, version);
|
||||||
headerSize = buffer.position();
|
|
||||||
} else {
|
|
||||||
headerSize = buffer.readInt();
|
|
||||||
populateOptions(buffer, headerSize, options);
|
|
||||||
buffer.position(headerSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (headerSize < 0) {
|
|
||||||
throw new UnsupportedFormatException("header size can't be negative.");
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<Integer, Node> reverseNodeMapping = new TreeMap<Integer, Node>();
|
Map<Integer, Node> reverseNodeMapping = new TreeMap<Integer, Node>();
|
||||||
Map<Integer, CharGroup> reverseGroupMapping = new TreeMap<Integer, CharGroup>();
|
Map<Integer, CharGroup> reverseGroupMapping = new TreeMap<Integer, CharGroup>();
|
||||||
|
|
Loading…
Reference in a new issue