3232* <p>
3333* <b>Requirements:</b> Java SDK 1.4 or later<br>
3434* <p>
35- * Copyright (c) 2002-2020 IP2Location.com
35+ * Copyright (c) 2002-2021 IP2Location.com
3636* <p>
3737*
3838* @author IP2Location.com
39- * @version 8.5.0
39+ * @version 8.5.1
4040*/
4141public class IP2Location {
4242 private static final Pattern pattern = Pattern .compile ("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\ .){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" ); // IPv4
@@ -166,25 +166,15 @@ public void Open(String DBPath, boolean UseMMF) throws IOException {
166166 * This function destroys the mapped bytes.
167167 */
168168 public void Close () {
169- if (_MetaData != null ) {
170- _MetaData = null ;
171- }
169+ _MetaData = null ;
172170 DestroyMappedBytes ();
173171 }
174172
175173 private void DestroyMappedBytes () {
176- if (_IPv4Buffer != null ) {
177- _IPv4Buffer = null ;
178- }
179- // if (_IndexBuffer != null) {
180- // _IndexBuffer = null;
181- // }
182- if (_IPv6Buffer != null ) {
183- _IPv6Buffer = null ;
184- }
185- if (_MapDataBuffer != null ) {
186- _MapDataBuffer = null ;
187- }
174+ _IPv4Buffer = null ;
175+ // _IndexBuffer = null;
176+ _IPv6Buffer = null ;
177+ _MapDataBuffer = null ;
188178 }
189179
190180 private void CreateMappedBytes () throws IOException {
@@ -197,56 +187,41 @@ private void CreateMappedBytes() throws IOException {
197187 final FileChannel inChannel = aFile .getChannel ();
198188 CreateMappedBytes (inChannel );
199189 }
200- // catch (FileNotFoundException ex1) {
201- // throw ex1;
202- // }
203- catch (IOException ex ) { // includes FileNotFoundException
204- throw ex ;
205- }
206190 finally {
207191 if (aFile != null ) {
208192 aFile .close ();
209- aFile = null ;
210193 }
211194 }
212195 }
213196
214197 private void CreateMappedBytes (FileChannel inChannel ) throws IOException {
215- try {
216- if (_IPv4Buffer == null ) {
217- final long _IPv4Bytes = (long )_IPv4ColumnSize * (long )_MetaData .getDBCount ();
218- _IPv4Offset = _MetaData .getBaseAddr () - 1 ;
219- _IPv4Buffer = inChannel .map (FileChannel .MapMode .READ_ONLY , _IPv4Offset , _IPv4Bytes );
220- _IPv4Buffer .order (ByteOrder .LITTLE_ENDIAN );
221- _MapDataOffset = _IPv4Offset + _IPv4Bytes ;
222- }
223-
224- // NO LONGER USING BUFFER AS IT IS TOO SLOW, USING MULTI-DIMENSIONAL ARRAY NOW
225- // if (_MetaData.getIndexed()) {
226- // final int _IndexBytes = _MetaData.getBaseAddr() - _MetaData.getIndexBaseAddr();
227- // _IndexOffset = _MetaData.getIndexBaseAddr() - 1;
228- // _IndexBuffer = inChannel.map(FileChannel.MapMode.READ_ONLY, _IndexOffset, _IndexBytes);
229- // _IndexBuffer.order(ByteOrder.LITTLE_ENDIAN);
230- // }
231-
232- if (!_MetaData .getOldBIN () && _IPv6Buffer == null ) {
233- final long _IPv6Bytes = (long )_IPv6ColumnSize * (long )_MetaData .getDBCountIPv6 ();
234- _IPv6Offset = _MetaData .getBaseAddrIPv6 () - 1 ;
235- _IPv6Buffer = inChannel .map (FileChannel .MapMode .READ_ONLY , _IPv6Offset , _IPv6Bytes );
236- _IPv6Buffer .order (ByteOrder .LITTLE_ENDIAN );
237- _MapDataOffset = _IPv6Offset + _IPv6Bytes ;
238- }
239-
240- if (_MapDataBuffer == null ) {
241- _MapDataBuffer = inChannel .map (FileChannel .MapMode .READ_ONLY , _MapDataOffset , inChannel .size () - _MapDataOffset );
242- _MapDataBuffer .order (ByteOrder .LITTLE_ENDIAN );
243- }
198+ if (_IPv4Buffer == null ) {
199+ final long _IPv4Bytes = (long )_IPv4ColumnSize * (long )_MetaData .getDBCount ();
200+ _IPv4Offset = _MetaData .getBaseAddr () - 1 ;
201+ _IPv4Buffer = inChannel .map (FileChannel .MapMode .READ_ONLY , _IPv4Offset , _IPv4Bytes );
202+ _IPv4Buffer .order (ByteOrder .LITTLE_ENDIAN );
203+ _MapDataOffset = _IPv4Offset + _IPv4Bytes ;
244204 }
245- // catch (FileNotFoundException ex1) {
246- // throw ex1;
205+
206+ // NO LONGER USING BUFFER AS IT IS TOO SLOW, USING MULTI-DIMENSIONAL ARRAY NOW
207+ // if (_MetaData.getIndexed()) {
208+ // final int _IndexBytes = _MetaData.getBaseAddr() - _MetaData.getIndexBaseAddr();
209+ // _IndexOffset = _MetaData.getIndexBaseAddr() - 1;
210+ // _IndexBuffer = inChannel.map(FileChannel.MapMode.READ_ONLY, _IndexOffset, _IndexBytes);
211+ // _IndexBuffer.order(ByteOrder.LITTLE_ENDIAN);
247212 // }
248- catch (IOException ex ) { // includes FileNotFoundException
249- throw ex ;
213+
214+ if (!_MetaData .getOldBIN () && _IPv6Buffer == null ) {
215+ final long _IPv6Bytes = (long )_IPv6ColumnSize * (long )_MetaData .getDBCountIPv6 ();
216+ _IPv6Offset = _MetaData .getBaseAddrIPv6 () - 1 ;
217+ _IPv6Buffer = inChannel .map (FileChannel .MapMode .READ_ONLY , _IPv6Offset , _IPv6Bytes );
218+ _IPv6Buffer .order (ByteOrder .LITTLE_ENDIAN );
219+ _MapDataOffset = _IPv6Offset + _IPv6Bytes ;
220+ }
221+
222+ if (_MapDataBuffer == null ) {
223+ _MapDataBuffer = inChannel .map (FileChannel .MapMode .READ_ONLY , _MapDataOffset , inChannel .size () - _MapDataOffset );
224+ _MapDataBuffer .order (ByteOrder .LITTLE_ENDIAN );
250225 }
251226 }
252227
@@ -359,25 +334,25 @@ private boolean LoadBIN() throws IOException {
359334 ELEVATION_POSITION_OFFSET = (ELEVATION_POSITION [dbtype ] != 0 ) ? (ELEVATION_POSITION [dbtype ] - 2 ) << 2 : 0 ;
360335 USAGETYPE_POSITION_OFFSET = (USAGETYPE_POSITION [dbtype ] != 0 ) ? (USAGETYPE_POSITION [dbtype ] - 2 ) << 2 : 0 ;
361336
362- COUNTRY_ENABLED = (COUNTRY_POSITION [dbtype ]!=0 ) ? true : false ;
363- REGION_ENABLED = (REGION_POSITION [dbtype ]!=0 ) ? true : false ;
364- CITY_ENABLED = (CITY_POSITION [dbtype ]!=0 ) ? true : false ;
365- ISP_ENABLED = (ISP_POSITION [dbtype ]!=0 ) ? true : false ;
366- LATITUDE_ENABLED = (LATITUDE_POSITION [dbtype ]!=0 ) ? true : false ;
367- LONGITUDE_ENABLED = (LONGITUDE_POSITION [dbtype ]!=0 ) ? true : false ;
368- DOMAIN_ENABLED = (DOMAIN_POSITION [dbtype ]!=0 ) ? true : false ;
369- ZIPCODE_ENABLED = (ZIPCODE_POSITION [dbtype ]!=0 ) ? true : false ;
370- TIMEZONE_ENABLED = (TIMEZONE_POSITION [dbtype ]!=0 ) ? true : false ;
371- NETSPEED_ENABLED = (NETSPEED_POSITION [dbtype ]!=0 ) ? true : false ;
372- IDDCODE_ENABLED = (IDDCODE_POSITION [dbtype ]!=0 ) ? true : false ;
373- AREACODE_ENABLED = (AREACODE_POSITION [dbtype ]!=0 ) ? true : false ;
374- WEATHERSTATIONCODE_ENABLED = (WEATHERSTATIONCODE_POSITION [dbtype ]!=0 ) ? true : false ;
375- WEATHERSTATIONNAME_ENABLED = (WEATHERSTATIONNAME_POSITION [dbtype ]!=0 ) ? true : false ;
376- MCC_ENABLED = (MCC_POSITION [dbtype ]!=0 ) ? true : false ;
377- MNC_ENABLED = (MNC_POSITION [dbtype ]!=0 ) ? true : false ;
378- MOBILEBRAND_ENABLED = (MOBILEBRAND_POSITION [dbtype ]!=0 ) ? true : false ;
379- ELEVATION_ENABLED = (ELEVATION_POSITION [dbtype ]!=0 ) ? true : false ;
380- USAGETYPE_ENABLED = (USAGETYPE_POSITION [dbtype ]!=0 ) ? true : false ;
337+ COUNTRY_ENABLED = (COUNTRY_POSITION [dbtype ]!=0 );
338+ REGION_ENABLED = (REGION_POSITION [dbtype ]!=0 );
339+ CITY_ENABLED = (CITY_POSITION [dbtype ]!=0 );
340+ ISP_ENABLED = (ISP_POSITION [dbtype ]!=0 );
341+ LATITUDE_ENABLED = (LATITUDE_POSITION [dbtype ]!=0 );
342+ LONGITUDE_ENABLED = (LONGITUDE_POSITION [dbtype ]!=0 );
343+ DOMAIN_ENABLED = (DOMAIN_POSITION [dbtype ]!=0 );
344+ ZIPCODE_ENABLED = (ZIPCODE_POSITION [dbtype ]!=0 );
345+ TIMEZONE_ENABLED = (TIMEZONE_POSITION [dbtype ]!=0 );
346+ NETSPEED_ENABLED = (NETSPEED_POSITION [dbtype ]!=0 );
347+ IDDCODE_ENABLED = (IDDCODE_POSITION [dbtype ]!=0 );
348+ AREACODE_ENABLED = (AREACODE_POSITION [dbtype ]!=0 );
349+ WEATHERSTATIONCODE_ENABLED = (WEATHERSTATIONCODE_POSITION [dbtype ]!=0 );
350+ WEATHERSTATIONNAME_ENABLED = (WEATHERSTATIONNAME_POSITION [dbtype ]!=0 );
351+ MCC_ENABLED = (MCC_POSITION [dbtype ]!=0 );
352+ MNC_ENABLED = (MNC_POSITION [dbtype ]!=0 );
353+ MOBILEBRAND_ENABLED = (MOBILEBRAND_POSITION [dbtype ]!=0 );
354+ ELEVATION_ENABLED = (ELEVATION_POSITION [dbtype ]!=0 );
355+ USAGETYPE_ENABLED = (USAGETYPE_POSITION [dbtype ]!=0 );
381356
382357 if (_MetaData .getIndexed ()) {
383358 // aFile2 = new FileInputStream(IPDatabasePath);
@@ -421,13 +396,9 @@ private boolean LoadBIN() throws IOException {
421396 loadOK = true ;
422397 }
423398 }
424- catch (IOException ex ) {
425- throw ex ;
426- }
427399 finally {
428400 if (aFile != null ) {
429401 aFile .close ();
430- aFile = null ;
431402 }
432403 }
433404 return loadOK ;
@@ -819,14 +790,9 @@ public IPResult IPQuery(String IPAddress) throws IOException {
819790 }
820791 return record ;
821792 }
822- catch (IOException ex ) {
823- // ex.printStackTrace(System.out);
824- throw ex ;
825- }
826793 finally {
827794 if (filehandle != null ) {
828795 filehandle .close ();
829- filehandle = null ;
830796 }
831797 }
832798 }
@@ -869,8 +835,6 @@ private String[] ExpandIPv6(final String myIP, final int myiptype) {
869835 myIP2 = myIP2 .replaceAll (mymatch + "$" , ":" + b [3 ] + "." + b [2 ] + "." + b [1 ] + "." + b [0 ]);
870836 myIP2 = myIP2 .replaceAll ("::" , tmp );
871837 }
872- else {
873- }
874838 }
875839 }
876840 else if (myiptype == 6 ) {
@@ -961,7 +925,6 @@ else if (myiptype == 6) {
961925 bf2 .append (bf4 ).append (bf3 );
962926 myIP2 = bf2 .toString ().replaceAll (":$" , "" );
963927 }
964-
965928 }
966929 else {
967930 // expand IPv4-compatible IPv6
0 commit comments