53 lines
1.2 KiB
Java
53 lines
1.2 KiB
Java
|
package eu.dnetlib.dhp.oa.graph.dump;
|
||
|
|
||
|
import okhttp3.MediaType;
|
||
|
import okhttp3.RequestBody;
|
||
|
import okhttp3.internal.Util;
|
||
|
import okio.BufferedSink;
|
||
|
import okio.Okio;
|
||
|
import okio.Source;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStream;
|
||
|
|
||
|
public class InputStreamRequestBody extends RequestBody {
|
||
|
|
||
|
private InputStream inputStream;
|
||
|
private MediaType mediaType;
|
||
|
|
||
|
public static RequestBody create(final MediaType mediaType, final InputStream inputStream) {
|
||
|
|
||
|
|
||
|
return new InputStreamRequestBody(inputStream, mediaType);
|
||
|
}
|
||
|
|
||
|
private InputStreamRequestBody(InputStream inputStream, MediaType mediaType) {
|
||
|
this.inputStream = inputStream;
|
||
|
this.mediaType = mediaType;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public MediaType contentType() {
|
||
|
return mediaType;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public long contentLength() {
|
||
|
try {
|
||
|
return inputStream.available();
|
||
|
} catch (IOException e) {
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void writeTo(BufferedSink sink) throws IOException {
|
||
|
Source source = null;
|
||
|
try {
|
||
|
source = Okio.source(inputStream);
|
||
|
sink.writeAll(source);
|
||
|
} finally {
|
||
|
Util.closeQuietly(source);
|
||
|
}
|
||
|
}
|
||
|
}
|