Compare commits
4 Commits
2df96026bb
...
ca5d3b0d03
Author | SHA1 | Date |
---|---|---|
![]() |
ca5d3b0d03 | 5 years ago |
![]() |
a7b5c8c36c | 5 years ago |
![]() |
fd2e8cd199 | 5 years ago |
![]() |
7b26ce92fe | 5 years ago |
@ -0,0 +1,7 @@ |
|||||||
|
package me.bearns.fias.exceptions; |
||||||
|
|
||||||
|
public class DownloadException extends Exception { |
||||||
|
public DownloadException(Exception e) { |
||||||
|
super(e); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,11 @@ |
|||||||
|
package me.bearns.fias.exceptions; |
||||||
|
|
||||||
|
public class UnzipException extends Exception { |
||||||
|
public UnzipException(Exception e){ |
||||||
|
super(e); |
||||||
|
} |
||||||
|
|
||||||
|
public UnzipException(String s) { |
||||||
|
super(s); |
||||||
|
} |
||||||
|
} |
@ -1,16 +1,19 @@ |
|||||||
package me.bearns.fias.service; |
package me.bearns.fias.service; |
||||||
|
|
||||||
|
import me.bearns.fias.exceptions.DownloadException; |
||||||
|
import me.bearns.fias.exceptions.UnzipException; |
||||||
|
|
||||||
public interface Updater { |
public interface Updater { |
||||||
|
|
||||||
|
|
||||||
/** |
/** |
||||||
* Run online update for fias database |
* Run online update for fias database |
||||||
*/ |
*/ |
||||||
public void update(); |
public void update() throws DownloadException, UnzipException; |
||||||
|
|
||||||
/** |
/** |
||||||
* Load or reload database for selected regions |
* Load or reload database for selected regions |
||||||
* @param regions |
* @param regions |
||||||
*/ |
*/ |
||||||
public void reload(Long ... regions); |
public void reload(Long ... regions) throws DownloadException, UnzipException; |
||||||
} |
} |
||||||
|
@ -0,0 +1,164 @@ |
|||||||
|
package me.bearns.fias.util; |
||||||
|
|
||||||
|
import me.bearns.fias.exceptions.UnzipException; |
||||||
|
|
||||||
|
import java.io.Closeable; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
import java.util.HashSet; |
||||||
|
import java.util.Set; |
||||||
|
import java.util.concurrent.locks.ReentrantLock; |
||||||
|
import java.util.zip.ZipEntry; |
||||||
|
import java.util.zip.ZipInputStream; |
||||||
|
|
||||||
|
public class ZipReader implements Closeable { |
||||||
|
|
||||||
|
private final InputStream in; |
||||||
|
private final Set<String> set = new HashSet<>(); |
||||||
|
private final ReentrantLock mutex = new ReentrantLock(); |
||||||
|
|
||||||
|
public ZipReader(InputStream in){ |
||||||
|
this.in = in; |
||||||
|
} |
||||||
|
|
||||||
|
public Set<String> list() throws UnzipException { |
||||||
|
|
||||||
|
if(set.isEmpty()) init(); |
||||||
|
|
||||||
|
return set; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public InputStream read(String file) throws UnzipException { |
||||||
|
|
||||||
|
if(file == null) throw new UnzipException("Filename in archive cannot be null"); |
||||||
|
|
||||||
|
mutex.lock(); |
||||||
|
|
||||||
|
InputStream retval = null; |
||||||
|
ZipInputStream zis = null; |
||||||
|
|
||||||
|
try{ |
||||||
|
zis = new ZipInputStream(in); |
||||||
|
|
||||||
|
ZipEntry zipEntry; |
||||||
|
while ((zipEntry = zis.getNextEntry()) != null) { |
||||||
|
|
||||||
|
if(file.equals(zipEntry.getName())) { |
||||||
|
//TODO
|
||||||
|
retval = new FileStream(zis); |
||||||
|
|
||||||
|
} |
||||||
|
zis.closeEntry(); |
||||||
|
} |
||||||
|
|
||||||
|
} catch (IOException e) { |
||||||
|
throw new UnzipException(e); |
||||||
|
} finally { |
||||||
|
if(retval == null) { |
||||||
|
|
||||||
|
mutex.unlock(); |
||||||
|
|
||||||
|
try { |
||||||
|
if(zis!=null) zis.close(); |
||||||
|
} catch (IOException e) { |
||||||
|
//log
|
||||||
|
} |
||||||
|
|
||||||
|
throw new UnzipException("File not found in archive"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return retval; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
private synchronized void init() throws UnzipException { |
||||||
|
|
||||||
|
mutex.lock(); |
||||||
|
|
||||||
|
try(ZipInputStream zis = new ZipInputStream(in)){ |
||||||
|
|
||||||
|
ZipEntry zipEntry; |
||||||
|
while ((zipEntry = zis.getNextEntry()) != null) { |
||||||
|
|
||||||
|
set.add(zipEntry.getName()); |
||||||
|
zis.closeEntry(); |
||||||
|
} |
||||||
|
|
||||||
|
} catch (IOException e) { |
||||||
|
throw new UnzipException(e); |
||||||
|
} finally { |
||||||
|
mutex.unlock(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void close() throws IOException { |
||||||
|
|
||||||
|
mutex.unlock(); |
||||||
|
|
||||||
|
if(in!=null) { |
||||||
|
in.close(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public class FileStream extends InputStream{ |
||||||
|
|
||||||
|
private ZipInputStream zis; |
||||||
|
|
||||||
|
public FileStream(ZipInputStream zis) { |
||||||
|
this.zis = zis; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int read() throws IOException { |
||||||
|
return zis.read(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int read(byte[] b) throws IOException { |
||||||
|
return zis.read(b); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int read(byte[] b, int off, int len) throws IOException { |
||||||
|
return zis.read(b, off, len); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public long skip(long n) throws IOException { |
||||||
|
return zis.skip(n); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int available() throws IOException { |
||||||
|
return zis.available(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void close() throws IOException { |
||||||
|
try { |
||||||
|
zis.closeEntry(); |
||||||
|
zis.close(); |
||||||
|
} finally { |
||||||
|
mutex.unlock(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public synchronized void mark(int readlimit) { |
||||||
|
zis.mark(readlimit); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public synchronized void reset() throws IOException { |
||||||
|
zis.reset(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean markSupported() { |
||||||
|
return zis.markSupported(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue