Search in sources :

Example 71 with OutputStream

use of java.io.OutputStream in project platform_frameworks_base by android.

the class WindowManagerService method viewServerGetFocusedWindow.

// TODO(multidisplay): Extend to multiple displays.
/**
     * Returns the focused window in the following format:
     * windowHashCodeInHexadecimal windowName
     *
     * @param client The remote client to send the listing to.
     * @return False if an error occurred, true otherwise.
     */
boolean viewServerGetFocusedWindow(Socket client) {
    if (isSystemSecure()) {
        return false;
    }
    boolean result = true;
    WindowState focusedWindow = getFocusedWindow();
    BufferedWriter out = null;
    // Any uncaught exception will crash the system process
    try {
        OutputStream clientStream = client.getOutputStream();
        out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
        if (focusedWindow != null) {
            out.write(Integer.toHexString(System.identityHashCode(focusedWindow)));
            out.write(' ');
            out.append(focusedWindow.mAttrs.getTitle());
        }
        out.write('\n');
        out.flush();
    } catch (Exception e) {
        result = false;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                result = false;
            }
        }
    }
    return result;
}
Also used : OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) OutOfResourcesException(android.view.Surface.OutOfResourcesException) FileNotFoundException(java.io.FileNotFoundException) BufferedWriter(java.io.BufferedWriter)

Example 72 with OutputStream

use of java.io.OutputStream in project jstorm by alibaba.

the class Utils method unJar.

/*
     * Unpack matching files from a jar. Entries inside the jar that do
     * not match the given pattern will be skipped.
     *
     * @param jarFile the .jar file to unpack
     * @param toDir the destination directory into which to unpack the jar
     */
