Browse Source
# Conflicts: # src/main/java/org/leolo/map/osm/extract/ExtractElement.javapull/4/head
10 changed files with 247 additions and 33 deletions
@ -0,0 +1,6 @@
|
||||
package org.leolo.map.osm.extract.model; |
||||
|
||||
public enum InputType { |
||||
FILE, |
||||
URL; |
||||
} |
||||
@ -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; |
||||
} |
||||
} |
||||
@ -0,0 +1,27 @@
|
||||
package org.leolo.map.osm.extract.util; |
||||
|
||||
import org.leolo.map.osm.extract.model.InputType; |
||||
|
||||
import java.io.IOException; |
||||
import java.net.MalformedURLException; |
||||
import java.net.URL; |
||||
import java.util.Locale; |
||||
|
||||
public class InputUtil { |
||||
public static InputType identifyInputType(String str){ |
||||
try { |
||||
URL url = new URL(str); |
||||
} catch (Exception e) { |
||||
return InputType.FILE; |
||||
} |
||||
return InputType.URL; |
||||
} |
||||
|
||||
public static InputFile getInputFile(String path) throws IOException { |
||||
if(identifyInputType(path)==InputType.FILE){ |
||||
return new FileInputFile(path); |
||||
}else{ |
||||
return new URLInputFile(path); |
||||
} |
||||
} |
||||
} |
||||
@ -1,14 +0,0 @@
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.util.ArrayList; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.*; |
||||
|
||||
public class MainTest { |
||||
|
||||
@Test void test1(){ |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,50 @@
|
||||
package org.leolo.map.osm.extract.test; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.leolo.map.osm.extract.model.InputType; |
||||
import org.leolo.map.osm.extract.util.InputUtil; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.*; |
||||
|
||||
public class InputTypeTest { |
||||
|
||||
@Test void testFilePath(){ |
||||
assertEquals(InputUtil.identifyInputType("abc.xml"), InputType.FILE); |
||||
assertEquals(InputUtil.identifyInputType("../abc.xml"), InputType.FILE); |
||||
assertEquals(InputUtil.identifyInputType("./abc.xml"), InputType.FILE); |
||||
assertEquals(InputUtil.identifyInputType("..\\abc.xml"), InputType.FILE); |
||||
assertEquals(InputUtil.identifyInputType("/tmp/abc.xml"), InputType.FILE); |
||||
} |
||||
|
||||
@Test void testUPCPath(){ |
||||
assertEquals(InputUtil.identifyInputType("\\\\somecomputer\\somefolder\\abc.xml"), InputType.FILE); |
||||
} |
||||
|
||||
@Test void testURL(){ |
||||
assertEquals(InputUtil.identifyInputType("http://www.example.com"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("https://www.example.com"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("http://a@www.example.com"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("https://a@www.example.com"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("http://a:1234@www.example.com"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("https://a:1234@www.example.com"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("http://www.example.com:8521"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("https://www.example.com:8521"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("http://a@www.example.com:8521"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("https://a@www.example.com:8521"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("http://a:1234@www.example.com:8521"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("https://a:1234@www.example.com:8521"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("http://www.example.com/abc.xml"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("https://www.example.com/abc.xml"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("http://a@www.example.com/abc.xml"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("https://a@www.example.com/abc.xml"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("http://a:1234@www.example.com/abc.xml"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("https://a:1234@www.example.com/abc.xml"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("http://www.example.com:8521/abc.xml"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("https://www.example.com:8521/abc.xml"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("http://a@www.example.com:8521/abc.xml"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("https://a@www.example.com:8521/abc.xml"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("http://a:1234@www.example.com:8521/abc.xml"), InputType.URL); |
||||
assertEquals(InputUtil.identifyInputType("https://a:1234@www.example.com:8521/abc.xml"), InputType.URL); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,23 @@
|
||||
package org.leolo.map.osm.extract.test; |
||||
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.leolo.map.osm.extract.util.InputFile; |
||||
import org.leolo.map.osm.extract.util.InputUtil; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.ArrayList; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.*; |
||||
|
||||
public class MainTest { |
||||
|
||||
@Test void test1(){ |
||||
System.out.println("Test executed."); |
||||
} |
||||
|
||||
@Test void downloadFile() throws IOException { |
||||
InputFile inputFile = InputUtil.getInputFile("https://www.leolo.pro/sample"); |
||||
assertEquals(inputFile.length(), 8192); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue