https://github.com/protocolbuffers/protobuf/releases/download/v3.1.0/protoc-3.1.0-win32.zip
从源代码安装protoc可参考文章:https://lobin.iteye.com/blog/2435854
例子:
TestIntMessage.proto
syntax = "proto3"; option java_package = "com.grpc.service"; message TestIntRequest { //optional int32 value = 1; } message TestIntResponse { //optional int32 value = 1; }
编译
$ protoc --java_out=. TestIntMessage.proto
生成
package com.grpc.service; public final class TestIntMessage { private TestIntMessage() {} 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 TestIntRequestOrBuilder extends // @@protoc_insertion_point(interface_extends:TestIntRequest) com.google.protobuf.MessageOrBuilder { /** **optional ** *optional int32 value = 1;
*/ int getValue(); } /** * Protobuf type {@code TestIntRequest} */ public static final class TestIntRequest extends com.google.protobuf.GeneratedMessageV3 implements // @@protoc_insertion_point(message_implements:TestIntRequest) TestIntRequestOrBuilder { // Use TestIntRequest.newBuilder() to construct. private TestIntRequest(com.google.protobuf.GeneratedMessageV3.Builder> builder) { super(builder); } private TestIntRequest() { value_ = 0; } @java.lang.Override public final com.google.protobuf.UnknownFieldSet getUnknownFields() { return com.google.protobuf.UnknownFieldSet.getDefaultInstance(); } private TestIntRequest( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { this(); int mutable_bitField0_ = 0; try { boolean done = false; while (!done) { int tag = input.readTag(); switch (tag) { case 0: done = true; break; default: { if (!input.skipField(tag)) { done = true; } break; } case 8: { value_ = input.readInt32(); 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 { makeExtensionsImmutable(); } } public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { return com.grpc.service.TestIntMessage.internal_static_TestIntRequest_descriptor; } protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable internalGetFieldAccessorTable() { return com.grpc.service.TestIntMessage.internal_static_TestIntRequest_fieldAccessorTable .ensureFieldAccessorsInitialized( com.grpc.service.TestIntMessage.TestIntRequest.class, com.grpc.service.TestIntMessage.TestIntRequest.Builder.class); } public static final int VALUE_FIELD_NUMBER = 1; private int value_; /** **optional ** *optional int32 value = 1;
*/ public int getValue() { return value_; } private byte memoizedIsInitialized = -1; public final boolean isInitialized() { byte isInitialized = memoizedIsInitialized; if (isInitialized == 1) return true; if (isInitialized == 0) return false; memoizedIsInitialized = 1; return true; } public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException { if (value_ != 0) { output.writeInt32(1, value_); } } public int getSerializedSize() { int size = memoizedSize; if (size != -1) return size; size = 0; if (value_ != 0) { size += com.google.protobuf.CodedOutputStream .computeInt32Size(1, value_); } memoizedSize = size; return size; } private static final long serialVersionUID = 0L; @java.lang.Override public boolean equals(final java.lang.Object obj) { if (obj == this) { return true; } if (!(obj instanceof com.grpc.service.TestIntMessage.TestIntRequest)) { return super.equals(obj); } com.grpc.service.TestIntMessage.TestIntRequest other = (com.grpc.service.TestIntMessage.TestIntRequest) obj; boolean result = true; result = result && (getValue() == other.getValue()); return result; } @java.lang.Override public int hashCode() { if (memoizedHashCode != 0) { return memoizedHashCode; } int hash = 41; hash = (19 * hash) + getDescriptorForType().hashCode(); hash = (37 * hash) + VALUE_FIELD_NUMBER; hash = (53 * hash) + getValue(); hash = (29 * hash) + unknownFields.hashCode(); memoizedHashCode = hash; return hash; } public static com.grpc.service.TestIntMessage.TestIntRequest parseFrom( com.google.protobuf.ByteString data) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data); } public static com.grpc.service.TestIntMessage.TestIntRequest parseFrom( com.google.protobuf.ByteString data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data, extensionRegistry); } public static com.grpc.service.TestIntMessage.TestIntRequest parseFrom(byte[] data) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data); } public static com.grpc.service.TestIntMessage.TestIntRequest parseFrom( byte[] data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data, extensionRegistry); } public static com.grpc.service.TestIntMessage.TestIntRequest parseFrom(java.io.InputStream input) throws java.io.IOException { return com.google.protobuf.GeneratedMessageV3 .parseWithIOException(PARSER, input); } public static com.grpc.service.TestIntMessage.TestIntRequest 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 com.grpc.service.TestIntMessage.TestIntRequest parseDelimitedFrom(java.io.InputStream input) throws java.io.IOException { return com.google.protobuf.GeneratedMessageV3 .parseDelimitedWithIOException(PARSER, input); } public static com.grpc.service.TestIntMessage.TestIntRequest 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 com.grpc.service.TestIntMessage.TestIntRequest parseFrom( com.google.protobuf.CodedInputStream input) throws java.io.IOException { return com.google.protobuf.GeneratedMessageV3 .parseWithIOException(PARSER, input); } public static com.grpc.service.TestIntMessage.TestIntRequest parseFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { return com.google.protobuf.GeneratedMessageV3 .parseWithIOException(PARSER, input, extensionRegistry); } public Builder newBuilderForType() { return newBuilder(); } public static Builder newBuilder() { return DEFAULT_INSTANCE.toBuilder(); } public static Builder newBuilder(com.grpc.service.TestIntMessage.TestIntRequest prototype) { return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); } public Builder toBuilder() { return this == DEFAULT_INSTANCE ? new Builder() : new Builder().mergeFrom(this); } @java.lang.Override protected Builder newBuilderForType( com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { Builder builder = new Builder(parent); return builder; } /** * Protobuf type {@code TestIntRequest} */ public static final class Builder extends com.google.protobuf.GeneratedMessageV3.Builderimplements // @@protoc_insertion_point(builder_implements:TestIntRequest) com.grpc.service.TestIntMessage.TestIntRequestOrBuilder { public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { return com.grpc.service.TestIntMessage.internal_static_TestIntRequest_descriptor; } protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable internalGetFieldAccessorTable() { return com.grpc.service.TestIntMessage.internal_static_TestIntRequest_fieldAccessorTable .ensureFieldAccessorsInitialized( com.grpc.service.TestIntMessage.TestIntRequest.class, com.grpc.service.TestIntMessage.TestIntRequest.Builder.class); } // Construct using com.grpc.service.TestIntMessage.TestIntRequest.newBuilder() private Builder() { maybeForceBuilderInitialization(); } private Builder( com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { super(parent); maybeForceBuilderInitialization(); } private void maybeForceBuilderInitialization() { if (com.google.protobuf.GeneratedMessageV3 .alwaysUseFieldBuilders) { } } public Builder clear() { super.clear(); value_ = 0; return this; } public com.google.protobuf.Descriptors.Descriptor getDescriptorForType() { return com.grpc.service.TestIntMessage.internal_static_TestIntRequest_descriptor; } public com.grpc.service.TestIntMessage.TestIntRequest getDefaultInstanceForType() { return com.grpc.service.TestIntMessage.TestIntRequest.getDefaultInstance(); } public com.grpc.service.TestIntMessage.TestIntRequest build() { com.grpc.service.TestIntMessage.TestIntRequest result = buildPartial(); if (!result.isInitialized()) { throw newUninitializedMessageException(result); } return result; } public com.grpc.service.TestIntMessage.TestIntRequest buildPartial() { com.grpc.service.TestIntMessage.TestIntRequest result = new com.grpc.service.TestIntMessage.TestIntRequest(this); result.value_ = value_; onBuilt(); return result; } public Builder clone() { return (Builder) super.clone(); } public Builder setField( com.google.protobuf.Descriptors.FieldDescriptor field, Object value) { return (Builder) super.setField(field, value); } public Builder clearField( com.google.protobuf.Descriptors.FieldDescriptor field) { return (Builder) super.clearField(field); } public Builder clearOneof( com.google.protobuf.Descriptors.OneofDescriptor oneof) { return (Builder) super.clearOneof(oneof); } public Builder setRepeatedField( com.google.protobuf.Descriptors.FieldDescriptor field, int index, Object value) { return (Builder) super.setRepeatedField(field, index, value); } public Builder addRepeatedField( com.google.protobuf.Descriptors.FieldDescriptor field, Object value) { return (Builder) super.addRepeatedField(field, value); } public Builder mergeFrom(com.google.protobuf.Message other) { if (other instanceof com.grpc.service.TestIntMessage.TestIntRequest) { return mergeFrom((com.grpc.service.TestIntMessage.TestIntRequest)other); } else { super.mergeFrom(other); return this; } } public Builder mergeFrom(com.grpc.service.TestIntMessage.TestIntRequest other) { if (other == com.grpc.service.TestIntMessage.TestIntRequest.getDefaultInstance()) return this; if (other.getValue() != 0) { setValue(other.getValue()); } onChanged(); return this; } public final boolean isInitialized() { return true; } public Builder mergeFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { com.grpc.service.TestIntMessage.TestIntRequest parsedMessage = null; try { parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); } catch (com.google.protobuf.InvalidProtocolBufferException e) { parsedMessage = (com.grpc.service.TestIntMessage.TestIntRequest) e.getUnfinishedMessage(); throw e.unwrapIOException(); } finally { if (parsedMessage != null) { mergeFrom(parsedMessage); } } return this; } private int value_ ; /** * *optional ** *optional int32 value = 1;
*/ public int getValue() { return value_; } /** **optional ** *optional int32 value = 1;
*/ public Builder setValue(int value) { value_ = value; onChanged(); return this; } /** **optional ** *optional int32 value = 1;
*/ public Builder clearValue() { value_ = 0; onChanged(); return this; } public final Builder setUnknownFields( final com.google.protobuf.UnknownFieldSet unknownFields) { return this; } public final Builder mergeUnknownFields( final com.google.protobuf.UnknownFieldSet unknownFields) { return this; } // @@protoc_insertion_point(builder_scope:TestIntRequest) } // @@protoc_insertion_point(class_scope:TestIntRequest) private static final com.grpc.service.TestIntMessage.TestIntRequest DEFAULT_INSTANCE; static { DEFAULT_INSTANCE = new com.grpc.service.TestIntMessage.TestIntRequest(); } public static com.grpc.service.TestIntMessage.TestIntRequest getDefaultInstance() { return DEFAULT_INSTANCE; } private static final com.google.protobuf.ParserPARSER = new com.google.protobuf.AbstractParser () { public TestIntRequest parsePartialFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { return new TestIntRequest(input, extensionRegistry); } }; public static com.google.protobuf.Parser parser() { return PARSER; } @java.lang.Override public com.google.protobuf.Parser getParserForType() { return PARSER; } public com.grpc.service.TestIntMessage.TestIntRequest getDefaultInstanceForType() { return DEFAULT_INSTANCE; } } public interface TestIntResponseOrBuilder extends // @@protoc_insertion_point(interface_extends:TestIntResponse) com.google.protobuf.MessageOrBuilder { /** * *optional ** *optional int32 value = 1;
*/ int getValue(); } /** * Protobuf type {@code TestIntResponse} */ public static final class TestIntResponse extends com.google.protobuf.GeneratedMessageV3 implements // @@protoc_insertion_point(message_implements:TestIntResponse) TestIntResponseOrBuilder { // Use TestIntResponse.newBuilder() to construct. private TestIntResponse(com.google.protobuf.GeneratedMessageV3.Builder> builder) { super(builder); } private TestIntResponse() { value_ = 0; } @java.lang.Override public final com.google.protobuf.UnknownFieldSet getUnknownFields() { return com.google.protobuf.UnknownFieldSet.getDefaultInstance(); } private TestIntResponse( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { this(); int mutable_bitField0_ = 0; try { boolean done = false; while (!done) { int tag = input.readTag(); switch (tag) { case 0: done = true; break; default: { if (!input.skipField(tag)) { done = true; } break; } case 8: { value_ = input.readInt32(); 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 { makeExtensionsImmutable(); } } public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { return com.grpc.service.TestIntMessage.internal_static_TestIntResponse_descriptor; } protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable internalGetFieldAccessorTable() { return com.grpc.service.TestIntMessage.internal_static_TestIntResponse_fieldAccessorTable .ensureFieldAccessorsInitialized( com.grpc.service.TestIntMessage.TestIntResponse.class, com.grpc.service.TestIntMessage.TestIntResponse.Builder.class); } public static final int VALUE_FIELD_NUMBER = 1; private int value_; /** **optional ** *optional int32 value = 1;
*/ public int getValue() { return value_; } private byte memoizedIsInitialized = -1; public final boolean isInitialized() { byte isInitialized = memoizedIsInitialized; if (isInitialized == 1) return true; if (isInitialized == 0) return false; memoizedIsInitialized = 1; return true; } public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException { if (value_ != 0) { output.writeInt32(1, value_); } } public int getSerializedSize() { int size = memoizedSize; if (size != -1) return size; size = 0; if (value_ != 0) { size += com.google.protobuf.CodedOutputStream .computeInt32Size(1, value_); } memoizedSize = size; return size; } private static final long serialVersionUID = 0L; @java.lang.Override public boolean equals(final java.lang.Object obj) { if (obj == this) { return true; } if (!(obj instanceof com.grpc.service.TestIntMessage.TestIntResponse)) { return super.equals(obj); } com.grpc.service.TestIntMessage.TestIntResponse other = (com.grpc.service.TestIntMessage.TestIntResponse) obj; boolean result = true; result = result && (getValue() == other.getValue()); return result; } @java.lang.Override public int hashCode() { if (memoizedHashCode != 0) { return memoizedHashCode; } int hash = 41; hash = (19 * hash) + getDescriptorForType().hashCode(); hash = (37 * hash) + VALUE_FIELD_NUMBER; hash = (53 * hash) + getValue(); hash = (29 * hash) + unknownFields.hashCode(); memoizedHashCode = hash; return hash; } public static com.grpc.service.TestIntMessage.TestIntResponse parseFrom( com.google.protobuf.ByteString data) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data); } public static com.grpc.service.TestIntMessage.TestIntResponse parseFrom( com.google.protobuf.ByteString data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data, extensionRegistry); } public static com.grpc.service.TestIntMessage.TestIntResponse parseFrom(byte[] data) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data); } public static com.grpc.service.TestIntMessage.TestIntResponse parseFrom( byte[] data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data, extensionRegistry); } public static com.grpc.service.TestIntMessage.TestIntResponse parseFrom(java.io.InputStream input) throws java.io.IOException { return com.google.protobuf.GeneratedMessageV3 .parseWithIOException(PARSER, input); } public static com.grpc.service.TestIntMessage.TestIntResponse 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 com.grpc.service.TestIntMessage.TestIntResponse parseDelimitedFrom(java.io.InputStream input) throws java.io.IOException { return com.google.protobuf.GeneratedMessageV3 .parseDelimitedWithIOException(PARSER, input); } public static com.grpc.service.TestIntMessage.TestIntResponse 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 com.grpc.service.TestIntMessage.TestIntResponse parseFrom( com.google.protobuf.CodedInputStream input) throws java.io.IOException { return com.google.protobuf.GeneratedMessageV3 .parseWithIOException(PARSER, input); } public static com.grpc.service.TestIntMessage.TestIntResponse parseFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { return com.google.protobuf.GeneratedMessageV3 .parseWithIOException(PARSER, input, extensionRegistry); } public Builder newBuilderForType() { return newBuilder(); } public static Builder newBuilder() { return DEFAULT_INSTANCE.toBuilder(); } public static Builder newBuilder(com.grpc.service.TestIntMessage.TestIntResponse prototype) { return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); } public Builder toBuilder() { return this == DEFAULT_INSTANCE ? new Builder() : new Builder().mergeFrom(this); } @java.lang.Override protected Builder newBuilderForType( com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { Builder builder = new Builder(parent); return builder; } /** * Protobuf type {@code TestIntResponse} */ public static final class Builder extends com.google.protobuf.GeneratedMessageV3.Builderimplements // @@protoc_insertion_point(builder_implements:TestIntResponse) com.grpc.service.TestIntMessage.TestIntResponseOrBuilder { public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { return com.grpc.service.TestIntMessage.internal_static_TestIntResponse_descriptor; } protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable internalGetFieldAccessorTable() { return com.grpc.service.TestIntMessage.internal_static_TestIntResponse_fieldAccessorTable .ensureFieldAccessorsInitialized( com.grpc.service.TestIntMessage.TestIntResponse.class, com.grpc.service.TestIntMessage.TestIntResponse.Builder.class); } // Construct using com.grpc.service.TestIntMessage.TestIntResponse.newBuilder() private Builder() { maybeForceBuilderInitialization(); } private Builder( com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { super(parent); maybeForceBuilderInitialization(); } private void maybeForceBuilderInitialization() { if (com.google.protobuf.GeneratedMessageV3 .alwaysUseFieldBuilders) { } } public Builder clear() { super.clear(); value_ = 0; return this; } public com.google.protobuf.Descriptors.Descriptor getDescriptorForType() { return com.grpc.service.TestIntMessage.internal_static_TestIntResponse_descriptor; } public com.grpc.service.TestIntMessage.TestIntResponse getDefaultInstanceForType() { return com.grpc.service.TestIntMessage.TestIntResponse.getDefaultInstance(); } public com.grpc.service.TestIntMessage.TestIntResponse build() { com.grpc.service.TestIntMessage.TestIntResponse result = buildPartial(); if (!result.isInitialized()) { throw newUninitializedMessageException(result); } return result; } public com.grpc.service.TestIntMessage.TestIntResponse buildPartial() { com.grpc.service.TestIntMessage.TestIntResponse result = new com.grpc.service.TestIntMessage.TestIntResponse(this); result.value_ = value_; onBuilt(); return result; } public Builder clone() { return (Builder) super.clone(); } public Builder setField( com.google.protobuf.Descriptors.FieldDescriptor field, Object value) { return (Builder) super.setField(field, value); } public Builder clearField( com.google.protobuf.Descriptors.FieldDescriptor field) { return (Builder) super.clearField(field); } public Builder clearOneof( com.google.protobuf.Descriptors.OneofDescriptor oneof) { return (Builder) super.clearOneof(oneof); } public Builder setRepeatedField( com.google.protobuf.Descriptors.FieldDescriptor field, int index, Object value) { return (Builder) super.setRepeatedField(field, index, value); } public Builder addRepeatedField( com.google.protobuf.Descriptors.FieldDescriptor field, Object value) { return (Builder) super.addRepeatedField(field, value); } public Builder mergeFrom(com.google.protobuf.Message other) { if (other instanceof com.grpc.service.TestIntMessage.TestIntResponse) { return mergeFrom((com.grpc.service.TestIntMessage.TestIntResponse)other); } else { super.mergeFrom(other); return this; } } public Builder mergeFrom(com.grpc.service.TestIntMessage.TestIntResponse other) { if (other == com.grpc.service.TestIntMessage.TestIntResponse.getDefaultInstance()) return this; if (other.getValue() != 0) { setValue(other.getValue()); } onChanged(); return this; } public final boolean isInitialized() { return true; } public Builder mergeFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { com.grpc.service.TestIntMessage.TestIntResponse parsedMessage = null; try { parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); } catch (com.google.protobuf.InvalidProtocolBufferException e) { parsedMessage = (com.grpc.service.TestIntMessage.TestIntResponse) e.getUnfinishedMessage(); throw e.unwrapIOException(); } finally { if (parsedMessage != null) { mergeFrom(parsedMessage); } } return this; } private int value_ ; /** * *optional ** *optional int32 value = 1;
*/ public int getValue() { return value_; } /** **optional ** *optional int32 value = 1;
*/ public Builder setValue(int value) { value_ = value; onChanged(); return this; } /** **optional ** *optional int32 value = 1;
*/ public Builder clearValue() { value_ = 0; onChanged(); return this; } public final Builder setUnknownFields( final com.google.protobuf.UnknownFieldSet unknownFields) { return this; } public final Builder mergeUnknownFields( final com.google.protobuf.UnknownFieldSet unknownFields) { return this; } // @@protoc_insertion_point(builder_scope:TestIntResponse) } // @@protoc_insertion_point(class_scope:TestIntResponse) private static final com.grpc.service.TestIntMessage.TestIntResponse DEFAULT_INSTANCE; static { DEFAULT_INSTANCE = new com.grpc.service.TestIntMessage.TestIntResponse(); } public static com.grpc.service.TestIntMessage.TestIntResponse getDefaultInstance() { return DEFAULT_INSTANCE; } private static final com.google.protobuf.ParserPARSER = new com.google.protobuf.AbstractParser () { public TestIntResponse parsePartialFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { return new TestIntResponse(input, extensionRegistry); } }; public static com.google.protobuf.Parser parser() { return PARSER; } @java.lang.Override public com.google.protobuf.Parser getParserForType() { return PARSER; } public com.grpc.service.TestIntMessage.TestIntResponse getDefaultInstanceForType() { return DEFAULT_INSTANCE; } } private static final com.google.protobuf.Descriptors.Descriptor internal_static_TestIntRequest_descriptor; private static final com.google.protobuf.GeneratedMessageV3.FieldAccessorTable internal_static_TestIntRequest_fieldAccessorTable; private static final com.google.protobuf.Descriptors.Descriptor internal_static_TestIntResponse_descriptor; private static final com.google.protobuf.GeneratedMessageV3.FieldAccessorTable internal_static_TestIntResponse_fieldAccessorTable; public static com.google.protobuf.Descriptors.FileDescriptor getDescriptor() { return descriptor; } private static com.google.protobuf.Descriptors.FileDescriptor descriptor; static { java.lang.String[] descriptorData = { "\n\024TestIntMessage.proto\"\037\n\016TestIntRequest" + "\022\r\n\005value\030\001 \001(\005\" \n\017TestIntResponse\022\r\n\005va" + "lue\030\001 \001(\005B\022\n\020com.grpc.serviceb\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_TestIntRequest_descriptor = getDescriptor().getMessageTypes().get(0); internal_static_TestIntRequest_fieldAccessorTable = new com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( internal_static_TestIntRequest_descriptor, new java.lang.String[] { "Value", }); internal_static_TestIntResponse_descriptor = getDescriptor().getMessageTypes().get(1); internal_static_TestIntResponse_fieldAccessorTable = new com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( internal_static_TestIntResponse_descriptor, new java.lang.String[] { "Value", }); } // @@protoc_insertion_point(outer_class_scope) }
public static void main(String[] args) throws IOException { TestIntMessage.TestIntRequest.Builder requestBuilder1 = TestIntMessage.TestIntRequest.newBuilder(); requestBuilder1.setValue(1234); TestIntMessage.TestIntRequest request1 = requestBuilder1.build(); ByteArrayOutputStream os = new ByteArrayOutputStream(); request1.writeTo(os); InputStream is = new ByteArrayInputStream(os.toByteArray()); TestIntMessage.TestIntRequest request2 = TestIntMessage.TestIntRequest.parseFrom(is); System.out.println(request1); System.out.println(request2); System.out.println("request value1: " + request1.getValue() + ", request value2: " + request2.getValue()); TestIntMessage.TestIntResponse.Builder responseBuilder1 = TestIntMessage.TestIntResponse.newBuilder(); responseBuilder1.setValue(5678); TestIntMessage.TestIntResponse response1 = responseBuilder1.build(); os = new ByteArrayOutputStream(); response1.writeTo(os); is = new ByteArrayInputStream(os.toByteArray()); TestIntMessage.TestIntResponse response2 = TestIntMessage.TestIntResponse.parseFrom(is); System.out.println(response1); System.out.println(response2); System.out.println("response value1: " + response1.getValue() + ", response value2: " + response2.getValue()); }
value: 1234 value: 1234 request value1: 1234, request value2: 1234 value: 5678 value: 5678 response value1: 5678, response value2: 5678