4 changed files with 136 additions and 19 deletions
@ -0,0 +1,92 @@
|
||||
package org.leolo.map.osm.extract.util; |
||||
|
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
|
||||
import java.io.*; |
||||
import java.net.MalformedURLException; |
||||
import java.net.URL; |
||||
|
||||
public abstract class InputFile { |
||||
public abstract long getSize(); |
||||
public abstract InputStream getInputStream() throws IOException; |
||||
|
||||
public long length() { |
||||
return getSize(); |
||||
} |
||||
|
||||
public boolean isValid(){ |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
class FileInputFile extends InputFile{ |
||||
|
||||
private File f; |
||||
|
||||
FileInputFile(File f){ |
||||
this.f = f; |
||||
} |
||||
|
||||
FileInputFile(String f){ |
||||
this.f = new File(f); |
||||
} |
||||
|
||||
@Override |
||||
public long getSize(){ |
||||
return f.length(); |
||||
} |
||||
|
||||
@Override |
||||
public InputStream getInputStream() throws FileNotFoundException { |
||||
return new FileInputStream(f); |
||||
} |
||||
|
||||
@Override |
||||
public boolean isValid(){ |
||||
return f.exists() && f.canRead(); |
||||
} |
||||
} |
||||
|
||||
class URLInputFile extends InputFile{ |
||||
|
||||
private Logger log = LogManager.getLogger(); |
||||
private boolean ready = false; |
||||
|
||||
private byte[] data; |
||||
|
||||
public static final int READ_BLOCK_SIZE = 4096; |
||||
|
||||
URLInputFile(String url) throws MalformedURLException, IOException { |
||||
this(new URL(url)); |
||||
} |
||||
URLInputFile(URL url) throws IOException{ |
||||
log.atInfo().log("Connecting to {}", url); |
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
||||
InputStream is = url.openStream(); |
||||
byte [] buffer = new byte[READ_BLOCK_SIZE]; |
||||
int readSize; |
||||
while((readSize = is.read(buffer))>0){ |
||||
baos.write(buffer, 0, readSize); |
||||
} |
||||
data = baos.toByteArray(); |
||||
is.close(); |
||||
baos.close(); |
||||
ready = true; |
||||
} |
||||
|
||||
@Override |
||||
public long getSize(){ |
||||
return data.length; |
||||
} |
||||
|
||||
@Override |
||||
public InputStream getInputStream() throws IOException { |
||||
return new ByteArrayInputStream(data); |
||||
} |
||||
|
||||
@Override |
||||
public boolean isValid() { |
||||
return ready; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue