Commit db3b1a53 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

code that re-download source file if it's corrupted

parent 3fc1ffbe
......@@ -4,6 +4,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
......@@ -20,6 +21,7 @@ import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.NotOLE2FileException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
......@@ -250,35 +252,55 @@ public class MiRNAParser extends CachableInterface implements IExternalService {
*/
private InputStream getSourceInputStream() throws IOException {
if (this.sourceInputStream == null) {
this.sourceInputStream = new ByteArrayOutputStream();
if (!bigFileCache.isCached(URL_SOURCE_DATABASE)) {
try {
bigFileCache.downloadFile(URL_SOURCE_DATABASE, false, new IProgressUpdater() {
@Override
public void setProgress(double progress) {
}
});
} catch (URISyntaxException e) {
throw new IOException("Problem with downloading file: " + URL_SOURCE_DATABASE, e);
}
downloadSourceFile();
}
String filename = bigFileCache.getAbsolutePathForFile(URL_SOURCE_DATABASE);
byte[] buffer = new byte[BUFFER_SIZE];
int len;
InputStream stream = new FileInputStream(new File(filename));
this.sourceInputStream = downloadedFileToInputStream(filename);
//if downloaded file is corrupted download it again
try {
while ((len = stream.read(buffer)) > -1) {
this.sourceInputStream.write(buffer, 0, len);
}
} finally {
stream.close();
NPOIFSFileSystem element = new NPOIFSFileSystem(new ByteArrayInputStream(sourceInputStream.toByteArray()));
element.close();
} catch (NotOLE2FileException e) {
bigFileCache.removeFile(URL_SOURCE_DATABASE);
downloadSourceFile();
this.sourceInputStream = downloadedFileToInputStream(filename);
}
this.sourceInputStream.flush();
}
return new ByteArrayInputStream(sourceInputStream.toByteArray());
}
private ByteArrayOutputStream downloadedFileToInputStream(String filename) throws FileNotFoundException, IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int len;
InputStream stream = new FileInputStream(new File(filename));
try {
while ((len = stream.read(buffer)) > -1) {
result.write(buffer, 0, len);
}
} finally {
stream.close();
}
result.flush();
return result;
}
private void downloadSourceFile() throws IOException {
try {
bigFileCache.downloadFile(URL_SOURCE_DATABASE, false, new IProgressUpdater() {
@Override
public void setProgress(double progress) {
}
});
} catch (URISyntaxException e) {
throw new IOException("Problem with downloading file: " + URL_SOURCE_DATABASE, e);
}
}
/**
* @param originalNames
* names of miRNAs
......
......@@ -49,7 +49,7 @@ public class MiRNAParserTest extends AnnotationTestFunctions {
@Test
public void testExternalDBStatus() throws Exception {
ExternalServiceStatus status = miRNAParser.getServiceStatus();
assertTrue(status.getStatus().equals(ExternalServiceStatusType.OK));
assertEquals(status.getStatus(), ExternalServiceStatusType.OK);
}
@Test
......@@ -156,7 +156,7 @@ public class MiRNAParserTest extends AnnotationTestFunctions {
}
@Test(timeout = 50000)
public void testCachableInterfaceInvalidateChemical() throws Exception {
public void testCachableInterfaceInvalidateMiRNA() throws Exception {
try {
List<String> listIds = new ArrayList<>();
listIds.add("gga-miR-489-3p");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment