Implement Closeable interface on Searcher

This commit is contained in:
ijliym 2024-03-13 08:57:00 +08:00
parent f8f5f687e6
commit ee3852e753
1 changed files with 16 additions and 13 deletions

View File

@ -1,18 +1,21 @@
// Copyright 2022 The Ip2Region Authors. All rights reserved. // Copyright 2024 The Ip2Region Authors. All rights reserved.
// Use of this source code is governed by a Apache2.0-style // Use of this source code is governed by a Apache2.0-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package org.lionsoul.ip2region.xdb; package org.lionsoul.ip2region.xdb;
// xdb searcher (Not thread safe implementation) import java.io.Closeable;
// @Author Lion <chenxin619315@gmail.com>
// @Date 2022/06/23
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
public class Searcher { /**
* xdb searcher (Not thread-safe implementation).
*
* @author Lion <chenxin619315@gmail.com>
* @date 2022/06/23
*/
public class Searcher implements Closeable {
// constant defined copied from the xdb maker // constant defined copied from the xdb maker
public static final int HeaderInfoLength = 256; public static final int HeaderInfoLength = 256;
public static final int VectorIndexRows = 256; public static final int VectorIndexRows = 256;
@ -61,14 +64,15 @@ public class Searcher {
} }
} }
public void close() throws IOException { @Override
public void close() throws IOException {
if (this.handle != null) { if (this.handle != null) {
this.handle.close(); this.handle.close();
} }
} }
public int getIOCount() { public int getIOCount() {
return ioCount; return this.ioCount;
} }
public String search(String ipStr) throws Exception { public String search(String ipStr) throws Exception {
@ -135,7 +139,7 @@ public class Searcher {
// load and return the region data // load and return the region data
final byte[] regionBuff = new byte[dataLen]; final byte[] regionBuff = new byte[dataLen];
read(dataPtr, regionBuff); read(dataPtr, regionBuff);
return new String(regionBuff, "utf-8"); return new String(regionBuff, "UTF-8");
} }
protected void read(int offset, byte[] buffer) throws IOException { protected void read(int offset, byte[] buffer) throws IOException {
@ -241,8 +245,7 @@ public class Searcher {
} }
/* long int to ip string */ /* long int to ip string */
public static String long2ip( long ip ) public static String long2ip( long ip ) {
{
return String.valueOf((ip >> 24) & 0xFF) + '.' + return String.valueOf((ip >> 24) & 0xFF) + '.' +
((ip >> 16) & 0xFF) + '.' + ((ip >> 8) & 0xFF) + '.' + ((ip) & 0xFF); ((ip >> 16) & 0xFF) + '.' + ((ip >> 8) & 0xFF) + '.' + ((ip) & 0xFF);
} }