public static void unJar(File jarFile, File toDir) throws IOException {
    JarFile jar = new JarFile(jarFile);
    try {
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = jar.getInputStream(entry);
                try {
                    File file = new File(toDir, entry.getName());
                    ensureDirectory(file.getParentFile());
                    OutputStream out = new FileOutputStream(file);
                    try {
                        copyBytes(in, out, 8192);
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    } finally {
        jar.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileInputStream(java.io.FileInputStream) ClassLoaderObjectInputStream(org.apache.commons.io.input.ClassLoaderObjectInputStream) InputStream(java.io.InputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) RandomAccessFile(java.io.RandomAccessFile) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 73 with OutputStream

use of java.io.OutputStream in project lombok by rzwitserloot.

the class CreateLombokRuntimeApp method writeRuntimeJar.

private int writeRuntimeJar(File outFile) throws Exception {
    Map<String, Class<?>> deps = new LinkedHashMap<String, Class<?>>();
    for (RuntimeDependencyInfo info : infoObjects) {
        List<String> depNames = info.getRuntimeDependencies();
        if (depNames != null)
            for (String depName : depNames) {
                if (!deps.containsKey(depName))
                    deps.put(depName, info.getClass());
            }
    }
    if (deps.isEmpty()) {
        System.out.println("Not generating lombok-runtime.jar: No lombok transformations currently have any runtime dependencies!");
        return 1;
    }
    OutputStream out = new FileOutputStream(outFile);
    boolean success = false;
    try {
        JarOutputStream jar = new JarOutputStream(out);
        deps.put("LICENSE", CreateLombokRuntimeApp.class);
        deps.put("AUTHORS", CreateLombokRuntimeApp.class);
        for (Entry<String, Class<?>> dep : deps.entrySet()) {
            InputStream in = dep.getValue().getResourceAsStream("/" + dep.getKey());
            try {
                if (in == null) {
                    throw new Fail(String.format("Dependency %s contributed by %s cannot be found", dep.getKey(), dep.getValue()));
                }
                writeIntoJar(jar, dep.getKey(), in);
            } finally {
                if (in != null)
                    in.close();
            }
        }
        jar.close();
        out.close();
        System.out.println("Successfully created: " + canonical(outFile));
        return 0;
    } catch (Throwable t) {
        try {
            out.close();
        } catch (Throwable ignore) {
        }
        if (!success)
            outFile.delete();
        if (t instanceof Fail) {
            System.err.println(t.getMessage());
            return 1;
        } else if (t instanceof Exception) {
            throw (Exception) t;
        } else if (t instanceof Error) {
            throw (Error) t;
        } else {
            throw new Exception(t);
        }
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException) InvalidCommandLineException(com.zwitserloot.cmdreader.InvalidCommandLineException) LinkedHashMap(java.util.LinkedHashMap) FileOutputStream(java.io.FileOutputStream)

Example 74 with OutputStream

use of java.io.OutputStream in project lombok by rzwitserloot.

the class AssertionLogger method logToFile.

private static synchronized void logToFile(String msg) {
    if (msg == null)
        return;
    try {
        OutputStream out = new FileOutputStream(LOG_PATH, true);
        out.write(msg.getBytes("UTF-8"));
        out.close();
    } catch (Exception e) {
        throw new RuntimeException("assertion logging can't write to log file", e);
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream)

Example 75 with OutputStream

use of java.io.OutputStream in project Conversations by siacs.

the class PgpDecryptionService method executeApi.

private void executeApi(Message message) {
    synchronized (message) {
        Intent params = new Intent();
        params.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
        if (message.getType() == Message.TYPE_TEXT) {
            InputStream is = new ByteArrayInputStream(message.getBody().getBytes());
            final OutputStream os = new ByteArrayOutputStream();
            Intent result = getOpenPgpApi().executeApi(params, is, os);
            switch(result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
                case OpenPgpApi.RESULT_CODE_SUCCESS:
                    try {
                        os.flush();
                        final String body = os.toString();
                        if (body == null) {
                            throw new IOException("body was null");
                        }
                        message.setBody(body);
                        message.setEncryption(Message.ENCRYPTION_DECRYPTED);
                        final HttpConnectionManager manager = mXmppConnectionService.getHttpConnectionManager();
                        if (message.trusted() && message.treatAsDownloadable() != Message.Decision.NEVER && manager.getAutoAcceptFileSize() > 0) {
                            manager.createNewDownloadConnection(message);
                        }
                    } catch (IOException e) {
                        message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
                    }
                    mXmppConnectionService.updateMessage(message);
                    break;
                case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
                    synchronized (PgpDecryptionService.this) {
                        PendingIntent pendingIntent = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
                        messages.addFirst(message);
                        currentMessage = null;
                        storePendingIntent(pendingIntent);
                    }
                    break;
                case OpenPgpApi.RESULT_CODE_ERROR:
                    message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
                    mXmppConnectionService.updateMessage(message);
                    break;
            }
        } else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
            try {
                final DownloadableFile inputFile = mXmppConnectionService.getFileBackend().getFile(message, false);
                final DownloadableFile outputFile = mXmppConnectionService.getFileBackend().getFile(message, true);
                outputFile.getParentFile().mkdirs();
                outputFile.createNewFile();
                InputStream is = new FileInputStream(inputFile);
                OutputStream os = new FileOutputStream(outputFile);
                Intent result = getOpenPgpApi().executeApi(params, is, os);
                switch(result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
                    case OpenPgpApi.RESULT_CODE_SUCCESS:
                        URL url = message.getFileParams().url;
                        mXmppConnectionService.getFileBackend().updateFileParams(message, url);
                        message.setEncryption(Message.ENCRYPTION_DECRYPTED);
                        inputFile.delete();
                        mXmppConnectionService.getFileBackend().updateMediaScanner(outputFile);
                        mXmppConnectionService.updateMessage(message);
                        break;
                    case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
                        synchronized (PgpDecryptionService.this) {
                            PendingIntent pendingIntent = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
                            messages.addFirst(message);
                            currentMessage = null;
                            storePendingIntent(pendingIntent);
                        }
                        break;
                    case OpenPgpApi.RESULT_CODE_ERROR:
                        message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
                        mXmppConnectionService.updateMessage(message);
                        break;
                }
            } catch (final IOException e) {
                message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
                mXmppConnectionService.updateMessage(message);
            }
        }
    }
    notifyIfPending(message);
}
Also used : FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) URL(java.net.URL) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) HttpConnectionManager(eu.siacs.conversations.http.HttpConnectionManager) PendingIntent(android.app.PendingIntent) DownloadableFile(eu.siacs.conversations.entities.DownloadableFile)

Aggregations

OutputStream (java.io.OutputStream)4178 IOException (java.io.IOException)1673 FileOutputStream (java.io.FileOutputStream)1331 InputStream (java.io.InputStream)1304 File (java.io.File)925 ByteArrayOutputStream (java.io.ByteArrayOutputStream)830 Test (org.junit.Test)735 BufferedOutputStream (java.io.BufferedOutputStream)477 FileInputStream (java.io.FileInputStream)431 Socket (java.net.Socket)375 ByteArrayInputStream (java.io.ByteArrayInputStream)233 URL (java.net.URL)231 OutputStreamWriter (java.io.OutputStreamWriter)230 HttpURLConnection (java.net.HttpURLConnection)189 BufferedInputStream (java.io.BufferedInputStream)178 InputStreamReader (java.io.InputStreamReader)176 BufferedReader (java.io.BufferedReader)159 FileNotFoundException (java.io.FileNotFoundException)158 GZIPOutputStream (java.util.zip.GZIPOutputStream)157 Path (java.nio.file.Path)155