Search in sources :

Example 71 with FileOutputStream

use of java.io.FileOutputStream in project liquibase by liquibase.

the class ConvertCommand method run.

@Override
protected CommandResult run() throws Exception {
    List<ResourceAccessor> openers = new ArrayList<ResourceAccessor>();
    openers.add(new FileSystemResourceAccessor());
    openers.add(new ClassLoaderResourceAccessor());
    if (classpath != null) {
        openers.add(new FileSystemResourceAccessor(classpath));
    }
    ResourceAccessor resourceAccessor = new CompositeResourceAccessor(openers);
    ChangeLogParser sourceParser = ChangeLogParserFactory.getInstance().getParser(src, resourceAccessor);
    ChangeLogSerializer outSerializer = ChangeLogSerializerFactory.getInstance().getSerializer(out);
    DatabaseChangeLog changeLog = sourceParser.parse(src, new ChangeLogParameters(), resourceAccessor);
    File outFile = new File(out);
    if (!outFile.exists()) {
        outFile.getParentFile().mkdirs();
    }
    FileOutputStream outputStream = new FileOutputStream(outFile);
    try {
        outSerializer.write(changeLog.getChangeSets(), outputStream);
    } finally {
        outputStream.flush();
        outputStream.close();
    }
    return new CommandResult("Converted successfully");
}
Also used : CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ResourceAccessor(liquibase.resource.ResourceAccessor) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ArrayList(java.util.ArrayList) ChangeLogSerializer(liquibase.serializer.ChangeLogSerializer) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) CommandResult(liquibase.command.CommandResult) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) ChangeLogParameters(liquibase.changelog.ChangeLogParameters) ChangeLogParser(liquibase.parser.ChangeLogParser) FileOutputStream(java.io.FileOutputStream) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) File(java.io.File)

Example 72 with FileOutputStream

use of java.io.FileOutputStream in project android-gif-drawable by koral--.

the class ReLinker method unpackLibrary.

/**
	 * Attempts to unpack the given library to the workaround directory. Implements retry logic for
	 * IO operations to ensure they succeed.
	 *
	 * @param context {@link Context} to describe the location of the installed APK file
	 */
private static File unpackLibrary(final Context context) {
    final String outputFileName = MAPPED_BASE_LIB_NAME + BuildConfig.VERSION_NAME;
    File outputFile = new File(context.getDir(LIB_DIR, Context.MODE_PRIVATE), outputFileName);
    if (outputFile.isFile()) {
        return outputFile;
    }
    final File cachedLibraryFile = new File(context.getCacheDir(), outputFileName);
    if (cachedLibraryFile.isFile()) {
        return cachedLibraryFile;
    }
    final String mappedSurfaceLibraryName = System.mapLibraryName(LibraryLoader.SURFACE_LIBRARY_NAME);
    final FilenameFilter filter = new FilenameFilter() {

        @Override
        public boolean accept(File dir, String filename) {
            return filename.startsWith(MAPPED_BASE_LIB_NAME) || filename.startsWith(mappedSurfaceLibraryName);
        }
    };
    clearOldLibraryFiles(outputFile, filter);
    clearOldLibraryFiles(cachedLibraryFile, filter);
    final ApplicationInfo appInfo = context.getApplicationInfo();
    final File apkFile = new File(appInfo.sourceDir);
    ZipFile zipFile = null;
    try {
        zipFile = openZipFile(apkFile);
        int tries = 0;
        while (tries++ < MAX_TRIES) {
            ZipEntry libraryEntry = findLibraryEntry(zipFile);
            if (libraryEntry == null) {
                throw new IllegalStateException("Library " + MAPPED_BASE_LIB_NAME + " for supported ABIs not found in APK file");
            }
            InputStream inputStream = null;
            FileOutputStream fileOut = null;
            try {
                inputStream = zipFile.getInputStream(libraryEntry);
                fileOut = new FileOutputStream(outputFile);
                copy(inputStream, fileOut);
            } catch (IOException e) {
                if (tries > MAX_TRIES / 2) {
                    outputFile = cachedLibraryFile;
                }
                continue;
            } finally {
                closeSilently(inputStream);
                closeSilently(fileOut);
            }
            setFilePermissions(outputFile);
            break;
        }
    } finally {
        //http://bugs.java.com/view_bug.do?bug_id=6389768
        try {
            if (zipFile != null) {
                zipFile.close();
            }
        } catch (IOException ignored) {
        }
    }
    return outputFile;
}
Also used : FilenameFilter(java.io.FilenameFilter) ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) ApplicationInfo(android.content.pm.ApplicationInfo) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile) SuppressLint(android.annotation.SuppressLint)

Example 73 with FileOutputStream

use of java.io.FileOutputStream in project AndroidAsync by koush.

the class AsyncHttpClient method executeFile.

public Future<File> executeFile(AsyncHttpRequest req, final String filename, final FileCallback callback) {
    final File file = new File(filename);
    file.getParentFile().mkdirs();
    final OutputStream fout;
    try {
        fout = new BufferedOutputStream(new FileOutputStream(file), 8192);
    } catch (FileNotFoundException e) {
        SimpleFuture<File> ret = new SimpleFuture<File>();
        ret.setComplete(e);
        return ret;
    }
    final FutureAsyncHttpResponse cancel = new FutureAsyncHttpResponse();
    final SimpleFuture<File> ret = new SimpleFuture<File>() {

        @Override
        public void cancelCleanup() {
            try {
                cancel.get().setDataCallback(new DataCallback.NullDataCallback());
                cancel.get().close();
            } catch (Exception e) {
            }
            try {
                fout.close();
            } catch (Exception e) {
            }
            file.delete();
        }
    };
    ret.setParent(cancel);
    execute(req, 0, cancel, new HttpConnectCallback() {

        long mDownloaded = 0;

        @Override
        public void onConnectCompleted(Exception ex, final AsyncHttpResponse response) {
            if (ex != null) {
                try {
                    fout.close();
                } catch (IOException e) {
                }
                file.delete();
                invoke(callback, ret, response, ex, null);
                return;
            }
            invokeConnect(callback, response);
            final long contentLength = HttpUtil.contentLength(response.headers());
            response.setDataCallback(new OutputStreamDataCallback(fout) {

                @Override
                public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                    mDownloaded += bb.remaining();
                    super.onDataAvailable(emitter, bb);
                    invokeProgress(callback, response, mDownloaded, contentLength);
                }
            });
            response.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    try {
                        fout.close();
                    } catch (IOException e) {
                        ex = e;
                    }
                    if (ex != null) {
                        file.delete();
                        invoke(callback, ret, response, ex, null);
                    } else {
                        invoke(callback, ret, response, null, file);
                    }
                }
            });
        }
    });
    return ret;
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) OutputStreamDataCallback(com.koushikdutta.async.stream.OutputStreamDataCallback) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) OutputStreamDataCallback(com.koushikdutta.async.stream.OutputStreamDataCallback) DataCallback(com.koushikdutta.async.callback.DataCallback) TimeoutException(java.util.concurrent.TimeoutException) AsyncSSLException(com.koushikdutta.async.AsyncSSLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileOutputStream(java.io.FileOutputStream) DataEmitter(com.koushikdutta.async.DataEmitter) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Example 74 with FileOutputStream

use of java.io.FileOutputStream in project android-gif-drawable by koral--.

the class GifSourcesResolver method getFileFromAssets.

private File getFileFromAssets(Context context, String filename) {
    try {
        File file = new File(context.getCacheDir(), filename);
        final AssetFileDescriptor assetFileDescriptor = context.getResources().getAssets().openFd(filename);
        FileInputStream input = assetFileDescriptor.createInputStream();
        byte[] buf = new byte[(int) assetFileDescriptor.getDeclaredLength()];
        int bytesRead = input.read(buf);
        input.close();
        if (bytesRead != buf.length) {
            throw new IOException("Asset read failed");
        }
        FileOutputStream output = new FileOutputStream(file);
        output.write(buf, 0, bytesRead);
        output.close();
        return file;
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 75 with FileOutputStream

use of java.io.FileOutputStream in project ion by koush.

the class StreamTests method testStream.

public void testStream() throws Exception {
    new Random(39548394).nextBytes(random);
    Ion.with(getContext()).load("http://localhost:" + port + "/").write(new FileOutputStream(getContext().getFileStreamPath("test")), true).get();
    assertEquals(Md5.createInstance().update(random).digest(), Md5.digest(getContext().getFileStreamPath("test")));
}
Also used : Random(java.util.Random) FileOutputStream(java.io.FileOutputStream)

Aggregations

FileOutputStream (java.io.FileOutputStream)6586 File (java.io.File)3769 IOException (java.io.IOException)2864 FileInputStream (java.io.FileInputStream)1189 OutputStream (java.io.OutputStream)1071 BufferedOutputStream (java.io.BufferedOutputStream)907 InputStream (java.io.InputStream)864 FileNotFoundException (java.io.FileNotFoundException)682 OutputStreamWriter (java.io.OutputStreamWriter)644 Test (org.junit.Test)528 BufferedWriter (java.io.BufferedWriter)313 PrintWriter (java.io.PrintWriter)311 ZipEntry (java.util.zip.ZipEntry)311 BufferedInputStream (java.io.BufferedInputStream)287 DataOutputStream (java.io.DataOutputStream)275 ZipOutputStream (java.util.zip.ZipOutputStream)273 ByteArrayOutputStream (java.io.ByteArrayOutputStream)267 ArrayList (java.util.ArrayList)247 Writer (java.io.Writer)215 URL (java.net.URL)206