Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: preserve KnownLength when wrapping InputStream #6852

Merged
merged 1 commit into from Mar 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 23 additions & 13 deletions api/src/main/java/io/grpc/ServerInterceptors.java
Expand Up @@ -139,24 +139,34 @@ public static ServerServiceDefinition useInputStreamMessages(
final ServerServiceDefinition serviceDef) {
final MethodDescriptor.Marshaller<InputStream> marshaller =
new MethodDescriptor.Marshaller<InputStream>() {
@Override
public InputStream stream(final InputStream value) {
return value;
}
@Override
public InputStream stream(final InputStream value) {
return value;
}

@Override
public InputStream parse(final InputStream stream) {
if (stream.markSupported()) {
return stream;
} else {
return new BufferedInputStream(stream);
}
}
};
@Override
public InputStream parse(final InputStream stream) {
if (stream.markSupported()) {
return stream;
} else if (stream instanceof KnownLength) {
return new KnownLengthBufferedInputStream(stream);
} else {
return new BufferedInputStream(stream);
}
}
};

return useMarshalledMessages(serviceDef, marshaller);
}

/** {@link BufferedInputStream} that also implements {@link KnownLength}. */
private static final class KnownLengthBufferedInputStream extends BufferedInputStream
implements KnownLength {
KnownLengthBufferedInputStream(InputStream in) {
super(in);
}
}

/**
* Create a new {@code ServerServiceDefinition} whose {@link MethodDescriptor} serializes to
* and from T for all methods. The {@code ServerCallHandler} created will automatically
Expand Down