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 2009/05/30 02:14:40 UTC
svn commit: r780147 - in /activemq/sandbox/activemq-flow:
activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/
activemq-protobuf/activemq-protobuf/
activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/
a...
Author: chirino
Date: Sat May 30 00:14:39 2009
New Revision: 780147
URL: http://svn.apache.org/viewvc?rev=780147&view=rev
Log:
moved the util related protobuf classes into the util module.
Added:
activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/
activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/AsciiBuffer.java
activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/Buffer.java (with props)
activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/BufferInputStream.java (with props)
activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/BufferOutputStream.java (with props)
activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java
Removed:
activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/AsciiBuffer.java
activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java
activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferInputStream.java
activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferOutputStream.java
activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java
Modified:
activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/WireFormatTest.java
activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/pom.xml
activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java
activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedInputStream.java
activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedOutputStream.java
activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/DeferredDecodeMessage.java
activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java
activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/MessageBuffer.java
activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/MessageBufferSupport.java
Modified: activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/WireFormatTest.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/WireFormatTest.java?rev=780147&r1=780146&r2=780147&view=diff
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/WireFormatTest.java (original)
+++ activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf-test/src/test/java/com/google/protobuf/WireFormatTest.java Sat May 30 00:14:39 2009
@@ -18,8 +18,8 @@
import junit.framework.TestCase;
-import org.apache.activemq.protobuf.Buffer;
import org.apache.activemq.protobuf.CodedInputStream;
+import org.apache.activemq.protobuf.Buffer;
import protobuf_unittest.UnittestProto.TestAllTypes;
Modified: activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/pom.xml
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/pom.xml?rev=780147&r1=780146&r2=780147&view=diff
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/pom.xml (original)
+++ activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/pom.xml Sat May 30 00:14:39 2009
@@ -36,12 +36,13 @@
</description>
<dependencies>
+
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-util</artifactId>
<version>6.0-SNAPSHOT</version>
- <optional>true</optional>
</dependency>
+
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
Modified: activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java?rev=780147&r1=780146&r2=780147&view=diff
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java (original)
+++ activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java Sat May 30 00:14:39 2009
@@ -28,6 +28,7 @@
import java.util.Collection;
import java.util.List;
+
abstract public class BaseMessage<T> implements Message<T> {
protected int memoizedSerializedSize = -1;
Modified: activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedInputStream.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedInputStream.java?rev=780147&r1=780146&r2=780147&view=diff
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedInputStream.java (original)
+++ activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedInputStream.java Sat May 30 00:14:39 2009
@@ -21,6 +21,7 @@
import java.io.IOException;
import java.io.InputStream;
+
/**
* Reads and decodes protocol message fields.
*
Modified: activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedOutputStream.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedOutputStream.java?rev=780147&r1=780146&r2=780147&view=diff
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedOutputStream.java (original)
+++ activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedOutputStream.java Sat May 30 00:14:39 2009
@@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.OutputStream;
+
/**
* Encodes and writes protocol message fields.
*
Modified: activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/DeferredDecodeMessage.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/DeferredDecodeMessage.java?rev=780147&r1=780146&r2=780147&view=diff
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/DeferredDecodeMessage.java (original)
+++ activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/DeferredDecodeMessage.java Sat May 30 00:14:39 2009
@@ -18,6 +18,7 @@
import java.io.IOException;
+
abstract public class DeferredDecodeMessage<T> extends BaseMessage<T> {
protected Buffer encodedForm;
Modified: activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java?rev=780147&r1=780146&r2=780147&view=diff
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java (original)
+++ activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java Sat May 30 00:14:39 2009
@@ -20,6 +20,7 @@
import java.io.InputStream;
import java.io.OutputStream;
+
public interface Message<T> {
public T clone() throws CloneNotSupportedException;
Modified: activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/MessageBuffer.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/MessageBuffer.java?rev=780147&r1=780146&r2=780147&view=diff
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/MessageBuffer.java (original)
+++ activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/MessageBuffer.java Sat May 30 00:14:39 2009
@@ -3,6 +3,7 @@
import java.io.IOException;
import java.io.OutputStream;
+
public interface MessageBuffer<B, MB extends MessageBuffer> extends PBMessage<B, MB> {
public int serializedSizeUnframed();
Modified: activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/MessageBufferSupport.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/MessageBufferSupport.java?rev=780147&r1=780146&r2=780147&view=diff
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/MessageBufferSupport.java (original)
+++ activemq/sandbox/activemq-flow/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/MessageBufferSupport.java Sat May 30 00:14:39 2009
@@ -7,6 +7,7 @@
import java.util.Collection;
+
final public class MessageBufferSupport {
public static final String FORZEN_ERROR_MESSAGE = "Modification not allowed after object has been fozen. Try modifying a copy of this object.";
Added: activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/AsciiBuffer.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/AsciiBuffer.java?rev=780147&view=auto
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/AsciiBuffer.java (added)
+++ activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/AsciiBuffer.java Sat May 30 00:14:39 2009
@@ -0,0 +1,75 @@
+package org.apache.activemq.protobuf;
+
+
+final public class AsciiBuffer extends Buffer {
+
+ private int hashCode;
+
+ public AsciiBuffer(Buffer other) {
+ super(other);
+ }
+
+ public AsciiBuffer(byte[] data, int offset, int length) {
+ super(data, offset, length);
+ }
+
+ public AsciiBuffer(byte[] data) {
+ super(data);
+ }
+
+ public AsciiBuffer(String input) {
+ super(encode(input));
+ }
+
+ public AsciiBuffer compact() {
+ if (length != data.length) {
+ return new AsciiBuffer(toByteArray());
+ }
+ return this;
+ }
+
+ public String toString()
+ {
+ return decode(this);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if( obj==this )
+ return true;
+
+ if( obj==null || obj.getClass()!=AsciiBuffer.class )
+ return false;
+
+ return equals((Buffer)obj);
+ }
+
+ @Override
+ public int hashCode() {
+ if( hashCode==0 ) {
+ hashCode = super.hashCode();;
+ }
+ return hashCode;
+ }
+
+ static public byte[] encode(String value)
+ {
+ int size = value.length();
+ byte rc[] = new byte[size];
+ for( int i=0; i < size; i++ ) {
+ rc[i] = (byte)(value.charAt(i)&0xFF);
+ }
+ return rc;
+ }
+ static public String decode(Buffer value)
+ {
+ int size = value.getLength();
+ char rc[] = new char[size];
+ for( int i=0; i < size; i++ ) {
+ rc[i] = (char)(value.byteAt(i) & 0xFF );
+ }
+ return new String(rc);
+ }
+
+
+}
Added: activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/Buffer.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/Buffer.java?rev=780147&view=auto
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/Buffer.java (added)
+++ activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/Buffer.java Sat May 30 00:14:39 2009
@@ -0,0 +1,208 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.activemq.protobuf;
+
+import java.util.List;
+
+public class Buffer implements Comparable<Buffer> {
+
+ final public byte[] data;
+ final public int offset;
+ final public int length;
+
+ public Buffer(Buffer other) {
+ this(other.data, other.offset, other.length);
+ }
+
+ public Buffer(byte data[]) {
+ this(data, 0, data.length);
+ }
+
+ public Buffer(byte data[], int offset, int length) {
+ this.data = data;
+ this.offset = offset;
+ this.length = length;
+ }
+
+ @Deprecated
+ public Buffer(String value) {
+ this(UTF8Buffer.encode(value));
+ }
+
+ public final Buffer slice(int low, int high) {
+ int sz;
+
+ if (high < 0) {
+ sz = length + high;
+ } else {
+ sz = high - low;
+ }
+
+ if (sz < 0) {
+ sz = 0;
+ }
+
+ return new Buffer(data, offset + low, sz);
+ }
+
+ public final byte[] getData() {
+ return data;
+ }
+
+ public final int getLength() {
+ return length;
+ }
+
+ public final int getOffset() {
+ return offset;
+ }
+
+ public Buffer compact() {
+ if (length != data.length) {
+ return new Buffer(toByteArray());
+ }
+ return this;
+ }
+
+ final public byte[] toByteArray() {
+ if (length != data.length) {
+ byte t[] = new byte[length];
+ System.arraycopy(data, offset, t, 0, length);
+ }
+ return data;
+ }
+
+ public byte byteAt(int i) {
+ return data[offset + i];
+ }
+
+
+ @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);
+ }
+
+ final 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;
+ }
+
+ final public BufferInputStream newInput() {
+ return new BufferInputStream(this);
+ }
+
+ final public BufferOutputStream newOutput() {
+ return new BufferOutputStream(this);
+ }
+
+ final public boolean isEmpty() {
+ return length == 0;
+ }
+
+ final public boolean contains(byte value) {
+ return indexOf(value, 0) >= 0;
+ }
+
+ final public int indexOf(byte value, int pos) {
+ for (int i = pos; i < length; i++) {
+ if (data[offset + i] == value) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ final public static Buffer join(List<Buffer> items, Buffer seperator) {
+ if (items.isEmpty())
+ return new Buffer(seperator.data, 0, 0);
+
+ int size = 0;
+ for (Buffer item : items) {
+ size += item.length;
+ }
+ size += seperator.length * (items.size() - 1);
+
+ int pos = 0;
+ byte data[] = new byte[size];
+ for (Buffer item : items) {
+ if (pos != 0) {
+ System.arraycopy(seperator.data, seperator.offset, data, pos, seperator.length);
+ pos += seperator.length;
+ }
+ System.arraycopy(item.data, item.offset, data, pos, item.length);
+ pos += item.length;
+ }
+
+ return new Buffer(data, 0, size);
+ }
+
+ @Deprecated
+ public String toStringUtf8() {
+ return UTF8Buffer.decode(this);
+ }
+
+ public int compareTo(Buffer o) {
+ int minLength = Math.min(length, o.length);
+ if (offset == o.offset) {
+ int pos = offset;
+ int limit = minLength + offset;
+ while (pos < limit) {
+ byte b1 = data[pos];
+ byte b2 = o.data[pos];
+ if (b1 != b2) {
+ return b1 - b2;
+ }
+ pos++;
+ }
+ } else {
+ int offset1 = offset;
+ int offset2 = o.offset;
+ while ( minLength-- != 0) {
+ byte b1 = data[offset1++];
+ byte b2 = o.data[offset2++];
+ if (b1 != b2) {
+ return b1 - b2;
+ }
+ }
+ }
+ return length - o.length;
+ }
+
+}
Propchange: activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/Buffer.java
------------------------------------------------------------------------------
svn:mergeinfo =
Added: activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/BufferInputStream.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/BufferInputStream.java?rev=780147&view=auto
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/BufferInputStream.java (added)
+++ activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/BufferInputStream.java Sat May 30 00:14:39 2009
@@ -0,0 +1,109 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.protobuf;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Very similar to the java.io.ByteArrayInputStream but this version is not
+ * thread safe.
+ */
+final public class BufferInputStream extends InputStream {
+
+ byte buffer[];
+ int limit;
+ int pos;
+ int mark;
+
+ public BufferInputStream(byte data[]) {
+ this(data, 0, data.length);
+ }
+
+ public BufferInputStream(Buffer sequence) {
+ this(sequence.getData(), sequence.getOffset(), sequence.getLength());
+ }
+
+ public BufferInputStream(byte data[], int offset, int size) {
+ this.buffer = data;
+ this.mark = offset;
+ this.pos = offset;
+ this.limit = offset + size;
+ }
+
+ public int read() throws IOException {
+ if (pos < limit) {
+ return buffer[pos++] & 0xff;
+ } else {
+ return -1;
+ }
+ }
+
+ public int read(byte[] b) throws IOException {
+ return read(b, 0, b.length);
+ }
+
+ public int read(byte b[], int off, int len) {
+ if (pos < limit) {
+ len = Math.min(len, limit - pos);
+ 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) {
+ len = Math.min(len, limit - pos);
+ if (len > 0) {
+ pos += len;
+ }
+ return len;
+ } else {
+ return -1;
+ }
+ }
+
+ public int available() {
+ return limit - pos;
+ }
+
+ public boolean markSupported() {
+ return true;
+ }
+
+ public void mark(int markpos) {
+ mark = pos;
+ }
+
+ public void reset() {
+ pos = mark;
+ }
+
+}
Propchange: activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/BufferInputStream.java
------------------------------------------------------------------------------
svn:mergeinfo =
Added: activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/BufferOutputStream.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/BufferOutputStream.java?rev=780147&view=auto
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/BufferOutputStream.java (added)
+++ activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/BufferOutputStream.java Sat May 30 00:14:39 2009
@@ -0,0 +1,101 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.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 Buffer
+ * to avoid an extra byte[] allocation. It also does not re-grow it's
+ * internal buffer.
+ */
+final public class BufferOutputStream extends OutputStream {
+
+ 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) throws IOException {
+ int newPos = pos + 1;
+ checkCapacity(newPos);
+ buffer[pos] = (byte) b;
+ pos = newPos;
+ }
+
+ 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) throws IOException {
+ if( minimumCapacity > limit ) {
+ throw new EOFException("Buffer limit reached.");
+ }
+ }
+
+ public void reset() {
+ pos = offset;
+ }
+
+ public Buffer toBuffer() {
+ return new Buffer(buffer, offset, pos);
+ }
+
+ public byte[] toByteArray() {
+ return toBuffer().toByteArray();
+ }
+
+ public int size() {
+ return offset-pos;
+ }
+
+
+}
Propchange: activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/BufferOutputStream.java
------------------------------------------------------------------------------
svn:mergeinfo =
Added: activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java?rev=780147&view=auto
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java (added)
+++ activemq/sandbox/activemq-flow/activemq-util/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java Sat May 30 00:14:39 2009
@@ -0,0 +1,85 @@
+package org.apache.activemq.protobuf;
+
+import java.io.UnsupportedEncodingException;
+
+final public class UTF8Buffer extends Buffer {
+
+ int hashCode;
+ String value;
+
+ public UTF8Buffer(Buffer other) {
+ super(other);
+ }
+
+ public UTF8Buffer(byte[] data, int offset, int length) {
+ super(data, offset, length);
+ }
+
+ public UTF8Buffer(byte[] data) {
+ super(data);
+ }
+
+ public UTF8Buffer(String input) {
+ super(encode(input));
+ }
+
+ public UTF8Buffer compact() {
+ if (length != data.length) {
+ return new UTF8Buffer(toByteArray());
+ }
+ return this;
+ }
+
+ public String toString()
+ {
+ if( value==null ) {
+ value = decode(this);
+ }
+ return value;
+ }
+
+ @Override
+ public int compareTo(Buffer other) {
+ // Do a char comparison.. not a byte for byte comparison.
+ return toString().compareTo(other.toString());
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if( obj==this )
+ return true;
+
+ if( obj==null || obj.getClass()!=UTF8Buffer.class )
+ return false;
+
+ return equals((Buffer)obj);
+ }
+
+ @Override
+ public int hashCode() {
+ if( hashCode==0 ) {
+ hashCode = super.hashCode();;
+ }
+ return hashCode;
+ }
+
+ static public byte[] encode(String value)
+ {
+ try {
+ return value.getBytes("UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException("A UnsupportedEncodingException was thrown for teh UTF-8 encoding. (This should never happen)");
+ }
+ }
+
+ static public String decode(Buffer buffer)
+ {
+ try {
+ return new String(buffer.getData(), buffer.getOffset(), buffer.getLength(), "UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException("A UnsupportedEncodingException was thrown for teh UTF-8 encoding. (This should never happen)");
+ }
+ }
+
+
+}