Skip to content
Snippets Groups Projects
Commit 67ecb276 authored by Mick Jordan's avatar Mick Jordan
Browse files

Use xz internal for RCompression.lzmaUncompressFromFile

parent f222f71e
Branches
No related tags found
No related merge requests found
......@@ -28,12 +28,15 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ProcessBuilder.Redirect;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.zip.GZIPInputStream;
import com.oracle.truffle.r.runtime.conn.GZIPConnections.GZIPRConnection;
import com.oracle.truffle.r.runtime.ffi.RFFIFactory;
import org.tukaani.xz.LZMA2InputStream;
import org.tukaani.xz.XZInputStream;
/**
* Abstracts the implementation of the various forms of compression used in R.
......@@ -180,7 +183,28 @@ public class RCompression {
* This is used by {@link GZIPRConnection}.
*/
public static byte[] lzmaUncompressFromFile(String path) {
return genericUncompressFromFile(new String[]{"xz", "--decompress", "--lzma2", "--stdout", path});
try {
byte[] data = Files.readAllBytes(Paths.get(path));
byte[] buffer = new byte[data.length * 4];
try (XZInputStream lzmaStream = new XZInputStream(new ByteArrayInputStream(data))) {
int totalRead = 0;
int n;
while ((n = lzmaStream.read(buffer, totalRead, buffer.length - totalRead)) > 0) {
totalRead += n;
if (totalRead == buffer.length) {
byte[] newbuffer = new byte[buffer.length * 2];
System.arraycopy(buffer, 0, newbuffer, 0, buffer.length);
buffer = newbuffer;
}
}
byte[] result = new byte[totalRead];
System.arraycopy(buffer, 0, result, 0, totalRead);
return result;
}
} catch (IOException ex) {
throw RInternalError.shouldNotReachHere(ex);
}
}
public static byte[] bzipUncompressFromFile(String path) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment