Search in sources :

Example 76 with OutputStream

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

the class JingleSocks5Transport method receive.

public void receive(final DownloadableFile file, final OnFileTransmissionStatusChanged callback) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            OutputStream fileOutputStream = null;
            final PowerManager.WakeLock wakeLock = connection.getConnectionManager().createWakeLock("jingle_receive_" + connection.getSessionId());
            try {
                wakeLock.acquire();
                MessageDigest digest = MessageDigest.getInstance("SHA-1");
                digest.reset();
                //inputStream.skip(45);
                socket.setSoTimeout(30000);
                file.getParentFile().mkdirs();
                file.createNewFile();
                fileOutputStream = connection.getFileOutputStream();
                if (fileOutputStream == null) {
                    callback.onFileTransferAborted();
                    Log.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": could not create output stream");
                    return;
                }
                double size = file.getExpectedSize();
                long remainingSize = file.getExpectedSize();
                byte[] buffer = new byte[8192];
                int count;
                while (remainingSize > 0) {
                    count = inputStream.read(buffer);
                    if (count == -1) {
                        callback.onFileTransferAborted();
                        Log.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": file ended prematurely with " + remainingSize + " bytes remaining");
                        return;
                    } else {
                        fileOutputStream.write(buffer, 0, count);
                        digest.update(buffer, 0, count);
                        remainingSize -= count;
                    }
                    connection.updateProgress((int) (((size - remainingSize) / size) * 100));
                }
                fileOutputStream.flush();
                fileOutputStream.close();
                file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest()));
                callback.onFileTransmitted(file);
            } catch (Exception e) {
                Log.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": " + e.getMessage());
                callback.onFileTransferAborted();
            } finally {
                wakeLock.release();
                FileBackend.close(fileOutputStream);
                FileBackend.close(inputStream);
            }
        }
    }).start();
}
Also used : PowerManager(android.os.PowerManager) OutputStream(java.io.OutputStream) MessageDigest(java.security.MessageDigest) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 77 with OutputStream

use of java.io.OutputStream in project antlr4 by antlr.

the class BaseGoTest method copyFile.

private static void copyFile(File source, File dest) throws IOException {
    InputStream is = new FileInputStream(source);
    OutputStream os = new FileOutputStream(dest);
    byte[] buf = new byte[4 << 10];
    int l;
    while ((l = is.read(buf)) > -1) {
        os.write(buf, 0, l);
    }
    is.close();
    os.close();
}
Also used : ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream)

Example 78 with OutputStream

use of java.io.OutputStream in project antlr4 by antlr.

the class BaseCSharpTest method saveResourceAsFile.

private void saveResourceAsFile(String resourceName, File file) throws IOException {
    InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
    if (input == null) {
        System.err.println("Can't find " + resourceName + " as resource");
        throw new IOException("Missing resource:" + resourceName);
    }
    OutputStream output = new FileOutputStream(file.getAbsolutePath());
    while (input.available() > 0) {
        output.write(input.read());
    }
    output.close();
    input.close();
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException)

Example 79 with OutputStream

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

the class ApfTest method stageFile.

/**
     * Stage a file for testing, i.e. make it native accessible. Given a resource ID,
     * copy that resource into the app's data directory and return the path to it.
     */
private String stageFile(int rawId) throws Exception {
    File file = new File(getContext().getFilesDir(), "staged_file");
    new File(file.getParent()).mkdirs();
    InputStream in = null;
    OutputStream out = null;
    try {
        in = getContext().getResources().openRawResource(rawId);
        out = new FileOutputStream(file);
        Streams.copy(in, out);
    } finally {
        if (in != null)
            in.close();
        if (out != null)
            out.close();
    }
    return file.getAbsolutePath();
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 80 with OutputStream

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

the class PackageManagerShellCommand method doWriteSplit.

private int doWriteSplit(int sessionId, String inPath, long sizeBytes, String splitName, boolean logSuccess) throws RemoteException {
    final PrintWriter pw = getOutPrintWriter();
    if (sizeBytes <= 0) {
        pw.println("Error: must specify a APK size");
        return 1;
    }
    if (inPath != null && !"-".equals(inPath)) {
        pw.println("Error: APK content must be streamed");
        return 1;
    }
    inPath = null;
    final SessionInfo info = mInterface.getPackageInstaller().getSessionInfo(sessionId);
    PackageInstaller.Session session = null;
    InputStream in = null;
    OutputStream out = null;
    try {
        session = new PackageInstaller.Session(mInterface.getPackageInstaller().openSession(sessionId));
        if (inPath != null) {
            in = new FileInputStream(inPath);
        } else {
            in = new SizedInputStream(getRawInputStream(), sizeBytes);
        }
        out = session.openWrite(splitName, 0, sizeBytes);
        int total = 0;
        byte[] buffer = new byte[65536];
        int c;
        while ((c = in.read(buffer)) != -1) {
            total += c;
            out.write(buffer, 0, c);
            if (info.sizeBytes > 0) {
                final float fraction = ((float) c / (float) info.sizeBytes);
                session.addProgress(fraction);
            }
        }
        session.fsync(out);
        if (logSuccess) {
            pw.println("Success: streamed " + total + " bytes");
        }
        return 0;
    } catch (IOException e) {
        pw.println("Error: failed to write; " + e.getMessage());
        return 1;
    } finally {
        IoUtils.closeQuietly(out);
        IoUtils.closeQuietly(in);
        IoUtils.closeQuietly(session);
    }
}
Also used : SizedInputStream(com.android.internal.util.SizedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) SessionInfo(android.content.pm.PackageInstaller.SessionInfo) PackageInstaller(android.content.pm.PackageInstaller) SizedInputStream(com.android.internal.util.SizedInputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) PrintWriter(java.io.PrintWriter)

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