Skip to content

Commit 763bd1e

Browse files
committed
Review comments
1 parent 03a66ec commit 763bd1e

File tree

8 files changed

+251
-265
lines changed

8 files changed

+251
-265
lines changed

core/src/main/java/org/infinispan/protostream/ProtobufUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static void writeTo(ImmutableSerializationContext ctx, OutputStream out,
7575
write(ctx, TagWriterImpl.newInstance(ctx, out), t);
7676
}
7777

78-
public static void writeTo(ImmutableSerializationContext ctx, TagWriterImpl.Encoder encoder, Object t) throws IOException {
78+
public static void writeTo(ImmutableSerializationContext ctx, Encoder encoder, Object t) throws IOException {
7979
write(ctx, TagWriterImpl.newInstance(ctx, encoder), t);
8080
}
8181

@@ -116,7 +116,7 @@ public static <A> A fromByteBuffer(ImmutableSerializationContext ctx, ByteBuffer
116116
return readFrom(TagReaderImpl.newInstance(ctx, byteBuffer), clazz);
117117
}
118118

119-
public static <A> A fromDecoder(ImmutableSerializationContext ctx, TagReaderImpl.Decoder decoder, Class<A> clazz) throws IOException {
119+
public static <A> A fromDecoder(ImmutableSerializationContext ctx, Decoder decoder, Class<A> clazz) throws IOException {
120120
return readFrom(TagReaderImpl.newInstance(ctx, decoder), clazz);
121121
}
122122

@@ -145,7 +145,7 @@ public static <A> A fromWrappedStream(ImmutableSerializationContext ctx, InputSt
145145
return WrappedMessage.read(ctx, TagReaderImpl.newInstance(ctx, in));
146146
}
147147

148-
public static <A> A fromWrappedDecoder(ImmutableSerializationContext ctx, TagReaderImpl.Decoder decoder) throws IOException {
148+
public static <A> A fromWrappedDecoder(ImmutableSerializationContext ctx, Decoder decoder) throws IOException {
149149
return WrappedMessage.read(ctx, TagReaderImpl.newInstance(ctx, decoder));
150150
}
151151

@@ -174,7 +174,7 @@ public static void toWrappedStream(ImmutableSerializationContext ctx, OutputStre
174174
WrappedMessage.write(ctx, TagWriterImpl.newInstance(ctx, out, bufferSize), t);
175175
}
176176

177-
public static void toWrappedEncoder(ImmutableSerializationContext ctx, TagWriterImpl.Encoder encoder, Object t) throws IOException {
177+
public static void toWrappedEncoder(ImmutableSerializationContext ctx, Encoder encoder, Object t) throws IOException {
178178
WrappedMessage.write(ctx, TagWriterImpl.newInstance(ctx, encoder), t);
179179
}
180180

core/src/main/java/org/infinispan/protostream/TagReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ default int readEnum() throws IOException {
5757
* itself, possibly avoiding byte[] allocations
5858
* @return a new TagReader
5959
*/
60-
TagReader subReaderFromArray() throws IOException;
60+
ProtobufTagMarshaller.ReadContext subReaderFromArray() throws IOException;
6161

6262
default double readDouble() throws IOException {
6363
return Double.longBitsToDouble(readFixed64());

core/src/main/java/org/infinispan/protostream/TagWriter.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.infinispan.protostream;
22

3+
import java.io.Closeable;
34
import java.io.IOException;
45
import java.nio.ByteBuffer;
56

@@ -9,14 +10,14 @@
910
1011
* @since 4.4
1112
*/
12-
public interface TagWriter extends RawProtoStreamWriter {
13+
public interface TagWriter extends RawProtoStreamWriter, Closeable {
1314

1415
// start low level ops
1516
void flush() throws IOException;
1617

1718
/**
1819
* Invoke after done with writer, this implies a flush if necessary
19-
* It is necessary to invoke this on a writer returned from {@link #subWriter(int)} to actually push the data
20+
* It is necessary to invoke this on a writer returned from {@link #subWriter(int, boolean)} to actually push the data
2021
*/
2122
void close() throws IOException;
2223

@@ -99,9 +100,12 @@ default void writeBytes(int number, byte[] value) throws IOException {
99100

100101
/**
101102
* Used to write a sub message that can be optimized by implementation. When the sub writer is complete, flush
102-
* should be invoked to ensure
103-
* @return
104-
* @throws IOException
103+
* should be invoked to ensure bytes are written and close should be invoked to free any resources related to the
104+
* context (note close will flush as well)
105+
* @param number the message number of the sub message
106+
* @param nested whether this is a nested message or a new one
107+
* @return a write context for a sub message
108+
* @throws IOException exception if there is an issue
105109
*/
106-
TagWriter subWriter(int number, boolean nested) throws IOException;
110+
ProtobufTagMarshaller.WriteContext subWriter(int number, boolean nested) throws IOException;
107111
}

core/src/main/java/org/infinispan/protostream/WrappedMessage.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.infinispan.protostream;
22

3+
import java.io.Closeable;
34
import java.io.IOException;
45
import java.time.Instant;
56
import java.util.Date;
@@ -296,9 +297,11 @@ private static void writeMessage(ImmutableSerializationContext ctx, TagWriter ou
296297
if (t.getClass().isEnum()) {
297298
((EnumMarshallerDelegate) marshallerDelegate).encode(WRAPPED_ENUM, (Enum) t, out);
298299
} else {
299-
TagWriter nestedWriter = out.subWriter(WRAPPED_MESSAGE, false);
300-
marshallerDelegate.marshall((ProtobufTagMarshaller.WriteContext) nestedWriter, null, t);
301-
nestedWriter.close();
300+
ProtobufTagMarshaller.WriteContext nestedWriter = out.subWriter(WRAPPED_MESSAGE, false);
301+
marshallerDelegate.marshall(nestedWriter, null, t);
302+
if (nestedWriter instanceof Closeable) {
303+
((Closeable) nestedWriter).close();
304+
}
302305
}
303306
}
304307
}
@@ -353,7 +356,7 @@ private static <T> T readMessage(ImmutableSerializationContext ctx, TagReader in
353356
String typeName = null;
354357
Integer typeId = null;
355358
int enumValue = -1;
356-
TagReader messageReader = null;
359+
ProtobufTagMarshaller.ReadContext readContext = null;
357360
Object value = null;
358361
int fieldCount = 0;
359362
int expectedFieldCount = 1;
@@ -396,7 +399,7 @@ private static <T> T readMessage(ImmutableSerializationContext ctx, TagReader in
396399
}
397400
case WRAPPED_MESSAGE << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_LENGTH_DELIMITED: {
398401
expectedFieldCount = 2;
399-
messageReader = in.subReaderFromArray();
402+
readContext = in.subReaderFromArray();
400403
break;
401404
}
402405
case WRAPPED_STRING << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_LENGTH_DELIMITED: {
@@ -512,7 +515,7 @@ private static <T> T readMessage(ImmutableSerializationContext ctx, TagReader in
512515
}
513516
}
514517

515-
if (value == null && typeName == null && typeId == null && messageReader == null) {
518+
if (value == null && typeName == null && typeId == null && readContext == null) {
516519
return null;
517520
}
518521

@@ -531,9 +534,9 @@ private static <T> T readMessage(ImmutableSerializationContext ctx, TagReader in
531534
typeName = ctx.getDescriptorByTypeId(typeId).getFullName();
532535
}
533536
BaseMarshallerDelegate marshallerDelegate = ((SerializationContextImpl) ctx).getMarshallerDelegate(typeName);
534-
if (messageReader != null) {
537+
if (readContext != null) {
535538
// it's a Message type
536-
return (T) marshallerDelegate.unmarshall((ProtobufTagMarshaller.ReadContext) messageReader, null);
539+
return (T) marshallerDelegate.unmarshall(readContext, null);
537540
} else {
538541
// it's an Enum
539542
EnumMarshaller marshaller = (EnumMarshaller) marshallerDelegate.getMarshaller();

core/src/main/java/org/infinispan/protostream/annotations/impl/GeneratedMarshallerBase.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package org.infinispan.protostream.annotations.impl;
22

3+
import java.io.Closeable;
34
import java.io.IOException;
45

56
import org.infinispan.protostream.ProtobufTagMarshaller;
67
import org.infinispan.protostream.TagWriter;
78
import org.infinispan.protostream.impl.BaseMarshallerDelegate;
8-
import org.infinispan.protostream.impl.ByteArrayOutputStreamEx;
99
import org.infinispan.protostream.impl.Log;
10-
import org.infinispan.protostream.impl.TagWriterImpl;
1110

1211
/**
1312
* Base class for generated message marshallers. Provides some handy helper methods.
@@ -47,20 +46,11 @@ protected final <T> void writeNestedMessage(BaseMarshallerDelegate<T> marshaller
4746
throw log.maxNestedMessageDepth(maxNestedMessageDepth, message.getClass());
4847
}
4948

50-
if (ctx instanceof TagWriter) {
51-
TagWriter nestedWriter = ((TagWriter) ctx).subWriter(fieldNumber, true);
52-
marshallerDelegate.marshall((ProtobufTagMarshaller.WriteContext) nestedWriter, null, message);
53-
nestedWriter.close();
54-
} else {
55-
handleNonTagWriter(marshallerDelegate, ctx, fieldNumber, message);
49+
TagWriter tagWriter = ctx.getWriter();
50+
ProtobufTagMarshaller.WriteContext nestedWriter = tagWriter.subWriter(fieldNumber, true);
51+
marshallerDelegate.marshall(nestedWriter, null, message);
52+
if (nestedWriter instanceof Closeable) {
53+
((Closeable) nestedWriter).close();
5654
}
5755
}
58-
59-
private <T> void handleNonTagWriter(BaseMarshallerDelegate<T> marshallerDelegate, ProtobufTagMarshaller.WriteContext ctx,
60-
int fieldNumber, T message) throws IOException {
61-
ByteArrayOutputStreamEx baos = new ByteArrayOutputStreamEx();
62-
TagWriterImpl nested = TagWriterImpl.newNestedInstance(ctx, baos);
63-
writeMessage(marshallerDelegate, nested, message);
64-
ctx.getWriter().writeBytes(fieldNumber, baos.getByteBuffer());
65-
}
6656
}

0 commit comments

Comments
 (0)