Skip to content
Permalink
Browse files
Added better compatibility with the existing protobuf java api.. made…
… implementing it's unit tests easier.

git-svn-id: https://svn.apache.org/repos/asf/activemq/sandbox/activemq-protobuf@691373 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
chirino committed Sep 2, 2008
1 parent 79921c2 commit 25de73af70f2c65554fc0dd6237970e7551c6991
Showing 7 changed files with 1,472 additions and 101 deletions.
@@ -16,18 +16,38 @@
*/
package org.apache.activemq.protobuf;

import com.google.protobuf.ByteString;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.CodedOutputStream;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.InvalidProtocolBufferException;

import static org.apache.activemq.protobuf.WireInfo.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

abstract public class Message<T> {

protected int memoizedSerializedSize = -1;

static protected <T> void addAll(Iterable<T> values, Collection<? super T> list) {
if (values instanceof Collection) {
@SuppressWarnings("unsafe")
Collection<T> collection = (Collection<T>)values;
list.addAll(collection);
} else {
for (T value : values) {
list.add(value);
}
}
}

static protected void writeGroup(CodedOutputStream output, int tag, Message message) throws IOException {
output.writeTag(tag, WIRETYPE_START_GROUP);
message.writeTo(output);
@@ -46,25 +66,23 @@ static protected <T extends Message> T readGroup(CodedInputStream input, Extensi
return group;
}

static protected <T extends Message> T readMessage(CodedInputStream input, ExtensionRegistry extensionRegistry, T message) throws IOException {
static protected <T extends Message> T readMessage(CodedInputStream input, ExtensionRegistry extensionRegistry, T message) throws IOException {
int length = input.readRawVarint32();
int oldLimit = input.pushLimit(length);
message.mergeFrom(input, extensionRegistry);
input.checkLastTagWas(0);
input.popLimit(oldLimit);
return message;
}

static protected int computeGroupSize(int tag, Message message) {
return CodedOutputStream.computeTagSize(tag) * 2 + message.serializedSize();
}

static protected int computeMessageSize(int tag, Message message) {
int t = message.serializedSize();
return CodedOutputStream.computeTagSize(tag) +
CodedOutputStream.computeRawVarint32Size(t)+t;
return CodedOutputStream.computeTagSize(tag) + CodedOutputStream.computeRawVarint32Size(t) + t;
}


abstract public T mergeFrom(T other);

@@ -82,5 +100,98 @@ public T mergeFrom(CodedInputStream input) throws IOException {

abstract public void clear();

abstract public boolean isInitialized();
abstract public T assertInitialized() throws com.google.protobuf.UninitializedMessageException;

public byte[] toByteArray() {
try {
byte[] result = new byte[serializedSize()];
CodedOutputStream output = CodedOutputStream.newInstance(result);
writeTo(output);
output.checkNoSpaceLeft();
return result;
} catch (IOException e) {
throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e);
}
}

protected List<String> prefix(List<String> missingFields, String prefix) {
ArrayList<String> rc = new ArrayList<String>(missingFields.size());
for (String v : missingFields) {
rc.add(prefix+v);
}
return rc;
}

public void writeTo(OutputStream output) throws IOException {
CodedOutputStream codedOutput = CodedOutputStream.newInstance(output);
writeTo(codedOutput);
codedOutput.flush();
}

public T mergeFrom(ByteString data) throws InvalidProtocolBufferException {
try {
CodedInputStream input = data.newCodedInput();
mergeFrom(input);
input.checkLastTagWas(0);
return (T)this;
} catch (InvalidProtocolBufferException e) {
throw e;
} catch (IOException e) {
throw new RuntimeException("Reading from a ByteString threw an IOException (should " + "never happen).", e);
}
}

public T mergeFrom(ByteString data, ExtensionRegistry extensionRegistry) throws InvalidProtocolBufferException {
try {
CodedInputStream input = data.newCodedInput();
mergeFrom(input, extensionRegistry);
input.checkLastTagWas(0);
return (T)this;
} catch (InvalidProtocolBufferException e) {
throw e;
} catch (IOException e) {
throw new RuntimeException("Reading from a ByteString threw an IOException (should " + "never happen).", e);
}
}

public T mergeFrom(byte[] data) throws InvalidProtocolBufferException {
try {
CodedInputStream input = CodedInputStream.newInstance(data);
mergeFrom(input);
input.checkLastTagWas(0);
return (T)this;
} catch (InvalidProtocolBufferException e) {
throw e;
} catch (IOException e) {
throw new RuntimeException("Reading from a byte array threw an IOException (should " + "never happen).", e);
}
}

public T mergeFrom(byte[] data, ExtensionRegistry extensionRegistry) throws InvalidProtocolBufferException {
try {
CodedInputStream input = CodedInputStream.newInstance(data);
mergeFrom(input, extensionRegistry);
input.checkLastTagWas(0);
return (T)this;
} catch (InvalidProtocolBufferException e) {
throw e;
} catch (IOException e) {
throw new RuntimeException("Reading from a byte array threw an IOException (should " + "never happen).", e);
}
}

public T mergeFrom(InputStream input) throws IOException {
CodedInputStream codedInput = CodedInputStream.newInstance(input);
mergeFrom(codedInput);
codedInput.checkLastTagWas(0);
return (T)this;
}

public T mergeFrom(InputStream input, ExtensionRegistry extensionRegistry) throws IOException {
CodedInputStream codedInput = CodedInputStream.newInstance(input);
mergeFrom(codedInput, extensionRegistry);
codedInput.checkLastTagWas(0);
return (T)this;
}

}
@@ -0,0 +1,82 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.
// http://code.google.com/p/protobuf/
//
// Licensed 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.activemq.protobuf;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Descriptors.FieldDescriptor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* Thrown when attempting to build a protocol message that is missing required
* fields. This is a {@code RuntimeException} because it normally represents
* a programming error: it happens when some code which constructs a message
* fails to set all the fields. {@code parseFrom()} methods <b>do not</b>
* throw this; they throw an {@link InvalidProtocolBufferException} if
* required fields are missing, because it is not a programming error to
* receive an incomplete message. In other words,
* {@code UninitializedMessageException} should never be thrown by correct
* code, but {@code InvalidProtocolBufferException} might be.
*
* @author kenton@google.com Kenton Varda
*/
public class UninitializedMessageException extends RuntimeException {

public UninitializedMessageException(List<String> missingFields) {
super(buildDescription(missingFields));
this.missingFields = missingFields;
}

private final List<String> missingFields;

/**
* Get a list of human-readable names of required fields missing from this
* message. Each name is a full path to a field, e.g. "foo.bar[5].baz".
*/
public List<String> getMissingFields() {
return Collections.unmodifiableList(missingFields);
}

/**
* Converts this exception to an {@link InvalidProtocolBufferException}.
* When a parsed message is missing required fields, this should be thrown
* instead of {@code UninitializedMessageException}.
*/
public com.google.protobuf.InvalidProtocolBufferException asInvalidProtocolBufferException() {
return new InvalidProtocolBufferException(getMessage());
}

/** Construct the description string for this exception. */
private static String buildDescription(List<String> missingFields) {
StringBuilder description =
new StringBuilder("Message missing required fields: ");
boolean first = true;
for (String field : missingFields) {
if (first) {
first = false;
} else {
description.append(", ");
}
description.append(field);
}
return description.toString();
}

}
@@ -51,6 +51,9 @@ public class FieldDescriptor {
public static final Set<String> NUMBER_TYPES = new HashSet<String>();
public static final Set<String> SCALAR_TYPES = new HashSet<String>();

public static final Set<String> SIGNED_TYPES = new HashSet<String>();
public static final Set<String> UNSIGNED_TYPES = new HashSet<String>();

static {
INT32_TYPES.add(INT32_TYPE);
INT32_TYPES.add(UINT32_TYPE);

0 comments on commit 25de73a

Please sign in to comment.