Search in sources :

Example 1 with DigestingOutputStream

use of org.whispersystems.signalservice.api.crypto.DigestingOutputStream in project libsignal-service-java by signalapp.

the class DigestingRequestBody method writeTo.

@Override
public void writeTo(BufferedSink sink) throws IOException {
    DigestingOutputStream outputStream = outputStreamFactory.createFor(sink.outputStream());
    byte[] buffer = new byte[8192];
    int read;
    long total = 0;
    while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
        outputStream.write(buffer, 0, read);
        total += read;
        if (progressListener != null) {
            progressListener.onAttachmentProgress(contentLength, total);
        }
    }
    outputStream.flush();
    digest = outputStream.getTransmittedDigest();
}
Also used : DigestingOutputStream(org.whispersystems.signalservice.api.crypto.DigestingOutputStream)

Example 2 with DigestingOutputStream

use of org.whispersystems.signalservice.api.crypto.DigestingOutputStream in project libsignal-service-java by signalapp.

the class PushServiceSocket method uploadAttachment.

private byte[] uploadAttachment(String method, String url, InputStream data, long dataSize, OutputStreamFactory outputStreamFactory, ProgressListener listener) throws IOException {
    URL uploadUrl = new URL(url);
    HttpsURLConnection connection = (HttpsURLConnection) uploadUrl.openConnection();
    connection.setDoOutput(true);
    if (dataSize > 0) {
        connection.setFixedLengthStreamingMode(Util.toIntExact(dataSize));
    } else {
        connection.setChunkedStreamingMode(0);
    }
    connection.setRequestMethod(method);
    connection.setRequestProperty("Content-Type", "application/octet-stream");
    connection.setRequestProperty("Connection", "close");
    connection.connect();
    try {
        DigestingOutputStream out = outputStreamFactory.createFor(connection.getOutputStream());
        byte[] buffer = new byte[4096];
        int read, written = 0;
        while ((read = data.read(buffer)) != -1) {
            out.write(buffer, 0, read);
            written += read;
            if (listener != null) {
                listener.onAttachmentProgress(dataSize, written);
            }
        }
        out.flush();
        data.close();
        out.close();
        if (connection.getResponseCode() != 200) {
            throw new IOException("Bad response: " + connection.getResponseCode() + " " + connection.getResponseMessage());
        }
        return out.getTransmittedDigest();
    } finally {
        connection.disconnect();
    }
}
Also used : DigestingOutputStream(org.whispersystems.signalservice.api.crypto.DigestingOutputStream) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 3 with DigestingOutputStream

use of org.whispersystems.signalservice.api.crypto.DigestingOutputStream in project Signal-Android by WhisperSystems.

the class DigestingRequestBody method writeTo.

@Override
public void writeTo(BufferedSink sink) throws IOException {
    DigestingOutputStream outputStream = outputStreamFactory.createFor(new SkippingOutputStream(contentStart, sink.outputStream()));
    byte[] buffer = new byte[8192];
    int read;
    long total = 0;
    while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
        if (cancelationSignal != null && cancelationSignal.isCanceled()) {
            throw new IOException("Canceled!");
        }
        outputStream.write(buffer, 0, read);
        total += read;
        if (progressListener != null) {
            progressListener.onAttachmentProgress(contentLength, total);
        }
    }
    outputStream.flush();
    digest = outputStream.getTransmittedDigest();
}
Also used : DigestingOutputStream(org.whispersystems.signalservice.api.crypto.DigestingOutputStream) SkippingOutputStream(org.whispersystems.signalservice.api.crypto.SkippingOutputStream) IOException(java.io.IOException)

Example 4 with DigestingOutputStream

use of org.whispersystems.signalservice.api.crypto.DigestingOutputStream in project Signal-Android by signalapp.

the class DigestingRequestBody method writeTo.

@Override
public void writeTo(BufferedSink sink) throws IOException {
    DigestingOutputStream outputStream = outputStreamFactory.createFor(new SkippingOutputStream(contentStart, sink.outputStream()));
    byte[] buffer = new byte[8192];
    int read;
    long total = 0;
    while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
        if (cancelationSignal != null && cancelationSignal.isCanceled()) {
            throw new IOException("Canceled!");
        }
        outputStream.write(buffer, 0, read);
        total += read;
        if (progressListener != null) {
            progressListener.onAttachmentProgress(contentLength, total);
        }
    }
    outputStream.flush();
    digest = outputStream.getTransmittedDigest();
}
Also used : DigestingOutputStream(org.whispersystems.signalservice.api.crypto.DigestingOutputStream) SkippingOutputStream(org.whispersystems.signalservice.api.crypto.SkippingOutputStream) IOException(java.io.IOException)

Aggregations

DigestingOutputStream (org.whispersystems.signalservice.api.crypto.DigestingOutputStream)4 IOException (java.io.IOException)3 SkippingOutputStream (org.whispersystems.signalservice.api.crypto.SkippingOutputStream)2 URL (java.net.URL)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1