This is an automated email from the ASF dual-hosted git repository.
liujun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new 5f938bf fix the bug when use protobuf-json (#4634)
5f938bf is described below
commit 5f938bfcc991e8f85b10347c13d4e152549afc11
Author: Mr.Z <[email protected]>
AuthorDate: Tue Jul 23 15:45:01 2019 +0800
fix the bug when use protobuf-json (#4634)
Fix #4632
---
.../support/GenericProtobufObjectInput.java | 36 +-
.../support/GenericProtobufObjectOutput.java | 40 +-
.../serialize/protobuf/support/MapValue.java | 807 +++++++++++++++++++++
.../support/GenericProtobufObjectOutputTest.java | 122 +++-
4 files changed, 978 insertions(+), 27 deletions(-)
diff --git
a/dubbo-serialization/dubbo-serialization-protobuf-json/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufObjectInput.java
b/dubbo-serialization/dubbo-serialization-protobuf-json/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufObjectInput.java
index a1c5650..92b1fe3 100644
---
a/dubbo-serialization/dubbo-serialization-protobuf-json/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufObjectInput.java
+++
b/dubbo-serialization/dubbo-serialization-protobuf-json/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufObjectInput.java
@@ -16,6 +16,13 @@
*/
package org.apache.dubbo.common.serialize.protobuf.support;
+import com.google.common.base.Charsets;
+import com.google.protobuf.BoolValue;
+import com.google.protobuf.DoubleValue;
+import com.google.protobuf.FloatValue;
+import com.google.protobuf.Int32Value;
+import com.google.protobuf.Int64Value;
+import com.google.protobuf.StringValue;
import org.apache.dubbo.common.serialize.ObjectInput;
import java.io.BufferedReader;
@@ -24,6 +31,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
+import java.util.Map;
/**
* GenericGoogleProtobuf object input implementation
@@ -37,52 +45,52 @@ public class GenericProtobufObjectInput implements
ObjectInput {
@Override
public boolean readBool() throws IOException {
- return read(boolean.class);
+ return read(BoolValue.class).getValue();
}
@Override
public byte readByte() throws IOException {
- return read(byte.class);
+ return (byte) read(Int32Value.class).getValue();
}
@Override
public short readShort() throws IOException {
- return read(short.class);
+ return (short) read(Int32Value.class).getValue();
}
@Override
public int readInt() throws IOException {
- return read(int.class);
+ return read(Int32Value.class).getValue();
}
@Override
public long readLong() throws IOException {
- return read(long.class);
+ return read(Int64Value.class).getValue();
}
@Override
public float readFloat() throws IOException {
- return read(float.class);
+ return read(FloatValue.class).getValue();
}
@Override
public double readDouble() throws IOException {
- return read(double.class);
+ return read(DoubleValue.class).getValue();
}
@Override
public String readUTF() throws IOException {
- return read(String.class);
+ return read(StringValue.class).getValue();
}
@Override
public byte[] readBytes() throws IOException {
- return readLine().getBytes();
+ return readUTF().getBytes(Charsets.ISO_8859_1);
}
@Override
- public Object readObject() throws IOException {
- return read(String.class);
+ public Object readObject() {
+ throw new UnsupportedOperationException();
}
@Override
@@ -105,7 +113,11 @@ public class GenericProtobufObjectInput implements
ObjectInput {
}
private <T> T read(Class<T> cls) throws IOException {
- if (!ProtobufUtils.isSupported(cls)) {
+ if (cls.equals(Map.class)) {
+ // only for attachments
+ String json = readLine();
+ return (T) ProtobufUtils.deserialize(json,
MapValue.Map.class).getAttachmentsMap();
+ } else if (!ProtobufUtils.isSupported(cls)) {
throw new IllegalArgumentException("This serialization only
support google protobuf entity, the class is :" + cls.getName());
}
diff --git
a/dubbo-serialization/dubbo-serialization-protobuf-json/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufObjectOutput.java
b/dubbo-serialization/dubbo-serialization-protobuf-json/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufObjectOutput.java
index 93b488e..de9f545 100644
---
a/dubbo-serialization/dubbo-serialization-protobuf-json/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufObjectOutput.java
+++
b/dubbo-serialization/dubbo-serialization-protobuf-json/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufObjectOutput.java
@@ -16,12 +16,20 @@
*/
package org.apache.dubbo.common.serialize.protobuf.support;
+import com.google.common.base.Charsets;
+import com.google.protobuf.BoolValue;
+import com.google.protobuf.DoubleValue;
+import com.google.protobuf.FloatValue;
+import com.google.protobuf.Int32Value;
+import com.google.protobuf.Int64Value;
+import com.google.protobuf.StringValue;
import org.apache.dubbo.common.serialize.ObjectOutput;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
+import java.util.Map;
/**
* GenericGoogleProtobuf object output implementation
@@ -36,61 +44,65 @@ public class GenericProtobufObjectOutput implements
ObjectOutput {
@Override
public void writeBool(boolean v) throws IOException {
- writeObject(v);
+
+ writeObject(BoolValue.newBuilder().setValue(v).build());
}
@Override
public void writeByte(byte v) throws IOException {
- writeObject(v);
+ writeObject(Int32Value.newBuilder().setValue((v)).build());
}
@Override
public void writeShort(short v) throws IOException {
- writeObject(v);
+ writeObject(Int32Value.newBuilder().setValue(v).build());
}
@Override
public void writeInt(int v) throws IOException {
- writeObject(v);
+ writeObject(Int32Value.newBuilder().setValue(v).build());
}
@Override
public void writeLong(long v) throws IOException {
- writeObject(v);
+ writeObject(Int64Value.newBuilder().setValue(v).build());
}
@Override
public void writeFloat(float v) throws IOException {
- writeObject(v);
+ writeObject(FloatValue.newBuilder().setValue(v).build());
}
@Override
public void writeDouble(double v) throws IOException {
- writeObject(v);
+ writeObject(DoubleValue.newBuilder().setValue(v).build());
}
@Override
public void writeUTF(String v) throws IOException {
- writeObject(v);
+ writeObject(StringValue.newBuilder().setValue(v).build());
}
@Override
- public void writeBytes(byte[] b) {
- writer.println(new String(b));
+ public void writeBytes(byte[] b) throws IOException {
+ writeUTF(new String(b, Charsets.ISO_8859_1));
}
@Override
- public void writeBytes(byte[] b, int off, int len) {
- writer.println(new String(b, off, len));
+ public void writeBytes(byte[] b, int off, int len) throws IOException {
+ writeUTF(new String(b, off, len, Charsets.ISO_8859_1));
}
+
@Override
public void writeObject(Object obj) throws IOException {
if (obj == null) {
throw new IllegalArgumentException("This serialization only
support google protobuf object, the object is : null");
}
-
- if (!ProtobufUtils.isSupported(obj.getClass())) {
+ if (obj instanceof Map) {
+ // only for attachment
+ obj = MapValue.Map.newBuilder().putAllAttachments((Map)
obj).build();
+ } else if (!ProtobufUtils.isSupported(obj.getClass())) {
throw new IllegalArgumentException("This serialization only
support google protobuf object, the object class is: " +
obj.getClass().getName());
}
diff --git
a/dubbo-serialization/dubbo-serialization-protobuf-json/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/MapValue.java
b/dubbo-serialization/dubbo-serialization-protobuf-json/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/MapValue.java
new file mode 100644
index 0000000..1dd844b
--- /dev/null
+++
b/dubbo-serialization/dubbo-serialization-protobuf-json/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/MapValue.java
@@ -0,0 +1,807 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.common.serialize.protobuf.support;
+
+/**
+ * Generated by the protocol buffer compiler. DO NOT EDIT!
+ */
+public final class MapValue {
+ private MapValue() {}
+ public static void registerAllExtensions(
+ com.google.protobuf.ExtensionRegistryLite registry) {
+ }
+
+ public static void registerAllExtensions(
+ com.google.protobuf.ExtensionRegistry registry) {
+ registerAllExtensions(
+ (com.google.protobuf.ExtensionRegistryLite) registry);
+ }
+ public interface MapOrBuilder extends
+ // @@protoc_insertion_point(interface_extends:Map)
+ com.google.protobuf.MessageOrBuilder {
+
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+ int getAttachmentsCount();
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+ boolean containsAttachments(
+ String key);
+ /**
+ * Use {@link #getAttachmentsMap()} instead.
+ */
+ @Deprecated
+ java.util.Map<String, String>
+ getAttachments();
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+ java.util.Map<String, String>
+ getAttachmentsMap();
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+
+ String getAttachmentsOrDefault(
+ String key,
+ String defaultValue);
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+
+ String getAttachmentsOrThrow(
+ String key);
+ }
+ /**
+ * Protobuf type {@code Map}
+ */
+ public static final class Map extends
+ com.google.protobuf.GeneratedMessageV3 implements
+ // @@protoc_insertion_point(message_implements:Map)
+ MapOrBuilder {
+ private static final long serialVersionUID = 0L;
+ // Use Map.newBuilder() to construct.
+ private Map(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
+ super(builder);
+ }
+ private Map() {
+ }
+
+ @Override
+ public final com.google.protobuf.UnknownFieldSet
+ getUnknownFields() {
+ return this.unknownFields;
+ }
+ private Map(
+ com.google.protobuf.CodedInputStream input,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws com.google.protobuf.InvalidProtocolBufferException {
+ this();
+ if (extensionRegistry == null) {
+ throw new NullPointerException();
+ }
+ int mutable_bitField0_ = 0;
+ com.google.protobuf.UnknownFieldSet.Builder unknownFields =
+ com.google.protobuf.UnknownFieldSet.newBuilder();
+ try {
+ boolean done = false;
+ while (!done) {
+ int tag = input.readTag();
+ switch (tag) {
+ case 0:
+ done = true;
+ break;
+ case 10: {
+ if (!((mutable_bitField0_ & 0x00000001) == 0x00000001)) {
+ attachments_ = com.google.protobuf.MapField.newMapField(
+ AttachmentsDefaultEntryHolder.defaultEntry);
+ mutable_bitField0_ |= 0x00000001;
+ }
+ com.google.protobuf.MapEntry<String, String>
+ attachments__ = input.readMessage(
+
AttachmentsDefaultEntryHolder.defaultEntry.getParserForType(),
extensionRegistry);
+ attachments_.getMutableMap().put(
+ attachments__.getKey(), attachments__.getValue());
+ break;
+ }
+ default: {
+ if (!parseUnknownFieldProto3(
+ input, unknownFields, extensionRegistry, tag)) {
+ done = true;
+ }
+ break;
+ }
+ }
+ }
+ } catch (com.google.protobuf.InvalidProtocolBufferException e) {
+ throw e.setUnfinishedMessage(this);
+ } catch (java.io.IOException e) {
+ throw new com.google.protobuf.InvalidProtocolBufferException(
+ e).setUnfinishedMessage(this);
+ } finally {
+ this.unknownFields = unknownFields.build();
+ makeExtensionsImmutable();
+ }
+ }
+ public static final com.google.protobuf.Descriptors.Descriptor
+ getDescriptor() {
+ return MapValue.internal_static_Map_descriptor;
+ }
+
+ @SuppressWarnings({"rawtypes"})
+ @Override
+ protected com.google.protobuf.MapField internalGetMapField(
+ int number) {
+ switch (number) {
+ case 1:
+ return internalGetAttachments();
+ default:
+ throw new RuntimeException(
+ "Invalid map field number: " + number);
+ }
+ }
+ @Override
+ protected FieldAccessorTable
+ internalGetFieldAccessorTable() {
+ return MapValue.internal_static_Map_fieldAccessorTable
+ .ensureFieldAccessorsInitialized(
+ Map.class, Builder.class);
+ }
+
+ public static final int ATTACHMENTS_FIELD_NUMBER = 1;
+ private static final class AttachmentsDefaultEntryHolder {
+ static final com.google.protobuf.MapEntry<
+ String, String> defaultEntry =
+ com.google.protobuf.MapEntry
+ .<String, String>newDefaultInstance(
+ MapValue.internal_static_Map_AttachmentsEntry_descriptor,
+ com.google.protobuf.WireFormat.FieldType.STRING,
+ "",
+ com.google.protobuf.WireFormat.FieldType.STRING,
+ "");
+ }
+ private com.google.protobuf.MapField<
+ String, String> attachments_;
+ private com.google.protobuf.MapField<String, String>
+ internalGetAttachments() {
+ if (attachments_ == null) {
+ return com.google.protobuf.MapField.emptyMapField(
+ AttachmentsDefaultEntryHolder.defaultEntry);
+ }
+ return attachments_;
+ }
+
+ public int getAttachmentsCount() {
+ return internalGetAttachments().getMap().size();
+ }
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+
+ public boolean containsAttachments(
+ String key) {
+ if (key == null) { throw new NullPointerException(); }
+ return internalGetAttachments().getMap().containsKey(key);
+ }
+ /**
+ * Use {@link #getAttachmentsMap()} instead.
+ */
+ @Deprecated
+ public java.util.Map<String, String> getAttachments() {
+ return getAttachmentsMap();
+ }
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+
+ public java.util.Map<String, String> getAttachmentsMap() {
+ return internalGetAttachments().getMap();
+ }
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+
+ public String getAttachmentsOrDefault(
+ String key,
+ String defaultValue) {
+ if (key == null) { throw new NullPointerException(); }
+ java.util.Map<String, String> map =
+ internalGetAttachments().getMap();
+ return map.containsKey(key) ? map.get(key) : defaultValue;
+ }
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+
+ public String getAttachmentsOrThrow(
+ String key) {
+ if (key == null) { throw new NullPointerException(); }
+ java.util.Map<String, String> map =
+ internalGetAttachments().getMap();
+ if (!map.containsKey(key)) {
+ throw new IllegalArgumentException();
+ }
+ return map.get(key);
+ }
+
+ private byte memoizedIsInitialized = -1;
+ @Override
+ public final boolean isInitialized() {
+ byte isInitialized = memoizedIsInitialized;
+ if (isInitialized == 1) return true;
+ if (isInitialized == 0) return false;
+
+ memoizedIsInitialized = 1;
+ return true;
+ }
+
+ @Override
+ public void writeTo(com.google.protobuf.CodedOutputStream output)
+ throws java.io.IOException {
+ com.google.protobuf.GeneratedMessageV3
+ .serializeStringMapTo(
+ output,
+ internalGetAttachments(),
+ AttachmentsDefaultEntryHolder.defaultEntry,
+ 1);
+ unknownFields.writeTo(output);
+ }
+
+ @Override
+ public int getSerializedSize() {
+ int size = memoizedSize;
+ if (size != -1) return size;
+
+ size = 0;
+ for (java.util.Map.Entry<String, String> entry
+ : internalGetAttachments().getMap().entrySet()) {
+ com.google.protobuf.MapEntry<String, String>
+ attachments__ =
AttachmentsDefaultEntryHolder.defaultEntry.newBuilderForType()
+ .setKey(entry.getKey())
+ .setValue(entry.getValue())
+ .build();
+ size += com.google.protobuf.CodedOutputStream
+ .computeMessageSize(1, attachments__);
+ }
+ size += unknownFields.getSerializedSize();
+ memoizedSize = size;
+ return size;
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (!(obj instanceof Map)) {
+ return super.equals(obj);
+ }
+ Map other = (Map) obj;
+
+ boolean result = true;
+ result = result && internalGetAttachments().equals(
+ other.internalGetAttachments());
+ result = result && unknownFields.equals(other.unknownFields);
+ return result;
+ }
+
+ @Override
+ public int hashCode() {
+ if (memoizedHashCode != 0) {
+ return memoizedHashCode;
+ }
+ int hash = 41;
+ hash = (19 * hash) + getDescriptor().hashCode();
+ if (!internalGetAttachments().getMap().isEmpty()) {
+ hash = (37 * hash) + ATTACHMENTS_FIELD_NUMBER;
+ hash = (53 * hash) + internalGetAttachments().hashCode();
+ }
+ hash = (29 * hash) + unknownFields.hashCode();
+ memoizedHashCode = hash;
+ return hash;
+ }
+
+ public static Map parseFrom(
+ java.nio.ByteBuffer data)
+ throws com.google.protobuf.InvalidProtocolBufferException {
+ return PARSER.parseFrom(data);
+ }
+ public static Map parseFrom(
+ java.nio.ByteBuffer data,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws com.google.protobuf.InvalidProtocolBufferException {
+ return PARSER.parseFrom(data, extensionRegistry);
+ }
+ public static Map parseFrom(
+ com.google.protobuf.ByteString data)
+ throws com.google.protobuf.InvalidProtocolBufferException {
+ return PARSER.parseFrom(data);
+ }
+ public static Map parseFrom(
+ com.google.protobuf.ByteString data,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws com.google.protobuf.InvalidProtocolBufferException {
+ return PARSER.parseFrom(data, extensionRegistry);
+ }
+ public static Map parseFrom(byte[] data)
+ throws com.google.protobuf.InvalidProtocolBufferException {
+ return PARSER.parseFrom(data);
+ }
+ public static Map parseFrom(
+ byte[] data,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws com.google.protobuf.InvalidProtocolBufferException {
+ return PARSER.parseFrom(data, extensionRegistry);
+ }
+ public static Map parseFrom(java.io.InputStream input)
+ throws java.io.IOException {
+ return com.google.protobuf.GeneratedMessageV3
+ .parseWithIOException(PARSER, input);
+ }
+ public static Map parseFrom(
+ java.io.InputStream input,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws java.io.IOException {
+ return com.google.protobuf.GeneratedMessageV3
+ .parseWithIOException(PARSER, input, extensionRegistry);
+ }
+ public static Map parseDelimitedFrom(java.io.InputStream input)
+ throws java.io.IOException {
+ return com.google.protobuf.GeneratedMessageV3
+ .parseDelimitedWithIOException(PARSER, input);
+ }
+ public static Map parseDelimitedFrom(
+ java.io.InputStream input,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws java.io.IOException {
+ return com.google.protobuf.GeneratedMessageV3
+ .parseDelimitedWithIOException(PARSER, input, extensionRegistry);
+ }
+ public static Map parseFrom(
+ com.google.protobuf.CodedInputStream input)
+ throws java.io.IOException {
+ return com.google.protobuf.GeneratedMessageV3
+ .parseWithIOException(PARSER, input);
+ }
+ public static Map parseFrom(
+ com.google.protobuf.CodedInputStream input,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws java.io.IOException {
+ return com.google.protobuf.GeneratedMessageV3
+ .parseWithIOException(PARSER, input, extensionRegistry);
+ }
+
+ @Override
+ public Builder newBuilderForType() { return newBuilder(); }
+ public static Builder newBuilder() {
+ return DEFAULT_INSTANCE.toBuilder();
+ }
+ public static Builder newBuilder(Map prototype) {
+ return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
+ }
+ @Override
+ public Builder toBuilder() {
+ return this == DEFAULT_INSTANCE
+ ? new Builder() : new Builder().mergeFrom(this);
+ }
+
+ @Override
+ protected Builder newBuilderForType(
+ BuilderParent parent) {
+ Builder builder = new Builder(parent);
+ return builder;
+ }
+ /**
+ * Protobuf type {@code Map}
+ */
+ public static final class Builder extends
+ com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
+ // @@protoc_insertion_point(builder_implements:Map)
+ MapOrBuilder {
+ public static final com.google.protobuf.Descriptors.Descriptor
+ getDescriptor() {
+ return MapValue.internal_static_Map_descriptor;
+ }
+
+ @SuppressWarnings({"rawtypes"})
+ protected com.google.protobuf.MapField internalGetMapField(
+ int number) {
+ switch (number) {
+ case 1:
+ return internalGetAttachments();
+ default:
+ throw new RuntimeException(
+ "Invalid map field number: " + number);
+ }
+ }
+ @SuppressWarnings({"rawtypes"})
+ protected com.google.protobuf.MapField internalGetMutableMapField(
+ int number) {
+ switch (number) {
+ case 1:
+ return internalGetMutableAttachments();
+ default:
+ throw new RuntimeException(
+ "Invalid map field number: " + number);
+ }
+ }
+ @Override
+ protected FieldAccessorTable
+ internalGetFieldAccessorTable() {
+ return MapValue.internal_static_Map_fieldAccessorTable
+ .ensureFieldAccessorsInitialized(
+ Map.class, Builder.class);
+ }
+
+ // Construct using
org.apache.dubbo.common.serialize.protobuf.support.MapValue.Map.newBuilder()
+ private Builder() {
+ maybeForceBuilderInitialization();
+ }
+
+ private Builder(
+ BuilderParent parent) {
+ super(parent);
+ maybeForceBuilderInitialization();
+ }
+ private void maybeForceBuilderInitialization() {
+ if (com.google.protobuf.GeneratedMessageV3
+ .alwaysUseFieldBuilders) {
+ }
+ }
+ @Override
+ public Builder clear() {
+ super.clear();
+ internalGetMutableAttachments().clear();
+ return this;
+ }
+
+ @Override
+ public com.google.protobuf.Descriptors.Descriptor
+ getDescriptorForType() {
+ return MapValue.internal_static_Map_descriptor;
+ }
+
+ @Override
+ public Map getDefaultInstanceForType() {
+ return Map.getDefaultInstance();
+ }
+
+ @Override
+ public Map build() {
+ Map result = buildPartial();
+ if (!result.isInitialized()) {
+ throw newUninitializedMessageException(result);
+ }
+ return result;
+ }
+
+ @Override
+ public Map buildPartial() {
+ Map result = new Map(this);
+ int from_bitField0_ = bitField0_;
+ result.attachments_ = internalGetAttachments();
+ result.attachments_.makeImmutable();
+ onBuilt();
+ return result;
+ }
+
+ @Override
+ public Builder clone() {
+ return (Builder) super.clone();
+ }
+ @Override
+ public Builder setField(
+ com.google.protobuf.Descriptors.FieldDescriptor field,
+ Object value) {
+ return (Builder) super.setField(field, value);
+ }
+ @Override
+ public Builder clearField(
+ com.google.protobuf.Descriptors.FieldDescriptor field) {
+ return (Builder) super.clearField(field);
+ }
+ @Override
+ public Builder clearOneof(
+ com.google.protobuf.Descriptors.OneofDescriptor oneof) {
+ return (Builder) super.clearOneof(oneof);
+ }
+ @Override
+ public Builder setRepeatedField(
+ com.google.protobuf.Descriptors.FieldDescriptor field,
+ int index, Object value) {
+ return (Builder) super.setRepeatedField(field, index, value);
+ }
+ @Override
+ public Builder addRepeatedField(
+ com.google.protobuf.Descriptors.FieldDescriptor field,
+ Object value) {
+ return (Builder) super.addRepeatedField(field, value);
+ }
+ @Override
+ public Builder mergeFrom(com.google.protobuf.Message other) {
+ if (other instanceof Map) {
+ return mergeFrom((Map)other);
+ } else {
+ super.mergeFrom(other);
+ return this;
+ }
+ }
+
+ public Builder mergeFrom(Map other) {
+ if (other == Map.getDefaultInstance()) return this;
+ internalGetMutableAttachments().mergeFrom(
+ other.internalGetAttachments());
+ this.mergeUnknownFields(other.unknownFields);
+ onChanged();
+ return this;
+ }
+
+ @Override
+ public final boolean isInitialized() {
+ return true;
+ }
+
+ @Override
+ public Builder mergeFrom(
+ com.google.protobuf.CodedInputStream input,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws java.io.IOException {
+ Map parsedMessage = null;
+ try {
+ parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
+ } catch (com.google.protobuf.InvalidProtocolBufferException e) {
+ parsedMessage = (Map) e.getUnfinishedMessage();
+ throw e.unwrapIOException();
+ } finally {
+ if (parsedMessage != null) {
+ mergeFrom(parsedMessage);
+ }
+ }
+ return this;
+ }
+ private int bitField0_;
+
+ private com.google.protobuf.MapField<
+ String, String> attachments_;
+ private com.google.protobuf.MapField<String, String>
+ internalGetAttachments() {
+ if (attachments_ == null) {
+ return com.google.protobuf.MapField.emptyMapField(
+ AttachmentsDefaultEntryHolder.defaultEntry);
+ }
+ return attachments_;
+ }
+ private com.google.protobuf.MapField<String, String>
+ internalGetMutableAttachments() {
+ onChanged();;
+ if (attachments_ == null) {
+ attachments_ = com.google.protobuf.MapField.newMapField(
+ AttachmentsDefaultEntryHolder.defaultEntry);
+ }
+ if (!attachments_.isMutable()) {
+ attachments_ = attachments_.copy();
+ }
+ return attachments_;
+ }
+
+ public int getAttachmentsCount() {
+ return internalGetAttachments().getMap().size();
+ }
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+
+ public boolean containsAttachments(
+ String key) {
+ if (key == null) { throw new NullPointerException(); }
+ return internalGetAttachments().getMap().containsKey(key);
+ }
+ /**
+ * Use {@link #getAttachmentsMap()} instead.
+ */
+ @Deprecated
+ public java.util.Map<String, String> getAttachments() {
+ return getAttachmentsMap();
+ }
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+
+ public java.util.Map<String, String> getAttachmentsMap() {
+ return internalGetAttachments().getMap();
+ }
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+
+ public String getAttachmentsOrDefault(
+ String key,
+ String defaultValue) {
+ if (key == null) { throw new NullPointerException(); }
+ java.util.Map<String, String> map =
+ internalGetAttachments().getMap();
+ return map.containsKey(key) ? map.get(key) : defaultValue;
+ }
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+
+ public String getAttachmentsOrThrow(
+ String key) {
+ if (key == null) { throw new NullPointerException(); }
+ java.util.Map<String, String> map =
+ internalGetAttachments().getMap();
+ if (!map.containsKey(key)) {
+ throw new IllegalArgumentException();
+ }
+ return map.get(key);
+ }
+
+ public Builder clearAttachments() {
+ internalGetMutableAttachments().getMutableMap()
+ .clear();
+ return this;
+ }
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+
+ public Builder removeAttachments(
+ String key) {
+ if (key == null) { throw new NullPointerException(); }
+ internalGetMutableAttachments().getMutableMap()
+ .remove(key);
+ return this;
+ }
+ /**
+ * Use alternate mutation accessors instead.
+ */
+ @Deprecated
+ public java.util.Map<String, String>
+ getMutableAttachments() {
+ return internalGetMutableAttachments().getMutableMap();
+ }
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+ public Builder putAttachments(
+ String key,
+ String value) {
+ if (key == null) { throw new NullPointerException(); }
+ if (value == null) { throw new NullPointerException(); }
+ internalGetMutableAttachments().getMutableMap()
+ .put(key, value);
+ return this;
+ }
+ /**
+ * <code>map<string, string> attachments = 1;</code>
+ */
+
+ public Builder putAllAttachments(
+ java.util.Map<String, String> values) {
+ internalGetMutableAttachments().getMutableMap()
+ .putAll(values);
+ return this;
+ }
+ @Override
+ public final Builder setUnknownFields(
+ final com.google.protobuf.UnknownFieldSet unknownFields) {
+ return super.setUnknownFieldsProto3(unknownFields);
+ }
+
+ @Override
+ public final Builder mergeUnknownFields(
+ final com.google.protobuf.UnknownFieldSet unknownFields) {
+ return super.mergeUnknownFields(unknownFields);
+ }
+
+
+ // @@protoc_insertion_point(builder_scope:Map)
+ }
+
+ // @@protoc_insertion_point(class_scope:Map)
+ private static final Map DEFAULT_INSTANCE;
+ static {
+ DEFAULT_INSTANCE = new Map();
+ }
+
+ public static Map getDefaultInstance() {
+ return DEFAULT_INSTANCE;
+ }
+
+ private static final com.google.protobuf.Parser<Map>
+ PARSER = new com.google.protobuf.AbstractParser<Map>() {
+ @Override
+ public Map parsePartialFrom(
+ com.google.protobuf.CodedInputStream input,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws com.google.protobuf.InvalidProtocolBufferException {
+ return new Map(input, extensionRegistry);
+ }
+ };
+
+ public static com.google.protobuf.Parser<Map> parser() {
+ return PARSER;
+ }
+
+ @Override
+ public com.google.protobuf.Parser<Map> getParserForType() {
+ return PARSER;
+ }
+
+ @Override
+ public Map getDefaultInstanceForType() {
+ return DEFAULT_INSTANCE;
+ }
+
+ }
+
+ private static final com.google.protobuf.Descriptors.Descriptor
+ internal_static_Map_descriptor;
+ private static final
+ com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
+ internal_static_Map_fieldAccessorTable;
+ private static final com.google.protobuf.Descriptors.Descriptor
+ internal_static_Map_AttachmentsEntry_descriptor;
+ private static final
+ com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
+ internal_static_Map_AttachmentsEntry_fieldAccessorTable;
+
+ public static com.google.protobuf.Descriptors.FileDescriptor
+ getDescriptor() {
+ return descriptor;
+ }
+ private static com.google.protobuf.Descriptors.FileDescriptor
+ descriptor;
+ static {
+ String[] descriptorData = {
+ "\n\tmap.proto\"e\n\003Map\022*\n\013attachments\030\001 \003(\0132" +
+ "\025.Map.AttachmentsEntry\0322\n\020AttachmentsEnt" +
+ "ry\022\013\n\003key\030\001 \001(\t\022\r\n\005value\030\002
\001(\t:\0028\001B>\n2or" +
+ "g.apache.dubbo.common.serialize.protobuf" +
+ ".supportB\010MapValueb\006proto3"
+ };
+ com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner
assigner =
+ new com.google.protobuf.Descriptors.FileDescriptor.
InternalDescriptorAssigner() {
+ public com.google.protobuf.ExtensionRegistry assignDescriptors(
+ com.google.protobuf.Descriptors.FileDescriptor root) {
+ descriptor = root;
+ return null;
+ }
+ };
+ com.google.protobuf.Descriptors.FileDescriptor
+ .internalBuildGeneratedFileFrom(descriptorData,
+ new com.google.protobuf.Descriptors.FileDescriptor[] {
+ }, assigner);
+ internal_static_Map_descriptor =
+ getDescriptor().getMessageTypes().get(0);
+ internal_static_Map_fieldAccessorTable = new
+ com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
+ internal_static_Map_descriptor,
+ new String[] { "Attachments", });
+ internal_static_Map_AttachmentsEntry_descriptor =
+ internal_static_Map_descriptor.getNestedTypes().get(0);
+ internal_static_Map_AttachmentsEntry_fieldAccessorTable = new
+ com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
+ internal_static_Map_AttachmentsEntry_descriptor,
+ new String[] { "Key", "Value", });
+ }
+
+ // @@protoc_insertion_point(outer_class_scope)
+}
diff --git
a/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufObjectOutputTest.java
b/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufObjectOutputTest.java
index e420aec..8f9b08d 100644
---
a/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufObjectOutputTest.java
+++
b/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufObjectOutputTest.java
@@ -17,7 +17,6 @@
package org.apache.dubbo.common.serialize.protobuf.support;
import org.apache.dubbo.common.serialize.protobuf.support.model.GooglePB;
-
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -76,6 +75,127 @@ public class GenericProtobufObjectOutputTest {
assertThat(genericProtobufObjectInput.readObject(GooglePB.PBRequestType.class),
is(request));
}
+ @Test
+ public void testWriteBoolean() throws IOException {
+ boolean random = new Random().nextBoolean();
+ this.genericProtobufObjectOutput.writeBool(random);
+ this.flushToInput();
+ assertThat(genericProtobufObjectInput.readBool(), is(random));
+ }
+
+ @Test
+ public void testWriteByte() throws IOException {
+ int random = new Random().nextInt();
+ this.genericProtobufObjectOutput.writeByte((byte) random);
+ this.flushToInput();
+ assertThat(genericProtobufObjectInput.readByte(), is((byte) random));
+ }
+
+ @Test
+ public void testWriteShort() throws IOException {
+ int random = new Random().nextInt();
+ this.genericProtobufObjectOutput.writeShort((short) random);
+ this.flushToInput();
+ assertThat(genericProtobufObjectInput.readShort(), is((short) random));
+ }
+
+ @Test
+ public void testWriteInt() throws IOException {
+ int random = new Random().nextInt();
+ this.genericProtobufObjectOutput.writeInt(random);
+ this.flushToInput();
+ assertThat(genericProtobufObjectInput.readInt(), is(random));
+ }
+
+ @Test
+ public void testWriteFloat() throws IOException {
+ float random = new Random().nextFloat();
+ this.genericProtobufObjectOutput.writeFloat(random);
+ this.flushToInput();
+ assertThat(genericProtobufObjectInput.readFloat(), is(random));
+ }
+
+
+ @Test
+ public void testWriteDouble() throws IOException {
+ double random = new Random().nextDouble();
+ this.genericProtobufObjectOutput.writeDouble(random);
+ this.flushToInput();
+ assertThat(genericProtobufObjectInput.readDouble(), is(random));
+ }
+
+
+ @Test
+ public void testWriteString() throws IOException {
+ byte[] bytes = new byte[new Random().nextInt(100)];
+ new Random().nextBytes(bytes);
+
+ this.genericProtobufObjectOutput.writeUTF(new String(bytes));
+ this.flushToInput();
+ assertThat(genericProtobufObjectInput.readUTF(), is(new
String(bytes)));
+ }
+
+ @Test
+ public void testWriteBytes() throws IOException {
+ byte[] bytes = new byte[new Random().nextInt(100)];
+ new Random().nextBytes(bytes);
+ this.genericProtobufObjectOutput.writeBytes(bytes);
+ this.flushToInput();
+ final byte[] bytes1 = genericProtobufObjectInput.readBytes();
+ assertThat(bytes1, is(bytes));
+ }
+
+ @Test
+ public void testWriteBytesSpecLength() throws IOException {
+ final int length = new Random().nextInt(100);
+ byte[] bytes = new byte[length];
+ new Random().nextBytes(bytes);
+ this.genericProtobufObjectOutput.writeBytes(bytes, 0, length);
+ this.flushToInput();
+ assertThat(genericProtobufObjectInput.readBytes(), is(bytes));
+ }
+
+ @Test
+ public void testWriteLong() throws IOException {
+ long random = new Random().nextLong();
+ this.genericProtobufObjectOutput.writeLong(random);
+ this.flushToInput();
+ assertThat(genericProtobufObjectInput.readLong(), is(random));
+ }
+
+
+ @Test
+ public void testWriteMap() throws IOException {
+ Map<String, String> map = new HashMap<>();
+ map.put("key", "hello");
+ map.put("value", "dubbo");
+ this.genericProtobufObjectOutput.writeObject(map);
+ this.flushToInput();
+ assertThat(genericProtobufObjectInput.readObject(Map.class), is(map));
+ }
+
+
+ @Test
+ void testWriteMultiType() throws IOException {
+ long random = new Random().nextLong();
+ this.genericProtobufObjectOutput.writeLong(random);
+ Map<String, String> map = new HashMap<>();
+ map.put("key", "hello");
+ map.put("value", "world");
+ this.genericProtobufObjectOutput.writeObject(map);
+ final int length = new Random().nextInt(100);
+ byte[] bytes = new byte[length];
+ new Random().nextBytes(bytes);
+ this.genericProtobufObjectOutput.writeBytes(bytes, 0, length);
+ int randomShort = new Random().nextInt();
+ this.genericProtobufObjectOutput.writeShort((short) randomShort);
+ this.flushToInput();
+ assertThat(genericProtobufObjectInput.readLong(), is(random));
+ assertThat(genericProtobufObjectInput.readObject(Map.class), is(map));
+ assertThat(genericProtobufObjectInput.readBytes(), is(bytes));
+ assertThat(genericProtobufObjectInput.readShort(), is((short)
randomShort));
+ }
+
private void flushToInput() {
this.genericProtobufObjectOutput.flushBuffer();
this.byteArrayInputStream = new
ByteArrayInputStream(byteArrayOutputStream.toByteArray());