Cleaned up the Message interface so that it's obvious and easy to use Framed and Unframed encoding of the messages.
 


git-svn-id: https://svn.apache.org/repos/asf/activemq/sandbox/activemq-protobuf@702614 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/activemq-protobuf-test/src/test/java/com/google/protobuf/GeneratedMessageTest.java b/activemq-protobuf-test/src/test/java/com/google/protobuf/GeneratedMessageTest.java
index f190255..296e05a 100644
--- a/activemq-protobuf-test/src/test/java/com/google/protobuf/GeneratedMessageTest.java
+++ b/activemq-protobuf-test/src/test/java/com/google/protobuf/GeneratedMessageTest.java
@@ -111,6 +111,6 @@
         .setNestedEnum(MessageWithNoOuter.NestedEnum.BAZ)
         .setForeignEnum(EnumWithNoOuter.BAR)
         ;
-    assertEquals(message.toString(), MessageWithNoOuter.parseFrom(message.toByteArray()).toString());
+    assertEquals(message.toString(), MessageWithNoOuter.parseUnframed(message.toUnframedByteArray()).toString());
   }
 }
diff --git a/activemq-protobuf-test/src/test/java/com/google/protobuf/WireFormatTest.java b/activemq-protobuf-test/src/test/java/com/google/protobuf/WireFormatTest.java
index 5c3360e..d96dc9e 100644
--- a/activemq-protobuf-test/src/test/java/com/google/protobuf/WireFormatTest.java
+++ b/activemq-protobuf-test/src/test/java/com/google/protobuf/WireFormatTest.java
@@ -28,10 +28,10 @@
   public void testSerialization() throws Exception {
     TestAllTypes message = TestUtil.getAllSet();
 
-    byte[] rawBytes = message.toByteArray();
-    assertEquals(rawBytes.length, message.serializedSize());
+    byte[] rawBytes = message.toUnframedByteArray();
+    assertEquals(rawBytes.length, message.serializedSizeUnframed());
 
-    TestAllTypes message2 = TestAllTypes.parseFrom(rawBytes);
+    TestAllTypes message2 = TestAllTypes.parseUnframed(rawBytes);
 
     TestUtil.assertAllFieldsSet(message2);
   }
diff --git a/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java b/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java
index a885bb6..b6b7793 100644
--- a/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java
+++ b/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java
@@ -16,26 +16,169 @@
  */
 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 static org.apache.activemq.protobuf.WireInfo.WIRETYPE_END_GROUP;
+import static org.apache.activemq.protobuf.WireInfo.WIRETYPE_LENGTH_DELIMITED;
+import static org.apache.activemq.protobuf.WireInfo.WIRETYPE_START_GROUP;
+import static org.apache.activemq.protobuf.WireInfo.makeTag;
 
 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;
 
