use of java.io.FileOutputStream in project buck by facebook.
the class ExopackageSoLoader method copySoFileIfRequired.
private static File copySoFileIfRequired(String libname) {
File libraryFile = new File(privateNativeLibsDir, libname + ".so");
if (libraryFile.exists()) {
return libraryFile;
}
if (!abi1Libraries.containsKey(libname) && !abi2Libraries.containsKey(libname)) {
return null;
}
String abiDir;
String sourceFilename;
if (abi1Libraries.containsKey(libname)) {
sourceFilename = abi1Libraries.get(libname);
abiDir = Build.CPU_ABI;
} else {
sourceFilename = abi2Libraries.get(libname);
abiDir = Build.CPU_ABI2;
}
String sourcePath = nativeLibsDir + abiDir + "/" + sourceFilename;
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(sourcePath));
out = new BufferedOutputStream(new FileOutputStream(libraryFile));
byte[] buffer = new byte[4 * 1024];
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return libraryFile;
}
use of java.io.FileOutputStream in project druid by druid-io.
the class S3DataSegmentPullerTest method testGZUncompress.
@Test
public void testGZUncompress() throws ServiceException, IOException, SegmentLoadingException {
final String bucket = "bucket";
final String keyPrefix = "prefix/dir/0";
final RestS3Service s3Client = EasyMock.createStrictMock(RestS3Service.class);
final byte[] value = bucket.getBytes("utf8");
final File tmpFile = temporaryFolder.newFile("gzTest.gz");
try (OutputStream outputStream = new GZIPOutputStream(new FileOutputStream(tmpFile))) {
outputStream.write(value);
}
final S3Object object0 = new S3Object();
object0.setBucketName(bucket);
object0.setKey(keyPrefix + "/renames-0.gz");
object0.setLastModifiedDate(new Date(0));
object0.setDataInputStream(new FileInputStream(tmpFile));
final File tmpDir = temporaryFolder.newFolder("gzTestDir");
EasyMock.expect(s3Client.getObjectDetails(EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))).andReturn(null).once();
EasyMock.expect(s3Client.getObjectDetails(EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))).andReturn(object0).once();
EasyMock.expect(s3Client.getObject(EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))).andReturn(object0).once();
S3DataSegmentPuller puller = new S3DataSegmentPuller(s3Client);
EasyMock.replay(s3Client);
FileUtils.FileCopyResult result = puller.getSegmentFiles(new S3DataSegmentPuller.S3Coords(bucket, object0.getKey()), tmpDir);
EasyMock.verify(s3Client);
Assert.assertEquals(value.length, result.size());
File expected = new File(tmpDir, "renames-0");
Assert.assertTrue(expected.exists());
Assert.assertEquals(value.length, expected.length());
}
use of java.io.FileOutputStream in project lucida by claritylab.
the class FileUtils method writeSerialized.
/**
* Writes a serialized object to a file.
*
* @param o object
* @param output output file
*/
public static void writeSerialized(Object o, File output) throws IOException {
FileOutputStream fos = new FileOutputStream(output);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(o);
oos.close();
}
use of java.io.FileOutputStream in project cogtool by cogtool.
the class ZipUtil method unzip.
/**
* Unzips a zipfile to a destination directory.
* @param zip the file to unzip
* @param fileDir the destination directory for zipfile contents
* @throws FileNotFoundException
* @throws IOException
*/
public static void unzip(ZipFile zip, File fileDir) throws FileNotFoundException, IOException {
// Read out all entries from ZipFile via input streams
for (Enumeration<? extends ZipEntry> en = zip.entries(); en.hasMoreElements(); ) {
ZipEntry ze = en.nextElement();
// Get info from file entry
long size = ze.getSize();
// Create File in fileDir for unpacked entry
String name = ze.getName();
// System.out.println("Unzipping: " + name);
File zeFile = new File(fileDir, name);
// Check for a trailing slash to see if this is a directory entry
if (name.charAt(name.length() - 1) == '/') {
// If this entry is a directory, make it
zeFile.mkdirs();
} else {
// if this entry is a file, make its parent directories, then it
zeFile.getParentFile().mkdirs();
zeFile.createNewFile();
// Create plus OutputStream to the new file
FileOutputStream fout = null;
OutputStream out = null;
// Get ZipInputStream for reading data
InputStream zin = null;
try {
fout = new FileOutputStream(zeFile);
out = new BufferedOutputStream(fout);
zin = zip.getInputStream(ze);
// Set modification time
zeFile.setLastModified(ze.getTime());
// Copy data from zin to out, 100k at a time
int chunkSize = 100 * 1024;
byte[] buff = new byte[chunkSize];
int len = chunkSize;
for (; size > 0; size -= len) {
if (size < chunkSize) {
len = (int) size;
} else {
len = chunkSize;
}
int actualBytes = 0;
int off = 0;
do {
actualBytes = zin.read(buff, off, len);
if (actualBytes == -1) {
out.write(buff, off, len);
// System.out.print("!" + len + ':' + actualBytes + ':' + off + ' ');
throw new RuntimeException("Bad math in unzip!");
} else {
out.write(buff, off, actualBytes);
// System.out.print("" + len + ':' + actualBytes + ':' + off + ' ');
}
len -= actualBytes;
size -= actualBytes;
off += actualBytes;
} while ((len > 0));
}
} finally {
// Close the streams
if (fout != null) {
if (out != null) {
if (zin != null) {
zin.close();
}
out.close();
}
fout.close();
}
}
}
}
}
use of java.io.FileOutputStream in project cogtool by cogtool.
the class ZipUtil method zip.
/**
* Zips a set of files into a single zip archive file.
* @param srcFiles a list containing Files to compress
* @param zip the destination file location for the archive
*/
public static void zip(List<File> srcFiles, File dst) throws IOException {
// Create a ZipOutputStream
FileOutputStream fos = null;
ZipOutputStream zip = null;
try {
fos = new FileOutputStream(dst);
zip = new ZipOutputStream(fos);
zip.setLevel(9);
zip.setMethod(ZipOutputStream.DEFLATED);
// Recursively add file entries
for (File src : srcFiles) {
if (src.isDirectory()) {
zipDirectory(src, "", zip);
} else {
zipOneFile(src, "", zip);
}
}
} finally {
// Close the output stream
if (fos != null) {
if (zip != null) {
zip.close();
}
fos.close();
}
}
}
Aggregations