Search in sources :

Example 6 with FileOutputStream

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream)

Example 7 with FileOutputStream

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());
}
Also used : FileUtils(io.druid.java.util.common.FileUtils) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) Date(java.util.Date) FileInputStream(java.io.FileInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) RestS3Service(org.jets3t.service.impl.rest.httpclient.RestS3Service) S3Object(org.jets3t.service.model.S3Object) File(java.io.File) Test(org.junit.Test)

Example 8 with FileOutputStream

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();
}
Also used : FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 9 with FileOutputStream

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();
                }
            }
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File) ZipFile(java.util.zip.ZipFile) BufferedOutputStream(java.io.BufferedOutputStream)

Example 10 with FileOutputStream

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();
        }
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Aggregations

FileOutputStream (java.io.FileOutputStream)13792 File (java.io.File)8295 IOException (java.io.IOException)6166 FileInputStream (java.io.FileInputStream)2644 OutputStream (java.io.OutputStream)2605 InputStream (java.io.InputStream)2077 BufferedOutputStream (java.io.BufferedOutputStream)1755 FileNotFoundException (java.io.FileNotFoundException)1531 OutputStreamWriter (java.io.OutputStreamWriter)1440 Test (org.junit.Test)1115 ZipEntry (java.util.zip.ZipEntry)734 BufferedWriter (java.io.BufferedWriter)668 ArrayList (java.util.ArrayList)654 ZipOutputStream (java.util.zip.ZipOutputStream)642 BufferedInputStream (java.io.BufferedInputStream)604 ByteArrayOutputStream (java.io.ByteArrayOutputStream)556 PrintWriter (java.io.PrintWriter)530 Properties (java.util.Properties)497 URL (java.net.URL)478 Writer (java.io.Writer)477