Skip to content

Commit ac52303

Browse files
committed
more comments, improve HttpSerializers.bytesStreamingSerializer
1 parent d43d132 commit ac52303

File tree

6 files changed

+49
-8
lines changed

6 files changed

+49
-8
lines changed

servicetalk-http-api/src/main/java/io/servicetalk/http/api/DefaultHttpStreamingSerializer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ final class DefaultHttpStreamingSerializer<T> implements HttpStreamingSerializer
3636
DefaultHttpStreamingSerializer(final Serializer<T> serializer,
3737
final ToIntFunction<T> bytesEstimator,
3838
final Consumer<HttpHeaders> headersSerializeConsumer) {
39-
this.serializer = new NonFramedStreamingSerializer<>(serializer, bytesEstimator);
39+
this(new NonFramedStreamingSerializer<>(serializer, bytesEstimator), headersSerializeConsumer);
40+
}
41+
42+
DefaultHttpStreamingSerializer(final StreamingSerializer<T> serializer,
43+
final Consumer<HttpHeaders> headersSerializeConsumer) {
44+
this.serializer = requireNonNull(serializer);
4045
this.headersSerializeConsumer = requireNonNull(headersSerializeConsumer);
4146
}
4247

servicetalk-http-api/src/main/java/io/servicetalk/http/api/HttpSerializers.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import static io.servicetalk.http.api.HttpHeaderValues.TEXT_PLAIN;
4040
import static io.servicetalk.http.api.HttpHeaderValues.TEXT_PLAIN_US_ASCII;
4141
import static io.servicetalk.http.api.HttpHeaderValues.TEXT_PLAIN_UTF_8;
42-
import static io.servicetalk.serializer.utils.ByteArraySerializer.byteArraySerializer;
4342
import static io.servicetalk.serializer.utils.StringSerializer.stringSerializer;
4443
import static java.nio.charset.StandardCharsets.US_ASCII;
4544
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -288,15 +287,13 @@ public static HttpStreamingSerializer<String> stringStreamingSerializer(
288287
* body is provided in {@link String} and the {@link HttpHeaderNames#CONTENT_TYPE} is known a-prior
289288
* (e.g. streaming raw json data from a stream of {@link String}s). Deserialization should be done using
290289
* the a-prior knowledge to use a compatible {@link HttpStreamingDeserializer}.
291-
* @param forceCopy {@code true} means that data will always be copied from {@link Buffer} memory. {@code false}
292-
* means that if {@link Buffer#hasArray()} is {@code true} and the array offsets are aligned the result of
293-
* serialization doesn't have to be copied.
294290
* @param headersSerializeConsumer Sets the headers to indicate the appropriate encoding and content type.
295291
* @return a {@link HttpStreamingSerializer} that uses a {@link Serializer} for serialization.
296292
*/
297293
public static HttpStreamingSerializer<byte[]> bytesStreamingSerializer(
298-
boolean forceCopy, Consumer<HttpHeaders> headersSerializeConsumer) {
299-
return streamingSerializer(byteArraySerializer(forceCopy), bytes -> bytes.length, headersSerializeConsumer);
294+
Consumer<HttpHeaders> headersSerializeConsumer) {
295+
return new DefaultHttpStreamingSerializer<>(NonFramedBytesStreamingSerializer.INSTANCE,
296+
headersSerializeConsumer);
300297
}
301298

302299
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright © 2021 Apple Inc. and the ServiceTalk project authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.servicetalk.http.api;
17+
18+
import io.servicetalk.buffer.api.Buffer;
19+
import io.servicetalk.buffer.api.BufferAllocator;
20+
import io.servicetalk.concurrent.api.Publisher;
21+
import io.servicetalk.serializer.api.StreamingSerializer;
22+
23+
final class NonFramedBytesStreamingSerializer implements StreamingSerializer<byte[]> {
24+
static final StreamingSerializer<byte[]> INSTANCE = new NonFramedBytesStreamingSerializer();
25+
26+
private NonFramedBytesStreamingSerializer() {
27+
}
28+
29+
@Override
30+
public Publisher<Buffer> serialize(final Publisher<byte[]> toSerialize, final BufferAllocator allocator) {
31+
return toSerialize.map(allocator::wrap);
32+
}
33+
}

servicetalk-http-netty/src/test/java/io/servicetalk/http/netty/HttpRawDataSerializationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void bytesSerialization() throws Exception {
6262
expectedContentLength += s.length;
6363
}
6464

65-
runTest(content, contentType, expectedContentLength, bytesStreamingSerializer(false,
65+
runTest(content, contentType, expectedContentLength, bytesStreamingSerializer(
6666
headers -> headers.set(CONTENT_TYPE, contentType)));
6767
}
6868

servicetalk-serializer-api/src/main/java/io/servicetalk/serializer/api/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
/**
17+
* Serializer and deserializer APIs.
18+
*/
1619
@ElementsAreNonnullByDefault
1720
package io.servicetalk.serializer.api;
1821

servicetalk-serializer-utils/src/main/java/io/servicetalk/serializer/utils/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
/**
17+
* Serializer utilities.
18+
*/
1619
@ElementsAreNonnullByDefault
1720
package io.servicetalk.serializer.utils;
1821

0 commit comments

Comments
 (0)