org.apache.rocketmq.shaded.io.opentelemetry.exporter.internal.okhttp.ProtoRequestBody Maven / Gradle / Ivy
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.apache.rocketmq.shaded.io.opentelemetry.exporter.internal.okhttp;
import org.apache.rocketmq.shaded.io.opentelemetry.exporter.internal.marshal.Marshaler;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.BufferedSink;
/**
* A {@link RequestBody} for reading from a {@link Marshaler}.
*
* This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public final class ProtoRequestBody extends RequestBody {
private static final MediaType PROTOBUF_MEDIA_TYPE = MediaType.parse("application/x-protobuf");
private final Marshaler marshaler;
private final int contentLength;
/** Creates a new {@link ProtoRequestBody}. */
public ProtoRequestBody(Marshaler marshaler) {
this.marshaler = marshaler;
contentLength = marshaler.getBinarySerializedSize();
}
@Override
public long contentLength() {
return contentLength;
}
@Override
public MediaType contentType() {
return PROTOBUF_MEDIA_TYPE;
}
@Override
public void writeTo(BufferedSink bufferedSink) throws IOException {
marshaler.writeBinaryTo(bufferedSink.outputStream());
}
}