+import com.google.protobuf.ByteString;
+import com.google.protobuf.CodedInputStream;
+import com.google.protobuf.CodedOutputStream;
+import com.google.protobuf.InvalidProtocolBufferException;
+
 abstract public class BaseMessage<T> implements Message<T> {
 
     protected int memoizedSerializedSize = -1;
+    
+    abstract public T clone() throws CloneNotSupportedException;
 
+    ///////////////////////////////////////////////////////////////////
+    // Write related helpers.
+    ///////////////////////////////////////////////////////////////////
+
+    public void writeFramed(CodedOutputStream output) throws IOException {
+        output.writeRawVarint32(serializedSizeUnframed());
+        writeUnframed(output);
+    }
+
+    public byte[] toUnframedByteArray() {
+        try {
+            byte[] result = new byte[serializedSizeUnframed()];
+            CodedOutputStream output = CodedOutputStream.newInstance(result);
+            writeUnframed(output);
+            output.checkNoSpaceLeft();
+            return result;
+        } catch (IOException e) {
+            throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e);
+        }
+    }
+    
+    
+	public byte[] toFramedByteArray() {
+        try {
+            byte[] result = new byte[serializedSizeFramed()];
+            CodedOutputStream output = CodedOutputStream.newInstance(result);
+            writeFramed(output);
+            output.checkNoSpaceLeft();
+            return result;
+        } catch (IOException e) {
+            throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e);
+        }
+	}
+    
+	public void writeFramed(OutputStream output) throws IOException {
+        CodedOutputStream codedOutput = CodedOutputStream.newInstance(output);
+        writeFramed(codedOutput);
+        codedOutput.flush();
+	}
+
+    public void writeUnframed(OutputStream output) throws IOException {
+        CodedOutputStream codedOutput = CodedOutputStream.newInstance(output);
+        writeUnframed(codedOutput);
+        codedOutput.flush();
+    }
+    
+    public int serializedSizeFramed() {
+        int t = serializedSizeUnframed();
+        return CodedOutputStream.computeRawVarint32Size(t) + t;
+
+    }
+
+    ///////////////////////////////////////////////////////////////////
+    // Read related helpers.
+    ///////////////////////////////////////////////////////////////////
+
+    public T mergeFramed(CodedInputStream input) throws IOException {
+        int length = input.readRawVarint32();
+        int oldLimit = input.pushLimit(length);
+        T rc=  mergeUnframed(input);
+        input.checkLastTagWas(0);
+        input.popLimit(oldLimit);
+        return rc;
+    }
+
+    public T mergeUnframed(ByteString data) throws InvalidProtocolBufferException {
+        try {
+            CodedInputStream input = data.newCodedInput();
+            mergeUnframed(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 mergeFramed(ByteString data) throws InvalidProtocolBufferException {
+        try {
+            CodedInputStream input = data.newCodedInput();
+            mergeFramed(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 mergeUnframed(byte[] data) throws InvalidProtocolBufferException {
+        try {
+            CodedInputStream input = CodedInputStream.newInstance(data);
+            mergeUnframed(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 mergeFramed(byte[] data) throws InvalidProtocolBufferException {
+        try {
+            CodedInputStream input = CodedInputStream.newInstance(data);
+            mergeFramed(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 mergeUnframed(InputStream input) throws IOException {
+        CodedInputStream codedInput = CodedInputStream.newInstance(input);
+        mergeUnframed(codedInput);
+        return (T)this;
+    }
+    
+    public T mergeFramed(InputStream input) throws IOException {
+		int length = readRawVarint32(input);
+		byte []data = new byte[length];
+		int pos = 0;
+		while( pos < length ) {
+			int r = input.read(data, pos, length-pos);
+			if( r < 0 ) {
+				throw new InvalidProtocolBufferException("Input stream ended before a full message frame could be read.");	
+			}
+			pos+=r;
+		}
+		return mergeUnframed(data);
+    }
+
+    ///////////////////////////////////////////////////////////////////
+    // Internal implementation methods.
+    ///////////////////////////////////////////////////////////////////
     static protected <T> void addAll(Iterable<T> values, Collection<? super T> list) {
         if (values instanceof Collection) {
             @SuppressWarnings("unsafe")
@@ -48,60 +191,32 @@
         }
     }
     
-    abstract public T clone() throws CloneNotSupportedException;
-
     static protected void writeGroup(CodedOutputStream output, int tag, BaseMessage message) throws IOException {
         output.writeTag(tag, WIRETYPE_START_GROUP);
-        message.writePartialTo(output);
+        message.writeUnframed(output);
         output.writeTag(tag, WIRETYPE_END_GROUP);
     }
 
-    static protected void writeMessage(CodedOutputStream output, int tag, BaseMessage message) throws IOException {
-        output.writeTag(tag, WIRETYPE_LENGTH_DELIMITED);
-        output.writeRawVarint32(message.serializedSize());
-        message.writePartialTo(output);
-    }
-
-    static protected <T extends BaseMessage> T readGroup(CodedInputStream input, ExtensionRegistry extensionRegistry, int tag, T group) throws IOException {
-        group.mergeFrom(input, extensionRegistry);
+    static protected <T extends BaseMessage> T readGroup(CodedInputStream input, int tag, T group) throws IOException {
+        group.mergeUnframed(input);
         input.checkLastTagWas(makeTag(tag, WIRETYPE_END_GROUP));
         return group;
     }
 
-    static protected <T extends BaseMessage> 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, BaseMessage message) {
-        return CodedOutputStream.computeTagSize(tag) * 2 + message.serializedSize();
+        return CodedOutputStream.computeTagSize(tag) * 2 + message.serializedSizeUnframed();
     }
 
+
+    static protected void writeMessage(CodedOutputStream output, int tag, BaseMessage message) throws IOException {
+        output.writeTag(tag, WIRETYPE_LENGTH_DELIMITED);
+        message.writeFramed(output);
+    }
+    
     static protected int computeMessageSize(int tag, BaseMessage message) {
-        int t = message.serializedSize();
-        return CodedOutputStream.computeTagSize(tag) + CodedOutputStream.computeRawVarint32Size(t) + t;
+        return CodedOutputStream.computeTagSize(tag) + message.serializedSizeFramed();
     }
-
-    public T mergeFrom(CodedInputStream input) throws IOException {
-        return mergeFrom(input, ExtensionRegistry.getEmptyRegistry());
-    }
-
-    public byte[] toByteArray() {
-        try {
-            byte[] result = new byte[serializedSize()];
-            CodedOutputStream output = CodedOutputStream.newInstance(result);
-            writePartialTo(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) {
@@ -110,79 +225,53 @@
         return rc;
     }
 
-    public void writeTo(OutputStream output) throws IOException {
-        CodedOutputStream codedOutput = CodedOutputStream.newInstance(output);
-        writeTo(codedOutput);
-        codedOutput.flush();
-    }
-    
-    public void writeTo(CodedOutputStream output) throws java.io.IOException {
-        writePartialTo(output);
-        output.writeTag(0, WIRETYPE_END_GROUP);
-    }
 
-    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);
+    /**
+     * Read a raw Varint from the stream.  If larger than 32 bits, discard the
+     * upper bits.
+     */
+    static protected int readRawVarint32(InputStream is) throws IOException {
+      byte tmp = readRawByte(is);
+      if (tmp >= 0) {
+        return tmp;
+      }
+      int result = tmp & 0x7f;
+      if ((tmp = readRawByte(is)) >= 0) {
+        result |= tmp << 7;
+      } else {
+        result |= (tmp & 0x7f) << 7;
+        if ((tmp = readRawByte(is)) >= 0) {
+          result |= tmp << 14;
+        } else {
+          result |= (tmp & 0x7f) << 14;
+          if ((tmp = readRawByte(is)) >= 0) {
+            result |= tmp << 21;
+          } else {
+            result |= (tmp & 0x7f) << 21;
+            result |= (tmp = readRawByte(is)) << 28;
+            if (tmp < 0) {
+              // Discard upper 32 bits.
+              for (int i = 0; i < 5; i++) {
+                if (readRawByte(is) >= 0) return result;
+              }
+              throw new InvalidProtocolBufferException(
+              "CodedInputStream encountered a malformed varint.");
+            }
+          }
         }
+      }
+      return result;
     }
 
-    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);
-        }
+    static protected byte readRawByte(InputStream is) throws IOException {
+    	int rc = is.read();
+    	if( rc == -1 ) {
+	        throw new InvalidProtocolBufferException(
+	        	      "While parsing a protocol message, the input ended unexpectedly " +
+	        	      "in the middle of a field.  This could mean either than the " +
+	        	      "input has been truncated or that an embedded message " +
+	        	      "misreported its own length.");
+    	}
+    	return (byte) rc;
     }
-
-    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);
-        return (T)this;
-    }
-
-    public T mergeFrom(InputStream input, ExtensionRegistry extensionRegistry) throws IOException {
-        CodedInputStream codedInput = CodedInputStream.newInstance(input);
-        mergeFrom(codedInput, extensionRegistry);
-        return (T)this;
-    }
-
 }
diff --git a/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java b/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java
index 675c703..36757d6 100644
--- a/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java
+++ b/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java
@@ -16,56 +16,59 @@
  */
 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;
+
+import com.google.protobuf.ByteString;
+import com.google.protobuf.CodedInputStream;
+import com.google.protobuf.CodedOutputStream;
+import com.google.protobuf.InvalidProtocolBufferException;
 
 public interface Message<T> {
 
-    public T mergeFrom(T other);
-
-    public T mergeFrom(CodedInputStream input) throws IOException;
-
-    public T mergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry) throws IOException;
-
-    public void writeTo(CodedOutputStream output) throws java.io.IOException;
-
     public T clone() throws CloneNotSupportedException;
 
-    public int serializedSize();
+    public int serializedSizeUnframed();
+    
+    public int serializedSizeFramed();
 
     public void clear();
 
     public T assertInitialized() throws com.google.protobuf.UninitializedMessageException;
 
-    public byte[] toByteArray();
+    public T mergeFrom(T other);
 
-    public void writePartialTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException;
+
+    public T mergeUnframed(byte[] data) throws InvalidProtocolBufferException;
     
-    public void writeTo(OutputStream output) throws IOException;
+    public T mergeFramed(byte[] data) throws InvalidProtocolBufferException;
 
-    public T mergeFrom(ByteString data) throws InvalidProtocolBufferException;
+    public T mergeUnframed(CodedInputStream input) throws IOException;
+    
+    public T mergeFramed(CodedInputStream input) throws IOException;
+    
+    public T mergeUnframed(ByteString data) throws InvalidProtocolBufferException;
 
-    public T mergeFrom(ByteString data, ExtensionRegistry extensionRegistry) throws InvalidProtocolBufferException;
+    public T mergeFramed(ByteString data) throws InvalidProtocolBufferException;
+    
+    public T mergeUnframed(InputStream input) throws IOException;
+    
+    public T mergeFramed(InputStream input) throws IOException;
 
-    public T mergeFrom(byte[] data) throws InvalidProtocolBufferException;
+    
 
-    public T mergeFrom(byte[] data, ExtensionRegistry extensionRegistry) throws InvalidProtocolBufferException;
+    public byte[] toUnframedByteArray();
+   
+    public byte[] toFramedByteArray();
+    
+    public void writeUnframed(CodedOutputStream output) throws java.io.IOException;
+    
+    public void writeFramed(CodedOutputStream output) throws java.io.IOException;
+    
+    public void writeUnframed(OutputStream output) throws IOException;
+    
+    public void writeFramed(OutputStream output) throws java.io.IOException;
 
-    public T mergeFrom(InputStream input) throws IOException;
-
-    public T mergeFrom(InputStream input, ExtensionRegistry extensionRegistry) throws IOException;
 
 }
diff --git a/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/JavaGenerator.java b/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/JavaGenerator.java
index 959c997..ed29e7c 100644
--- a/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/JavaGenerator.java
+++ b/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/JavaGenerator.java
@@ -23,8 +23,6 @@
 import static org.apache.activemq.protobuf.WireInfo.WIRETYPE_VARINT;
 import static org.apache.activemq.protobuf.WireInfo.makeTag;
 
-import com.google.protobuf.ByteString;
-
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -53,6 +51,7 @@
     private String optimizeFor;
     private ArrayList<String> errors = new ArrayList<String>();
     private boolean multipleFiles;
+	private boolean defferedUnmarshall;
 
     public static void main(String[] args) {
         
@@ -124,6 +123,7 @@
         outerClassName = javaClassName(proto);
         optimizeFor = getOption(proto.getOptions(), "optimize_for", "SPEED");
         multipleFiles = isMultipleFilesEnabled(proto);
+        defferedUnmarshall = Boolean.getBoolean(getOption(proto.getOptions(), "deferred_unmarshall", "false"));
         
         if( multipleFiles ) {
             generateProtoFile();
@@ -288,6 +288,9 @@
             }
         }
 
+//        if( defferedUnmarshall ) {
+//        }
+
         // Generate the field accessors..
         for (FieldDescriptor field : m.getFields().values()) {
             generateFieldAccessor(className, field);
@@ -317,7 +320,7 @@
         generateMethodVisitor(m);
                 
         generateMethodType(m, className);
-        
+                
         unindent();
         p("}");
         p();
@@ -408,54 +411,68 @@
     }
     
     private void generateMethodParseFrom(MessageDescriptor m, String className) {
-        p("public static "+className+" parseFrom(com.google.protobuf.ByteString data) throws com.google.protobuf.InvalidProtocolBufferException {");
+        p("public static "+className+" parseUnframed(com.google.protobuf.CodedInputStream data) throws com.google.protobuf.InvalidProtocolBufferException, java.io.IOException {");
         indent();
-        p("return new "+className+"().mergeFrom(data).checktInitialized();");
+        p("return new "+className+"().mergeUnframed(data).checktInitialized();");
         unindent();
         p("}");
         p();
 
-        p("public static "+className+" parseFrom(com.google.protobuf.ByteString data, com.google.protobuf.ExtensionRegistry extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException {");
+        p("public static "+className+" parseUnframed(com.google.protobuf.ByteString data) throws com.google.protobuf.InvalidProtocolBufferException {");
         indent();
-        p("return new "+className+"().mergeFrom(data, extensionRegistry).checktInitialized();");
+        p("return new "+className+"().mergeUnframed(data).checktInitialized();");
         unindent();
         p("}");
         p();
 
-        p("public static "+className+" parseFrom(byte[] data) throws com.google.protobuf.InvalidProtocolBufferException {");
+        p("public static "+className+" parseUnframed(byte[] data) throws com.google.protobuf.InvalidProtocolBufferException {");
         indent();
-        p("return new "+className+"().mergeFrom(data).checktInitialized();");
-        unindent();
-        p("}");
-        p();
-
-        p("public static "+className+" parseFrom(byte[] data, com.google.protobuf.ExtensionRegistry extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException {");
-        indent();
-        p("return new "+className+"().mergeFrom(data,extensionRegistry).checktInitialized();");
+        p("return new "+className+"().mergeUnframed(data).checktInitialized();");
         unindent();
         p("}");
         p();
         
-        p("public static "+className+" parseFrom(java.io.InputStream data) throws com.google.protobuf.InvalidProtocolBufferException, java.io.IOException {");
+        p("public static "+className+" parseUnframed(java.io.InputStream data) throws com.google.protobuf.InvalidProtocolBufferException, java.io.IOException {");
         indent();
-        p("return new "+className+"().mergeFrom(data).checktInitialized();");
+        p("return new "+className+"().mergeUnframed(data).checktInitialized();");
+        unindent();
+        p("}");
+        p();
+        
+        p("public static "+className+" parseFramed(com.google.protobuf.CodedInputStream data) throws com.google.protobuf.InvalidProtocolBufferException, java.io.IOException {");
+        indent();
+        p("return new "+className+"().mergeFramed(data).checktInitialized();");
+        unindent();
+        p("}");
+        p();
+        
+        p("public static "+className+" parseFramed(com.google.protobuf.ByteString data) throws com.google.protobuf.InvalidProtocolBufferException {");
+        indent();
+        p("return new "+className+"().mergeFramed(data).checktInitialized();");
         unindent();
         p("}");
         p();
 
-        p("public static "+className+" parseFrom(java.io.InputStream data, com.google.protobuf.ExtensionRegistry extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException, java.io.IOException {");
+        p("public static "+className+" parseFramed(byte[] data) throws com.google.protobuf.InvalidProtocolBufferException {");
         indent();
-        p("return new "+className+"().mergeFrom(data,extensionRegistry).checktInitialized();");
+        p("return new "+className+"().mergeFramed(data).checktInitialized();");
         unindent();
         p("}");
-        p();        
+        p();
+        
+        p("public static "+className+" parseFramed(java.io.InputStream data) throws com.google.protobuf.InvalidProtocolBufferException, java.io.IOException {");
+        indent();
+        p("return new "+className+"().mergeFramed(data).checktInitialized();");
+        unindent();
+        p("}");
+        p();
     }
 
     /**
      * @param m
      */
     private void generateMethodSerializedSize(MessageDescriptor m) {
-        p("public int serializedSize() {");
+        p("public int serializedSizeUnframed() {");
         indent();
         p("if (memoizedSerializedSize != -1)");
         p("   return memoizedSerializedSize;");
@@ -534,7 +551,7 @@
      * @param m
      */
     private void generateMethodWriteTo(MessageDescriptor m) {
-        p("public void writePartialTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException {");
+        p("public void writeUnframed(com.google.protobuf.CodedOutputStream output) throws java.io.IOException {");
         indent();
         for (FieldDescriptor field : m.getFields().values()) {
             String uname = uCamel(field.getName());
@@ -608,32 +625,20 @@
      * @param className
      */
     private void generateMethodMergeFromStream(MessageDescriptor m, String className) {
-        p("public "+className+" mergeFrom(com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistry extensionRegistry) throws java.io.IOException {");
+        p("public "+className+" mergeUnframed(com.google.protobuf.CodedInputStream input) throws java.io.IOException {");
         indent(); {
-          //TODO: handle unknown fields
-          // UnknownFieldSet.Builder unknownFields = com.google.protobuf.UnknownFieldSet.newBuilder(this.unknownFields);
-            
           p("while (true) {");
           indent(); {
               p("int tag = input.readTag();");
-              // Is it an end group tag?
               p("if ((tag & 0x07) == 4) {");
               p("   return this;");
               p("}");
               
               p("switch (tag) {");
-              // The end of stream..
               p("case 0:");
-//              p("   this.setUnknownFields(unknownFields.build());");
               p("   return this;");
               p("default: {");
-              
-              //TODO: handle unknown field types.
-//              p("   if (!parseUnknownField(input, unknownFields, extensionRegistry, tag)) {");
-//              p("       this.setUnknownFields(unknownFields.build());");
-//              p("       return this;");
-//              p("   }");
-              
+
               p("   break;");
               p("}");
               
@@ -645,9 +650,6 @@
                   if( repeated ) {
                       setter = "get"+uname+"List().add";
                   }
-                  
-                  
-                  
                   if( field.getType()==FieldDescriptor.STRING_TYPE ) {
                       p("case "+makeTag(field.getTag(), WIRETYPE_LENGTH_DELIMITED)+":");
                       indent();
@@ -731,15 +733,15 @@
                       indent();
                       String type = javaType(field);
                       if( repeated ) {
-                          p(setter+"(readGroup(input, extensionRegistry, "+field.getTag()+", new "+type+"()));");
+                          p(setter+"(readGroup(input, "+field.getTag()+", new "+type+"()));");
                       } else {
                           p("if (has"+uname+"()) {");
                           indent();
-                          p("readGroup(input, extensionRegistry, "+field.getTag()+", get"+uname+"());");
+                          p("readGroup(input, "+field.getTag()+", get"+uname+"());");
                           unindent();
                           p("} else {");
                           indent();
-                          p(setter+"(readGroup(input, extensionRegistry, "+field.getTag()+",new "+type+"()));");
+                          p(setter+"(readGroup(input, "+field.getTag()+",new "+type+"()));");
                           unindent();
                           p("}");
                       }
@@ -749,15 +751,15 @@
                       indent();
                       String type = javaType(field);
                       if( repeated ) {
-                          p(setter+"(readMessage(input, extensionRegistry, new "+type+"()));");
+                          p(setter+"(new "+type+"().mergeFramed(input));");
                       } else {
                           p("if (has"+uname+"()) {");
                           indent();
-                          p("readMessage(input, extensionRegistry,get"+uname+"());");
+                          p("get"+uname+"().mergeFramed(input);");
                           unindent();
                           p("} else {");
                           indent();
-                          p(setter+"(readMessage(input, extensionRegistry, new "+type+"()));");
+                          p(setter+"(new "+type+"().mergeFramed(input));");
                           unindent();
                           p("}");
                       }