You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by rg...@apache.org on 2012/03/10 10:48:56 UTC
svn commit: r1299173 [3/10] - in /qpid/proton/proton-j: ./ codec/ codec/src/
codec/src/org/ codec/src/org/apache/ codec/src/org/apache/qpid/
codec/src/org/apache/qpid/proton/ codec/src/org/apache/qpid/proton/codec/
codec/src/org/apache/qpid/proton/fram...
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/FixedSizePrimitiveTypeEncoding.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/FixedSizePrimitiveTypeEncoding.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/FixedSizePrimitiveTypeEncoding.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/FixedSizePrimitiveTypeEncoding.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,42 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+abstract class FixedSizePrimitiveTypeEncoding<T> extends AbstractPrimitiveTypeEncoding<T>
+{
+
+ FixedSizePrimitiveTypeEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ public final boolean isFixedSizeVal()
+ {
+ return true;
+ }
+
+ public final int getValueSize(final T val)
+ {
+ return getFixedSize();
+ }
+
+ protected abstract int getFixedSize();
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/FloatType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/FloatType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/FloatType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/FloatType.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,128 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+import java.util.Collection;
+import java.util.Collections;
+
+public class FloatType extends AbstractPrimitiveType<Float>
+{
+ private FloatEncoding _floatEncoding;
+
+ FloatType(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _floatEncoding = new FloatEncoding(encoder, decoder);
+ encoder.register(Float.class, this);
+ decoder.register(this);
+ }
+
+ public Class<Float> getTypeClass()
+ {
+ return Float.class;
+ }
+
+ public FloatEncoding getEncoding(final Float val)
+ {
+ return _floatEncoding;
+ }
+
+
+ public FloatEncoding getCanonicalEncoding()
+ {
+ return _floatEncoding;
+ }
+
+ public Collection<FloatEncoding> getAllEncodings()
+ {
+ return Collections.singleton(_floatEncoding);
+ }
+
+ public void write(float f)
+ {
+ _floatEncoding.write(f);
+ }
+
+ public class FloatEncoding extends FixedSizePrimitiveTypeEncoding<Float>
+ {
+
+ public FloatEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 4;
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.FLOAT;
+ }
+
+ public FloatType getType()
+ {
+ return FloatType.this;
+ }
+
+ public void writeValue(final Float val)
+ {
+ getEncoder().writeRaw(val.floatValue());
+ }
+
+ public void writeValue(final float val)
+ {
+ getEncoder().writeRaw(val);
+ }
+
+
+ public void write(final float f)
+ {
+ writeConstructor();
+ getEncoder().writeRaw(f);
+
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Float> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public Float readValue()
+ {
+ return readPrimitiveValue();
+ }
+
+ public float readPrimitiveValue()
+ {
+ return getDecoder().readRawFloat();
+ }
+
+
+ @Override
+ public boolean encodesJavaPrimitive()
+ {
+ return true;
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/FloatingSizePrimitiveTypeEncoding.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/FloatingSizePrimitiveTypeEncoding.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/FloatingSizePrimitiveTypeEncoding.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/FloatingSizePrimitiveTypeEncoding.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,54 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+abstract class FloatingSizePrimitiveTypeEncoding<T> extends AbstractPrimitiveTypeEncoding<T>
+{
+
+ FloatingSizePrimitiveTypeEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ public final boolean isFixedSizeVal()
+ {
+ return false;
+ }
+
+ abstract int getSizeBytes();
+
+ public void writeValue(final T val)
+ {
+ writeSize(val);
+ writeEncodedValue(val);
+ }
+
+ protected abstract void writeEncodedValue(final T val);
+
+ protected abstract void writeSize(final T val);
+
+ public int getValueSize(final T val)
+ {
+ return getSizeBytes() + getEncodedValueSize(val);
+ }
+
+ protected abstract int getEncodedValueSize(final T val);
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/IntegerType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/IntegerType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/IntegerType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/IntegerType.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,213 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+public class IntegerType extends AbstractPrimitiveType<Integer>
+{
+
+ public static interface IntegerEncoding extends PrimitiveTypeEncoding<Integer>
+ {
+ void write(int i);
+ void writeValue(int i);
+ int readPrimitiveValue();
+ }
+
+ private IntegerEncoding _integerEncoding;
+ private IntegerEncoding _smallIntegerEncoding;
+
+ IntegerType(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _integerEncoding = new AllIntegerEncoding(encoder, decoder);
+ _smallIntegerEncoding = new SmallIntegerEncoding(encoder, decoder);
+ encoder.register(Integer.class, this);
+ decoder.register(this);
+ }
+
+ public Class<Integer> getTypeClass()
+ {
+ return Integer.class;
+ }
+
+ public IntegerEncoding getEncoding(final Integer val)
+ {
+ return getEncoding(val.intValue());
+ }
+
+ public IntegerEncoding getEncoding(final int i)
+ {
+
+ return (i >= -128 && i <= 127) ? _smallIntegerEncoding : _integerEncoding;
+ }
+
+
+ public IntegerEncoding getCanonicalEncoding()
+ {
+ return _integerEncoding;
+ }
+
+ public Collection<IntegerEncoding> getAllEncodings()
+ {
+ return Arrays.asList(_integerEncoding, _smallIntegerEncoding);
+ }
+
+ public void write(int i)
+ {
+ if(i >= -128 && i <= 127)
+ {
+ _smallIntegerEncoding.write(i);
+ }
+ else
+ {
+ _integerEncoding.write(i);
+ }
+ }
+
+ private class AllIntegerEncoding extends FixedSizePrimitiveTypeEncoding<Integer> implements IntegerEncoding
+ {
+
+ public AllIntegerEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 4;
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.INT;
+ }
+
+ public IntegerType getType()
+ {
+ return IntegerType.this;
+ }
+
+ public void writeValue(final Integer val)
+ {
+ getEncoder().writeRaw(val.intValue());
+ }
+
+ public void write(final int i)
+ {
+ writeConstructor();
+ getEncoder().writeRaw(i);
+
+ }
+
+ public void writeValue(final int i)
+ {
+ getEncoder().writeRaw(i);
+ }
+
+ public int readPrimitiveValue()
+ {
+ return getDecoder().readRawInt();
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Integer> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public Integer readValue()
+ {
+ return readPrimitiveValue();
+ }
+
+
+ @Override
+ public boolean encodesJavaPrimitive()
+ {
+ return true;
+ }
+ }
+
+ private class SmallIntegerEncoding extends FixedSizePrimitiveTypeEncoding<Integer> implements IntegerEncoding
+ {
+ public SmallIntegerEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.SMALLINT;
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 1;
+ }
+
+ public void write(final int i)
+ {
+ writeConstructor();
+ getEncoder().writeRaw((byte)i);
+ }
+
+ public void writeValue(final int i)
+ {
+ getEncoder().writeRaw((byte)i);
+ }
+
+ public int readPrimitiveValue()
+ {
+ return getDecoder().readRawByte();
+ }
+
+ public IntegerType getType()
+ {
+ return IntegerType.this;
+ }
+
+ public void writeValue(final Integer val)
+ {
+ getEncoder().writeRaw((byte)val.intValue());
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Integer> encoder)
+ {
+ return encoder == this;
+ }
+
+ public Integer readValue()
+ {
+ return readPrimitiveValue();
+ }
+
+
+ @Override
+ public boolean encodesJavaPrimitive()
+ {
+ return true;
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/LargeFloatingSizePrimitiveTypeEncoding.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/LargeFloatingSizePrimitiveTypeEncoding.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/LargeFloatingSizePrimitiveTypeEncoding.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/LargeFloatingSizePrimitiveTypeEncoding.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,42 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+abstract class LargeFloatingSizePrimitiveTypeEncoding<T> extends FloatingSizePrimitiveTypeEncoding<T>
+{
+
+ LargeFloatingSizePrimitiveTypeEncoding(final EncoderImpl encoder, DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ public int getSizeBytes()
+ {
+ return 4;
+ }
+
+ @Override
+ protected void writeSize(final T val)
+ {
+ getEncoder().writeRaw(getEncodedValueSize(val));
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/ListType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/ListType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/ListType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/ListType.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,235 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+public class ListType extends AbstractPrimitiveType<List>
+{
+ private final ListEncoding _listEncoding;
+ private final ListEncoding _shortListEncoding;
+ private EncoderImpl _encoder;
+
+ private static interface ListEncoding extends PrimitiveTypeEncoding<List>
+ {
+ void setValue(List value, int length);
+ }
+
+ ListType(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _encoder = encoder;
+ _listEncoding = new AllListEncoding(encoder, decoder);
+ _shortListEncoding = new ShortListEncoding(encoder, decoder);
+ encoder.register(List.class, this);
+ decoder.register(this);
+ }
+
+ public Class<List> getTypeClass()
+ {
+ return List.class;
+ }
+
+ public ListEncoding getEncoding(final List val)
+ {
+
+ int calculatedSize = calculateSize(val, _encoder);
+ ListEncoding encoding = (val.size() > 255 || calculatedSize >= 254)
+ ? _listEncoding
+ : _shortListEncoding;
+
+ encoding.setValue(val, calculatedSize);
+ return encoding;
+ }
+
+ private static int calculateSize(final List val, EncoderImpl encoder)
+ {
+ int len = 0;
+ final int count = val.size();
+
+ for(int i = 0; i < count; i++)
+ {
+ Object element = val.get(i);
+ TypeEncoding elementEncoding = encoder.getType(element).getEncoding(element);
+ len += elementEncoding.getConstructorSize()+elementEncoding.getValueSize(element);
+ }
+ return len;
+ }
+
+
+ public ListEncoding getCanonicalEncoding()
+ {
+ return _listEncoding;
+ }
+
+ public Collection<ListEncoding> getAllEncodings()
+ {
+ return Arrays.asList(_shortListEncoding, _listEncoding);
+ }
+
+ private class AllListEncoding
+ extends LargeFloatingSizePrimitiveTypeEncoding<List>
+ implements ListEncoding
+ {
+
+ private List _value;
+ private int _length;
+
+ public AllListEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected void writeEncodedValue(final List val)
+ {
+ getEncoder().writeRaw(val.size());
+
+ final int count = val.size();
+
+ for(int i = 0; i < count; i++)
+ {
+ Object element = val.get(i);
+ TypeEncoding elementEncoding = getEncoder().getType(element).getEncoding(element);
+ elementEncoding.writeConstructor();
+ elementEncoding.writeValue(element);
+ }
+ }
+
+ @Override
+ protected int getEncodedValueSize(final List val)
+ {
+ return 4 + ((val == _value) ? _length : calculateSize(val, getEncoder()));
+ }
+
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.LIST32;
+ }
+
+ public ListType getType()
+ {
+ return ListType.this;
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<List> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public List readValue()
+ {
+ DecoderImpl decoder = getDecoder();
+ int size = decoder.readRawInt();
+ // todo - limit the decoder with size
+ int count = decoder.readRawInt();
+ List list = new ArrayList(count);
+ for(int i = 0; i < count; i++)
+ {
+ list.add(decoder.readObject());
+ }
+ return list;
+ }
+
+ public void setValue(final List value, final int length)
+ {
+ _value = value;
+ _length = length;
+ }
+ }
+
+ private class ShortListEncoding
+ extends SmallFloatingSizePrimitiveTypeEncoding<List>
+ implements ListEncoding
+ {
+
+ private List _value;
+ private int _length;
+
+ public ShortListEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected void writeEncodedValue(final List val)
+ {
+ getEncoder().writeRaw((byte)val.size());
+
+ final int count = val.size();
+
+ for(int i = 0; i < count; i++)
+ {
+ Object element = val.get(i); TypeEncoding elementEncoding = getEncoder().getType(element).getEncoding(element);
+ elementEncoding.writeConstructor();
+ elementEncoding.writeValue(element);
+ }
+ }
+
+ @Override
+ protected int getEncodedValueSize(final List val)
+ {
+ return 1 + ((val == _value) ? _length : calculateSize(val, getEncoder()));
+ }
+
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.LIST8;
+ }
+
+ public ListType getType()
+ {
+ return ListType.this;
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<List> encoder)
+ {
+ return encoder == this;
+ }
+
+ public List readValue()
+ {
+
+ DecoderImpl decoder = getDecoder();
+ int size = ((int)decoder.readRawByte()) & 0xff;
+ // todo - limit the decoder with size
+ int count = ((int)decoder.readRawByte()) & 0xff;
+ List list = new ArrayList(count);
+ for(int i = 0; i < count; i++)
+ {
+ list.add(decoder.readObject());
+ }
+ return list;
+ }
+
+ public void setValue(final List value, final int length)
+ {
+ _value = value;
+ _length = length;
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/LongType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/LongType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/LongType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/LongType.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,212 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+public class LongType extends AbstractPrimitiveType<Long>
+{
+
+ public static interface LongEncoding extends PrimitiveTypeEncoding<Long>
+ {
+ void write(long l);
+ void writeValue(long l);
+ public long readPrimitiveValue();
+ }
+
+ private LongEncoding _longEncoding;
+ private LongEncoding _smallLongEncoding;
+
+ LongType(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _longEncoding = new AllLongEncoding(encoder, decoder);
+ _smallLongEncoding = new SmallLongEncoding(encoder, decoder);
+ encoder.register(Long.class, this);
+ decoder.register(this);
+ }
+
+ public Class<Long> getTypeClass()
+ {
+ return Long.class;
+ }
+
+ public LongEncoding getEncoding(final Long val)
+ {
+ return getEncoding(val.longValue());
+ }
+
+ public LongEncoding getEncoding(final long l)
+ {
+ return (l >= -128l && l <= 127l) ? _smallLongEncoding : _longEncoding;
+ }
+
+
+ public LongEncoding getCanonicalEncoding()
+ {
+ return _longEncoding;
+ }
+
+ public Collection<LongEncoding> getAllEncodings()
+ {
+ return Arrays.asList(_smallLongEncoding, _longEncoding);
+ }
+
+ public void write(long l)
+ {
+ if(l >= -128l && l <= 127l)
+ {
+ _smallLongEncoding.write(l);
+ }
+ else
+ {
+ _longEncoding.write(l);
+ }
+ }
+
+ private class AllLongEncoding extends FixedSizePrimitiveTypeEncoding<Long> implements LongEncoding
+ {
+
+ public AllLongEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 8;
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.LONG;
+ }
+
+ public LongType getType()
+ {
+ return LongType.this;
+ }
+
+ public void writeValue(final Long val)
+ {
+ getEncoder().writeRaw(val.longValue());
+ }
+
+ public void write(final long l)
+ {
+ writeConstructor();
+ getEncoder().writeRaw(l);
+
+ }
+
+ public void writeValue(final long l)
+ {
+ getEncoder().writeRaw(l);
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Long> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public Long readValue()
+ {
+ return readPrimitiveValue();
+ }
+
+ public long readPrimitiveValue()
+ {
+ return getDecoder().readRawLong();
+ }
+
+
+ @Override
+ public boolean encodesJavaPrimitive()
+ {
+ return true;
+ }
+ }
+
+ private class SmallLongEncoding extends FixedSizePrimitiveTypeEncoding<Long> implements LongEncoding
+ {
+ public SmallLongEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.SMALLLONG;
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 1;
+ }
+
+ public void write(final long l)
+ {
+ writeConstructor();
+ getEncoder().writeRaw((byte)l);
+ }
+
+ public void writeValue(final long l)
+ {
+ getEncoder().writeRaw((byte)l);
+ }
+
+ public long readPrimitiveValue()
+ {
+ return (long) getDecoder().readRawByte();
+ }
+
+ public LongType getType()
+ {
+ return LongType.this;
+ }
+
+ public void writeValue(final Long val)
+ {
+ getEncoder().writeRaw((byte)val.longValue());
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Long> encoder)
+ {
+ return encoder == this;
+ }
+
+ public Long readValue()
+ {
+ return readPrimitiveValue();
+ }
+
+
+ @Override
+ public boolean encodesJavaPrimitive()
+ {
+ return true;
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/MapType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/MapType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/MapType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/MapType.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,251 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+import java.util.*;
+
+public class MapType extends AbstractPrimitiveType<Map>
+{
+ private final MapEncoding _mapEncoding;
+ private final MapEncoding _shortMapEncoding;
+ private EncoderImpl _encoder;
+
+ private static interface MapEncoding extends PrimitiveTypeEncoding<Map>
+ {
+ void setValue(Map value, int length);
+ }
+
+ MapType(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _encoder = encoder;
+ _mapEncoding = new AllMapEncoding(encoder, decoder);
+ _shortMapEncoding = new ShortMapEncoding(encoder, decoder);
+ encoder.register(Map.class, this);
+ decoder.register(this);
+ }
+
+ public Class<Map> getTypeClass()
+ {
+ return Map.class;
+ }
+
+ public MapEncoding getEncoding(final Map val)
+ {
+
+ int calculatedSize = calculateSize(val, _encoder);
+ MapEncoding encoding = (val.size() > 127 || calculatedSize >= 254)
+ ? _mapEncoding
+ : _shortMapEncoding;
+
+ encoding.setValue(val, calculatedSize);
+ return encoding;
+ }
+
+ private static int calculateSize(final Map val, EncoderImpl encoder)
+ {
+ int len = 0;
+ Iterator<Map.Entry> iter = val.entrySet().iterator();
+
+ while(iter.hasNext())
+ {
+ Map.Entry element = iter.next();
+ TypeEncoding elementEncoding = encoder.getType(element.getKey()).getEncoding(element.getKey());
+ len += elementEncoding.getConstructorSize()+elementEncoding.getValueSize(element.getKey());
+ elementEncoding = encoder.getType(element.getValue()).getEncoding(element.getValue());
+ len += elementEncoding.getConstructorSize()+elementEncoding.getValueSize(element.getValue());
+
+ }
+ return len;
+ }
+
+
+ public MapEncoding getCanonicalEncoding()
+ {
+ return _mapEncoding;
+ }
+
+ public Collection<MapEncoding> getAllEncodings()
+ {
+ return Arrays.asList(_shortMapEncoding, _mapEncoding);
+ }
+
+ private class AllMapEncoding
+ extends LargeFloatingSizePrimitiveTypeEncoding<Map>
+ implements MapEncoding
+ {
+
+ private Map _value;
+ private int _length;
+
+ public AllMapEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected void writeEncodedValue(final Map val)
+ {
+ getEncoder().writeRaw(2 * val.size());
+
+
+ Iterator<Map.Entry> iter = val.entrySet().iterator();
+
+ while(iter.hasNext())
+ {
+ Map.Entry element = iter.next();
+ TypeEncoding elementEncoding = getEncoder().getType(element.getKey()).getEncoding(element.getKey());
+ elementEncoding.writeConstructor();
+ elementEncoding.writeValue(element.getKey());
+ elementEncoding = getEncoder().getType(element.getValue()).getEncoding(element.getValue());
+ elementEncoding.writeConstructor();
+ elementEncoding.writeValue(element.getValue());
+ }
+ }
+
+ @Override
+ protected int getEncodedValueSize(final Map val)
+ {
+ return 4 + ((val == _value) ? _length : calculateSize(val, getEncoder()));
+ }
+
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.MAP32;
+ }
+
+ public MapType getType()
+ {
+ return MapType.this;
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Map> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public Map readValue()
+ {
+
+ DecoderImpl decoder = getDecoder();
+ int size = decoder.readRawInt();
+ // todo - limit the decoder with size
+ int count = decoder.readRawInt();
+ Map map = new HashMap(count);
+ for(int i = 0; i < count; i++)
+ {
+ Object key = decoder.readObject();
+ i++;
+ Object value = decoder.readObject();
+ map.put(key, value);
+ }
+ return map;
+ }
+
+ public void setValue(final Map value, final int length)
+ {
+ _value = value;
+ _length = length;
+ }
+ }
+
+ private class ShortMapEncoding
+ extends SmallFloatingSizePrimitiveTypeEncoding<Map>
+ implements MapEncoding
+ {
+
+ private Map _value;
+ private int _length;
+
+ public ShortMapEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected void writeEncodedValue(final Map val)
+ {
+ getEncoder().writeRaw((byte)(2*val.size()));
+
+
+ Iterator<Map.Entry> iter = val.entrySet().iterator();
+
+ while(iter.hasNext())
+ {
+ Map.Entry element = iter.next();
+ TypeEncoding elementEncoding = getEncoder().getType(element.getKey()).getEncoding(element.getKey());
+ elementEncoding.writeConstructor();
+ elementEncoding.writeValue(element.getKey());
+ elementEncoding = getEncoder().getType(element.getValue()).getEncoding(element.getValue());
+ elementEncoding.writeConstructor();
+ elementEncoding.writeValue(element.getValue());
+ }
+ }
+
+ @Override
+ protected int getEncodedValueSize(final Map val)
+ {
+ return 1 + ((val == _value) ? _length : calculateSize(val, getEncoder()));
+ }
+
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.MAP8;
+ }
+
+ public MapType getType()
+ {
+ return MapType.this;
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Map> encoder)
+ {
+ return encoder == this;
+ }
+
+ public Map readValue()
+ {
+ DecoderImpl decoder = getDecoder();
+ int size = ((int)decoder.readRawByte()) & 0xff;
+ // todo - limit the decoder with size
+ int count = ((int)decoder.readRawByte()) & 0xff;
+
+ Map map = new HashMap(count);
+ for(int i = 0; i < count; i++)
+ {
+ Object key = decoder.readObject();
+ i++;
+ Object value = decoder.readObject();
+ map.put(key, value);
+ }
+ return map;
+ }
+
+ public void setValue(final Map value, final int length)
+ {
+ _value = value;
+ _length = length;
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/NullType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/NullType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/NullType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/NullType.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,111 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+import java.util.Collection;
+import java.util.Collections;
+
+public final class NullType extends AbstractPrimitiveType<Void>
+{
+ private NullEncoding _nullEncoding;
+
+ NullType(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _nullEncoding = new NullEncoding(encoder, decoder);
+ encoder.register(Void.class, this);
+ decoder.register(this);
+ }
+
+ public Class<Void> getTypeClass()
+ {
+ return Void.class;
+ }
+
+ public NullEncoding getEncoding(final Void val)
+ {
+ return _nullEncoding;
+ }
+
+
+ public NullEncoding getCanonicalEncoding()
+ {
+ return _nullEncoding;
+ }
+
+ public Collection<NullEncoding> getAllEncodings()
+ {
+ return Collections.singleton(_nullEncoding);
+ }
+
+ public void write()
+ {
+ _nullEncoding.write();
+ }
+
+ private class NullEncoding extends FixedSizePrimitiveTypeEncoding<Void>
+ {
+
+ public NullEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 0;
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.NULL;
+ }
+
+ public NullType getType()
+ {
+ return NullType.this;
+ }
+
+ public void writeValue(final Void val)
+ {
+ }
+
+ public void writeValue()
+ {
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Void> encoding)
+ {
+ return encoding == this;
+ }
+
+ public Void readValue()
+ {
+ return null;
+ }
+
+ public void write()
+ {
+ writeConstructor();
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/PrimitiveType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/PrimitiveType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/PrimitiveType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/PrimitiveType.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,35 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+import java.util.Collection;
+
+public interface PrimitiveType<V> extends AMQPType<V>
+{
+
+ PrimitiveTypeEncoding<V> getEncoding(V val);
+
+ PrimitiveTypeEncoding<V> getCanonicalEncoding();
+
+ Collection<? extends PrimitiveTypeEncoding<V>> getAllEncodings();
+
+
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/PrimitiveTypeEncoding.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/PrimitiveTypeEncoding.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/PrimitiveTypeEncoding.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/PrimitiveTypeEncoding.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,32 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+public interface PrimitiveTypeEncoding<T> extends TypeEncoding<T>
+{
+ PrimitiveType<T> getType();
+
+ byte getEncodingCode();
+
+ void writeConstructor();
+
+ int getConstructorSize();
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/ShortType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/ShortType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/ShortType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/ShortType.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,127 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+import java.util.Collection;
+import java.util.Collections;
+
+public class ShortType extends AbstractPrimitiveType<Short>
+{
+ private ShortEncoding _shortEncoding;
+
+ ShortType(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _shortEncoding = new ShortEncoding(encoder, decoder);
+ encoder.register(Short.class, this);
+ decoder.register(this);
+ }
+
+ public Class<Short> getTypeClass()
+ {
+ return Short.class;
+ }
+
+ public ShortEncoding getEncoding(final Short val)
+ {
+ return _shortEncoding;
+ }
+
+ public void write(short s)
+ {
+ _shortEncoding.write(s);
+ }
+
+ public ShortEncoding getCanonicalEncoding()
+ {
+ return _shortEncoding;
+ }
+
+ public Collection<ShortEncoding> getAllEncodings()
+ {
+ return Collections.singleton(_shortEncoding);
+ }
+
+ public class ShortEncoding extends FixedSizePrimitiveTypeEncoding<Short>
+ {
+
+ public ShortEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 2;
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.SHORT;
+ }
+
+ public ShortType getType()
+ {
+ return ShortType.this;
+ }
+
+ public void writeValue(final Short val)
+ {
+ getEncoder().writeRaw(val);
+ }
+
+ public void writeValue(final short val)
+ {
+ getEncoder().writeRaw(val);
+ }
+
+
+ public void write(final short s)
+ {
+ writeConstructor();
+ getEncoder().writeRaw(s);
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Short> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public Short readValue()
+ {
+ return readPrimitiveValue();
+ }
+
+ public short readPrimitiveValue()
+ {
+ return getDecoder().readRawShort();
+ }
+
+
+ @Override
+ public boolean encodesJavaPrimitive()
+ {
+ return true;
+ }
+
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/SmallFloatingSizePrimitiveTypeEncoding.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/SmallFloatingSizePrimitiveTypeEncoding.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/SmallFloatingSizePrimitiveTypeEncoding.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/SmallFloatingSizePrimitiveTypeEncoding.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,42 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+abstract class SmallFloatingSizePrimitiveTypeEncoding<T> extends FloatingSizePrimitiveTypeEncoding<T>
+{
+
+ SmallFloatingSizePrimitiveTypeEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ public int getSizeBytes()
+ {
+ return 1;
+ }
+
+ @Override
+ protected void writeSize(final T val)
+ {
+ getEncoder().writeRaw((byte)getEncodedValueSize(val));
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/StringType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/StringType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/StringType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/StringType.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,262 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CharsetEncoder;
+import java.util.Arrays;
+import java.util.Collection;
+
+public class StringType extends AbstractPrimitiveType<String>
+{
+ private static final Charset Charset_UTF8 = Charset.forName("UTF-8");
+ private static final DecoderImpl.TypeDecoder<String> _stringCreator =
+ new DecoderImpl.TypeDecoder<String>()
+ {
+
+ public String decode(final ByteBuffer buf)
+ {
+ CharsetDecoder charsetDecoder = Charset_UTF8.newDecoder();
+ try
+ {
+ CharBuffer charBuf = charsetDecoder.decode(buf);
+ return charBuf.toString();
+ }
+ catch (CharacterCodingException e)
+ {
+ throw new IllegalArgumentException("Cannot parse String");
+ }
+
+ }
+ };
+
+
+ public static interface StringEncoding extends PrimitiveTypeEncoding<String>
+ {
+ void setValue(String val, int length);
+ }
+
+ private final StringEncoding _stringEncoding;
+ private final StringEncoding _shortStringEncoding;
+
+ StringType(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _stringEncoding = new AllStringEncoding(encoder, decoder);
+ _shortStringEncoding = new ShortStringEncoding(encoder, decoder);
+ encoder.register(String.class, this);
+ decoder.register(this);
+ }
+
+ public Class<String> getTypeClass()
+ {
+ return String.class;
+ }
+
+ public StringEncoding getEncoding(final String val)
+ {
+ final int length = calculateUTF8Length(val);
+ StringEncoding encoding = length <= 255
+ ? _shortStringEncoding
+ : _stringEncoding;
+ encoding.setValue(val, length);
+ return encoding;
+ }
+
+ private static int calculateUTF8Length(final String s)
+ {
+ int len = s.length();
+ int i = 0;
+ final int length = s.length();
+ while(i < length)
+ {
+ char c = s.charAt(i);
+ if(c > 127)
+ {
+ len++;
+ if(c > 0x07ff)
+ {
+ len++;
+ if(c >= 0xD800 && c <= 0xDBFF)
+ {
+ i++;
+ len++;
+ }
+ }
+ }
+ i++;
+
+ }
+ return len;
+ }
+
+
+ public StringEncoding getCanonicalEncoding()
+ {
+ return _stringEncoding;
+ }
+
+ public Collection<StringEncoding> getAllEncodings()
+ {
+ return Arrays.asList(_shortStringEncoding, _stringEncoding);
+ }
+
+ private class AllStringEncoding
+ extends LargeFloatingSizePrimitiveTypeEncoding<String>
+ implements StringEncoding
+ {
+
+ private String _value;
+ private int _length;
+
+
+ public AllStringEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected void writeEncodedValue(final String val)
+ {
+ final CharsetEncoder charsetEncoder = Charset_UTF8.newEncoder();
+ getEncoder().writeRaw(new StringWritable(charsetEncoder, val));
+ }
+
+ @Override
+ protected int getEncodedValueSize(final String val)
+ {
+ return (val == _value) ? _length : calculateUTF8Length(val);
+ }
+
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.STR32;
+ }
+
+ public StringType getType()
+ {
+ return StringType.this;
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<String> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public String readValue()
+ {
+
+ DecoderImpl decoder = getDecoder();
+ int size = decoder.readRawInt();
+ return decoder.readRaw(_stringCreator, size);
+ }
+
+ public void setValue(final String val, final int length)
+ {
+ _value = val;
+ _length = length;
+ }
+
+ }
+
+ private class ShortStringEncoding
+ extends SmallFloatingSizePrimitiveTypeEncoding<String>
+ implements StringEncoding
+ {
+
+ private String _value;
+ private int _length;
+
+ public ShortStringEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+
+ @Override
+ protected void writeEncodedValue(final String val)
+ {
+ final CharsetEncoder charsetEncoder = Charset_UTF8.newEncoder();
+ getEncoder().writeRaw(new StringWritable(charsetEncoder, val));
+ }
+
+ @Override
+ protected int getEncodedValueSize(final String val)
+ {
+ return (val == _value) ? _length : calculateUTF8Length(val);
+ }
+
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.STR8;
+ }
+
+ public StringType getType()
+ {
+ return StringType.this;
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<String> encoder)
+ {
+ return encoder == this;
+ }
+
+ public String readValue()
+ {
+
+ DecoderImpl decoder = getDecoder();
+ int size = ((int)decoder.readRawByte()) & 0xff;
+ return decoder.readRaw(_stringCreator, size);
+ }
+
+ public void setValue(final String val, final int length)
+ {
+ _value = val;
+ _length = length;
+ }
+ }
+
+
+ private static class StringWritable implements EncoderImpl.Writable
+ {
+
+ private final CharsetEncoder _charsetEncoder;
+ private final String _val;
+
+ public StringWritable(final CharsetEncoder charsetEncoder, final String val)
+ {
+ _charsetEncoder = charsetEncoder;
+ _val = val;
+ }
+
+ public void writeTo(final ByteBuffer buf)
+ {
+ _charsetEncoder.encode(CharBuffer.wrap(_val), buf, true);
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/SymbolType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/SymbolType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/SymbolType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/SymbolType.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,201 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+import org.apache.qpid.proton.type.Symbol;
+
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+public class SymbolType extends AbstractPrimitiveType<Symbol>
+{
+ private static final Charset ASCII_CHARSET = Charset.forName("US-ASCII");
+ private final SymbolEncoding _symbolEncoding;
+ private final SymbolEncoding _shortSymbolEncoding;
+
+ private final Map<ByteBuffer, Symbol> _symbolCache = new HashMap<ByteBuffer, Symbol>();
+ private DecoderImpl.TypeDecoder<Symbol> _symbolCreator =
+ new DecoderImpl.TypeDecoder<Symbol>()
+ {
+
+ public Symbol decode(final ByteBuffer buf)
+ {
+
+ Symbol symbol = _symbolCache.get(buf);
+ if(symbol == null)
+ {
+ byte[] bytes = new byte[buf.limit()];
+ buf.get(bytes);
+
+ String str = new String(bytes, ASCII_CHARSET);
+ symbol = Symbol.getSymbol(str);
+
+ _symbolCache.put(ByteBuffer.wrap(bytes), symbol);
+ }
+ return symbol;
+ }
+ };
+
+ public static interface SymbolEncoding extends PrimitiveTypeEncoding<Symbol>
+ {
+
+ }
+
+ SymbolType(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _symbolEncoding = new LongSymbolEncoding(encoder, decoder);
+ _shortSymbolEncoding = new ShortSymbolEncoding(encoder, decoder);
+ encoder.register(Symbol.class, this);
+ decoder.register(this);
+ }
+
+ public Class<Symbol> getTypeClass()
+ {
+ return Symbol.class;
+ }
+
+ public SymbolEncoding getEncoding(final Symbol val)
+ {
+ return val.length() <= 255 ? _shortSymbolEncoding : _symbolEncoding;
+ }
+
+
+ public SymbolEncoding getCanonicalEncoding()
+ {
+ return _symbolEncoding;
+ }
+
+ public Collection<SymbolEncoding> getAllEncodings()
+ {
+ return Arrays.asList(_shortSymbolEncoding, _symbolEncoding);
+ }
+
+ private class LongSymbolEncoding
+ extends LargeFloatingSizePrimitiveTypeEncoding<Symbol>
+ implements SymbolEncoding
+ {
+
+ public LongSymbolEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected void writeEncodedValue(final Symbol val)
+ {
+ final int length = val.length();
+ final EncoderImpl encoder = getEncoder();
+
+ for(int i = 0; i < length; i++)
+ {
+ encoder.writeRaw((byte)val.charAt(i));
+ }
+ }
+
+ @Override
+ protected int getEncodedValueSize(final Symbol val)
+ {
+ return val.length();
+ }
+
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.SYM32;
+ }
+
+ public SymbolType getType()
+ {
+ return SymbolType.this;
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Symbol> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public Symbol readValue()
+ {
+ DecoderImpl decoder = getDecoder();
+ int size = decoder.readRawInt();
+ return decoder.readRaw(_symbolCreator, size);
+ }
+ }
+
+ private class ShortSymbolEncoding
+ extends SmallFloatingSizePrimitiveTypeEncoding<Symbol>
+ implements SymbolEncoding
+ {
+
+ public ShortSymbolEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected void writeEncodedValue(final Symbol val)
+ {
+
+ final int length = val.length();
+ final EncoderImpl encoder = getEncoder();
+
+ for(int i = 0; i < length; i++)
+ {
+ encoder.writeRaw((byte)val.charAt(i));
+ }
+ }
+
+ @Override
+ protected int getEncodedValueSize(final Symbol val)
+ {
+ return val.length();
+ }
+
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.SYM8;
+ }
+
+ public SymbolType getType()
+ {
+ return SymbolType.this;
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Symbol> encoder)
+ {
+ return encoder == this;
+ }
+
+ public Symbol readValue()
+ {
+ DecoderImpl decoder = getDecoder();
+ int size = ((int)decoder.readRawByte()) & 0xff;
+ return decoder.readRaw(_symbolCreator, size);
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/TimestampType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/TimestampType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/TimestampType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/TimestampType.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,111 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+
+public class TimestampType extends AbstractPrimitiveType<Date>
+{
+ private TimestampEncoding _timestampEncoding;
+
+ TimestampType(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _timestampEncoding = new TimestampEncoding(encoder, decoder);
+ encoder.register(Date.class, this);
+ decoder.register(this);
+ }
+
+ public Class<Date> getTypeClass()
+ {
+ return Date.class;
+ }
+
+ public TimestampEncoding getEncoding(final Date val)
+ {
+ return _timestampEncoding;
+ }
+
+
+ public TimestampEncoding getCanonicalEncoding()
+ {
+ return _timestampEncoding;
+ }
+
+ public Collection<TimestampEncoding> getAllEncodings()
+ {
+ return Collections.singleton(_timestampEncoding);
+ }
+
+ public void write(long l)
+ {
+ _timestampEncoding.write(l);
+ }
+
+ private class TimestampEncoding extends FixedSizePrimitiveTypeEncoding<Date>
+ {
+
+ public TimestampEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 8;
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.TIMESTAMP;
+ }
+
+ public TimestampType getType()
+ {
+ return TimestampType.this;
+ }
+
+ public void writeValue(final Date val)
+ {
+ getEncoder().writeRaw(val.getTime());
+ }
+
+ public void write(final long l)
+ {
+ writeConstructor();
+ getEncoder().writeRaw(l);
+
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Date> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public Date readValue()
+ {
+ return new Date(getDecoder().readRawLong());
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/TypeConstructor.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/TypeConstructor.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/TypeConstructor.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/TypeConstructor.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,30 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+public interface TypeConstructor<V>
+{
+ V readValue();
+
+ boolean encodesJavaPrimitive();
+
+ Class<V> getTypeClass();
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/TypeEncoding.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/TypeEncoding.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/TypeEncoding.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/TypeEncoding.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,40 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+public interface TypeEncoding<V> extends TypeConstructor<V>
+{
+ AMQPType<V> getType();
+
+ void writeConstructor();
+
+ int getConstructorSize();
+
+ void writeValue(V val);
+
+ int getValueSize(V val);
+
+ boolean isFixedSizeVal();
+
+ boolean encodesSuperset(TypeEncoding<V> encoder);
+
+ boolean encodesJavaPrimitive();
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/UUIDType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/UUIDType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/UUIDType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/UUIDType.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,103 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.UUID;
+
+public class UUIDType extends AbstractPrimitiveType<UUID>
+{
+ private UUIDEncoding _uuidEncoding;
+
+ UUIDType(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _uuidEncoding = new UUIDEncoding(encoder, decoder);
+ encoder.register(UUID.class, this);
+ decoder.register(this);
+ }
+
+ public Class<UUID> getTypeClass()
+ {
+ return UUID.class;
+ }
+
+ public UUIDEncoding getEncoding(final UUID val)
+ {
+ return _uuidEncoding;
+ }
+
+
+ public UUIDEncoding getCanonicalEncoding()
+ {
+ return _uuidEncoding;
+ }
+
+ public Collection<UUIDEncoding> getAllEncodings()
+ {
+ return Collections.singleton(_uuidEncoding);
+ }
+
+ private class UUIDEncoding extends FixedSizePrimitiveTypeEncoding<UUID>
+ {
+
+ public UUIDEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 16;
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.UUID;
+ }
+
+ public UUIDType getType()
+ {
+ return UUIDType.this;
+ }
+
+ public void writeValue(final UUID val)
+ {
+ getEncoder().writeRaw(val.getMostSignificantBits());
+ getEncoder().writeRaw(val.getLeastSignificantBits());
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<UUID> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public UUID readValue()
+ {
+ long msb = getDecoder().readRawLong();
+ long lsb = getDecoder().readRawLong();
+
+ return new UUID(msb, lsb);
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/UnsignedByteType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/UnsignedByteType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/UnsignedByteType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/UnsignedByteType.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,100 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+import org.apache.qpid.proton.type.UnsignedByte;
+
+import java.util.Collection;
+import java.util.Collections;
+
+public class UnsignedByteType extends AbstractPrimitiveType<UnsignedByte>
+{
+ private UnsignedByteEncoding _unsignedByteEncoding;
+
+ UnsignedByteType(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _unsignedByteEncoding = new UnsignedByteEncoding(encoder, decoder);
+ encoder.register(UnsignedByte.class, this);
+ decoder.register(this);
+ }
+
+ public Class<UnsignedByte> getTypeClass()
+ {
+ return UnsignedByte.class;
+ }
+
+ public UnsignedByteEncoding getEncoding(final UnsignedByte val)
+ {
+ return _unsignedByteEncoding;
+ }
+
+
+ public UnsignedByteEncoding getCanonicalEncoding()
+ {
+ return _unsignedByteEncoding;
+ }
+
+ public Collection<UnsignedByteEncoding> getAllEncodings()
+ {
+ return Collections.singleton(_unsignedByteEncoding);
+ }
+
+ public class UnsignedByteEncoding extends FixedSizePrimitiveTypeEncoding<UnsignedByte>
+ {
+
+ public UnsignedByteEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 1;
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.UBYTE;
+ }
+
+ public UnsignedByteType getType()
+ {
+ return UnsignedByteType.this;
+ }
+
+ public void writeValue(final UnsignedByte val)
+ {
+ getEncoder().writeRaw(val.byteValue());
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<UnsignedByte> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public UnsignedByte readValue()
+ {
+ return UnsignedByte.valueOf(getDecoder().readRawByte());
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/UnsignedIntegerType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/UnsignedIntegerType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/UnsignedIntegerType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/UnsignedIntegerType.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,209 @@
+/*
+ *
+ * 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.qpid.proton.codec;
+
+import org.apache.qpid.proton.type.UnsignedInteger;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+public class UnsignedIntegerType extends AbstractPrimitiveType<UnsignedInteger>
+{
+ public static interface UnsignedIntegerEncoding extends PrimitiveTypeEncoding<UnsignedInteger>
+ {
+
+ }
+
+ private UnsignedIntegerEncoding _unsignedIntegerEncoding;
+ private UnsignedIntegerEncoding _smallUnsignedIntegerEncoding;
+ private UnsignedIntegerEncoding _zeroUnsignedIntegerEncoding;
+
+
+ UnsignedIntegerType(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _unsignedIntegerEncoding = new AllUnsignedIntegerEncoding(encoder, decoder);
+ _smallUnsignedIntegerEncoding = new SmallUnsignedIntegerEncoding(encoder, decoder);
+ _zeroUnsignedIntegerEncoding = new ZeroUnsignedIntegerEncoding(encoder, decoder);
+ encoder.register(UnsignedInteger.class, this);
+ decoder.register(this);
+ }
+
+ public Class<UnsignedInteger> getTypeClass()
+ {
+ return UnsignedInteger.class;
+ }
+
+ public UnsignedIntegerEncoding getEncoding(final UnsignedInteger val)
+ {
+ int i = val.intValue();
+ return i == 0
+ ? _zeroUnsignedIntegerEncoding
+ : (i <= 255) ? _smallUnsignedIntegerEncoding : _unsignedIntegerEncoding;
+ }
+
+
+ public UnsignedIntegerEncoding getCanonicalEncoding()
+ {
+ return _unsignedIntegerEncoding;
+ }
+
+ public Collection<UnsignedIntegerEncoding> getAllEncodings()
+ {
+ return Arrays.asList(_unsignedIntegerEncoding, _smallUnsignedIntegerEncoding, _zeroUnsignedIntegerEncoding);
+ }
+
+
+ private class AllUnsignedIntegerEncoding
+ extends FixedSizePrimitiveTypeEncoding<UnsignedInteger>
+ implements UnsignedIntegerEncoding
+ {
+
+ public AllUnsignedIntegerEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 4;
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.UINT;
+ }
+
+ public UnsignedIntegerType getType()
+ {
+ return UnsignedIntegerType.this;
+ }
+
+ public void writeValue(final UnsignedInteger val)
+ {
+ getEncoder().writeRaw(val.intValue());
+ }
+
+ public void write(final int i)
+ {
+ writeConstructor();
+ getEncoder().writeRaw(i);
+
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<UnsignedInteger> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public UnsignedInteger readValue()
+ {
+ return UnsignedInteger.valueOf(getDecoder().readRawInt());
+ }
+ }
+
+ private class SmallUnsignedIntegerEncoding
+ extends FixedSizePrimitiveTypeEncoding<UnsignedInteger>
+ implements UnsignedIntegerEncoding
+ {
+ public SmallUnsignedIntegerEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.SMALLUINT;
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 1;
+ }
+
+
+ public UnsignedIntegerType getType()
+ {
+ return UnsignedIntegerType.this;
+ }
+
+ public void writeValue(final UnsignedInteger val)
+ {
+ getEncoder().writeRaw((byte)val.intValue());
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<UnsignedInteger> encoder)
+ {
+ return encoder == this || encoder instanceof ZeroUnsignedIntegerEncoding;
+ }
+
+ public UnsignedInteger readValue()
+ {
+ return UnsignedInteger.valueOf(((int)getDecoder().readRawByte()) & 0xff);
+ }
+ }
+
+
+ private class ZeroUnsignedIntegerEncoding
+ extends FixedSizePrimitiveTypeEncoding<UnsignedInteger>
+ implements UnsignedIntegerEncoding
+ {
+ public ZeroUnsignedIntegerEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.UINT0;
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 0;
+ }
+
+
+ public UnsignedIntegerType getType()
+ {
+ return UnsignedIntegerType.this;
+ }
+
+ public void writeValue(final UnsignedInteger val)
+ {
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<UnsignedInteger> encoder)
+ {
+ return encoder == this;
+ }
+
+ public UnsignedInteger readValue()
+ {
+ return UnsignedInteger.ZERO;
+ }
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org