diff --git a/CHANGELOG.md b/CHANGELOG.md index f34680a5..f1f31dbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,8 +11,8 @@ CHANGELOG ------------------ * Messages for `DeserializationException` have been improved, and the cause - is included, if any. Moreover, the message provides detail about the involved - types, if the exception is caused by an `IllegalArgumentException`. + is included, if any. Moreover, the message provides detail about the involved + types, if the exception is caused by an `IllegalArgumentException`. 2.0.0 (2020-10-13) ------------------ @@ -107,7 +107,6 @@ CHANGELOG * Several optimizations have been made to reduce allocations when decoding a record. Pull requests by Viktor Szathmáry. GitHub #16 & #17. - 1.0.0 (2014-09-29) ------------------ diff --git a/checkstyle.xml b/checkstyle.xml index ebc2c9c7..e40ecfc1 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -32,7 +32,7 @@ + default="checkstyle-suppressions.xml"/> @@ -362,14 +362,14 @@ + default="checkstyle-xpath-suppressions.xml"/> - + - - - + + + diff --git a/src/main/java/com/maxmind/db/BufferHolder.java b/src/main/java/com/maxmind/db/BufferHolder.java index 856bebcd..19f56ba8 100644 --- a/src/main/java/com/maxmind/db/BufferHolder.java +++ b/src/main/java/com/maxmind/db/BufferHolder.java @@ -1,5 +1,6 @@ package com.maxmind.db; +import com.maxmind.db.Reader.FileMode; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; @@ -9,23 +10,21 @@ import java.nio.channels.FileChannel; import java.nio.channels.FileChannel.MapMode; -import com.maxmind.db.Reader.FileMode; - final class BufferHolder { // DO NOT PASS OUTSIDE THIS CLASS. Doing so will remove thread safety. private final ByteBuffer buffer; BufferHolder(File database, FileMode mode) throws IOException { try ( - final RandomAccessFile file = new RandomAccessFile(database, "r"); - final FileChannel channel = file.getChannel() + final RandomAccessFile file = new RandomAccessFile(database, "r"); + final FileChannel channel = file.getChannel() ) { if (mode == FileMode.MEMORY) { final ByteBuffer buf = ByteBuffer.wrap(new byte[(int) channel.size()]); if (channel.read(buf) != buf.capacity()) { throw new IOException("Unable to read " - + database.getName() - + " into memory. Unexpected end of stream."); + + database.getName() + + " into memory. Unexpected end of stream."); } this.buffer = buf.asReadOnlyBuffer(); } else { diff --git a/src/main/java/com/maxmind/db/CachedConstructor.java b/src/main/java/com/maxmind/db/CachedConstructor.java index c6b4760e..2b5b3160 100644 --- a/src/main/java/com/maxmind/db/CachedConstructor.java +++ b/src/main/java/com/maxmind/db/CachedConstructor.java @@ -10,10 +10,10 @@ final class CachedConstructor { private final Map parameterIndexes; CachedConstructor( - Constructor constructor, - Class[] parameterTypes, - java.lang.reflect.Type[] parameterGenericTypes, - Map parameterIndexes + Constructor constructor, + Class[] parameterTypes, + java.lang.reflect.Type[] parameterGenericTypes, + Map parameterIndexes ) { this.constructor = constructor; this.parameterTypes = parameterTypes; diff --git a/src/main/java/com/maxmind/db/DecodedValue.java b/src/main/java/com/maxmind/db/DecodedValue.java index c116a59a..c7708183 100644 --- a/src/main/java/com/maxmind/db/DecodedValue.java +++ b/src/main/java/com/maxmind/db/DecodedValue.java @@ -1,7 +1,7 @@ package com.maxmind.db; public final class DecodedValue { - final Object value; + final Object value; DecodedValue(Object value) { this.value = value; diff --git a/src/main/java/com/maxmind/db/Decoder.java b/src/main/java/com/maxmind/db/Decoder.java index 3f69e325..8edd4e6e 100644 --- a/src/main/java/com/maxmind/db/Decoder.java +++ b/src/main/java/com/maxmind/db/Decoder.java @@ -12,10 +12,10 @@ import java.nio.charset.CharsetDecoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.concurrent.ConcurrentHashMap; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /* * Decoder for MaxMind DB data. @@ -26,7 +26,7 @@ final class Decoder { private static final Charset UTF_8 = StandardCharsets.UTF_8; - private static final int[] POINTER_VALUE_OFFSETS = {0, 0, 1 << 11, (1 << 19) + ((1) << 11), 0}; + private static final int[] POINTER_VALUE_OFFSETS = {0, 0, 1 << 11, (1 << 19) + (1 << 11), 0}; // XXX - This is only for unit testings. We should possibly make a // constructor to set this @@ -44,18 +44,18 @@ final class Decoder { Decoder(NodeCache cache, ByteBuffer buffer, long pointerBase) { this( - cache, - buffer, - pointerBase, - new ConcurrentHashMap<>() + cache, + buffer, + pointerBase, + new ConcurrentHashMap<>() ); } Decoder( - NodeCache cache, - ByteBuffer buffer, - long pointerBase, - ConcurrentHashMap constructors + NodeCache cache, + ByteBuffer buffer, + long pointerBase, + ConcurrentHashMap constructors ) { this.cache = cache; this.pointerBase = pointerBase; @@ -68,8 +68,8 @@ final class Decoder { public T decode(int offset, Class cls) throws IOException { if (offset >= this.buffer.capacity()) { throw new InvalidDatabaseException( - "The MaxMind DB file's data section contains bad data: " - + "pointer larger than the database."); + "The MaxMind DB file's data section contains bad data: " + + "pointer larger than the database."); } this.buffer.position(offset); @@ -80,8 +80,8 @@ private DecodedValue decode(CacheKey key) throws IOException { int offset = key.getOffset(); if (offset >= this.buffer.capacity()) { throw new InvalidDatabaseException( - "The MaxMind DB file's data section contains bad data: " - + "pointer larger than the database."); + "The MaxMind DB file's data section contains bad data: " + + "pointer larger than the database."); } this.buffer.position(offset); @@ -90,7 +90,7 @@ private DecodedValue decode(CacheKey key) throws IOException { } private DecodedValue decode(Class cls, java.lang.reflect.Type genericType) - throws IOException { + throws IOException { int ctrlByte = 0xFF & this.buffer.get(); Type type = Type.fromControlByte(ctrlByte); @@ -126,9 +126,9 @@ private DecodedValue decode(Class cls, java.lang.reflect.Type genericType if (typeNum < 8) { throw new InvalidDatabaseException( - "Something went horribly wrong in the decoder. An extended type " - + "resolved to a type number < 8 (" + typeNum - + ")"); + "Something went horribly wrong in the decoder. An extended type " + + "resolved to a type number < 8 (" + typeNum + + ")"); } type = Type.get(typeNum); @@ -152,10 +152,10 @@ private DecodedValue decode(Class cls, java.lang.reflect.Type genericType } private Object decodeByType( - Type type, - int size, - Class cls, - java.lang.reflect.Type genericType + Type type, + int size, + Class cls, + java.lang.reflect.Type genericType ) throws IOException { switch (type) { case MAP: @@ -192,7 +192,7 @@ private Object decodeByType( return this.decodeBigInteger(size); default: throw new InvalidDatabaseException( - "Unknown or unexpected type: " + type.name()); + "Unknown or unexpected type: " + type.name()); } } @@ -248,8 +248,8 @@ private BigInteger decodeBigInteger(int size) { private double decodeDouble(int size) throws InvalidDatabaseException { if (size != 8) { throw new InvalidDatabaseException( - "The MaxMind DB file's data section contains bad data: " - + "invalid size of double."); + "The MaxMind DB file's data section contains bad data: " + + "invalid size of double."); } return this.buffer.getDouble(); } @@ -257,14 +257,14 @@ private double decodeDouble(int size) throws InvalidDatabaseException { private float decodeFloat(int size) throws InvalidDatabaseException { if (size != 4) { throw new InvalidDatabaseException( - "The MaxMind DB file's data section contains bad data: " - + "invalid size of float."); + "The MaxMind DB file's data section contains bad data: " + + "invalid size of float."); } return this.buffer.getFloat(); } private static boolean decodeBoolean(int size) - throws InvalidDatabaseException { + throws InvalidDatabaseException { switch (size) { case 0: return false; @@ -272,15 +272,15 @@ private static boolean decodeBoolean(int size) return true; default: throw new InvalidDatabaseException( - "The MaxMind DB file's data section contains bad data: " - + "invalid size of boolean."); + "The MaxMind DB file's data section contains bad data: " + + "invalid size of boolean."); } } private List decodeArray( - int size, - Class cls, - Class elementClass + int size, + Class cls, + Class elementClass ) throws IOException { if (!List.class.isAssignableFrom(cls) && !cls.equals(Object.class)) { throw new DeserializationException("Unable to deserialize an array into an " + cls); @@ -294,7 +294,8 @@ private List decodeArray( try { constructor = cls.getConstructor(Integer.TYPE); } catch (NoSuchMethodException e) { - throw new DeserializationException("No constructor found for the List: " + e.getMessage(), e); + throw new DeserializationException( + "No constructor found for the List: " + e.getMessage(), e); } Object[] parameters = {size}; try { @@ -302,8 +303,8 @@ private List decodeArray( List array2 = (List) constructor.newInstance(parameters); array = array2; } catch (InstantiationException | - IllegalAccessException | - InvocationTargetException e) { + IllegalAccessException | + InvocationTargetException e) { throw new DeserializationException("Error creating list: " + e.getMessage(), e); } } @@ -317,9 +318,9 @@ private List decodeArray( } private Object decodeMap( - int size, - Class cls, - java.lang.reflect.Type genericType + int size, + Class cls, + java.lang.reflect.Type genericType ) throws IOException { if (Map.class.isAssignableFrom(cls) || cls.equals(Object.class)) { Class valueClass = Object.class; @@ -343,9 +344,9 @@ private Object decodeMap( } private Map decodeMapIntoMap( - Class cls, - int size, - Class valueClass + Class cls, + int size, + Class valueClass ) throws IOException { Map map; if (cls.equals(Map.class) || cls.equals(Object.class)) { @@ -355,7 +356,8 @@ private Map decodeMapIntoMap( try { constructor = cls.getConstructor(Integer.TYPE); } catch (NoSuchMethodException e) { - throw new DeserializationException("No constructor found for the Map: " + e.getMessage(), e); + throw new DeserializationException( + "No constructor found for the Map: " + e.getMessage(), e); } Object[] parameters = {size}; try { @@ -363,8 +365,8 @@ private Map decodeMapIntoMap( Map map2 = (Map) constructor.newInstance(parameters); map = map2; } catch (InstantiationException | - IllegalAccessException | - InvocationTargetException e) { + IllegalAccessException | + InvocationTargetException e) { throw new DeserializationException("Error creating map: " + e.getMessage(), e); } } @@ -379,7 +381,7 @@ private Map decodeMapIntoMap( } private Object decodeMapIntoObject(int size, Class cls) - throws IOException { + throws IOException { CachedConstructor cachedConstructor = this.constructors.get(cls); Constructor constructor; Class[] parameterTypes; @@ -400,13 +402,13 @@ private Object decodeMapIntoObject(int size, Class cls) } this.constructors.put( - cls, - new CachedConstructor( - constructor, - parameterTypes, - parameterGenericTypes, - parameterIndexes - ) + cls, + new CachedConstructor( + constructor, + parameterTypes, + parameterGenericTypes, + parameterIndexes + ) ); } else { constructor = cachedConstructor.getConstructor(); @@ -435,25 +437,26 @@ private Object decodeMapIntoObject(int size, Class cls) try { return constructor.newInstance(parameters); } catch (InstantiationException | - IllegalAccessException | - InvocationTargetException e) { + IllegalAccessException | + InvocationTargetException e) { throw new DeserializationException("Error creating object: " + e.getMessage(), e); - } - catch (IllegalArgumentException e){ - StringBuilder sbErrors = new StringBuilder(); + } catch (IllegalArgumentException e) { + StringBuilder sbErrors = new StringBuilder(); for (String key : parameterIndexes.keySet()) { int index = parameterIndexes.get(key); - if (!parameters[index].getClass().isAssignableFrom( parameterTypes[index])) { - sbErrors.append(" argument type mismatch in " + key + " MMDB Type: " + parameters[index].getClass().getCanonicalName() - + " Java Type: " +parameterTypes[index].getCanonicalName()); - } + if (!parameters[index].getClass().isAssignableFrom(parameterTypes[index])) { + sbErrors.append(" argument type mismatch in " + key + " MMDB Type: " + + parameters[index].getClass().getCanonicalName() + + " Java Type: " + parameterTypes[index].getCanonicalName()); + } } - throw new DeserializationException("Error creating object of type: " + cls.getSimpleName() + " - " + sbErrors, e); + throw new DeserializationException( + "Error creating object of type: " + cls.getSimpleName() + " - " + sbErrors, e); } } private static Constructor findConstructor(Class cls) - throws ConstructorNotFoundException { + throws ConstructorNotFoundException { Constructor[] constructors = cls.getConstructors(); for (Constructor constructor : constructors) { if (constructor.getAnnotation(MaxMindDbConstructor.class) == null) { @@ -464,13 +467,14 @@ private static Constructor findConstructor(Class cls) return constructor2; } - throw new ConstructorNotFoundException("No constructor on class " + cls.getName() + " with the MaxMindDbConstructor annotation was found."); + throw new ConstructorNotFoundException("No constructor on class " + cls.getName() + + " with the MaxMindDbConstructor annotation was found."); } private static String getParameterName( - Class cls, - int index, - Annotation[] annotations + Class cls, + int index, + Annotation[] annotations ) throws ParameterNotFoundException { for (Annotation annotation : annotations) { if (!annotation.annotationType().equals(MaxMindDbParameter.class)) { @@ -479,7 +483,9 @@ private static String getParameterName( MaxMindDbParameter paramAnnotation = (MaxMindDbParameter) annotation; return paramAnnotation.name(); } - throw new ParameterNotFoundException("Constructor parameter " + index + " on class " + cls.getName() + " is not annotated with MaxMindDbParameter."); + throw new ParameterNotFoundException( + "Constructor parameter " + index + " on class " + cls.getName() + + " is not annotated with MaxMindDbParameter."); } private int nextValueOffset(int offset, int numberToSkip) @@ -495,21 +501,21 @@ private int nextValueOffset(int offset, int numberToSkip) Type type = ctrlData.getType(); switch (type) { - case POINTER: - int pointerSize = ((ctrlByte >>> 3) & 0x3) + 1; - offset += pointerSize; - break; - case MAP: - numberToSkip += 2 * size; - break; - case ARRAY: - numberToSkip += size; - break; - case BOOLEAN: - break; - default: - offset += size; - break; + case POINTER: + int pointerSize = ((ctrlByte >>> 3) & 0x3) + 1; + offset += pointerSize; + break; + case MAP: + numberToSkip += 2 * size; + break; + case ARRAY: + numberToSkip += size; + break; + case BOOLEAN: + break; + default: + offset += size; + break; } return nextValueOffset(offset, numberToSkip - 1); @@ -519,8 +525,8 @@ private CtrlData getCtrlData(int offset) throws InvalidDatabaseException { if (offset >= this.buffer.capacity()) { throw new InvalidDatabaseException( - "The MaxMind DB file's data section contains bad data: " - + "pointer larger than the database."); + "The MaxMind DB file's data section contains bad data: " + + "pointer larger than the database."); } this.buffer.position(offset); @@ -536,9 +542,9 @@ private CtrlData getCtrlData(int offset) if (typeNum < 8) { throw new InvalidDatabaseException( - "Something went horribly wrong in the decoder. An extended type " - + "resolved to a type number < 8 (" + typeNum - + ")"); + "Something went horribly wrong in the decoder. An extended type " + + "resolved to a type number < 8 (" + typeNum + + ")"); } type = Type.get(typeNum); diff --git a/src/main/java/com/maxmind/db/DeserializationException.java b/src/main/java/com/maxmind/db/DeserializationException.java index 1a66aacf..c18b24ba 100644 --- a/src/main/java/com/maxmind/db/DeserializationException.java +++ b/src/main/java/com/maxmind/db/DeserializationException.java @@ -13,6 +13,7 @@ public class DeserializationException extends RuntimeException { DeserializationException(String message) { super(message); } + DeserializationException(String message, Throwable cause) { super(message, cause); } diff --git a/src/main/java/com/maxmind/db/MaxMindDbConstructor.java b/src/main/java/com/maxmind/db/MaxMindDbConstructor.java index 3d51d7ff..def9da19 100644 --- a/src/main/java/com/maxmind/db/MaxMindDbConstructor.java +++ b/src/main/java/com/maxmind/db/MaxMindDbConstructor.java @@ -4,4 +4,5 @@ import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) -public @interface MaxMindDbConstructor {} +public @interface MaxMindDbConstructor { +} diff --git a/src/main/java/com/maxmind/db/Metadata.java b/src/main/java/com/maxmind/db/Metadata.java index ff041706..46a6301c 100644 --- a/src/main/java/com/maxmind/db/Metadata.java +++ b/src/main/java/com/maxmind/db/Metadata.java @@ -29,24 +29,24 @@ public final class Metadata { @MaxMindDbConstructor public Metadata( - @MaxMindDbParameter(name="binary_format_major_version") - int binaryFormatMajorVersion, - @MaxMindDbParameter(name="binary_format_minor_version") - int binaryFormatMinorVersion, - @MaxMindDbParameter(name="build_epoch") - BigInteger buildEpoch, - @MaxMindDbParameter(name="database_type") - String databaseType, - @MaxMindDbParameter(name="languages") - List languages, - @MaxMindDbParameter(name="description") - Map description, - @MaxMindDbParameter(name="ip_version") - int ipVersion, - @MaxMindDbParameter(name="node_count") - long nodeCount, - @MaxMindDbParameter(name="record_size") - int recordSize + @MaxMindDbParameter(name = "binary_format_major_version") + int binaryFormatMajorVersion, + @MaxMindDbParameter(name = "binary_format_minor_version") + int binaryFormatMinorVersion, + @MaxMindDbParameter(name = "build_epoch") + BigInteger buildEpoch, + @MaxMindDbParameter(name = "database_type") + String databaseType, + @MaxMindDbParameter(name = "languages") + List languages, + @MaxMindDbParameter(name = "description") + Map description, + @MaxMindDbParameter(name = "ip_version") + int ipVersion, + @MaxMindDbParameter(name = "node_count") + long nodeCount, + @MaxMindDbParameter(name = "record_size") + int recordSize ) { this.binaryFormatMajorVersion = binaryFormatMajorVersion; this.binaryFormatMinorVersion = binaryFormatMinorVersion; @@ -151,11 +151,11 @@ int getSearchTreeSize() { @Override public String toString() { return "Metadata [binaryFormatMajorVersion=" - + this.binaryFormatMajorVersion + ", binaryFormatMinorVersion=" - + this.binaryFormatMinorVersion + ", buildEpoch=" - + this.buildEpoch + ", databaseType=" + this.databaseType - + ", description=" + this.description + ", ipVersion=" - + this.ipVersion + ", nodeCount=" + this.nodeCount - + ", recordSize=" + this.recordSize + "]"; + + this.binaryFormatMajorVersion + ", binaryFormatMinorVersion=" + + this.binaryFormatMinorVersion + ", buildEpoch=" + + this.buildEpoch + ", databaseType=" + this.databaseType + + ", description=" + this.description + ", ipVersion=" + + this.ipVersion + ", nodeCount=" + this.nodeCount + + ", recordSize=" + this.recordSize + "]"; } } diff --git a/src/main/java/com/maxmind/db/Network.java b/src/main/java/com/maxmind/db/Network.java index cc78594b..aee5c84f 100644 --- a/src/main/java/com/maxmind/db/Network.java +++ b/src/main/java/com/maxmind/db/Network.java @@ -46,7 +46,8 @@ public InetAddress getNetworkAddress() { try { networkAddress = InetAddress.getByAddress(networkBytes); } catch (UnknownHostException e) { - throw new RuntimeException("Illegal network address byte length of " + networkBytes.length); + throw new RuntimeException( + "Illegal network address byte length of " + networkBytes.length); } return networkAddress; } @@ -66,4 +67,4 @@ public int getPrefixLength() { public String toString() { return getNetworkAddress().getHostAddress() + "/" + prefixLength; } -} \ No newline at end of file +} diff --git a/src/main/java/com/maxmind/db/NoCache.java b/src/main/java/com/maxmind/db/NoCache.java index 7d968239..69e879c4 100644 --- a/src/main/java/com/maxmind/db/NoCache.java +++ b/src/main/java/com/maxmind/db/NoCache.java @@ -5,7 +5,7 @@ /** * A no-op cache singleton. */ -public class NoCache implements NodeCache { +public final class NoCache implements NodeCache { private static final NoCache INSTANCE = new NoCache(); diff --git a/src/main/java/com/maxmind/db/Reader.java b/src/main/java/com/maxmind/db/Reader.java index 1bcb6000..aa3330f6 100644 --- a/src/main/java/com/maxmind/db/Reader.java +++ b/src/main/java/com/maxmind/db/Reader.java @@ -4,12 +4,10 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.lang.Class; import java.net.InetAddress; import java.nio.ByteBuffer; -import java.util.concurrent.atomic.AtomicReference; -import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicReference; /** * Instances of this class provide a reader for the MaxMind DB format. IP @@ -18,8 +16,8 @@ public final class Reader implements Closeable { private static final int DATA_SECTION_SEPARATOR_SIZE = 16; private static final byte[] METADATA_START_MARKER = {(byte) 0xAB, - (byte) 0xCD, (byte) 0xEF, 'M', 'a', 'x', 'M', 'i', 'n', 'd', '.', - 'c', 'o', 'm'}; + (byte) 0xCD, (byte) 0xEF, 'M', 'a', 'x', 'M', 'i', 'n', 'd', '.', + 'c', 'o', 'm'}; private final int ipV4Start; private final Metadata metadata; @@ -120,7 +118,7 @@ public Reader(File database, FileMode fileMode, NodeCache cache) throws IOExcept private Reader(BufferHolder bufferHolder, String name, NodeCache cache) throws IOException { this.bufferHolderReference = new AtomicReference<>( - bufferHolder); + bufferHolder); if (cache == null) { throw new NullPointerException("Cache cannot be null"); @@ -141,9 +139,9 @@ private Reader(BufferHolder bufferHolder, String name, NodeCache cache) throws I /** * Looks up ipAddress in the MaxMind DB. * - * @param the type to populate. + * @param the type to populate. * @param ipAddress the IP address to look up. - * @param cls the class of object to populate. + * @param cls the class of object to populate. * @return the object. * @throws IOException if a file I/O error occurs. */ @@ -154,15 +152,15 @@ public T get(InetAddress ipAddress, Class cls) throws IOException { /** * Looks up ipAddress in the MaxMind DB. * - * @param the type to populate. + * @param the type to populate. * @param ipAddress the IP address to look up. - * @param cls the class of object to populate. + * @param cls the class of object to populate. * @return the record for the IP address. If there is no data for the * address, the non-null {@link DatabaseRecord} will still be returned. * @throws IOException if a file I/O error occurs. */ public DatabaseRecord getRecord(InetAddress ipAddress, Class cls) - throws IOException { + throws IOException { ByteBuffer buffer = this.getBufferHolder().get(); byte[] rawAddress = ipAddress.getAddress(); @@ -184,7 +182,9 @@ record = this.readNode(buffer, record, bit); try { dataRecord = this.resolveDataPointer(buffer, record, cls); } catch (DeserializationException exception) { - throw new DeserializationException("Error getting record for IP " + ipAddress + " - " + exception.getMessage(), exception); + throw new DeserializationException( + "Error getting record for IP " + ipAddress + " - " + exception.getMessage(), + exception); } } return new DatabaseRecord<>(dataRecord, ipAddress, pl); @@ -210,7 +210,7 @@ private int startNode(int bitLength) { } private int findIpV4StartNode(ByteBuffer buffer) - throws InvalidDatabaseException { + throws InvalidDatabaseException { if (this.metadata.getIpVersion() == 4) { return 0; } @@ -223,7 +223,7 @@ private int findIpV4StartNode(ByteBuffer buffer) } private int readNode(ByteBuffer buffer, int nodeNumber, int index) - throws InvalidDatabaseException { + throws InvalidDatabaseException { int baseOffset = nodeNumber * this.metadata.getNodeByteSize(); switch (this.metadata.getRecordSize()) { @@ -245,31 +245,31 @@ private int readNode(ByteBuffer buffer, int nodeNumber, int index) return Decoder.decodeInteger(buffer, 0, 4); default: throw new InvalidDatabaseException("Unknown record size: " - + this.metadata.getRecordSize()); + + this.metadata.getRecordSize()); } } private T resolveDataPointer( - ByteBuffer buffer, - int pointer, - Class cls + ByteBuffer buffer, + int pointer, + Class cls ) throws IOException { int resolved = (pointer - this.metadata.getNodeCount()) - + this.metadata.getSearchTreeSize(); + + this.metadata.getSearchTreeSize(); if (resolved >= buffer.capacity()) { throw new InvalidDatabaseException( - "The MaxMind DB file's search tree is corrupt: " - + "contains pointer larger than the database."); + "The MaxMind DB file's search tree is corrupt: " + + "contains pointer larger than the database."); } // We only want the data from the decoder, not the offset where it was // found. Decoder decoder = new Decoder( - this.cache, - buffer, - this.metadata.getSearchTreeSize() + DATA_SECTION_SEPARATOR_SIZE, - this.constructors + this.cache, + buffer, + this.metadata.getSearchTreeSize() + DATA_SECTION_SEPARATOR_SIZE, + this.constructors ); return decoder.decode(resolved, cls); } @@ -283,7 +283,7 @@ private T resolveDataPointer( * an issue, but I suspect it won't be. */ private int findMetadataStart(ByteBuffer buffer, String databaseName) - throws InvalidDatabaseException { + throws InvalidDatabaseException { int fileSize = buffer.capacity(); FILE: @@ -291,15 +291,15 @@ private int findMetadataStart(ByteBuffer buffer, String databaseName) for (int j = 0; j < METADATA_START_MARKER.length; j++) { byte b = buffer.get(fileSize - i - j - 1); if (b != METADATA_START_MARKER[METADATA_START_MARKER.length - j - - 1]) { + - 1]) { continue FILE; } } return fileSize - i; } throw new InvalidDatabaseException( - "Could not find a MaxMind DB metadata marker in this file (" - + databaseName + "). Is this a valid MaxMind DB file?"); + "Could not find a MaxMind DB metadata marker in this file (" + + databaseName + "). Is this a valid MaxMind DB file?"); } /** diff --git a/src/main/java/com/maxmind/db/Type.java b/src/main/java/com/maxmind/db/Type.java index 84f67f8c..a438a736 100644 --- a/src/main/java/com/maxmind/db/Type.java +++ b/src/main/java/com/maxmind/db/Type.java @@ -1,15 +1,17 @@ package com.maxmind.db; enum Type { - EXTENDED, POINTER, UTF8_STRING, DOUBLE, BYTES, UINT16, UINT32, MAP, INT32, UINT64, UINT128, ARRAY, CONTAINER, END_MARKER, BOOLEAN, FLOAT; + EXTENDED, POINTER, UTF8_STRING, DOUBLE, BYTES, UINT16, UINT32, MAP, INT32, UINT64, UINT128, + ARRAY, CONTAINER, END_MARKER, BOOLEAN, FLOAT; // Java clones the array when you call values(). Caching it increased // the speed by about 5000 requests per second on my machine. - final static Type[] values = Type.values(); + static final Type[] values = Type.values(); static Type get(int i) throws InvalidDatabaseException { if (i >= Type.values.length) { - throw new InvalidDatabaseException("The MaxMind DB file's data section contains bad data"); + throw new InvalidDatabaseException( + "The MaxMind DB file's data section contains bad data"); } return Type.values[i]; } diff --git a/src/main/java/com/maxmind/db/package-info.java b/src/main/java/com/maxmind/db/package-info.java index 73aed012..897b0615 100644 --- a/src/main/java/com/maxmind/db/package-info.java +++ b/src/main/java/com/maxmind/db/package-info.java @@ -1,4 +1,5 @@ /** * @author greg */ -package com.maxmind.db; \ No newline at end of file + +package com.maxmind.db; diff --git a/src/test/java/com/maxmind/db/DecoderTest.java b/src/test/java/com/maxmind/db/DecoderTest.java index d8f71057..be9cb660 100644 --- a/src/test/java/com/maxmind/db/DecoderTest.java +++ b/src/test/java/com/maxmind/db/DecoderTest.java @@ -1,5 +1,11 @@ package com.maxmind.db; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; @@ -14,13 +20,8 @@ import java.util.List; import java.util.Map; import java.util.UUID; - import org.junit.Test; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.*; - @SuppressWarnings({"boxing", "static-method"}) public class DecoderTest { @@ -28,28 +29,28 @@ private static Map int32() { int max = (2 << 30) - 1; HashMap int32 = new HashMap<>(); - int32.put(0, new byte[]{0x0, 0x1}); - int32.put(-1, new byte[]{0x4, 0x1, (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff}); - int32.put((2 << 7) - 1, new byte[]{0x1, 0x1, (byte) 0xff}); - int32.put(1 - (2 << 7), new byte[]{0x4, 0x1, (byte) 0xff, - (byte) 0xff, (byte) 0xff, 0x1}); - int32.put(500, new byte[]{0x2, 0x1, 0x1, (byte) 0xf4}); - - int32.put(-500, new byte[]{0x4, 0x1, (byte) 0xff, (byte) 0xff, - (byte) 0xfe, 0xc}); - - int32.put((2 << 15) - 1, new byte[]{0x2, 0x1, (byte) 0xff, - (byte) 0xff}); - int32.put(1 - (2 << 15), new byte[]{0x4, 0x1, (byte) 0xff, - (byte) 0xff, 0x0, 0x1}); - int32.put((2 << 23) - 1, new byte[]{0x3, 0x1, (byte) 0xff, - (byte) 0xff, (byte) 0xff}); - int32.put(1 - (2 << 23), new byte[]{0x4, 0x1, (byte) 0xff, 0x0, 0x0, - 0x1}); - int32.put(max, new byte[]{0x4, 0x1, 0x7f, (byte) 0xff, (byte) 0xff, - (byte) 0xff}); - int32.put(-max, new byte[]{0x4, 0x1, (byte) 0x80, 0x0, 0x0, 0x1}); + int32.put(0, new byte[] {0x0, 0x1}); + int32.put(-1, new byte[] {0x4, 0x1, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff}); + int32.put((2 << 7) - 1, new byte[] {0x1, 0x1, (byte) 0xff}); + int32.put(1 - (2 << 7), new byte[] {0x4, 0x1, (byte) 0xff, + (byte) 0xff, (byte) 0xff, 0x1}); + int32.put(500, new byte[] {0x2, 0x1, 0x1, (byte) 0xf4}); + + int32.put(-500, new byte[] {0x4, 0x1, (byte) 0xff, (byte) 0xff, + (byte) 0xfe, 0xc}); + + int32.put((2 << 15) - 1, new byte[] {0x2, 0x1, (byte) 0xff, + (byte) 0xff}); + int32.put(1 - (2 << 15), new byte[] {0x4, 0x1, (byte) 0xff, + (byte) 0xff, 0x0, 0x1}); + int32.put((2 << 23) - 1, new byte[] {0x3, 0x1, (byte) 0xff, + (byte) 0xff, (byte) 0xff}); + int32.put(1 - (2 << 23), new byte[] {0x4, 0x1, (byte) 0xff, 0x0, 0x0, + 0x1}); + int32.put(max, new byte[] {0x4, 0x1, 0x7f, (byte) 0xff, (byte) 0xff, + (byte) 0xff}); + int32.put(-max, new byte[] {0x4, 0x1, (byte) 0x80, 0x0, 0x0, 0x1}); return int32; } @@ -57,17 +58,17 @@ private static Map uint32() { long max = (((long) 1) << 32) - 1; HashMap uint32s = new HashMap<>(); - uint32s.put((long) 0, new byte[]{(byte) 0xc0}); - uint32s.put((long) ((1 << 8) - 1), new byte[]{(byte) 0xc1, - (byte) 0xff}); - uint32s.put((long) 500, new byte[]{(byte) 0xc2, 0x1, (byte) 0xf4}); - uint32s.put((long) 10872, new byte[]{(byte) 0xc2, 0x2a, 0x78}); - uint32s.put((long) ((1 << 16) - 1), new byte[]{(byte) 0xc2, - (byte) 0xff, (byte) 0xff}); - uint32s.put((long) ((1 << 24) - 1), new byte[]{(byte) 0xc3, - (byte) 0xff, (byte) 0xff, (byte) 0xff}); - uint32s.put(max, new byte[]{(byte) 0xc4, (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff}); + uint32s.put((long) 0, new byte[] {(byte) 0xc0}); + uint32s.put((long) ((1 << 8) - 1), new byte[] {(byte) 0xc1, + (byte) 0xff}); + uint32s.put((long) 500, new byte[] {(byte) 0xc2, 0x1, (byte) 0xf4}); + uint32s.put((long) 10872, new byte[] {(byte) 0xc2, 0x2a, 0x78}); + uint32s.put((long) ((1 << 16) - 1), new byte[] {(byte) 0xc2, + (byte) 0xff, (byte) 0xff}); + uint32s.put((long) ((1 << 24) - 1), new byte[] {(byte) 0xc3, + (byte) 0xff, (byte) 0xff, (byte) 0xff}); + uint32s.put(max, new byte[] {(byte) 0xc4, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff}); return uint32s; } @@ -77,11 +78,11 @@ private static Map uint16() { Map uint16s = new HashMap<>(); - uint16s.put(0, new byte[]{(byte) 0xa0}); - uint16s.put((1 << 8) - 1, new byte[]{(byte) 0xa1, (byte) 0xff}); - uint16s.put(500, new byte[]{(byte) 0xa2, 0x1, (byte) 0xf4}); - uint16s.put(10872, new byte[]{(byte) 0xa2, 0x2a, 0x78}); - uint16s.put(max, new byte[]{(byte) 0xa2, (byte) 0xff, (byte) 0xff}); + uint16s.put(0, new byte[] {(byte) 0xa0}); + uint16s.put((1 << 8) - 1, new byte[] {(byte) 0xa1, (byte) 0xff}); + uint16s.put(500, new byte[] {(byte) 0xa2, 0x1, (byte) 0xf4}); + uint16s.put(10872, new byte[] {(byte) 0xa2, 0x2a, 0x78}); + uint16s.put(max, new byte[] {(byte) 0xa2, (byte) 0xff, (byte) 0xff}); return uint16s; } @@ -90,16 +91,16 @@ private static Map largeUint(int bits) { byte ctrlByte = (byte) (bits == 64 ? 0x2 : 0x3); - uints.put(BigInteger.valueOf(0), new byte[]{0x0, ctrlByte}); - uints.put(BigInteger.valueOf(500), new byte[]{0x2, ctrlByte, 0x1, - (byte) 0xf4}); - uints.put(BigInteger.valueOf(10872), new byte[]{0x2, ctrlByte, 0x2a, - 0x78}); + uints.put(BigInteger.valueOf(0), new byte[] {0x0, ctrlByte}); + uints.put(BigInteger.valueOf(500), new byte[] {0x2, ctrlByte, 0x1, + (byte) 0xf4}); + uints.put(BigInteger.valueOf(10872), new byte[] {0x2, ctrlByte, 0x2a, + 0x78}); for (int power = 1; power <= bits / 8; power++) { BigInteger key = BigInteger.valueOf(2).pow(8 * power) - .subtract(BigInteger.valueOf(1)); + .subtract(BigInteger.valueOf(1)); byte[] value = new byte[2 + power]; value[0] = (byte) power; @@ -116,22 +117,22 @@ private static Map largeUint(int bits) { private static Map pointers() { Map pointers = new HashMap<>(); - pointers.put((long) 0, new byte[]{0x20, 0x0}); - pointers.put((long) 5, new byte[]{0x20, 0x5}); - pointers.put((long) 10, new byte[]{0x20, 0xa}); - pointers.put((long) ((1 << 10) - 1), new byte[]{0x23, (byte) 0xff,}); - pointers.put((long) 3017, new byte[]{0x28, 0x3, (byte) 0xc9}); - pointers.put((long) ((1 << 19) - 5), new byte[]{0x2f, (byte) 0xf7, - (byte) 0xfb}); - pointers.put((long) ((1 << 19) + (1 << 11) - 1), new byte[]{0x2f, - (byte) 0xff, (byte) 0xff}); - pointers.put((long) ((1 << 27) - 2), new byte[]{0x37, (byte) 0xf7, - (byte) 0xf7, (byte) 0xfe}); + pointers.put((long) 0, new byte[] {0x20, 0x0}); + pointers.put((long) 5, new byte[] {0x20, 0x5}); + pointers.put((long) 10, new byte[] {0x20, 0xa}); + pointers.put((long) ((1 << 10) - 1), new byte[] {0x23, (byte) 0xff,}); + pointers.put((long) 3017, new byte[] {0x28, 0x3, (byte) 0xc9}); + pointers.put((long) ((1 << 19) - 5), new byte[] {0x2f, (byte) 0xf7, + (byte) 0xfb}); + pointers.put((long) ((1 << 19) + (1 << 11) - 1), new byte[] {0x2f, + (byte) 0xff, (byte) 0xff}); + pointers.put((long) ((1 << 27) - 2), new byte[] {0x37, (byte) 0xf7, + (byte) 0xf7, (byte) 0xfe}); pointers.put((((long) 1) << 27) + (1 << 19) + (1 << 11) - 1, - new byte[]{0x37, (byte) 0xff, (byte) 0xff, (byte) 0xff}); + new byte[] {0x37, (byte) 0xff, (byte) 0xff, (byte) 0xff}); - pointers.put((((long) 1) << 31) - 1, new byte[]{0x38, (byte) 0x7f, - (byte) 0xff, (byte) 0xff, (byte) 0xff}); + pointers.put((((long) 1) << 31) - 1, new byte[] {0x38, (byte) 0x7f, + (byte) 0xff, (byte) 0xff, (byte) 0xff}); return pointers; } @@ -144,29 +145,29 @@ private static Map strings() { DecoderTest.addTestString(strings, (byte) 0x43, "人"); DecoderTest.addTestString(strings, (byte) 0x43, "123"); DecoderTest.addTestString(strings, (byte) 0x5b, - "123456789012345678901234567"); + "123456789012345678901234567"); DecoderTest.addTestString(strings, (byte) 0x5c, - "1234567890123456789012345678"); + "1234567890123456789012345678"); DecoderTest.addTestString(strings, (byte) 0x5c, - "1234567890123456789012345678"); - DecoderTest.addTestString(strings, new byte[]{0x5d, 0x0}, - "12345678901234567890123456789"); - DecoderTest.addTestString(strings, new byte[]{0x5d, (byte) 128}, - "x".repeat(157)); + "1234567890123456789012345678"); + DecoderTest.addTestString(strings, new byte[] {0x5d, 0x0}, + "12345678901234567890123456789"); + DecoderTest.addTestString(strings, new byte[] {0x5d, (byte) 128}, + "x".repeat(157)); DecoderTest - .addTestString(strings, new byte[]{0x5d, 0x0, (byte) 0xd7}, - "x".repeat(500)); + .addTestString(strings, new byte[] {0x5d, 0x0, (byte) 0xd7}, + "x".repeat(500)); DecoderTest - .addTestString(strings, new byte[]{0x5e, 0x0, (byte) 0xd7}, - "x".repeat(500)); + .addTestString(strings, new byte[] {0x5e, 0x0, (byte) 0xd7}, + "x".repeat(500)); DecoderTest.addTestString(strings, - new byte[]{0x5e, 0x6, (byte) 0xb3}, - "x".repeat(2000)); + new byte[] {0x5e, 0x6, (byte) 0xb3}, + "x".repeat(2000)); DecoderTest.addTestString(strings, - new byte[]{0x5f, 0x0, 0x10, 0x53,}, - "x".repeat(70000)); + new byte[] {0x5f, 0x0, 0x10, 0x53,}, + "x".repeat(70000)); return strings; @@ -189,7 +190,7 @@ private static Map bytes() { private static void addTestString(Map tests, byte ctrl, String str) { - DecoderTest.addTestString(tests, new byte[]{ctrl}, str); + DecoderTest.addTestString(tests, new byte[] {ctrl}, str); } private static void addTestString(Map tests, byte[] ctrl, @@ -205,45 +206,45 @@ private static void addTestString(Map tests, byte[] ctrl, private static Map doubles() { Map doubles = new HashMap<>(); - doubles.put(0.0, new byte[]{0x68, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, - 0x0}); - doubles.put(0.5, new byte[]{0x68, 0x3F, (byte) 0xE0, 0x0, 0x0, 0x0, - 0x0, 0x0, 0x0}); - doubles.put(3.14159265359, new byte[]{0x68, 0x40, 0x9, 0x21, - (byte) 0xFB, 0x54, 0x44, 0x2E, (byte) 0xEA}); - doubles.put(123.0, new byte[]{0x68, 0x40, 0x5E, (byte) 0xC0, 0x0, - 0x0, 0x0, 0x0, 0x0}); - doubles.put(1073741824.12457, new byte[]{0x68, 0x41, (byte) 0xD0, - 0x0, 0x0, 0x0, 0x7, (byte) 0xF8, (byte) 0xF4}); - doubles.put(-0.5, new byte[]{0x68, (byte) 0xBF, (byte) 0xE0, 0x0, - 0x0, 0x0, 0x0, 0x0, 0x0}); - doubles.put(-3.14159265359, new byte[]{0x68, (byte) 0xC0, 0x9, 0x21, - (byte) 0xFB, 0x54, 0x44, 0x2E, (byte) 0xEA}); - doubles.put(-1073741824.12457, new byte[]{0x68, (byte) 0xC1, - (byte) 0xD0, 0x0, 0x0, 0x0, 0x7, (byte) 0xF8, (byte) 0xF4}); + doubles.put(0.0, new byte[] {0x68, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x0}); + doubles.put(0.5, new byte[] {0x68, 0x3F, (byte) 0xE0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0}); + doubles.put(3.14159265359, new byte[] {0x68, 0x40, 0x9, 0x21, + (byte) 0xFB, 0x54, 0x44, 0x2E, (byte) 0xEA}); + doubles.put(123.0, new byte[] {0x68, 0x40, 0x5E, (byte) 0xC0, 0x0, + 0x0, 0x0, 0x0, 0x0}); + doubles.put(1073741824.12457, new byte[] {0x68, 0x41, (byte) 0xD0, + 0x0, 0x0, 0x0, 0x7, (byte) 0xF8, (byte) 0xF4}); + doubles.put(-0.5, new byte[] {0x68, (byte) 0xBF, (byte) 0xE0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0}); + doubles.put(-3.14159265359, new byte[] {0x68, (byte) 0xC0, 0x9, 0x21, + (byte) 0xFB, 0x54, 0x44, 0x2E, (byte) 0xEA}); + doubles.put(-1073741824.12457, new byte[] {0x68, (byte) 0xC1, + (byte) 0xD0, 0x0, 0x0, 0x0, 0x7, (byte) 0xF8, (byte) 0xF4}); return doubles; } private static Map floats() { Map floats = new HashMap<>(); - floats.put((float) 0.0, new byte[]{0x4, 0x8, 0x0, 0x0, 0x0, 0x0}); - floats.put((float) 1.0, new byte[]{0x4, 0x8, 0x3F, (byte) 0x80, 0x0, - 0x0}); - floats.put((float) 1.1, new byte[]{0x4, 0x8, 0x3F, (byte) 0x8C, - (byte) 0xCC, (byte) 0xCD}); - floats.put((float) 3.14, new byte[]{0x4, 0x8, 0x40, 0x48, - (byte) 0xF5, (byte) 0xC3}); - floats.put((float) 9999.99, new byte[]{0x4, 0x8, 0x46, 0x1C, 0x3F, - (byte) 0xF6}); - floats.put((float) -1.0, new byte[]{0x4, 0x8, (byte) 0xBF, - (byte) 0x80, 0x0, 0x0}); - floats.put((float) -1.1, new byte[]{0x4, 0x8, (byte) 0xBF, - (byte) 0x8C, (byte) 0xCC, (byte) 0xCD}); - floats.put((float) -3.14, new byte[]{0x4, 0x8, (byte) 0xC0, 0x48, - (byte) 0xF5, (byte) 0xC3}); - floats.put((float) -9999.99, new byte[]{0x4, 0x8, (byte) 0xC6, 0x1C, - 0x3F, (byte) 0xF6}); + floats.put((float) 0.0, new byte[] {0x4, 0x8, 0x0, 0x0, 0x0, 0x0}); + floats.put((float) 1.0, new byte[] {0x4, 0x8, 0x3F, (byte) 0x80, 0x0, + 0x0}); + floats.put((float) 1.1, new byte[] {0x4, 0x8, 0x3F, (byte) 0x8C, + (byte) 0xCC, (byte) 0xCD}); + floats.put((float) 3.14, new byte[] {0x4, 0x8, 0x40, 0x48, + (byte) 0xF5, (byte) 0xC3}); + floats.put((float) 9999.99, new byte[] {0x4, 0x8, 0x46, 0x1C, 0x3F, + (byte) 0xF6}); + floats.put((float) -1.0, new byte[] {0x4, 0x8, (byte) 0xBF, + (byte) 0x80, 0x0, 0x0}); + floats.put((float) -1.1, new byte[] {0x4, 0x8, (byte) 0xBF, + (byte) 0x8C, (byte) 0xCC, (byte) 0xCD}); + floats.put((float) -3.14, new byte[] {0x4, 0x8, (byte) 0xC0, 0x48, + (byte) 0xF5, (byte) 0xC3}); + floats.put((float) -9999.99, new byte[] {0x4, 0x8, (byte) 0xC6, 0x1C, + 0x3F, (byte) 0xF6}); return floats; } @@ -251,8 +252,8 @@ private static Map floats() { private static Map booleans() { Map booleans = new HashMap<>(); - booleans.put(Boolean.FALSE, new byte[]{0x0, 0x7}); - booleans.put(Boolean.TRUE, new byte[]{0x1, 0x7}); + booleans.put(Boolean.FALSE, new byte[] {0x0, 0x7}); + booleans.put(Boolean.TRUE, new byte[] {0x1, 0x7}); return booleans; } @@ -260,52 +261,52 @@ private static Map maps() { Map maps = new HashMap<>(); Map empty = new HashMap(); - maps.put(empty, new byte[]{(byte) 0xe0}); + maps.put(empty, new byte[] {(byte) 0xe0}); Map one = new HashMap<>(); one.put("en", "Foo"); - maps.put(one, new byte[]{(byte) 0xe1, /* en */0x42, 0x65, 0x6e, - /* Foo */0x43, 0x46, 0x6f, 0x6f}); + maps.put(one, new byte[] {(byte) 0xe1, /* en */0x42, 0x65, 0x6e, + /* Foo */0x43, 0x46, 0x6f, 0x6f}); Map two = new HashMap<>(); two.put("en", "Foo"); two.put("zh", "人"); - maps.put(two, new byte[]{(byte) 0xe2, - /* en */ - 0x42, 0x65, 0x6e, - /* Foo */ - 0x43, 0x46, 0x6f, 0x6f, - /* zh */ - 0x42, 0x7a, 0x68, - /* 人 */ - 0x43, (byte) 0xe4, (byte) 0xba, (byte) 0xba}); + maps.put(two, new byte[] {(byte) 0xe2, + /* en */ + 0x42, 0x65, 0x6e, + /* Foo */ + 0x43, 0x46, 0x6f, 0x6f, + /* zh */ + 0x42, 0x7a, 0x68, + /* 人 */ + 0x43, (byte) 0xe4, (byte) 0xba, (byte) 0xba}); Map> nested = new HashMap<>(); nested.put("name", two); - maps.put(nested, new byte[]{(byte) 0xe1, /* name */ - 0x44, 0x6e, 0x61, 0x6d, 0x65, (byte) 0xe2,/* en */ - 0x42, 0x65, 0x6e, - /* Foo */ - 0x43, 0x46, 0x6f, 0x6f, - /* zh */ - 0x42, 0x7a, 0x68, - /* 人 */ - 0x43, (byte) 0xe4, (byte) 0xba, (byte) 0xba}); + maps.put(nested, new byte[] {(byte) 0xe1, /* name */ + 0x44, 0x6e, 0x61, 0x6d, 0x65, (byte) 0xe2, /* en */ + 0x42, 0x65, 0x6e, + /* Foo */ + 0x43, 0x46, 0x6f, 0x6f, + /* zh */ + 0x42, 0x7a, 0x68, + /* 人 */ + 0x43, (byte) 0xe4, (byte) 0xba, (byte) 0xba}); Map> guess = new HashMap<>(); List languages = new ArrayList<>(); languages.add("en"); languages.add("zh"); guess.put("languages", languages); - maps.put(guess, new byte[]{(byte) 0xe1,/* languages */ - 0x49, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x73, - /* array */ - 0x2, 0x4, - /* en */ - 0x42, 0x65, 0x6e, - /* zh */ - 0x42, 0x7a, 0x68}); + maps.put(guess, new byte[] {(byte) 0xe1, /* languages */ + 0x49, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x73, + /* array */ + 0x2, 0x4, + /* en */ + 0x42, 0x65, 0x6e, + /* zh */ + 0x42, 0x7a, 0x68}); return maps; } @@ -315,21 +316,21 @@ private static Map, byte[]> arrays() { ArrayList f1 = new ArrayList<>(); f1.add("Foo"); - arrays.put(f1, new byte[]{0x1, 0x4, - /* Foo */ - 0x43, 0x46, 0x6f, 0x6f}); + arrays.put(f1, new byte[] {0x1, 0x4, + /* Foo */ + 0x43, 0x46, 0x6f, 0x6f}); ArrayList f2 = new ArrayList<>(); f2.add("Foo"); f2.add("人"); - arrays.put(f2, new byte[]{0x2, 0x4, - /* Foo */ - 0x43, 0x46, 0x6f, 0x6f, - /* 人 */ - 0x43, (byte) 0xe4, (byte) 0xba, (byte) 0xba}); + arrays.put(f2, new byte[] {0x2, 0x4, + /* Foo */ + 0x43, 0x46, 0x6f, 0x6f, + /* 人 */ + 0x43, (byte) 0xe4, (byte) 0xba, (byte) 0xba}); ArrayList empty = new ArrayList<>(); - arrays.put(empty, new byte[]{0x0, 0x4}); + arrays.put(empty, new byte[] {0x0, 0x4}); return arrays; } @@ -362,7 +363,7 @@ public void testUint128() throws IOException { @Test public void testDoubles() throws IOException { DecoderTest - .testTypeDecoding(Type.DOUBLE, DecoderTest.doubles()); + .testTypeDecoding(Type.DOUBLE, DecoderTest.doubles()); } @Test @@ -378,13 +379,13 @@ public void testPointers() throws IOException { @Test public void testStrings() throws IOException { DecoderTest.testTypeDecoding(Type.UTF8_STRING, - DecoderTest.strings()); + DecoderTest.strings()); } @Test public void testBooleans() throws IOException { DecoderTest.testTypeDecoding(Type.BOOLEAN, - DecoderTest.booleans()); + DecoderTest.booleans()); } @Test @@ -404,19 +405,20 @@ public void testArrays() throws IOException { @Test public void testInvalidControlByte() throws IOException { - try (FileChannel fc = DecoderTest.getFileChannel(new byte[]{0x0, 0xF})) { + try (FileChannel fc = DecoderTest.getFileChannel(new byte[] {0x0, 0xF})) { MappedByteBuffer mmap = fc.map(MapMode.READ_ONLY, 0, fc.size()); Decoder decoder = new Decoder(new CHMCache(), mmap, 0); InvalidDatabaseException ex = assertThrows( - InvalidDatabaseException.class, - () -> decoder.decode(0, String.class)); - assertThat(ex.getMessage(), containsString("The MaxMind DB file's data section contains bad data")); + InvalidDatabaseException.class, + () -> decoder.decode(0, String.class)); + assertThat(ex.getMessage(), + containsString("The MaxMind DB file's data section contains bad data")); } } private static void testTypeDecoding(Type type, Map tests) - throws IOException { + throws IOException { NodeCache cache = new CHMCache(); for (Map.Entry entry : tests.entrySet()) { @@ -436,18 +438,18 @@ private static void testTypeDecoding(Type type, Map tests) } else if (type.equals(Type.ARRAY)) { assertEquals(desc, expect, decoder.decode(0, List.class)); } else if (type.equals(Type.UINT16) - || type.equals(Type.INT32)) { + || type.equals(Type.INT32)) { assertEquals(desc, expect, decoder.decode(0, Integer.class)); } else if (type.equals(Type.UINT32) - || type.equals(Type.POINTER)) { + || type.equals(Type.POINTER)) { assertEquals(desc, expect, decoder.decode(0, Long.class)); } else if (type.equals(Type.UINT64) - || type.equals(Type.UINT128)) { + || type.equals(Type.UINT128)) { assertEquals(desc, expect, decoder.decode(0, BigInteger.class)); } else if (type.equals(Type.DOUBLE)) { assertEquals(desc, expect, decoder.decode(0, Double.class)); } else if (type.equals(Type.FLOAT)) { - assertEquals(desc, (Float) expect, decoder.decode(0, Float.class)); + assertEquals(desc, expect, decoder.decode(0, Float.class)); } else if (type.equals(Type.UTF8_STRING)) { assertEquals(desc, expect, decoder.decode(0, String.class)); } else if (type.equals(Type.BOOLEAN)) { diff --git a/src/test/java/com/maxmind/db/MultiThreadedTest.java b/src/test/java/com/maxmind/db/MultiThreadedTest.java index 8a57c33d..b6900aa3 100644 --- a/src/test/java/com/maxmind/db/MultiThreadedTest.java +++ b/src/test/java/com/maxmind/db/MultiThreadedTest.java @@ -12,14 +12,13 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; - import org.junit.Test; public class MultiThreadedTest { @Test public void multipleMmapOpens() throws InterruptedException, - ExecutionException { + ExecutionException { Callable task = () -> { try (Reader reader = new Reader(ReaderTest.getFile("MaxMind-DB-test-decoder.mmdb"))) { return reader.get(InetAddress.getByName("::1.1.1.0"), Map.class); @@ -30,7 +29,7 @@ public void multipleMmapOpens() throws InterruptedException, @Test public void streamThreadTest() throws IOException, InterruptedException, - ExecutionException { + ExecutionException { try (Reader reader = new Reader(ReaderTest.getStream("MaxMind-DB-test-decoder.mmdb"))) { MultiThreadedTest.threadTest(reader); } @@ -38,30 +37,30 @@ public void streamThreadTest() throws IOException, InterruptedException, @Test public void mmapThreadTest() throws IOException, InterruptedException, - ExecutionException { + ExecutionException { try (Reader reader = new Reader(ReaderTest.getFile("MaxMind-DB-test-decoder.mmdb"))) { MultiThreadedTest.threadTest(reader); } } private static void threadTest(final Reader reader) - throws InterruptedException, ExecutionException { + throws InterruptedException, ExecutionException { Callable task = () -> reader.get(InetAddress.getByName("::1.1.1.0"), Map.class); MultiThreadedTest.runThreads(task); } private static void runThreads(Callable task) - throws InterruptedException, ExecutionException { + throws InterruptedException, ExecutionException { int threadCount = 256; List> tasks = Collections.nCopies(threadCount, task); ExecutorService executorService = Executors - .newFixedThreadPool(threadCount); + .newFixedThreadPool(threadCount); List> futures = executorService.invokeAll(tasks); for (Future future : futures) { Map record = future.get(); assertEquals(268435456, (long) record.get("uint32")); - assertEquals("unicode! ☯ - ♫", (String) record.get("utf8_string")); + assertEquals("unicode! ☯ - ♫", record.get("utf8_string")); } } } diff --git a/src/test/java/com/maxmind/db/NetworkTest.java b/src/test/java/com/maxmind/db/NetworkTest.java index 00299980..ad3d4841 100644 --- a/src/test/java/com/maxmind/db/NetworkTest.java +++ b/src/test/java/com/maxmind/db/NetworkTest.java @@ -1,18 +1,17 @@ package com.maxmind.db; -import org.junit.Test; +import static junit.framework.TestCase.assertEquals; import java.net.InetAddress; import java.net.UnknownHostException; - -import static junit.framework.TestCase.assertEquals; +import org.junit.Test; public class NetworkTest { @Test public void testIPv6() throws UnknownHostException { Network network = new Network( - InetAddress.getByName("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), - 28 + InetAddress.getByName("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), + 28 ); assertEquals("2001:db0:0:0:0:0:0:0", network.getNetworkAddress().getHostAddress()); @@ -23,8 +22,8 @@ public void testIPv6() throws UnknownHostException { @Test public void TestIPv4() throws UnknownHostException { Network network = new Network( - InetAddress.getByName("192.168.213.111"), - 31 + InetAddress.getByName("192.168.213.111"), + 31 ); assertEquals("192.168.213.110", network.getNetworkAddress().getHostAddress()); diff --git a/src/test/java/com/maxmind/db/PointerTest.java b/src/test/java/com/maxmind/db/PointerTest.java index ddcfad14..f8c0dee4 100644 --- a/src/test/java/com/maxmind/db/PointerTest.java +++ b/src/test/java/com/maxmind/db/PointerTest.java @@ -2,15 +2,13 @@ import static org.junit.Assert.assertEquals; +import com.maxmind.db.Reader.FileMode; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; - import org.junit.Test; -import com.maxmind.db.Reader.FileMode; - public class PointerTest { @SuppressWarnings("static-method") @Test diff --git a/src/test/java/com/maxmind/db/ReaderTest.java b/src/test/java/com/maxmind/db/ReaderTest.java index e23622e8..4987a424 100644 --- a/src/test/java/com/maxmind/db/ReaderTest.java +++ b/src/test/java/com/maxmind/db/ReaderTest.java @@ -1,8 +1,15 @@ package com.maxmind.db; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; import java.io.File; import java.io.IOException; @@ -11,12 +18,17 @@ import java.math.BigInteger; import java.net.InetAddress; import java.net.UnknownHostException; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Vector; import java.util.concurrent.ConcurrentHashMap; - -import static org.junit.Assert.*; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.CoreMatchers.containsString; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; public class ReaderTest { private Reader testReader; @@ -35,8 +47,8 @@ public void teardownReader() throws IOException { @Test public void test() throws IOException { - for (long recordSize : new long[]{24, 28, 32}) { - for (int ipVersion : new int[]{4, 6}) { + for (long recordSize : new long[] {24, 28, 32}) { + for (int ipVersion : new int[] {4, 6}) { File file = getFile("MaxMind-DB-test-ipv" + ipVersion + "-" + recordSize + ".mmdb"); try (Reader reader = new Reader(file)) { this.testMetadata(reader, ipVersion, recordSize); @@ -56,7 +68,8 @@ static class GetRecordTest { String network; boolean hasRecord; - GetRecordTest(String ip, String file, String network, boolean hasRecord) throws UnknownHostException { + GetRecordTest(String ip, String file, String network, boolean hasRecord) + throws UnknownHostException { this.ip = InetAddress.getByName(ip); db = getFile(file); this.network = network; @@ -67,14 +80,18 @@ static class GetRecordTest { @Test public void testGetRecord() throws IOException { GetRecordTest[] mapTests = { - new GetRecordTest("1.1.1.1", "MaxMind-DB-test-ipv6-32.mmdb", "1.0.0.0/8", false), - new GetRecordTest("::1:ffff:ffff", "MaxMind-DB-test-ipv6-24.mmdb", "0:0:0:0:0:1:ffff:ffff/128", true), - new GetRecordTest("::2:0:1", "MaxMind-DB-test-ipv6-24.mmdb", "0:0:0:0:0:2:0:0/122", true), - new GetRecordTest("1.1.1.1", "MaxMind-DB-test-ipv4-24.mmdb", "1.1.1.1/32", true), - new GetRecordTest("1.1.1.3", "MaxMind-DB-test-ipv4-24.mmdb", "1.1.1.2/31", true), - new GetRecordTest("1.1.1.3", "MaxMind-DB-test-decoder.mmdb", "1.1.1.0/24", true), - new GetRecordTest("::ffff:1.1.1.128", "MaxMind-DB-test-decoder.mmdb", "1.1.1.0/24", true), - new GetRecordTest("::1.1.1.128", "MaxMind-DB-test-decoder.mmdb", "0:0:0:0:0:0:101:100/120", true), + new GetRecordTest("1.1.1.1", "MaxMind-DB-test-ipv6-32.mmdb", "1.0.0.0/8", false), + new GetRecordTest("::1:ffff:ffff", "MaxMind-DB-test-ipv6-24.mmdb", + "0:0:0:0:0:1:ffff:ffff/128", true), + new GetRecordTest("::2:0:1", "MaxMind-DB-test-ipv6-24.mmdb", "0:0:0:0:0:2:0:0/122", + true), + new GetRecordTest("1.1.1.1", "MaxMind-DB-test-ipv4-24.mmdb", "1.1.1.1/32", true), + new GetRecordTest("1.1.1.3", "MaxMind-DB-test-ipv4-24.mmdb", "1.1.1.2/31", true), + new GetRecordTest("1.1.1.3", "MaxMind-DB-test-decoder.mmdb", "1.1.1.0/24", true), + new GetRecordTest("::ffff:1.1.1.128", "MaxMind-DB-test-decoder.mmdb", "1.1.1.0/24", + true), + new GetRecordTest("::1.1.1.128", "MaxMind-DB-test-decoder.mmdb", + "0:0:0:0:0:0:101:100/120", true), }; for (GetRecordTest test : mapTests) { try (Reader reader = new Reader(test.db)) { @@ -91,10 +108,14 @@ public void testGetRecord() throws IOException { } GetRecordTest[] stringTests = { - new GetRecordTest("200.0.2.1", "MaxMind-DB-no-ipv4-search-tree.mmdb", "0.0.0.0/0", true), - new GetRecordTest("::200.0.2.1", "MaxMind-DB-no-ipv4-search-tree.mmdb", "0:0:0:0:0:0:0:0/64", true), - new GetRecordTest("0:0:0:0:ffff:ffff:ffff:ffff", "MaxMind-DB-no-ipv4-search-tree.mmdb", "0:0:0:0:0:0:0:0/64", true), - new GetRecordTest("ef00::", "MaxMind-DB-no-ipv4-search-tree.mmdb", "8000:0:0:0:0:0:0:0/1", false) + new GetRecordTest("200.0.2.1", "MaxMind-DB-no-ipv4-search-tree.mmdb", "0.0.0.0/0", + true), + new GetRecordTest("::200.0.2.1", "MaxMind-DB-no-ipv4-search-tree.mmdb", + "0:0:0:0:0:0:0:0/64", true), + new GetRecordTest("0:0:0:0:ffff:ffff:ffff:ffff", "MaxMind-DB-no-ipv4-search-tree.mmdb", + "0:0:0:0:0:0:0:0/64", true), + new GetRecordTest("ef00::", "MaxMind-DB-no-ipv4-search-tree.mmdb", + "8000:0:0:0:0:0:0:0/1", false) }; for (GetRecordTest test : stringTests) { try (Reader reader = new Reader(test.db)) { @@ -171,10 +192,10 @@ private void testDecodingTypes(Reader reader, boolean booleanValue) throws IOExc assertFalse((boolean) record.get("boolean")); } - assertArrayEquals(new byte[]{0, 0, 0, (byte) 42}, (byte[]) record - .get("bytes")); + assertArrayEquals(new byte[] {0, 0, 0, (byte) 42}, (byte[]) record + .get("bytes")); - assertEquals("unicode! ☯ - ♫", (String) record.get("utf8_string")); + assertEquals("unicode! ☯ - ♫", record.get("utf8_string")); @SuppressWarnings("unchecked") List array = (List) record.get("array"); @@ -196,21 +217,21 @@ private void testDecodingTypes(Reader reader, boolean booleanValue) throws IOExc assertEquals(8, (long) arrayX.get(1)); assertEquals(9, (long) arrayX.get(2)); - assertEquals("hello", (String) mapX.get("utf8_stringX")); + assertEquals("hello", mapX.get("utf8_stringX")); assertEquals(42.123456, (double) record.get("double"), 0.000000001); assertEquals(1.1, (float) record.get("float"), 0.000001); assertEquals(-268435456, (int) record.get("int32")); assertEquals(100, (int) record.get("uint16")); assertEquals(268435456, (long) record.get("uint32")); - assertEquals(new BigInteger("1152921504606846976"), (BigInteger) record - .get("uint64")); + assertEquals(new BigInteger("1152921504606846976"), record + .get("uint64")); assertEquals(new BigInteger("1329227995784915872903807060280344576"), - (BigInteger) record.get("uint128")); + record.get("uint128")); } private void testDecodingTypesIntoModelObject(Reader reader, boolean booleanValue) - throws IOException { + throws IOException { TestModel model = reader.get(InetAddress.getByName("::1.1.1.0"), TestModel.class); if (booleanValue) { @@ -219,7 +240,7 @@ private void testDecodingTypesIntoModelObject(Reader reader, boolean booleanValu assertFalse(model.booleanField); } - assertArrayEquals(new byte[]{0, 0, 0, (byte) 42}, model.bytesField); + assertArrayEquals(new byte[] {0, 0, 0, (byte) 42}, model.bytesField); assertEquals("unicode! ☯ - ♫", model.utf8StringField); @@ -242,7 +263,7 @@ private void testDecodingTypesIntoModelObject(Reader reader, boolean booleanValu assertEquals(268435456, model.uint32Field); assertEquals(new BigInteger("1152921504606846976"), model.uint64Field); assertEquals(new BigInteger("1329227995784915872903807060280344576"), - model.uint128Field); + model.uint128Field); } static class TestModel { @@ -260,30 +281,30 @@ static class TestModel { BigInteger uint128Field; @MaxMindDbConstructor - public TestModel ( - @MaxMindDbParameter(name="boolean") + public TestModel( + @MaxMindDbParameter(name = "boolean") boolean booleanField, - @MaxMindDbParameter(name="bytes") + @MaxMindDbParameter(name = "bytes") byte[] bytesField, - @MaxMindDbParameter(name="utf8_string") + @MaxMindDbParameter(name = "utf8_string") String utf8StringField, - @MaxMindDbParameter(name="array") + @MaxMindDbParameter(name = "array") List arrayField, - @MaxMindDbParameter(name="map") + @MaxMindDbParameter(name = "map") MapModel mapField, - @MaxMindDbParameter(name="double") + @MaxMindDbParameter(name = "double") double doubleField, - @MaxMindDbParameter(name="float") + @MaxMindDbParameter(name = "float") float floatField, - @MaxMindDbParameter(name="int32") + @MaxMindDbParameter(name = "int32") int int32Field, - @MaxMindDbParameter(name="uint16") + @MaxMindDbParameter(name = "uint16") int uint16Field, - @MaxMindDbParameter(name="uint32") + @MaxMindDbParameter(name = "uint32") long uint32Field, - @MaxMindDbParameter(name="uint64") + @MaxMindDbParameter(name = "uint64") BigInteger uint64Field, - @MaxMindDbParameter(name="uint128") + @MaxMindDbParameter(name = "uint128") BigInteger uint128Field ) { this.booleanField = booleanField; @@ -305,8 +326,8 @@ static class MapModel { MapXModel mapXField; @MaxMindDbConstructor - public MapModel ( - @MaxMindDbParameter(name="mapX") + public MapModel( + @MaxMindDbParameter(name = "mapX") MapXModel mapXField ) { this.mapXField = mapXField; @@ -318,10 +339,10 @@ static class MapXModel { String utf8StringXField; @MaxMindDbConstructor - public MapXModel ( - @MaxMindDbParameter(name="arrayX") + public MapXModel( + @MaxMindDbParameter(name = "arrayX") List arrayXField, - @MaxMindDbParameter(name="utf8_stringX") + @MaxMindDbParameter(name = "utf8_stringX") String utf8StringXField ) { this.arrayXField = arrayXField; @@ -330,7 +351,7 @@ public MapXModel ( } private void testDecodingTypesIntoModelObjectBoxed(Reader reader, boolean booleanValue) - throws IOException { + throws IOException { TestModelBoxed model = reader.get(InetAddress.getByName("::1.1.1.0"), TestModelBoxed.class); if (booleanValue) { @@ -339,7 +360,7 @@ private void testDecodingTypesIntoModelObjectBoxed(Reader reader, boolean boolea assertFalse(model.booleanField); } - assertArrayEquals(new byte[]{0, 0, 0, (byte) 42}, model.bytesField); + assertArrayEquals(new byte[] {0, 0, 0, (byte) 42}, model.bytesField); assertEquals("unicode! ☯ - ♫", model.utf8StringField); @@ -362,7 +383,7 @@ private void testDecodingTypesIntoModelObjectBoxed(Reader reader, boolean boolea assertEquals(Long.valueOf(268435456), model.uint32Field); assertEquals(new BigInteger("1152921504606846976"), model.uint64Field); assertEquals(new BigInteger("1329227995784915872903807060280344576"), - model.uint128Field); + model.uint128Field); } static class TestModelBoxed { @@ -380,30 +401,30 @@ static class TestModelBoxed { BigInteger uint128Field; @MaxMindDbConstructor - public TestModelBoxed ( - @MaxMindDbParameter(name="boolean") + public TestModelBoxed( + @MaxMindDbParameter(name = "boolean") Boolean booleanField, - @MaxMindDbParameter(name="bytes") + @MaxMindDbParameter(name = "bytes") byte[] bytesField, - @MaxMindDbParameter(name="utf8_string") + @MaxMindDbParameter(name = "utf8_string") String utf8StringField, - @MaxMindDbParameter(name="array") + @MaxMindDbParameter(name = "array") List arrayField, - @MaxMindDbParameter(name="map") + @MaxMindDbParameter(name = "map") MapModelBoxed mapField, - @MaxMindDbParameter(name="double") + @MaxMindDbParameter(name = "double") Double doubleField, - @MaxMindDbParameter(name="float") + @MaxMindDbParameter(name = "float") Float floatField, - @MaxMindDbParameter(name="int32") + @MaxMindDbParameter(name = "int32") Integer int32Field, - @MaxMindDbParameter(name="uint16") + @MaxMindDbParameter(name = "uint16") Integer uint16Field, - @MaxMindDbParameter(name="uint32") + @MaxMindDbParameter(name = "uint32") Long uint32Field, - @MaxMindDbParameter(name="uint64") + @MaxMindDbParameter(name = "uint64") BigInteger uint64Field, - @MaxMindDbParameter(name="uint128") + @MaxMindDbParameter(name = "uint128") BigInteger uint128Field ) { this.booleanField = booleanField; @@ -425,8 +446,8 @@ static class MapModelBoxed { MapXModelBoxed mapXField; @MaxMindDbConstructor - public MapModelBoxed ( - @MaxMindDbParameter(name="mapX") + public MapModelBoxed( + @MaxMindDbParameter(name = "mapX") MapXModelBoxed mapXField ) { this.mapXField = mapXField; @@ -438,10 +459,10 @@ static class MapXModelBoxed { String utf8StringXField; @MaxMindDbConstructor - public MapXModelBoxed ( - @MaxMindDbParameter(name="arrayX") + public MapXModelBoxed( + @MaxMindDbParameter(name = "arrayX") List arrayXField, - @MaxMindDbParameter(name="utf8_stringX") + @MaxMindDbParameter(name = "utf8_stringX") String utf8StringXField ) { this.arrayXField = arrayXField; @@ -450,7 +471,7 @@ public MapXModelBoxed ( } private void testDecodingTypesIntoModelWithList(Reader reader) - throws IOException { + throws IOException { TestModelList model = reader.get(InetAddress.getByName("::1.1.1.0"), TestModelList.class); assertEquals(Arrays.asList((long) 1, (long) 2, (long) 3), model.arrayField); @@ -460,8 +481,8 @@ static class TestModelList { List arrayField; @MaxMindDbConstructor - public TestModelList ( - @MaxMindDbParameter(name="array") List arrayField + public TestModelList( + @MaxMindDbParameter(name = "array") List arrayField ) { this.arrayField = arrayField; } @@ -488,7 +509,7 @@ private void testZeros(Reader reader) throws IOException { assertArrayEquals(new byte[0], (byte[]) record.get("bytes")); - assertEquals("", (String) record.get("utf8_string")); + assertEquals("", record.get("utf8_string")); @SuppressWarnings("unchecked") List array = (List) record.get("array"); @@ -502,8 +523,8 @@ private void testZeros(Reader reader) throws IOException { assertEquals(0, (int) record.get("int32")); assertEquals(0, (int) record.get("uint16")); assertEquals(0, (long) record.get("uint32")); - assertEquals(BigInteger.ZERO, (BigInteger) record.get("uint64")); - assertEquals(BigInteger.ZERO, (BigInteger) record.get("uint128")); + assertEquals(BigInteger.ZERO, record.get("uint64")); + assertEquals(BigInteger.ZERO, record.get("uint128")); } private void testZerosModelObject(Reader reader) throws IOException { @@ -547,8 +568,8 @@ static class TestModelSubdivisions { List subdivisions; @MaxMindDbConstructor - public TestModelSubdivisions ( - @MaxMindDbParameter(name="subdivisions") + public TestModelSubdivisions( + @MaxMindDbParameter(name = "subdivisions") List subdivisions ) { this.subdivisions = subdivisions; @@ -560,7 +581,7 @@ static class TestModelSubdivision { @MaxMindDbConstructor public TestModelSubdivision( - @MaxMindDbParameter(name="iso_code") + @MaxMindDbParameter(name = "iso_code") String isoCode ) { this.isoCode = isoCode; @@ -571,20 +592,21 @@ public TestModelSubdivision( public void testDecodeWrongTypeWithConstructorException() throws IOException { this.testReader = new Reader(getFile("GeoIP2-City-Test.mmdb")); DeserializationException ex = assertThrows(DeserializationException.class, - () -> this.testReader.get( InetAddress.getByName("2.125.160.216"), - TestModelSubdivisionsWithUnknownException.class)); - - assertThat(ex.getMessage(), containsString("Error getting record for IP /2.125.160.216 - Error creating object")); + () -> this.testReader.get(InetAddress.getByName("2.125.160.216"), + TestModelSubdivisionsWithUnknownException.class)); + + assertThat(ex.getMessage(), + containsString("Error getting record for IP /2.125.160.216 - Error creating object")); } static class TestModelSubdivisionsWithUnknownException { List subdivisions; @MaxMindDbConstructor - public TestModelSubdivisionsWithUnknownException ( - @MaxMindDbParameter(name="subdivisions") + public TestModelSubdivisionsWithUnknownException( + @MaxMindDbParameter(name = "subdivisions") List subdivisions - ) throws Exception{ + ) throws Exception { throw new Exception(); } } @@ -593,8 +615,8 @@ public TestModelSubdivisionsWithUnknownException ( public void testDecodeWrongTypeWithWrongArguments() throws IOException { this.testReader = new Reader(getFile("GeoIP2-City-Test.mmdb")); DeserializationException ex = assertThrows(DeserializationException.class, - () -> this.testReader.get( InetAddress.getByName("2.125.160.216"), - TestWrongModelSubdivisions.class)); + () -> this.testReader.get(InetAddress.getByName("2.125.160.216"), + TestWrongModelSubdivisions.class)); assertThat(ex.getMessage(), containsString("Error getting record for IP")); } @@ -602,8 +624,8 @@ static class TestWrongModelSubdivisions { List subdivisions; @MaxMindDbConstructor - public TestWrongModelSubdivisions ( - @MaxMindDbParameter(name="subdivisions") + public TestWrongModelSubdivisions( + @MaxMindDbParameter(name = "subdivisions") List subdivisions ) { this.subdivisions = subdivisions; @@ -612,13 +634,13 @@ public TestWrongModelSubdivisions ( static class TestWrongModelSubdivision { Integer uint16Field; + @MaxMindDbConstructor public TestWrongModelSubdivision( - @MaxMindDbParameter(name="iso_code") + @MaxMindDbParameter(name = "iso_code") Integer uint16Field - ) { + ) { this.uint16Field = uint16Field; - ; } } @@ -661,7 +683,7 @@ static class TestModelVector { @MaxMindDbConstructor public TestModelVector( - @MaxMindDbParameter(name="array") + @MaxMindDbParameter(name = "array") Vector arrayField ) { this.arrayField = arrayField; @@ -674,8 +696,8 @@ public void testCacheWithDifferentModels() throws IOException { NodeCache cache = new CHMCache(); this.testReader = new Reader( - getFile("MaxMind-DB-test-decoder.mmdb"), - cache + getFile("MaxMind-DB-test-decoder.mmdb"), + cache ); TestModelA modelA = this.testReader.get( @@ -695,8 +717,8 @@ static class TestModelA { String utf8StringFieldA; @MaxMindDbConstructor - public TestModelA ( - @MaxMindDbParameter(name="utf8_string") String utf8StringFieldA + public TestModelA( + @MaxMindDbParameter(name = "utf8_string") String utf8StringFieldA ) { this.utf8StringFieldA = utf8StringFieldA; } @@ -706,8 +728,8 @@ static class TestModelB { String utf8StringFieldB; @MaxMindDbConstructor - public TestModelB ( - @MaxMindDbParameter(name="utf8_string") String utf8StringFieldB + public TestModelB( + @MaxMindDbParameter(name = "utf8_string") String utf8StringFieldB ) { this.utf8StringFieldB = utf8StringFieldB; } @@ -745,10 +767,10 @@ private java.lang.reflect.Type getType(Class cls, int i) { } static class TestModelCacheKey { - private List a; - private List b; + private final List a; + private final List b; - public TestModelCacheKey (List a, List b) { + public TestModelCacheKey(List a, List b) { this.a = a; this.b = b; } @@ -768,9 +790,10 @@ public void testBrokenDatabaseStream() throws IOException { private void testBrokenDatabase(Reader reader) { InvalidDatabaseException ex = assertThrows( - InvalidDatabaseException.class, - () -> reader.get(InetAddress.getByName("2001:220::"), Map.class)); - assertThat(ex.getMessage(), containsString("The MaxMind DB file's data section contains bad data")); + InvalidDatabaseException.class, + () -> reader.get(InetAddress.getByName("2001:220::"), Map.class)); + assertThat(ex.getMessage(), + containsString("The MaxMind DB file's data section contains bad data")); } @Test @@ -787,7 +810,7 @@ public void testBrokenSearchTreePointerStream() throws IOException { private void testBrokenSearchTreePointer(Reader reader) { InvalidDatabaseException ex = assertThrows(InvalidDatabaseException.class, - () -> reader.get(InetAddress.getByName("1.1.1.32"), Map.class)); + () -> reader.get(InetAddress.getByName("1.1.1.32"), Map.class)); assertThat(ex.getMessage(), containsString("The MaxMind DB file's search tree is corrupt")); } @@ -805,8 +828,9 @@ public void testBrokenDataPointerStream() throws IOException { private void testBrokenDataPointer(Reader reader) { InvalidDatabaseException ex = assertThrows(InvalidDatabaseException.class, - () -> reader.get(InetAddress.getByName("1.1.1.16"), Map.class)); - assertThat(ex.getMessage(), containsString("The MaxMind DB file's data section contains bad data")); + () -> reader.get(InetAddress.getByName("1.1.1.16"), Map.class)); + assertThat(ex.getMessage(), + containsString("The MaxMind DB file's data section contains bad data")); } @Test @@ -815,7 +839,7 @@ public void testClosedReaderThrowsException() throws IOException { reader.close(); ClosedDatabaseException ex = assertThrows(ClosedDatabaseException.class, - () -> reader.get(InetAddress.getByName("1.1.1.16"), Map.class)); + () -> reader.get(InetAddress.getByName("1.1.1.16"), Map.class)); assertEquals("The MaxMind DB has been closed.", ex.getMessage()); } @@ -830,15 +854,16 @@ public void voidTestMapKeyIsString() throws IOException { TestModelInvalidMap.class ) ); - assertEquals("Error getting record for IP /2.125.160.216 - Map keys must be strings.", ex.getMessage()); + assertEquals("Error getting record for IP /2.125.160.216 - Map keys must be strings.", + ex.getMessage()); } static class TestModelInvalidMap { Map postal; @MaxMindDbConstructor - public TestModelInvalidMap ( - @MaxMindDbParameter(name="postal") + public TestModelInvalidMap( + @MaxMindDbParameter(name = "postal") Map postal ) { this.postal = postal; @@ -879,7 +904,7 @@ private void testIpV4(Reader reader, File file) throws IOException { data.put("ip", address); assertEquals("found expected data record for " + address + " in " - + file, data, reader.get(InetAddress.getByName(address), Map.class)); + + file, data, reader.get(InetAddress.getByName(address), Map.class)); } Map pairs = new HashMap<>(); @@ -895,25 +920,25 @@ private void testIpV4(Reader reader, File file) throws IOException { data.put("ip", pairs.get(address)); assertEquals("found expected data record for " + address + " in " - + file, data, reader.get(InetAddress.getByName(address), Map.class)); + + file, data, reader.get(InetAddress.getByName(address), Map.class)); } - for (String ip : new String[]{"1.1.1.33", "255.254.253.123"}) { + for (String ip : new String[] {"1.1.1.33", "255.254.253.123"}) { assertNull(reader.get(InetAddress.getByName(ip), Map.class)); } } // XXX - logic could be combined with above private void testIpV6(Reader reader, File file) throws IOException { - String[] subnets = new String[]{"::1:ffff:ffff", "::2:0:0", - "::2:0:40", "::2:0:50", "::2:0:58"}; + String[] subnets = new String[] {"::1:ffff:ffff", "::2:0:0", + "::2:0:40", "::2:0:50", "::2:0:58"}; for (String address : subnets) { Map data = new HashMap<>(); data.put("ip", address); assertEquals("found expected data record for " + address + " in " - + file, data, reader.get(InetAddress.getByName(address), Map.class)); + + file, data, reader.get(InetAddress.getByName(address), Map.class)); } Map pairs = new HashMap<>(); @@ -931,10 +956,10 @@ private void testIpV6(Reader reader, File file) throws IOException { data.put("ip", pairs.get(address)); assertEquals("found expected data record for " + address + " in " - + file, data, reader.get(InetAddress.getByName(address), Map.class)); + + file, data, reader.get(InetAddress.getByName(address), Map.class)); } - for (String ip : new String[]{"1.1.1.33", "255.254.253.123", "89fa::"}) { + for (String ip : new String[] {"1.1.1.33", "255.254.253.123", "89fa::"}) { assertNull(reader.get(InetAddress.getByName(ip), Map.class)); } } @@ -946,5 +971,4 @@ static File getFile(String name) { static InputStream getStream(String name) { return ReaderTest.class.getResourceAsStream("/maxmind-db/test-data/" + name); } - }