org.whispersystems.signalservice.internal.push.http.DigestingRequestBody Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of signal-service-java Show documentation
Show all versions of signal-service-java Show documentation
Signal Service communication library for Java
package org.whispersystems.signalservice.internal.push.http;
import org.whispersystems.signalservice.api.crypto.DigestingOutputStream;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.BufferedSink;
public class DigestingRequestBody extends RequestBody {
private final InputStream inputStream;
private final OutputStreamFactory outputStreamFactory;
private final String contentType;
private final long contentLength;
private byte[] digest;
public DigestingRequestBody(InputStream inputStream,
OutputStreamFactory outputStreamFactory,
String contentType, long contentLength)
{
this.inputStream = inputStream;
this.outputStreamFactory = outputStreamFactory;
this.contentType = contentType;
this.contentLength = contentLength;
}
@Override
public MediaType contentType() {
return MediaType.parse(contentType);
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
DigestingOutputStream outputStream = outputStreamFactory.createFor(sink.outputStream());
byte[] buffer = new byte[4096];
int read;
while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
outputStream.write(buffer, 0, read);
}
outputStream.flush();
digest = outputStream.getTransmittedDigest();
}
@Override
public long contentLength() {
return contentLength;
}
public byte[] getTransmittedDigest() {
return digest;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy