Skip to content

Commit

Permalink
PipedStreams replaced by ByteArrayStreams. This eats memory but works…
Browse files Browse the repository at this point in the history
… extremely faster (at least on my Nexus 7).
  • Loading branch information
geometer committed Jan 29, 2015
1 parent d1079df commit c44496d
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions unrar/src/main/java/com/github/junrar/Archive.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
Expand Down Expand Up @@ -461,28 +461,21 @@ public void extractFile(FileHeader hd, OutputStream os) throws RarException {
* @throws IOException
* if any IO error occur
*/
public InputStream getInputStream(final FileHeader hd) throws RarException,
IOException {
final PipedInputStream in = new PipedInputStream(32 * 1024);
final PipedOutputStream out = new PipedOutputStream(in);

// creates a new thread that will write data to the pipe. Data will be
// available in another InputStream, connected to the OutputStream.
new Thread(new Runnable() {
public void run() {
try {
extractFile(hd, out);
} catch (RarException e) {
} finally {
try {
out.close();
} catch (IOException e) {
}
}
public InputStream getInputStream(final FileHeader hd) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();

try {
extractFile(hd, out);
} catch (RarException e) {
throw new IOException("RAR extracting issue", e);
} finally {
try {
out.close();
} catch (IOException e) {
}
}).start();
}

return in;
return new ByteArrayInputStream(out.toByteArray());
}

private void doExtractFile(FileHeader hd, OutputStream os)
Expand Down

0 comments on commit c44496d

Please sign in to comment.