Skip to content

Commit

Permalink
Remove exception handling from extractArchive (#2)
Browse files Browse the repository at this point in the history
As the catches didn't permit to handle correctly any of the exceptions
thrown during the process of extraction, it is better to throw them to
the caller. At this level, it is not possible to know the handling
strategy to use and this code should not handle them at all.
  • Loading branch information
alecharp authored and beothorn committed Apr 26, 2018
1 parent 461483d commit ba780c8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/main/java/com/github/junrar/extract/ExtractArchive.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.List;

import org.apache.commons.logging.Log;

Expand All @@ -25,14 +27,16 @@ public void setLogger(Log logger) {
this.logger = logger;
}

public void extractArchive(File archive, File destination) {
public void extractArchive(File archive, File destination) throws RarException, IOException {
Archive arch = null;
try {
arch = new Archive(archive);
} catch (RarException e) {
logError(e);
throw e;
} catch (IOException e1) {
logError(e1);
throw e1;
}
if (arch != null) {
if (arch.isEncrypted()) {
Expand Down Expand Up @@ -63,8 +67,10 @@ public void extractArchive(File archive, File destination) {
}
} catch (IOException e) {
logError(e, "error extracting the file");
throw e;
} catch (RarException e) {
logError(e,"error extraction the file");
throw e;
}
}
}finally {
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/github/junrar/testutil/ExtractArchive.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.junrar.testutil;

import java.io.File;
import java.io.IOException;

import com.github.junrar.exception.RarException;
import org.apache.commons.logging.LogFactory;

/**
Expand All @@ -12,15 +14,15 @@
*/
public class ExtractArchive {

public static void main(String[] args) {
public static void main(String[] args) throws IOException, RarException {
if (args.length == 2) {
extractArchive(args[0], args[1]);
} else {
System.out.println("usage: java -jar extractArchive.jar <thearchive> <the destination directory>");
}
}

public static void extractArchive(String archive, String destination) {
public static void extractArchive(String archive, String destination) throws IOException, RarException {
if (archive == null || destination == null) {
throw new RuntimeException("archive and destination must me set");
}
Expand All @@ -35,7 +37,7 @@ public static void extractArchive(String archive, String destination) {
ExtractArchive.extractArchive(arch, dest);
}

public static void extractArchive(File archive, File destination){
public static void extractArchive(File archive, File destination) throws IOException, RarException {
com.github.junrar.extract.ExtractArchive extractArchive = new com.github.junrar.extract.ExtractArchive();
extractArchive.setLogger(LogFactory.getLog(ExtractArchive.class.getName()));
extractArchive.extractArchive(archive, destination);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static void setupFunctionalTests() throws IOException{
}

@Test
public void unrarFile_FileContentsShouldMatchExpected() throws IOException{
public void unrarFile_FileContentsShouldMatchExpected() throws IOException, RarException {
InputStream resourceAsStream = JUnRarTestUtil.class.getResourceAsStream("test.rar");
File testRar = new File(tempFolder,"test.rar");
FileUtils.writeByteArrayToFile(testRar, IOUtils.toByteArray(resourceAsStream));
Expand Down

0 comments on commit ba780c8

Please sign in to comment.