You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ch...@apache.org on 2008/11/13 22:39:10 UTC

svn commit: r713832 - in /activemq/sandbox/activemq-protobuf: activemq-protobuf-test/src/test/java/com/google/protobuf/ activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/

Author: chirino
Date: Thu Nov 13 13:39:10 2008
New Revision: 713832

URL: http://svn.apache.org/viewvc?rev=713832&view=rev
Log:
Renaming ByteSequence to Buffer to avoid name conflicts with similar objects in ActiveMQ

Added:
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java   (contents, props changed)
      - copied, changed from r713629, activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteSequence.java
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferInputStream.java   (contents, props changed)
      - copied, changed from r713629, activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteArrayInputStream.java
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferOutputStream.java   (contents, props changed)
      - copied, changed from r713629, activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteArrayOutputStream.java
Removed:
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteArrayInputStream.java
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteArrayOutputStream.java
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteSequence.java
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteString.java
Modified:
    activemq/sandbox/activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/TestUtil.java
    activemq/sandbox/activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/WireFormatTest.java
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedInputStream.java
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedOutputStream.java
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/DeferredDecodeMessage.java
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/JavaGenerator.java
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/TextFormat.java

Modified: activemq/sandbox/activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/TestUtil.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/TestUtil.java?rev=713832&r1=713831&r2=713832&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/TestUtil.java (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/TestUtil.java Thu Nov 13 13:39:10 2008
@@ -28,7 +28,7 @@
 
 import junit.framework.Assert;
 
-import org.apache.activemq.protobuf.ByteString;
+import org.apache.activemq.protobuf.Buffer;
 
 import protobuf_unittest.UnittestProto.ForeignEnum;
 import protobuf_unittest.UnittestProto.ForeignMessage;
@@ -49,10 +49,10 @@
 class TestUtil {
   private TestUtil() {}
 
-  /** Helper to convert a String to ByteString. */
-  private static ByteString toBytes(String str) {
+  /** Helper to convert a String to ByteSequence. */
+  private static Buffer toBytes(String str) {
     try {
-      return ByteString.copyFrom(str.getBytes("UTF-8"));
+      return new Buffer(str.getBytes("UTF-8"));
     } catch(java.io.UnsupportedEncodingException e) {
       throw new RuntimeException("UTF-8 not supported.", e);
     }
@@ -710,7 +710,7 @@
   private static void assertEqualsExactType(String a, String b) {
     Assert.assertEquals(a, b);
   }
-  private static void assertEqualsExactType(ByteString a, ByteString b) {
+  private static void assertEqualsExactType(Buffer a, Buffer b) {
     Assert.assertEquals(a, b);
   }
   private static void assertEqualsExactType(TestAllTypes.NestedEnum a,
@@ -758,13 +758,13 @@
    * @param filePath The path relative to
    * {@link com.google.testing.util.TestUtil#getDefaultSrcDir}.
    */
-  public static ByteString readBytesFromFile(String filename) {
+  public static Buffer readBytesFromFile(String filename) {
     File fullPath = new File(getTestDataDir(), filename);
     try {
       RandomAccessFile file = new RandomAccessFile(fullPath, "r");
       byte[] content = new byte[(int) file.length()];
       file.readFully(content);
-      return ByteString.copyFrom(content);
+      return new Buffer(content);
     } catch (IOException e) {
       // Throw a RuntimeException here so that we can call this function from
       // static initializers.
@@ -780,11 +780,11 @@
    * on disk rather than generated dynamically.  The file is actually generated
    * by C++ code, so testing against it verifies compatibility with C++.
    */
-  public static ByteString getGoldenMessage() {
+  public static Buffer getGoldenMessage() {
     if (goldenMessage == null) {
       goldenMessage = readBytesFromFile("golden_message");
     }
     return goldenMessage;
   }
-  private static ByteString goldenMessage = null;
+  private static Buffer goldenMessage = null;
 }

Modified: activemq/sandbox/activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/WireFormatTest.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/WireFormatTest.java?rev=713832&r1=713831&r2=713832&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/WireFormatTest.java (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/WireFormatTest.java Thu Nov 13 13:39:10 2008
@@ -18,7 +18,7 @@
 
 import junit.framework.TestCase;
 
-import org.apache.activemq.protobuf.ByteString;
+import org.apache.activemq.protobuf.Buffer;
 import org.apache.activemq.protobuf.CodedInputStream;
 
 import protobuf_unittest.UnittestProto.TestAllTypes;
@@ -40,8 +40,8 @@
     TestUtil.assertAllFieldsSet(message2);
   }
 
-  private void assertFieldsInOrder(ByteString data) throws Exception {
-    CodedInputStream input = data.newCodedInput();
+  private void assertFieldsInOrder(Buffer data) throws Exception {
+    CodedInputStream input = new CodedInputStream(data);
     int previousTag = 0;
 
     while (true) {

Modified: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java?rev=713832&r1=713831&r2=713832&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java Thu Nov 13 13:39:10 2008
@@ -48,7 +48,7 @@
         if (!missingFields.isEmpty()) {
             throw new UninitializedMessageException(missingFields);
         }
-        return (T) this;
+        return getThis();
     }
 
     @SuppressWarnings("unchecked")
@@ -57,7 +57,7 @@
         if (!missingFields.isEmpty()) {
             throw new UninitializedMessageException(missingFields).asInvalidProtocolBufferException();
         }
-        return (T) this;
+        return getThis();
     }
 
     public ArrayList<String> missingFields() {
@@ -74,7 +74,7 @@
 
     @SuppressWarnings("unchecked")
     public T mergeFrom(T other) {
-        return (T) this;
+        return getThis();
     }
 
     public void writeUnframed(CodedOutputStream output) throws java.io.IOException {
@@ -107,38 +107,54 @@
         writeUnframed(output);
     }
 
-    public byte[] toUnframedByteArray() {
+    public Buffer toUnframedBuffer() {
         try {
-            byte[] result = new byte[serializedSizeUnframed()];
-            CodedOutputStream output = CodedOutputStream.newInstance(result);
+            int size = serializedSizeUnframed();
+            BufferOutputStream baos = new BufferOutputStream(size);
+            CodedOutputStream output = new CodedOutputStream(baos);
             writeUnframed(output);
-            output.checkNoSpaceLeft();
-            return result;
+            Buffer rc = baos.toBuffer();
+            if( rc.length != size ) {
+                throw new IllegalStateException("Did not write as much data as expected.");
+            }
+            return rc;
         } catch (IOException e) {
             throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e);
         }
     }
 
-    public byte[] toFramedByteArray() {
+    public Buffer toFramedBuffer() {
         try {
-            byte[] result = new byte[serializedSizeFramed()];
-            CodedOutputStream output = CodedOutputStream.newInstance(result);
+            int size = serializedSizeFramed();
+            BufferOutputStream baos = new BufferOutputStream(size);
+            CodedOutputStream output = new CodedOutputStream(baos);
             writeFramed(output);
-            output.checkNoSpaceLeft();
-            return result;
+            Buffer rc = baos.toBuffer();
+            if( rc.length != size ) {
+                throw new IllegalStateException("Did not write as much data as expected.");
+            }
+            return rc;
         } catch (IOException e) {
             throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e);
         }
     }
 
+    public byte[] toUnframedByteArray() {
+        return toUnframedBuffer().toByteArray();
+    }
+
+    public byte[] toFramedByteArray() {
+        return toFramedBuffer().toByteArray();
+    }
+
     public void writeFramed(OutputStream output) throws IOException {
-        CodedOutputStream codedOutput = CodedOutputStream.newInstance(output);
+        CodedOutputStream codedOutput = new CodedOutputStream(output);
         writeFramed(codedOutput);
         codedOutput.flush();
     }
 
     public void writeUnframed(OutputStream output) throws IOException {
-        CodedOutputStream codedOutput = CodedOutputStream.newInstance(output);
+        CodedOutputStream codedOutput = new CodedOutputStream(output);
         writeUnframed(codedOutput);
         codedOutput.flush();
     }
@@ -157,67 +173,54 @@
         int length = input.readRawVarint32();
         int oldLimit = input.pushLimit(length);
         T rc = mergeUnframed(input);
-        // input.checkLastTagWas(0);
+        input.checkLastTagWas(0);
         input.popLimit(oldLimit);
         return rc;
     }
 
-    public T mergeUnframed(ByteString data) throws InvalidProtocolBufferException {
+    public T mergeUnframed(Buffer data) throws InvalidProtocolBufferException {
         try {
-            CodedInputStream input = data.newCodedInput();
+            CodedInputStream input = new CodedInputStream(data);
             mergeUnframed(input);
-            // input.checkLastTagWas(0);
-            return (T) this;
+            input.checkLastTagWas(0);
+            return getThis();
         } catch (InvalidProtocolBufferException e) {
             throw e;
         } catch (IOException e) {
-            throw new RuntimeException("Reading from a ByteString threw an IOException (should " + "never happen).", e);
+            throw new RuntimeException("An IOException was thrown (should never happen in this method).", e);
         }
     }
 
-    public T mergeFramed(ByteString data) throws InvalidProtocolBufferException {
+    @SuppressWarnings("unchecked")
+    private T getThis() {
+        return (T) this;
+    }
+
+    public T mergeFramed(Buffer data) throws InvalidProtocolBufferException {
         try {
-            CodedInputStream input = data.newCodedInput();
+            CodedInputStream input = new CodedInputStream(data);
             mergeFramed(input);
-            // input.checkLastTagWas(0);
-            return (T) this;
+            input.checkLastTagWas(0);
+            return getThis();
         } catch (InvalidProtocolBufferException e) {
             throw e;
         } catch (IOException e) {
-            throw new RuntimeException("Reading from a ByteString threw an IOException (should " + "never happen).", e);
+            throw new RuntimeException("An IOException was thrown (should never happen in this method).", 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);
-        }
+        return mergeUnframed(new Buffer(data));
     }
 
     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);
-        }
+        return mergeFramed(new Buffer(data));
     }
 
     public T mergeUnframed(InputStream input) throws IOException {
-        CodedInputStream codedInput = CodedInputStream.newInstance(input);
+        CodedInputStream codedInput = new CodedInputStream(input);
         mergeUnframed(codedInput);
-        return (T) this;
+        return getThis();
     }
 
     public T mergeFramed(InputStream input) throws IOException {

Copied: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java (from r713629, activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteSequence.java)
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java?p2=activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java&p1=activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteSequence.java&r1=713629&r2=713832&rev=713832&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteSequence.java (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java Thu Nov 13 13:39:10 2008
@@ -17,27 +17,34 @@
 
 package org.apache.activemq.protobuf;
 
-public class ByteSequence {
+import java.io.UnsupportedEncodingException;
+
+final public class Buffer {
 
     public byte[] data;
     public int offset;
     public int length;
 
-    public ByteSequence() {
+    public Buffer() {
+    }
+    
+    public Buffer(String input) {
+        this( encode(input) );
     }
     
-    public ByteSequence(byte data[]) {
+    public Buffer(byte data[]) {
         this.data = data;
         this.offset = 0;
         this.length = data.length;
     }
 
-    public ByteSequence(byte data[], int offset, int length) {
+    public Buffer(byte data[], int offset, int length) {
         this.data = data;
         this.offset = offset;
         this.length = length;
     }
 
+
     public byte[] getData() {
         return data;
     }
@@ -70,5 +77,74 @@
             offset = 0;
         }
     }
+    
+    public byte[] toByteArray() {
+        if (length != data.length) {
+            byte t[] = new byte[length];
+            System.arraycopy(data, offset, t, 0, length);
+            data = t;
+            offset = 0;
+        }
+        return data;
+    }
+
+    public String toStringUtf8() {
+        try {
+            return new String(data, offset, length, "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException("A UnsupportedEncodingException was thrown for teh UTF-8 encoding. (This should never happen)");
+        }
+    }
+
+    public byte byteAt(int i) {
+        return data[offset+i];
+    }
+    
+    private static byte[] encode(String input) {
+        try {
+            return input.getBytes("UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException("A UnsupportedEncodingException was thrown for teh UTF-8 encoding. (This should never happen)");
+        }
+    }
+
+    @Override
+    public int hashCode() {
+        byte []target = new byte[4];
+        for(int i=0; i < length; i++) {
+            target[i%4] ^= data[offset+i];
+        }
+        return target[0]<<24 | target[1]<<16 | target[2]<<8 | target[3];
+    }
+    
+    @Override
+    public boolean equals(Object obj) {
+        if( obj==this )
+           return true;
+        
+        if( obj==null || obj.getClass()!=Buffer.class )
+           return false;
+        
+        return equals((Buffer)obj);
+     }
+     
+     public boolean equals(Buffer obj) {
+        if( length != obj.length ) {
+            return false;
+        }
+        for(int i=0; i < length; i++) {
+           if( obj.data[obj.offset+i] != data[offset+i] ) {
+               return false;
+           }
+        }
+        return true;
+     }
 
+     public BufferInputStream newInput() {
+         return new BufferInputStream(this);
+     }
+     
+     public BufferOutputStream newOutput() {
+         return new BufferOutputStream(this);
+     }
 }

Propchange: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Copied: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferInputStream.java (from r713629, activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteArrayInputStream.java)
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferInputStream.java?p2=activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferInputStream.java&p1=activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteArrayInputStream.java&r1=713629&r2=713832&rev=713832&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteArrayInputStream.java (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferInputStream.java Thu Nov 13 13:39:10 2008
@@ -23,22 +23,22 @@
  * Very similar to the java.io.ByteArrayInputStream but this version is not
  * thread safe.
  */
-public class ByteArrayInputStream extends InputStream {
+final public class BufferInputStream extends InputStream {
 
     byte buffer[];
     int limit;
     int pos;
     int mark;
 
-    public ByteArrayInputStream(byte data[]) {
+    public BufferInputStream(byte data[]) {
         this(data, 0, data.length);
     }
 
-    public ByteArrayInputStream(ByteSequence sequence) {
+    public BufferInputStream(Buffer sequence) {
         this(sequence.getData(), sequence.getOffset(), sequence.getLength());
     }
 
-    public ByteArrayInputStream(byte data[], int offset, int size) {
+    public BufferInputStream(byte data[], int offset, int size) {
         this.buffer = data;
         this.mark = offset;
         this.pos = offset;
@@ -60,15 +60,23 @@
     public int read(byte b[], int off, int len) {
         if (pos < limit) {
             len = Math.min(len, limit - pos);
-            if (len > 0) {
-                System.arraycopy(buffer, pos, b, off, len);
-                pos += len;
-            }
+            System.arraycopy(buffer, pos, b, off, len);
+            pos += len;
             return len;
         } else {
             return -1;
         }
     }
+    
+    public Buffer readBuffer(int len) {
+        Buffer rc=null;
+        if (pos < limit) {
+            len = Math.min(len, limit - pos);
+            rc = new Buffer(buffer, pos, len);
+            pos += len;
+        }
+        return rc;
+    }
 
     public long skip(long len) throws IOException {
         if (pos < limit) {
@@ -97,4 +105,5 @@
     public void reset() {
         pos = mark;
     }
+
 }

Propchange: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferInputStream.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Copied: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferOutputStream.java (from r713629, activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteArrayOutputStream.java)
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferOutputStream.java?p2=activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferOutputStream.java&p1=activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteArrayOutputStream.java&r1=713629&r2=713832&rev=713832&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/ByteArrayOutputStream.java (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferOutputStream.java Thu Nov 13 13:39:10 2008
@@ -16,71 +16,86 @@
  */
 package org.apache.activemq.protobuf;
 
+import java.io.EOFException;
+import java.io.IOException;
 import java.io.OutputStream;
 
 
 /**
  * Very similar to the java.io.ByteArrayOutputStream but this version 
- * is not thread safe and the resulting data is returned in a ByteSequence
- * to avoid an extra byte[] allocation.
+ * is not thread safe and the resulting data is returned in a Buffer
+ * to avoid an extra byte[] allocation.  It also does not re-grow it's 
+ * internal buffer.
  */
-public class ByteArrayOutputStream extends OutputStream {
+final public class BufferOutputStream extends OutputStream {
 
     byte buffer[];
-    int size;
-
-    public ByteArrayOutputStream() {
-        this(1028);
-    }
-    public ByteArrayOutputStream(int capacity) {
-        buffer = new byte[capacity];
-    }
-
-    public ByteArrayOutputStream(byte[] buffer) {
+    int offset;
+    int limit;
+    int pos;
+
+    public BufferOutputStream(int size) {
+        this(new byte[size]);
+    }   
+    
+    public BufferOutputStream(byte[] buffer) {
         this.buffer = buffer;
+        this.limit = buffer.length;
+    }   
+    
+    public BufferOutputStream(Buffer data) {
+        this.buffer = data.data;
+        this.pos = this.offset = data.offset;
+        this.limit = data.offset+data.length;
     }
     
-    public void write(int b) {
-        int newsize = size + 1;
-        checkCapacity(newsize);
-        buffer[size] = (byte) b;
-        size = newsize;
+    
+    public void write(int b) throws IOException {
+        int newPos = pos + 1;
+        checkCapacity(newPos);
+        buffer[pos] = (byte) b;
+        pos = newPos;
     }
 
-    public void write(byte b[], int off, int len) {
-        int newsize = size + len;
-        checkCapacity(newsize);
-        System.arraycopy(b, off, buffer, size, len);
-        size = newsize;
+    public void write(byte b[], int off, int len) throws IOException {
+        int newPos = pos + len;
+        checkCapacity(newPos);
+        System.arraycopy(b, off, buffer, pos, len);
+        pos = newPos;
+    }
+    
+    public Buffer getNextBuffer(int len) throws IOException {
+        int newPos = pos + len;
+        checkCapacity(newPos);
+        return new Buffer(buffer, pos, len);
     }
     
     /**
      * Ensures the the buffer has at least the minimumCapacity specified. 
      * @param i
+     * @throws EOFException 
      */
-    private void checkCapacity(int minimumCapacity) {
-        if (minimumCapacity > buffer.length) {
-            byte b[] = new byte[Math.max(buffer.length << 1, minimumCapacity)];
-            System.arraycopy(buffer, 0, b, 0, size);
-            buffer = b;
+    private void checkCapacity(int minimumCapacity) throws IOException {
+        if( minimumCapacity > limit ) {
+            throw new EOFException("Buffer limit reached.");
         }
     }
 
     public void reset() {
-        size = 0;
+        pos = offset;
     }
 
-    public ByteSequence toByteSequence() {
-        return new ByteSequence(buffer, 0, size);
+    public Buffer toBuffer() {
+        return new Buffer(buffer, offset, pos);
     }
     
     public byte[] toByteArray() {
-        byte rc[] = new byte[size];
-        System.arraycopy(buffer, 0, rc, 0, size);
-        return rc;
+        return toBuffer().toByteArray();
     }
     
     public int size() {
-        return size;
+        return offset-pos;
     }
+    
+
 }

Propchange: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferOutputStream.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Modified: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedInputStream.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedInputStream.java?rev=713832&r1=713831&r2=713832&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedInputStream.java (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedInputStream.java Thu Nov 13 13:39:10 2008
@@ -38,17 +38,21 @@
     private int lastTag = 0;
     private int limit = Integer.MAX_VALUE;
     private int pos;
+    private BufferInputStream bis;
     
     public CodedInputStream(InputStream in) {
         super(in);
+        if( in.getClass() == BufferInputStream.class ) {
+            bis = (BufferInputStream)in;
+        }
     }
 
-    public static CodedInputStream newInstance(InputStream input) {
-        return new CodedInputStream( input );
+    public CodedInputStream(Buffer data) {
+        this(new BufferInputStream(data));
     }
 
-    public static CodedInputStream newInstance(byte[] originalForm) {
-        return new CodedInputStream( new ByteArrayInputStream(originalForm) );
+    public CodedInputStream(byte[] data) {
+        this(new BufferInputStream(data));
     }
 
     /**
@@ -66,6 +70,7 @@
             }
             return lastTag;
         } catch (EOFException e) {
+            lastTag=0;
             return 0;
         }
     }
@@ -172,13 +177,14 @@
     /** Read a {@code string} field value from the stream. */
     public String readString() throws IOException {
         int size = readRawVarint32();
-        return new String(readRawBytes(size), "UTF-8");
+        Buffer data = readRawBytes(size);
+        return new String(data.data, data.offset, data.length, "UTF-8");
     }
 
     /** Read a {@code bytes} field value from the stream. */
-    public ByteString readBytes() throws IOException {
+    public Buffer readBytes() throws IOException {
         int size = readRawVarint32();
-        return ByteString.copyFrom(readRawBytes(size));
+        return readRawBytes(size);
     }
 
     /** Read a {@code uint32} field value from the stream. */
@@ -343,14 +349,26 @@
      * @throws InvalidProtocolBufferException
      *             The end of the stream or the current limit was reached.
      */
-    public byte[] readRawBytes(int size) throws IOException {
+    public Buffer readRawBytes(int size) throws IOException {
         if( this.pos+size > limit ) {
             throw new EOFException();
         }
+        
+        // If the underlying stream is a ByteArrayInputStream
+        // then we can avoid an array copy.
+        if( bis!=null ) {
+            Buffer rc = bis.readBuffer(size);
+            if( rc==null || rc.getLength() < size ) {
+                throw new EOFException();
+            }
+            this.pos += rc.getLength();
+            return rc;
+        }
+
+        // Otherwise we, have to do it the old fasioned way
         byte[] rc = new byte[size];
         int c;
         int pos=0;
-        
         while( pos < size ) {
             c = in.read(rc, pos, size-pos);
             if( c < 0 ) {
@@ -359,7 +377,8 @@
             this.pos += c;
             pos += c;
         }
-        return rc;
+        
+        return new Buffer(rc);
     }
 
     /**

Modified: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedOutputStream.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedOutputStream.java?rev=713832&r1=713831&r2=713832&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedOutputStream.java (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedOutputStream.java Thu Nov 13 13:39:10 2008
@@ -37,18 +37,22 @@
  */
 public final class CodedOutputStream extends FilterOutputStream {
 
-    public static CodedOutputStream newInstance(byte[] data) {
-        return new CodedOutputStream(new ByteArrayOutputStream(data));
-    }
-
-    public static CodedOutputStream newInstance(OutputStream output) {
-        return new CodedOutputStream(output);
-    }
+    private BufferOutputStream bos;
 
     public CodedOutputStream(OutputStream os) {
         super(os);
+        if( os instanceof BufferOutputStream ) {
+            bos = (BufferOutputStream)os;
+        }
     }
     
+    public CodedOutputStream(byte[] data) {
+        super(new BufferOutputStream(data));
+    }
+    
+    public CodedOutputStream(Buffer data) {
+        super(new BufferOutputStream(data));
+    }
 
     // -----------------------------------------------------------------
 
@@ -119,11 +123,10 @@
     }
 
     /** Write a {@code bytes} field, including tag, to the stream. */
-    public void writeBytes(int fieldNumber, ByteString value) throws IOException {
+    public void writeBytes(int fieldNumber, Buffer value) throws IOException {
         writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED);
-        byte[] bytes = value.toByteArray();
-        writeRawVarint32(bytes.length);
-        writeRawBytes(bytes);
+        writeRawVarint32(value.length);
+        writeRawBytes(value.data, value.offset, value.length);
     }
 
     /** Write a {@code uint32} field, including tag, to the stream. */
@@ -253,8 +256,8 @@
      * Compute the number of bytes that would be needed to encode a {@code
      * bytes} field, including tag.
      */
-    public static int computeBytesSize(int fieldNumber, ByteString value) {
-        return computeTagSize(fieldNumber) + computeRawVarint32Size(value.size()) + value.size();
+    public static int computeBytesSize(int fieldNumber, Buffer value) {
+        return computeTagSize(fieldNumber) + computeRawVarint32Size(value.length) + value.length;
     }
 
     /**
@@ -326,6 +329,10 @@
         out.write(value, offset, length);
     }
 
+    public void writeRawBytes(Buffer data) throws IOException {
+        out.write(data.data, data.offset, data.length);
+    }
+
     /** Encode and write a tag. */
     public void writeTag(int fieldNumber, int wireType) throws IOException {
         writeRawVarint32(WireFormat.makeTag(fieldNumber, wireType));
@@ -464,4 +471,11 @@
     public void checkNoSpaceLeft() {
     }
 
+    public Buffer getNextBuffer(int size) throws IOException {
+        if( bos==null ) {
+            return null;
+        }
+        return bos.getNextBuffer(size);
+    }
+
 }

Modified: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/DeferredDecodeMessage.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/DeferredDecodeMessage.java?rev=713832&r1=713831&r2=713832&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/DeferredDecodeMessage.java (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/DeferredDecodeMessage.java Thu Nov 13 13:39:10 2008
@@ -20,7 +20,7 @@
 
 abstract public class DeferredDecodeMessage<T> extends BaseMessage<T> {
 
-    protected byte[] encodedForm;
+    protected Buffer encodedForm;
     protected boolean decoded = true;
 
     @Override
@@ -34,29 +34,29 @@
 
     @SuppressWarnings("unchecked")
     @Override
-    public T mergeUnframed(byte[] data) throws InvalidProtocolBufferException {
+    public T mergeUnframed(Buffer data) throws InvalidProtocolBufferException {
         encodedForm = data;
         decoded = false;
         return (T) this;
     }
 
     @Override
-    public byte[] toUnframedByteArray() {
+    public Buffer toUnframedBuffer() {
         if (encodedForm == null) {
-            encodedForm = super.toUnframedByteArray();
+            encodedForm = super.toUnframedBuffer();
         }
         return encodedForm;
     }
-
+    
     protected void load() {
         if (!decoded) {
             decoded = true;
             try {
-                byte[] originalForm = encodedForm;
+                Buffer originalForm = encodedForm;
                 encodedForm=null;
-                CodedInputStream input = CodedInputStream.newInstance(originalForm);
+                CodedInputStream input = new CodedInputStream(originalForm);
                 mergeUnframed(input);
-//                input.checkLastTagWas(0);
+                input.checkLastTagWas(0);
                 // We need to reset the encoded form because the mergeUnframed
                 // from a stream clears it out.
                 encodedForm = originalForm;
@@ -70,9 +70,6 @@
     protected void loadAndClear() {
         super.loadAndClear();
         load();
-//        if( encodedForm!=null ) {
-//            System.out.println("crap.");
-//        }
         encodedForm = null;
     }
 

Modified: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java?rev=713832&r1=713831&r2=713832&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java Thu Nov 13 13:39:10 2008
@@ -39,19 +39,22 @@
     
     public T mergeFramed(byte[] 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 mergeUnframed(Buffer buffer) throws InvalidProtocolBufferException;
+
+    public T mergeFramed(Buffer buffer) 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 mergeUnframed(CodedInputStream input) throws IOException;
+    
+    public T mergeFramed(CodedInputStream input) throws IOException;
+    
+
+    public Buffer toUnframedBuffer();
     
+    public Buffer toFramedBuffer();
 
     public byte[] toUnframedByteArray();
    

Modified: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/JavaGenerator.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/JavaGenerator.java?rev=713832&r1=713831&r2=713832&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/JavaGenerator.java (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/JavaGenerator.java Thu Nov 13 13:39:10 2008
@@ -35,6 +35,7 @@
 import java.util.Map;
 import java.util.StringTokenizer;
 
+import org.apache.activemq.protobuf.Buffer;
 import org.apache.activemq.protobuf.compiler.parser.ParseException;
 import org.apache.activemq.protobuf.compiler.parser.ProtoParser;
 
@@ -453,7 +454,7 @@
         p("}");
         p();
 
-        p("public static "+className+" parseUnframed(org.apache.activemq.protobuf.ByteString data) throws org.apache.activemq.protobuf.InvalidProtocolBufferException {");
+        p("public static "+className+" parseUnframed(org.apache.activemq.protobuf.Buffer data) throws org.apache.activemq.protobuf.InvalidProtocolBufferException {");
         indent();
         p("return new "+className+"().mergeUnframed(data)"+postMergeProcessing+";");
         unindent();
@@ -481,7 +482,7 @@
         p("}");
         p();
         
-        p("public static "+className+" parseFramed(org.apache.activemq.protobuf.ByteString data) throws org.apache.activemq.protobuf.InvalidProtocolBufferException {");
+        p("public static "+className+" parseFramed(org.apache.activemq.protobuf.Buffer data) throws org.apache.activemq.protobuf.InvalidProtocolBufferException {");
         indent();
         p("return new "+className+"().mergeFramed(data)"+postMergeProcessing+";");
         unindent();
@@ -520,7 +521,7 @@
         p("public boolean equals("+className+" obj) {");
         indent();
         if( deferredDecode ) {
-        	p("return java.util.Arrays.equals(toUnframedByteArray(), obj.toUnframedByteArray());");
+        	p("return toUnframedBuffer().equals(obj.toUnframedBuffer());");
         } else {        
 	        for (FieldDescriptor field : m.getFields().values()) {
 	            String uname = uCamel(field.getName());
@@ -550,19 +551,11 @@
         p("");
         p("public int hashCode() {");
         indent();
+        int hc = className.hashCode();
         if( deferredDecode ) {
-        	int hc = className.hashCode();
-        	p("byte []target = new byte[]{ (byte)"+((hc>>24)&0xFF)+", (byte)"+((hc>>16)&0xFF)+", (byte)"+((hc>>8)&0xFF)+", (byte)"+(hc&0xFF)+" };");
-        	p("byte []data = toUnframedByteArray();");
-        	p("for(int i=0; i < data.length; i++) {");
-            indent();
-        	p("target[i%4] ^= data[i];");
-            unindent();
-        	p("}");
-        	p("");
-            p("return target[0]<<24 | target[1]<<16 | target[2]<<8 | target[3];");
+            p("return "+hc+" ^ toUnframedBuffer().hashCode();");
         } else {
-            p("int rc="+className.hashCode()+";");
+            p("int rc="+hc+";");
 	        int counter=0;
 	        for (FieldDescriptor field : m.getFields().values()) {
 	        	counter++;
@@ -701,9 +694,16 @@
         if( deferredDecode ) {
 			p("if (encodedForm == null) {");
 			indent();
-			p("encodedForm = new byte[serializedSizeUnframed()];");
-			p("org.apache.activemq.protobuf.CodedOutputStream original = output;");
-			p("output = org.apache.activemq.protobuf.CodedOutputStream.newInstance(encodedForm);");
+            p("int size = serializedSizeUnframed();");
+            p("encodedForm = output.getNextBuffer(size);");
+            p("org.apache.activemq.protobuf.CodedOutputStream original=null;");
+            p("if( encodedForm == null ) {");
+            indent();
+            p("encodedForm = new Buffer(new byte[size]);");
+            p("original = output;");
+            p("output = new org.apache.activemq.protobuf.CodedOutputStream(encodedForm);");
+            unindent();
+            p("}");
         }
         
 
@@ -763,18 +763,25 @@
                 p("}");
             }
             
-            //TODO: finish this up.
             unindent();
             p("}");
         }
         
         if( deferredDecode ) {
-        	p("output.checkNoSpaceLeft();");
-			p("output = original;");
-			unindent();
-			p("}");
-	        p("output.writeRawBytes(encodedForm);");
-        }        
+            p("if( original !=null ) {");
+            indent();
+            p("output.checkNoSpaceLeft();");
+            p("output = original;");
+            p("output.writeRawBytes(encodedForm);");
+            unindent();
+            p("}");
+            unindent();
+            p("} else {");
+            indent();
+            p("output.writeRawBytes(encodedForm);");
+            unindent();
+            p("}");
+        }
 
         unindent();
         p("}");
@@ -1349,7 +1356,7 @@
             if( field.isStringType() ) {
                 return asJavaString(defaultOption.getValue());
             } else if( field.getType() == FieldDescriptor.BYTES_TYPE ) {
-                return "org.apache.activemq.protobuf.ByteString.copyFromUtf8("+asJavaString(defaultOption.getValue())+")";
+                return "new org.apache.activemq.protobuf.Buffer("+asJavaString(defaultOption.getValue())+")";
             } else if( field.isInteger32Type() ) {
                 int v;
                 if( field.getType() == FieldDescriptor.UINT32_TYPE ) {
@@ -1529,7 +1536,7 @@
             return "java.lang.String";
         }
         if( field.getType() == FieldDescriptor.BYTES_TYPE ) {
-            return "org.apache.activemq.protobuf.ByteString";
+            return "org.apache.activemq.protobuf.Buffer";
         }
         if( field.getType() == FieldDescriptor.BOOL_TYPE ) {
             return "java.lang.Boolean";
@@ -1556,7 +1563,7 @@
             return "java.lang.String";
         }
         if( field.getType() == FieldDescriptor.BYTES_TYPE ) {
-            return "org.apache.activemq.protobuf.ByteString";
+            return "org.apache.activemq.protobuf.Buffer";
         }
         if( field.getType() == FieldDescriptor.BOOL_TYPE ) {
             return "boolean";

Modified: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/TextFormat.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/TextFormat.java?rev=713832&r1=713831&r2=713832&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/TextFormat.java (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/TextFormat.java Thu Nov 13 13:39:10 2008
@@ -22,7 +22,7 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import org.apache.activemq.protobuf.ByteString;
+import org.apache.activemq.protobuf.Buffer;
 
 /** 
  * Provide ascii text parsing and formatting support for proto2 instances.
@@ -370,15 +370,15 @@
      * value.  Otherwise, throw a {@link ParseException}.
      */
     public String consumeString() throws ParseException {
-      return consumeByteString().toStringUtf8();
+      return consumeBuffer().toStringUtf8();
     }
 
     /**
      * If the next token is a string, consume it, unescape it as a
-     * {@link ByteString}, and return it.  Otherwise, throw a
+     * {@link Buffer}, and return it.  Otherwise, throw a
      * {@link ParseException}.
      */
-    public ByteString consumeByteString() throws ParseException {
+    public Buffer consumeBuffer() throws ParseException {
       char quote = currentToken.length() > 0 ? currentToken.charAt(0) : '\0';
       if (quote != '\"' && quote != '\'') {
         throw parseException("Expected string.");
@@ -391,7 +391,7 @@
 
       try {
         String escaped = currentToken.substring(1, currentToken.length() - 1);
-        ByteString result = unescapeBytes(escaped);
+        Buffer result = unescapeBytes(escaped);
         nextToken();
         return result;
       } catch (InvalidEscapeSequence e) {
@@ -477,9 +477,9 @@
    * which no defined short-hand escape sequence is defined will be escaped
    * using 3-digit octal sequences.
    */
-  static String escapeBytes(ByteString input) {
-    StringBuilder builder = new StringBuilder(input.size());
-    for (int i = 0; i < input.size(); i++) {
+  static String escapeBytes(Buffer input) {
+    StringBuilder builder = new StringBuilder(input.getLength());
+    for (int i = 0; i < input.getLength(); i++) {
       byte b = input.byteAt(i);
       switch (b) {
         // Java does not recognize \a or \v, apparently.
@@ -510,10 +510,10 @@
 
   /**
    * Un-escape a byte sequence as escaped using
-   * {@link #escapeBytes(ByteString)}.  Two-digit hex escapes (starting with
+   * {@link #escapeBytes(Buffer)}.  Two-digit hex escapes (starting with
    * "\x") are also recognized.
    */
-  static ByteString unescapeBytes(CharSequence input)
+  static Buffer unescapeBytes(CharSequence input)
       throws InvalidEscapeSequence {
     byte[] result = new byte[input.length()];
     int pos = 0;
@@ -579,7 +579,7 @@
       }
     }
 
-    return ByteString.copyFrom(result, 0, pos);
+    return new Buffer(result, 0, pos);
   }
 
   /**
@@ -593,12 +593,12 @@
   }
 
   /**
-   * Like {@link #escapeBytes(ByteString)}, but escapes a text string.
+   * Like {@link #escapeBytes(Buffer)}, but escapes a text string.
    * Non-ASCII characters are first encoded as UTF-8, then each byte is escaped
    * individually as a 3-digit octal escape.  Yes, it's weird.
    */
   static String escapeText(String input) {
-    return escapeBytes(ByteString.copyFromUtf8(input));
+    return escapeBytes(new Buffer(input));
   }
 
   /**