|
1 | 1 | package qiniu.ip17mon; |
2 | 2 |
|
3 | | -import java.io.File; |
4 | | -import java.io.FileInputStream; |
5 | | -import java.io.IOException; |
6 | | -import java.io.InputStream; |
| 3 | +import java.io.*; |
7 | 4 | import java.net.HttpURLConnection; |
8 | 5 | import java.net.URL; |
9 | 6 | import java.nio.charset.Charset; |
10 | 7 | import java.util.InputMismatchException; |
11 | 8 |
|
12 | 9 | public final class Locator implements ILocator { |
13 | | - public static final String VERSION = "0.1.1"; |
| 10 | + public static final String VERSION = "0.1.2"; |
14 | 11 | private static final Charset Utf8 = Charset.forName("UTF-8"); |
15 | 12 | private final byte[] ipData; |
16 | 13 | private final int textOffset; |
@@ -154,29 +151,20 @@ public static Locator loadFromLocal(String filePath) throws IOException { |
154 | 151 |
|
155 | 152 | return loadBinary(b); |
156 | 153 | } |
157 | | - |
158 | | - public static Locator loadFromHDFS(String dfsPath) throws Exception { |
159 | | - Configuration conf = new Configuration(); |
160 | | - FileSystem fs = FileSystem.get(conf); |
161 | 154 |
|
| 155 | + public static Locator loadFromStream(InputStream in) throws Exception { |
162 | 156 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 157 | + byte[] buffer = new byte[16 * 1024]; |
| 158 | + while (in.read(buffer) != -1) { |
| 159 | + byteArrayOutputStream.write(buffer); |
| 160 | + } |
163 | 161 |
|
164 | | - Path filePath = new Path(dfsPath); |
165 | | - |
166 | | - if(fs.exists(filePath)){ |
167 | | - FSDataInputStream in = fs.open(filePath); |
168 | | - byte[] buffer = new byte[1]; |
| 162 | + int n = 0; |
169 | 163 |
|
170 | | - while (in.read(buffer) != -1){ |
171 | | - byteArrayOutputStream.write(buffer); |
172 | | - } |
173 | | - } else { |
174 | | - throw new Exception("File does not exists!"); |
| 164 | + while ((n = in.read(buffer)) != -1) { |
| 165 | + byteArrayOutputStream.write(buffer, 0, n); |
175 | 166 | } |
176 | 167 |
|
177 | | - // Using in Spark, don't close it. |
178 | | - //fs.close(); |
179 | | - |
180 | 168 | return loadBinary(byteArrayOutputStream.toByteArray()); |
181 | 169 | } |
182 | 170 |
|
@@ -260,7 +248,7 @@ public void checkDb() throws IOException { |
260 | 248 | find(addr); |
261 | 249 | } |
262 | 250 | } catch (Exception e) { |
263 | | - throw new IOException(e); |
| 251 | + throw new IOException(e.getMessage()); |
264 | 252 | } |
265 | 253 | } |
266 | 254 | } |
0 commit comments