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 [2/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/CharacterType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/CharacterType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/CharacterType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/CharacterType.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 CharacterType extends AbstractPrimitiveType<Character>
+{
+ private CharacterEncoding _characterEncoding;
+
+ CharacterType(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _characterEncoding = new CharacterEncoding(encoder, decoder);
+ encoder.register(Character.class, this);
+ decoder.register(this);
+ }
+
+ public Class<Character> getTypeClass()
+ {
+ return Character.class;
+ }
+
+ public CharacterEncoding getEncoding(final Character val)
+ {
+ return _characterEncoding;
+ }
+
+
+ public CharacterEncoding getCanonicalEncoding()
+ {
+ return _characterEncoding;
+ }
+
+ public Collection<CharacterEncoding> getAllEncodings()
+ {
+ return Collections.singleton(_characterEncoding);
+ }
+
+ public void write(char c)
+ {
+ _characterEncoding.write(c);
+ }
+
+ public class CharacterEncoding extends FixedSizePrimitiveTypeEncoding<Character>
+ {
+
+ public CharacterEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 4;
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.CHAR;
+ }
+
+ public CharacterType getType()
+ {
+ return CharacterType.this;
+ }
+
+ public void writeValue(final Character val)
+ {
+ getEncoder().writeRaw((int)val.charValue() & 0xffff);
+ }
+
+ public void writeValue(final char val)
+ {
+ getEncoder().writeRaw((int)val & 0xffff);
+ }
+
+ public void write(final char c)
+ {
+ writeConstructor();
+ getEncoder().writeRaw((int)c & 0xffff);
+
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Character> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public Character readValue()
+ {
+ return readPrimitiveValue();
+ }
+
+ public char readPrimitiveValue()
+ {
+ return (char) (getDecoder().readRawInt() & 0xffff);
+ }
+
+
+ @Override
+ public boolean encodesJavaPrimitive()
+ {
+ return true;
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decimal128Type.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decimal128Type.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decimal128Type.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decimal128Type.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 org.apache.qpid.proton.type.Decimal128;
+
+import java.util.Collection;
+import java.util.Collections;
+
+public class Decimal128Type extends AbstractPrimitiveType<Decimal128>
+{
+ private Decimal128Encoding _decimal128Encoder;
+
+ Decimal128Type(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _decimal128Encoder = new Decimal128Encoding(encoder, decoder);
+ encoder.register(Decimal128.class, this);
+ decoder.register(this);
+ }
+
+ public Class<Decimal128> getTypeClass()
+ {
+ return Decimal128.class;
+ }
+
+ public Decimal128Encoding getEncoding(final Decimal128 val)
+ {
+ return _decimal128Encoder;
+ }
+
+
+ public Decimal128Encoding getCanonicalEncoding()
+ {
+ return _decimal128Encoder;
+ }
+
+ public Collection<Decimal128Encoding> getAllEncodings()
+ {
+ return Collections.singleton(_decimal128Encoder);
+ }
+
+ private class Decimal128Encoding extends FixedSizePrimitiveTypeEncoding<Decimal128>
+ {
+
+ public Decimal128Encoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 16;
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.DECIMAL128;
+ }
+
+ public Decimal128Type getType()
+ {
+ return Decimal128Type.this;
+ }
+
+ public void writeValue(final Decimal128 val)
+ {
+ getEncoder().writeRaw(val.getMostSignificantBits());
+ getEncoder().writeRaw(val.getLeastSignificantBits());
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Decimal128> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public Decimal128 readValue()
+ {
+ long msb = getDecoder().readRawLong();
+ long lsb = getDecoder().readRawLong();
+ return new Decimal128(msb, lsb);
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decimal32Type.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decimal32Type.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decimal32Type.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decimal32Type.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.Decimal32;
+
+import java.util.Collection;
+import java.util.Collections;
+
+public class Decimal32Type extends AbstractPrimitiveType<Decimal32>
+{
+ private Decimal32Encoding _decimal32Encoder;
+
+ Decimal32Type(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _decimal32Encoder = new Decimal32Encoding(encoder, decoder);
+ encoder.register(Decimal32.class, this);
+ decoder.register(this);
+ }
+
+ public Class<Decimal32> getTypeClass()
+ {
+ return Decimal32.class;
+ }
+
+ public Decimal32Encoding getEncoding(final Decimal32 val)
+ {
+ return _decimal32Encoder;
+ }
+
+
+ public Decimal32Encoding getCanonicalEncoding()
+ {
+ return _decimal32Encoder;
+ }
+
+ public Collection<Decimal32Encoding> getAllEncodings()
+ {
+ return Collections.singleton(_decimal32Encoder);
+ }
+
+ private class Decimal32Encoding extends FixedSizePrimitiveTypeEncoding<Decimal32>
+ {
+
+ public Decimal32Encoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 4;
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.DECIMAL32;
+ }
+
+ public Decimal32Type getType()
+ {
+ return Decimal32Type.this;
+ }
+
+ public void writeValue(final Decimal32 val)
+ {
+ getEncoder().writeRaw(val.getBits());
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Decimal32> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public Decimal32 readValue()
+ {
+ return new Decimal32(getDecoder().readRawInt());
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decimal64Type.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decimal64Type.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decimal64Type.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decimal64Type.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.Decimal64;
+
+import java.util.Collection;
+import java.util.Collections;
+
+public class Decimal64Type extends AbstractPrimitiveType<Decimal64>
+{
+ private Decimal64Encoding _decimal64Encoder;
+
+ Decimal64Type(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _decimal64Encoder = new Decimal64Encoding(encoder, decoder);
+ encoder.register(Decimal64.class, this);
+ decoder.register(this);
+ }
+
+ public Class<Decimal64> getTypeClass()
+ {
+ return Decimal64.class;
+ }
+
+ public Decimal64Encoding getEncoding(final Decimal64 val)
+ {
+ return _decimal64Encoder;
+ }
+
+
+ public Decimal64Encoding getCanonicalEncoding()
+ {
+ return _decimal64Encoder;
+ }
+
+ public Collection<Decimal64Encoding> getAllEncodings()
+ {
+ return Collections.singleton(_decimal64Encoder);
+ }
+
+ private class Decimal64Encoding extends FixedSizePrimitiveTypeEncoding<Decimal64>
+ {
+
+ public Decimal64Encoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 8;
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.DECIMAL64;
+ }
+
+ public Decimal64Type getType()
+ {
+ return Decimal64Type.this;
+ }
+
+ public void writeValue(final Decimal64 val)
+ {
+ getEncoder().writeRaw(val.getBits());
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Decimal64> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public Decimal64 readValue()
+ {
+ return new Decimal64(getDecoder().readRawLong());
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DecodeException.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DecodeException.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DecodeException.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DecodeException.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,44 @@
+/*
+ *
+ * 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 class DecodeException extends RuntimeException
+{
+ public DecodeException()
+ {
+ }
+
+ public DecodeException(String message)
+ {
+ super(message);
+ }
+
+ public DecodeException(String message, Throwable cause)
+ {
+ super(message, cause);
+ }
+
+ public DecodeException(Throwable cause)
+ {
+ super(cause);
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decoder.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decoder.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decoder.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Decoder.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,141 @@
+/*
+ *
+ * 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.*;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+public interface Decoder
+{
+ public static interface ListProcessor<T>
+ {
+ T process(int count, Encoder encoder);
+ }
+
+
+ Boolean readBoolean();
+ Boolean readBoolean(Boolean defaultVal);
+ boolean readBoolean(boolean defaultVal);
+
+ Byte readByte();
+ Byte readByte(Byte defaultVal);
+ byte readByte(byte defaultVal);
+
+ Short readShort();
+ Short readShort(Short defaultVal);
+ short readShort(short defaultVal);
+
+ Integer readInteger();
+ Integer readInteger(Integer defaultVal);
+ int readInteger(int defaultVal);
+
+ Long readLong();
+ Long readLong(Long defaultVal);
+ long readLong(long defaultVal);
+
+ UnsignedByte readUnsignedByte();
+ UnsignedByte readUnsignedByte(UnsignedByte defaultVal);
+
+ UnsignedShort readUnsignedShort();
+ UnsignedShort readUnsignedShort(UnsignedShort defaultVal);
+
+ UnsignedInteger readUnsignedInteger();
+ UnsignedInteger readUnsignedInteger(UnsignedInteger defaultVal);
+
+ UnsignedLong readUnsignedLong();
+ UnsignedLong readUnsignedLong(UnsignedLong defaultVal);
+
+ Character readCharacter();
+ Character readCharacter(Character defaultVal);
+ char readCharacter(char defaultVal);
+
+ Float readFloat();
+ Float readFloat(Float defaultVal);
+ float readFloat(float defaultVal);
+
+ Double readDouble();
+ Double readDouble(Double defaultVal);
+ double readDouble(double defaultVal);
+
+ UUID readUUID();
+ UUID readUUID(UUID defaultValue);
+
+ Decimal32 readDecimal32();
+ Decimal32 readDecimal32(Decimal32 defaultValue);
+
+ Decimal64 readDecimal64();
+ Decimal64 readDecimal64(Decimal64 defaultValue);
+
+ Decimal128 readDecimal128();
+ Decimal128 readDecimal128(Decimal128 defaultValue);
+
+ Date readTimestamp();
+ Date readTimestamp(Date defaultValue);
+
+ Binary readBinary();
+ Binary readBinary(Binary defaultValue);
+
+ Symbol readSymbol();
+ Symbol readSymbol(Symbol defaultValue);
+
+ String readString();
+ String readString(String defaultValue);
+
+ List readList();
+ <T> void readList(ListProcessor<T> processor);
+
+ Map readMap();
+
+ <T> T[] readArray(Class<T> clazz);
+
+ Object[] readArray();
+
+ boolean[] readBooleanArray();
+ byte[] readByteArray();
+ short[] readShortArray();
+ int[] readIntegerArray();
+ long[] readLongArray();
+ float[] readFloatArray();
+ double[] readDoubleArray();
+ char[] readCharacterArray();
+
+ <T> T[] readMultiple(Class<T> clazz);
+
+ Object[] readMultiple();
+ byte[] readByteMultiple();
+ short[] readShortMultiple();
+ int[] readIntegerMultiple();
+ long[] readLongMultiple();
+ float[] readFloatMultiple();
+ double[] readDoubleMultiple();
+ char[] readCharacterMultiple();
+
+ Object readObject();
+ Object readObject(Object defaultValue);
+
+ void register(final Object descriptor, final DescribedTypeConstructor dtc);
+
+
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DecoderImpl.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DecoderImpl.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DecoderImpl.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DecoderImpl.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,972 @@
+/*
+ *
+ * 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.*;
+
+import java.lang.reflect.Array;
+import java.nio.ByteBuffer;
+import java.util.*;
+
+public class DecoderImpl implements ByteBufferDecoder
+{
+
+ private ByteBuffer _buffer;
+ private PrimitiveTypeEncoding[] _constructors = new PrimitiveTypeEncoding[256];
+ private Map<Object, DescribedTypeConstructor> _dynamicTypeConstructors =
+ new HashMap<Object, DescribedTypeConstructor>();
+
+
+ public DecoderImpl()
+ {
+ }
+
+
+ DecoderImpl(final ByteBuffer buffer)
+ {
+ _buffer = buffer;
+ }
+
+ TypeConstructor readConstructor()
+ {
+ int code = ((int)readRawByte()) & 0xff;
+ if(code == EncodingCodes.DESCRIBED_TYPE_INDICATOR)
+ {
+ final Object descriptor = readObject();
+ TypeConstructor nestedEncoding = readConstructor();
+ DescribedTypeConstructor dtc = _dynamicTypeConstructors.get(descriptor);
+ if(dtc == null)
+ {
+ dtc = new DescribedTypeConstructor()
+ {
+
+ public DescribedType newInstance(final Object described)
+ {
+ return new UnknownDescribedType(descriptor, described);
+ }
+
+ public Class getTypeClass()
+ {
+ return UnknownDescribedType.class;
+ }
+ };
+ register(descriptor, dtc);
+ }
+ return new DynamicTypeConstructor(dtc, nestedEncoding);
+ }
+ else
+ {
+ return _constructors[code];
+ }
+ }
+
+ public void register(final Object descriptor, final DescribedTypeConstructor dtc)
+ {
+ _dynamicTypeConstructors.put(descriptor, dtc);
+ }
+
+ private ClassCastException unexpectedType(final Object val, Class clazz)
+ {
+ return new ClassCastException("Unexpected type "
+ + val.getClass().getName()
+ + ". Expected "
+ + clazz.getName() +".");
+ }
+
+
+ public Boolean readBoolean()
+ {
+ return readBoolean(null);
+ }
+
+ public Boolean readBoolean(final Boolean defaultVal)
+ {
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else if(val instanceof Boolean)
+ {
+ return (Boolean) val;
+ }
+ throw unexpectedType(val, Boolean.class);
+ }
+
+ public boolean readBoolean(final boolean defaultVal)
+ {
+ TypeConstructor constructor = readConstructor();
+ if(constructor instanceof BooleanType.BooleanEncoding)
+ {
+ return ((BooleanType.BooleanEncoding)constructor).readPrimitiveValue();
+ }
+ else
+ {
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else
+ {
+ throw unexpectedType(val, Boolean.class);
+ }
+ }
+ }
+
+ public Byte readByte()
+ {
+ return readByte(null);
+ }
+
+ public Byte readByte(final Byte defaultVal)
+ {
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else if(val instanceof Byte)
+ {
+ return (Byte) val;
+ }
+ throw unexpectedType(val, Byte.class);
+ }
+
+ public byte readByte(final byte defaultVal)
+ {
+ TypeConstructor constructor = readConstructor();
+ if(constructor instanceof ByteType.ByteEncoding)
+ {
+ return ((ByteType.ByteEncoding)constructor).readPrimitiveValue();
+ }
+ else
+ {
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else
+ {
+ throw unexpectedType(val, Byte.class);
+ }
+ }
+ }
+
+ public Short readShort()
+ {
+ return readShort(null);
+ }
+
+ public Short readShort(final Short defaultVal)
+ {
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else if(val instanceof Short)
+ {
+ return (Short) val;
+ }
+ throw unexpectedType(val, Short.class);
+
+ }
+
+ public short readShort(final short defaultVal)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ if(constructor instanceof ShortType.ShortEncoding)
+ {
+ return ((ShortType.ShortEncoding)constructor).readPrimitiveValue();
+ }
+ else
+ {
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else
+ {
+ throw unexpectedType(val, Short.class);
+ }
+ }
+ }
+
+ public Integer readInteger()
+ {
+ return readInteger(null);
+ }
+
+ public Integer readInteger(final Integer defaultVal)
+ {
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else if(val instanceof Integer)
+ {
+ return (Integer) val;
+ }
+ throw unexpectedType(val, Integer.class);
+
+ }
+
+ public int readInteger(final int defaultVal)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ if(constructor instanceof IntegerType.IntegerEncoding)
+ {
+ return ((IntegerType.IntegerEncoding)constructor).readPrimitiveValue();
+ }
+ else
+ {
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else
+ {
+ throw unexpectedType(val, Integer.class);
+ }
+ }
+ }
+
+ public Long readLong()
+ {
+ return readLong(null);
+ }
+
+ public Long readLong(final Long defaultVal)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else if(val instanceof Long)
+ {
+ return (Long) val;
+ }
+ throw unexpectedType(val, Long.class);
+
+ }
+
+ public long readLong(final long defaultVal)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ if(constructor instanceof LongType.LongEncoding)
+ {
+ return ((LongType.LongEncoding)constructor).readPrimitiveValue();
+ }
+ else
+ {
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else
+ {
+ throw unexpectedType(val, Long.class);
+ }
+ }
+ }
+
+ public UnsignedByte readUnsignedByte()
+ {
+ return readUnsignedByte(null);
+ }
+
+ public UnsignedByte readUnsignedByte(final UnsignedByte defaultVal)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else if(val instanceof UnsignedByte)
+ {
+ return (UnsignedByte) val;
+ }
+ throw unexpectedType(val, UnsignedByte.class);
+
+ }
+
+ public UnsignedShort readUnsignedShort()
+ {
+ return readUnsignedShort(null);
+ }
+
+ public UnsignedShort readUnsignedShort(final UnsignedShort defaultVal)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else if(val instanceof UnsignedShort)
+ {
+ return (UnsignedShort) val;
+ }
+ throw unexpectedType(val, UnsignedShort.class);
+
+ }
+
+ public UnsignedInteger readUnsignedInteger()
+ {
+ return readUnsignedInteger(null);
+ }
+
+ public UnsignedInteger readUnsignedInteger(final UnsignedInteger defaultVal)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else if(val instanceof UnsignedInteger)
+ {
+ return (UnsignedInteger) val;
+ }
+ throw unexpectedType(val, UnsignedInteger.class);
+
+ }
+
+ public UnsignedLong readUnsignedLong()
+ {
+ return readUnsignedLong(null);
+ }
+
+ public UnsignedLong readUnsignedLong(final UnsignedLong defaultVal)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else if(val instanceof UnsignedLong)
+ {
+ return (UnsignedLong) val;
+ }
+ throw unexpectedType(val, UnsignedLong.class);
+
+ }
+
+ public Character readCharacter()
+ {
+ return readCharacter(null);
+ }
+
+ public Character readCharacter(final Character defaultVal)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else if(val instanceof Character)
+ {
+ return (Character) val;
+ }
+ throw unexpectedType(val, Character.class);
+
+ }
+
+ public char readCharacter(final char defaultVal)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ if(constructor instanceof CharacterType.CharacterEncoding)
+ {
+ return ((CharacterType.CharacterEncoding)constructor).readPrimitiveValue();
+ }
+ else
+ {
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else
+ {
+ throw unexpectedType(val, Character.class);
+ }
+ }
+ }
+
+ public Float readFloat()
+ {
+ return readFloat(null);
+ }
+
+ public Float readFloat(final Float defaultVal)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else if(val instanceof Float)
+ {
+ return (Float) val;
+ }
+ throw unexpectedType(val, Float.class);
+
+ }
+
+ public float readFloat(final float defaultVal)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ if(constructor instanceof FloatType.FloatEncoding)
+ {
+ return ((FloatType.FloatEncoding)constructor).readPrimitiveValue();
+ }
+ else
+ {
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else
+ {
+ throw unexpectedType(val, Float.class);
+ }
+ }
+ }
+
+ public Double readDouble()
+ {
+ return readDouble(null);
+ }
+
+ public Double readDouble(final Double defaultVal)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else if(val instanceof Double)
+ {
+ return (Double) val;
+ }
+ throw unexpectedType(val, Double.class);
+
+ }
+
+ public double readDouble(final double defaultVal)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ if(constructor instanceof DoubleType.DoubleEncoding)
+ {
+ return ((DoubleType.DoubleEncoding)constructor).readPrimitiveValue();
+ }
+ else
+ {
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else
+ {
+ throw unexpectedType(val, Double.class);
+ }
+ }
+ }
+
+ public UUID readUUID()
+ {
+ return readUUID(null);
+ }
+
+ public UUID readUUID(final UUID defaultVal)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultVal;
+ }
+ else if(val instanceof UUID)
+ {
+ return (UUID) val;
+ }
+ throw unexpectedType(val, UUID.class);
+
+ }
+
+ public Decimal32 readDecimal32()
+ {
+ return readDecimal32(null);
+ }
+
+ public Decimal32 readDecimal32(final Decimal32 defaultValue)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultValue;
+ }
+ else if(val instanceof Decimal32)
+ {
+ return (Decimal32) val;
+ }
+ throw unexpectedType(val, Decimal32.class);
+
+ }
+
+ public Decimal64 readDecimal64()
+ {
+ return readDecimal64(null);
+ }
+
+ public Decimal64 readDecimal64(final Decimal64 defaultValue)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultValue;
+ }
+ else if(val instanceof Decimal64)
+ {
+ return (Decimal64) val;
+ }
+ throw unexpectedType(val, Decimal64.class);
+ }
+
+ public Decimal128 readDecimal128()
+ {
+ return readDecimal128(null);
+ }
+
+ public Decimal128 readDecimal128(final Decimal128 defaultValue)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultValue;
+ }
+ else if(val instanceof Decimal128)
+ {
+ return (Decimal128) val;
+ }
+ throw unexpectedType(val, Decimal128.class);
+ }
+
+ public Date readTimestamp()
+ {
+ return readTimestamp(null);
+ }
+
+ public Date readTimestamp(final Date defaultValue)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultValue;
+ }
+ else if(val instanceof Date)
+ {
+ return (Date) val;
+ }
+ throw unexpectedType(val, Date.class);
+ }
+
+ public Binary readBinary()
+ {
+ return readBinary(null);
+ }
+
+ public Binary readBinary(final Binary defaultValue)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultValue;
+ }
+ else if(val instanceof Binary)
+ {
+ return (Binary) val;
+ }
+ throw unexpectedType(val, Binary.class);
+ }
+
+ public Symbol readSymbol()
+ {
+ return readSymbol(null);
+ }
+
+ public Symbol readSymbol(final Symbol defaultValue)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultValue;
+ }
+ else if(val instanceof Symbol)
+ {
+ return (Symbol) val;
+ }
+ throw unexpectedType(val, Symbol.class);
+ }
+
+ public String readString()
+ {
+ return readString(null);
+ }
+
+ public String readString(final String defaultValue)
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return defaultValue;
+ }
+ else if(val instanceof String)
+ {
+ return (String) val;
+ }
+ throw unexpectedType(val, String.class);
+ }
+
+ public List readList()
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return null;
+ }
+ else if(val instanceof List)
+ {
+ return (List) val;
+ }
+ throw unexpectedType(val, List.class);
+ }
+
+ public <T> void readList(final ListProcessor<T> processor)
+ {
+ //TODO.
+ }
+
+ public Map readMap()
+ {
+
+ TypeConstructor constructor = readConstructor();
+ Object val = constructor.readValue();
+ if(val == null)
+ {
+ return null;
+ }
+ else if(val instanceof Map)
+ {
+ return (Map) val;
+ }
+ throw unexpectedType(val, Map.class);
+ }
+
+ public <T> T[] readArray(final Class<T> clazz)
+ {
+ return null; //TODO.
+ }
+
+ public Object[] readArray()
+ {
+ return (Object[]) readConstructor().readValue();
+
+ }
+
+ public boolean[] readBooleanArray()
+ {
+ return (boolean[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
+ }
+
+ public byte[] readByteArray()
+ {
+ return (byte[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
+ }
+
+ public short[] readShortArray()
+ {
+ return (short[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
+ }
+
+ public int[] readIntegerArray()
+ {
+ return (int[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
+ }
+
+ public long[] readLongArray()
+ {
+ return (long[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
+ }
+
+ public float[] readFloatArray()
+ {
+ return (float[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
+ }
+
+ public double[] readDoubleArray()
+ {
+ return (double[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
+ }
+
+ public char[] readCharacterArray()
+ {
+ return (char[]) ((ArrayType.ArrayEncoding)readConstructor()).readValueArray();
+ }
+
+ public <T> T[] readMultiple(final Class<T> clazz)
+ {
+ Object val = readObject();
+ if(val == null)
+ {
+ return null;
+ }
+ else if(val.getClass().isArray())
+ {
+ if(clazz.isAssignableFrom(val.getClass().getComponentType()))
+ {
+ return (T[]) val;
+ }
+ else
+ {
+ throw unexpectedType(val, Array.newInstance(clazz, 0).getClass());
+ }
+ }
+ else if(clazz.isAssignableFrom(val.getClass()))
+ {
+ T[] array = (T[]) Array.newInstance(clazz, 1);
+ array[0] = (T) val;
+ return array;
+ }
+ else
+ {
+ throw unexpectedType(val, Array.newInstance(clazz, 0).getClass());
+ }
+ }
+
+ public Object[] readMultiple()
+ {
+ Object val = readObject();
+ if(val == null)
+ {
+ return null;
+ }
+ else if(val.getClass().isArray())
+ {
+ return (Object[]) val;
+ }
+ else
+ {
+ Object[] array = (Object[]) Array.newInstance(val.getClass(), 1);
+ array[0] = val;
+ return array;
+ }
+ }
+
+ public byte[] readByteMultiple()
+ {
+ return new byte[0]; //TODO.
+ }
+
+ public short[] readShortMultiple()
+ {
+ return new short[0]; //TODO.
+ }
+
+ public int[] readIntegerMultiple()
+ {
+ return new int[0]; //TODO.
+ }
+
+ public long[] readLongMultiple()
+ {
+ return new long[0]; //TODO.
+ }
+
+ public float[] readFloatMultiple()
+ {
+ return new float[0]; //TODO.
+ }
+
+ public double[] readDoubleMultiple()
+ {
+ return new double[0]; //TODO.
+ }
+
+ public char[] readCharacterMultiple()
+ {
+ return new char[0]; //TODO.
+ }
+
+ public Object readObject()
+ {
+ TypeConstructor constructor = readConstructor();
+ if(constructor== null)
+ {
+ throw new DecodeException("Unknown constructor");
+ }
+ return constructor instanceof ArrayType.ArrayEncoding
+ ? ((ArrayType.ArrayEncoding)constructor).readValueArray()
+ : constructor.readValue();
+ }
+
+ public Object readObject(final Object defaultValue)
+ {
+ Object val = readObject();
+ return val == null ? defaultValue : val;
+ }
+
+ <V> void register(PrimitiveType<V> type)
+ {
+ Collection<? extends PrimitiveTypeEncoding<V>> encodings = type.getAllEncodings();
+
+ for(PrimitiveTypeEncoding<V> encoding : encodings)
+ {
+ _constructors[((int) encoding.getEncodingCode()) & 0xFF ] = encoding;
+ }
+
+ }
+
+ byte readRawByte()
+ {
+ return _buffer.get();
+ }
+
+ int readRawInt()
+ {
+ return _buffer.getInt();
+ }
+
+ long readRawLong()
+ {
+ return _buffer.getLong();
+ }
+
+ short readRawShort()
+ {
+ return _buffer.getShort();
+ }
+
+ float readRawFloat()
+ {
+ return _buffer.getFloat();
+ }
+
+ double readRawDouble()
+ {
+ return _buffer.getDouble();
+ }
+
+ void readRaw(final byte[] data, final int offset, final int length)
+ {
+ _buffer.get(data, offset, length);
+ }
+
+
+ <V> V readRaw(TypeDecoder<V> decoder, int size)
+ {
+ V decode = decoder.decode((ByteBuffer) _buffer.slice().limit(size));
+ _buffer.position(_buffer.position()+size);
+ return decode;
+ }
+
+ public void setByteBuffer(final ByteBuffer buffer)
+ {
+ _buffer = buffer;
+ }
+
+ interface TypeDecoder<V>
+ {
+ V decode(ByteBuffer buf);
+ }
+
+ private static class UnknownDescribedType implements DescribedType
+ {
+ private final Object _descriptor;
+ private final Object _described;
+
+ public UnknownDescribedType(final Object descriptor, final Object described)
+ {
+ _descriptor = descriptor;
+ _described = described;
+ }
+
+ public Object getDescriptor()
+ {
+ return _descriptor;
+ }
+
+ public Object getDescribed()
+ {
+ return _described;
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DescribedTypeConstructor.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DescribedTypeConstructor.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DescribedTypeConstructor.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DescribedTypeConstructor.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;
+
+import org.apache.qpid.proton.type.DescribedType;
+
+public interface DescribedTypeConstructor<V extends DescribedType>
+{
+ V newInstance(Object described);
+
+ Class getTypeClass();
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DoubleType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DoubleType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DoubleType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DoubleType.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 DoubleType extends AbstractPrimitiveType<Double>
+{
+ private DoubleEncoding _doubleEncoding;
+
+ DoubleType(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ _doubleEncoding = new DoubleEncoding(encoder, decoder);
+ encoder.register(Double.class, this);
+ decoder.register(this);
+ }
+
+ public Class<Double> getTypeClass()
+ {
+ return Double.class;
+ }
+
+ public DoubleEncoding getEncoding(final Double val)
+ {
+ return _doubleEncoding;
+ }
+
+
+ public DoubleEncoding getCanonicalEncoding()
+ {
+ return _doubleEncoding;
+ }
+
+ public Collection<DoubleEncoding> getAllEncodings()
+ {
+ return Collections.singleton(_doubleEncoding);
+ }
+
+ public void write(double d)
+ {
+ _doubleEncoding.write(d);
+ }
+
+ public class DoubleEncoding extends FixedSizePrimitiveTypeEncoding<Double>
+ {
+
+ public DoubleEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+ {
+ super(encoder, decoder);
+ }
+
+ @Override
+ protected int getFixedSize()
+ {
+ return 8;
+ }
+
+ @Override
+ public byte getEncodingCode()
+ {
+ return EncodingCodes.DOUBLE;
+ }
+
+ public DoubleType getType()
+ {
+ return DoubleType.this;
+ }
+
+ public void writeValue(final Double val)
+ {
+ getEncoder().writeRaw(val.doubleValue());
+ }
+
+ public void writeValue(final double val)
+ {
+ getEncoder().writeRaw(val);
+ }
+
+ public void write(final double d)
+ {
+ writeConstructor();
+ getEncoder().writeRaw(d);
+
+ }
+
+ public boolean encodesSuperset(final TypeEncoding<Double> encoding)
+ {
+ return (getType() == encoding.getType());
+ }
+
+ public Double readValue()
+ {
+ return readPrimitiveValue();
+ }
+
+ public double readPrimitiveValue()
+ {
+ return getDecoder().readRawDouble();
+ }
+
+
+ @Override
+ public boolean encodesJavaPrimitive()
+ {
+ return true;
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicDescribedType.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicDescribedType.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicDescribedType.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicDescribedType.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,150 @@
+/*
+ *
+ * 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.DescribedType;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+public class DynamicDescribedType implements AMQPType<DescribedType>
+{
+
+ private final EncoderImpl _encoder;
+ private final Map<TypeEncoding, TypeEncoding> _encodings = new HashMap<TypeEncoding, TypeEncoding>();
+ private final Object _descriptor;
+
+ public DynamicDescribedType(EncoderImpl encoder, final Object descriptor)
+ {
+ _encoder = encoder;
+ _descriptor = descriptor;
+ }
+
+
+ public Class<DescribedType> getTypeClass()
+ {
+ return DescribedType.class;
+ }
+
+ public TypeEncoding<DescribedType> getEncoding(final DescribedType val)
+ {
+ TypeEncoding underlyingEncoding = _encoder.getType(val.getDescribed()).getEncoding(val.getDescribed());
+ TypeEncoding encoding = _encodings.get(underlyingEncoding);
+ if(encoding == null)
+ {
+ encoding = new DynamicDescribedTypeEncoding(underlyingEncoding);
+ _encodings.put(underlyingEncoding, encoding);
+ }
+
+ return encoding;
+ }
+
+ public TypeEncoding<DescribedType> getCanonicalEncoding()
+ {
+ return null;
+ }
+
+ public Collection<TypeEncoding<DescribedType>> getAllEncodings()
+ {
+ Collection values = _encodings.values();
+ Collection unmodifiable = Collections.unmodifiableCollection(values);
+ return (Collection<TypeEncoding<DescribedType>>) unmodifiable;
+ }
+
+ public void write(final DescribedType val)
+ {
+ TypeEncoding<DescribedType> encoding = getEncoding(val);
+ encoding.writeConstructor();
+ encoding.writeValue(val);
+ }
+
+ private class DynamicDescribedTypeEncoding implements TypeEncoding
+ {
+ private final TypeEncoding _underlyingEncoding;
+ private final TypeEncoding _descriptorType;
+ private final int _constructorSize;
+
+
+ public DynamicDescribedTypeEncoding(final TypeEncoding underlyingEncoding)
+ {
+ _underlyingEncoding = underlyingEncoding;
+ _descriptorType = _encoder.getType(_descriptor).getEncoding(_descriptor);
+ _constructorSize = 1 + _descriptorType.getConstructorSize() + _descriptorType.getValueSize(_descriptor);
+ }
+
+ public AMQPType getType()
+ {
+ return DynamicDescribedType.this;
+ }
+
+ public void writeConstructor()
+ {
+ _encoder.writeRaw(EncodingCodes.DESCRIBED_TYPE_INDICATOR);
+ _descriptorType.writeConstructor();
+ _descriptorType.writeValue(_descriptor);
+ _underlyingEncoding.writeConstructor();
+ }
+
+ public int getConstructorSize()
+ {
+ return _constructorSize;
+ }
+
+ public void writeValue(final Object val)
+ {
+ _underlyingEncoding.writeValue(((DescribedType)val).getDescribed());
+ }
+
+ public int getValueSize(final Object val)
+ {
+ return _underlyingEncoding.getValueSize(((DescribedType)val).getDescribed());
+ }
+
+ public boolean isFixedSizeVal()
+ {
+ return _underlyingEncoding.isFixedSizeVal();
+ }
+
+ public boolean encodesSuperset(final TypeEncoding encoding)
+ {
+ return (getType() == encoding.getType())
+ && (_underlyingEncoding.encodesSuperset(((DynamicDescribedTypeEncoding)encoding)
+ ._underlyingEncoding));
+ }
+
+ public Object readValue()
+ {
+ return _underlyingEncoding.readValue();
+ }
+
+ public boolean encodesJavaPrimitive()
+ {
+ return false;
+ }
+
+ public Class getTypeClass()
+ {
+ return _underlyingEncoding.getTypeClass();
+ }
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicTypeConstructor.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicTypeConstructor.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicTypeConstructor.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicTypeConstructor.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,60 @@
+/*
+ *
+ * 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 class DynamicTypeConstructor implements TypeConstructor
+{
+ private final DescribedTypeConstructor _describedTypeConstructor;
+ private final TypeConstructor _underlyingEncoding;
+
+ public DynamicTypeConstructor(final DescribedTypeConstructor dtc,
+ final TypeConstructor underlyingEncoding)
+ {
+ _describedTypeConstructor = dtc;
+ _underlyingEncoding = underlyingEncoding;
+ }
+
+ public Object readValue()
+ {
+ try
+ {
+ return _describedTypeConstructor.newInstance(_underlyingEncoding.readValue());
+ }
+ catch (NullPointerException npe)
+ {
+ throw new DecodeException("Unexpected null value - mandatory field not set?", npe);
+ }
+ catch (ClassCastException cce)
+ {
+ throw new DecodeException("Incorrect type used", cce);
+ }
+ }
+
+ public boolean encodesJavaPrimitive()
+ {
+ return false;
+ }
+
+ public Class getTypeClass()
+ {
+ return _describedTypeConstructor.getTypeClass();
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/EncodeException.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/EncodeException.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/EncodeException.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/EncodeException.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,44 @@
+/*
+ *
+ * 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 class EncodeException extends RuntimeException
+{
+ public EncodeException()
+ {
+ }
+
+ public EncodeException(String message)
+ {
+ super(message);
+ }
+
+ public EncodeException(String message, Throwable cause)
+ {
+ super(message, cause);
+ }
+
+ public EncodeException(Throwable cause)
+ {
+ super(cause);
+ }
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Encoder.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Encoder.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Encoder.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/Encoder.java Sat Mar 10 09:48:50 2012
@@ -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.qpid.proton.codec;
+
+import org.apache.qpid.proton.type.*;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+public interface Encoder
+{
+ void writeNull();
+
+ void writeBoolean(boolean bool);
+
+ void writeBoolean(Boolean bool);
+
+ void writeUnsignedByte(UnsignedByte ubyte);
+
+ void writeUnsignedShort(UnsignedShort ushort);
+
+ void writeUnsignedInteger(UnsignedInteger ushort);
+
+ void writeUnsignedLong(UnsignedLong ulong);
+
+ void writeByte(byte b);
+
+ void writeByte(Byte b);
+
+ void writeShort(short s);
+
+ void writeShort(Short s);
+
+ void writeInteger(int i);
+
+ void writeInteger(Integer i);
+
+ void writeLong(long l);
+
+ void writeLong(Long l);
+
+ void writeFloat(float f);
+
+ void writeFloat(Float f);
+
+ void writeDouble(double d);
+
+ void writeDouble(Double d);
+
+ void writeDecimal32(Decimal32 d);
+
+ void writeDecimal64(Decimal64 d);
+
+ void writeDecimal128(Decimal128 d);
+
+ void writeCharacter(char c);
+
+ void writeCharacter(Character c);
+
+ void writeTimestamp(long d);
+ void writeTimestamp(Date d);
+
+ void writeUUID(UUID uuid);
+
+ void writeBinary(Binary b);
+
+ void writeString(String s);
+
+ void writeSymbol(Symbol s);
+
+ void writeList(List l);
+
+ void writeMap(Map m);
+
+ void writeDescribedType(DescribedType d);
+
+ void writeArray(boolean[] a);
+ void writeArray(byte[] a);
+ void writeArray(short[] a);
+ void writeArray(int[] a);
+ void writeArray(long[] a);
+ void writeArray(float[] a);
+ void writeArray(double[] a);
+ void writeArray(char[] a);
+ void writeArray(Object[] a);
+
+ void writeObject(Object o);
+
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/EncoderImpl.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/EncoderImpl.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/EncoderImpl.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/EncoderImpl.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,886 @@
+/*
+ *
+ * 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.*;
+
+import java.nio.ByteBuffer;
+import java.util.*;
+
+public final class EncoderImpl implements ByteBufferEncoder
+{
+
+
+ private static final byte DESCRIBED_TYPE_OP = (byte)0;
+
+ public static interface Writable
+ {
+ void writeTo(ByteBuffer buf);
+ }
+
+ private ByteBuffer _buffer;
+
+ private final Map<Class, AMQPType> _typeRegistry = new HashMap<Class, AMQPType>();
+ private Map<Object, AMQPType> _describedDescriptorRegistry = new HashMap<Object, AMQPType>();
+ private Map<Class, AMQPType> _describedTypesClassRegistry = new HashMap<Class, AMQPType>();
+
+ private final NullType _nullType;
+ private final BooleanType _booleanType;
+ private final ByteType _byteType;
+ private final UnsignedByteType _unsignedByteType;
+ private final ShortType _shortType;
+ private final UnsignedShortType _unsignedShortType;
+ private final IntegerType _integerType;
+ private final UnsignedIntegerType _unsignedIntegerType;
+ private final LongType _longType;
+ private final UnsignedLongType _unsignedLongType;
+
+ private final CharacterType _characterType;
+ private final FloatType _floatType;
+ private final DoubleType _doubleType;
+ private final TimestampType _timestampType;
+ private final UUIDType _uuidType;
+
+ private final Decimal32Type _decimal32Type;
+ private final Decimal64Type _decimal64Type;
+ private final Decimal128Type _decimal128Type;
+
+ private final BinaryType _binaryType;
+ private final SymbolType _symbolType;
+ private final StringType _stringType;
+
+ private final ListType _listType;
+ private final MapType _mapType;
+
+ private final ArrayType _arrayType;
+
+ EncoderImpl(ByteBuffer buffer, DecoderImpl decoder)
+ {
+ this(decoder);
+ setByteBuffer(buffer);
+ }
+
+ public EncoderImpl(DecoderImpl decoder)
+ {
+
+ _nullType = new NullType(this, decoder);
+ _booleanType = new BooleanType(this, decoder);
+ _byteType = new ByteType(this, decoder);
+ _unsignedByteType = new UnsignedByteType(this, decoder);
+ _shortType = new ShortType(this, decoder);
+ _unsignedShortType = new UnsignedShortType(this, decoder);
+ _integerType = new IntegerType(this, decoder);
+ _unsignedIntegerType = new UnsignedIntegerType(this, decoder);
+ _longType = new LongType(this, decoder);
+ _unsignedLongType = new UnsignedLongType(this, decoder);
+
+ _characterType = new CharacterType(this, decoder);
+ _floatType = new FloatType(this, decoder);
+ _doubleType = new DoubleType(this, decoder);
+ _timestampType = new TimestampType(this, decoder);
+ _uuidType = new UUIDType(this, decoder);
+
+ _decimal32Type = new Decimal32Type(this, decoder);
+ _decimal64Type = new Decimal64Type(this, decoder);
+ _decimal128Type = new Decimal128Type(this, decoder);
+
+
+ _binaryType = new BinaryType(this, decoder);
+ _symbolType = new SymbolType(this, decoder);
+ _stringType = new StringType(this, decoder);
+
+ _listType = new ListType(this, decoder);
+ _mapType = new MapType(this, decoder);
+
+ _arrayType = new ArrayType(this,
+ decoder,
+ _booleanType,
+ _byteType,
+ _shortType,
+ _integerType,
+ _longType,
+ _floatType,
+ _doubleType,
+ _characterType);
+
+
+ }
+
+ public void setByteBuffer(final ByteBuffer buf)
+ {
+ _buffer = buf;
+ }
+
+ public AMQPType getType(final Object element)
+ {
+ if(element instanceof DescribedType)
+ {
+ AMQPType amqpType;
+
+ Object descriptor = ((DescribedType)element).getDescriptor();
+ amqpType = _describedDescriptorRegistry.get(descriptor);
+ if(amqpType == null)
+ {
+ amqpType = new DynamicDescribedType(this, descriptor);
+ _describedDescriptorRegistry.put(descriptor, amqpType);
+ }
+ return amqpType;
+
+ }
+ else
+ {
+ return getTypeFromClass(element == null ? Void.class : element.getClass());
+ }
+ }
+
+ public AMQPType getTypeFromClass(final Class clazz)
+ {
+ AMQPType amqpType = _typeRegistry.get(clazz);
+ if(amqpType == null)
+ {
+
+ if(clazz.isArray())
+ {
+ amqpType = _arrayType;
+ }
+ else
+ {
+ if(List.class.isAssignableFrom(clazz))
+ {
+ amqpType = _listType;
+ }
+ else if(Map.class.isAssignableFrom(clazz))
+ {
+ amqpType = _mapType;
+ }
+ else if(DescribedType.class.isAssignableFrom(clazz))
+ {
+ amqpType = _describedTypesClassRegistry.get(clazz);
+ }
+ }
+ _typeRegistry.put(clazz, amqpType);
+ }
+ return amqpType;
+ }
+
+ <T> void register(Class<T> clazz, AMQPType<T> type)
+ {
+ _typeRegistry.put(clazz, type);
+ }
+
+ public void registerDescribedType(Class clazz, Object descriptor)
+ {
+ AMQPType type = _describedDescriptorRegistry.get(descriptor);
+ if(type == null)
+ {
+ type = new DynamicDescribedType(this, descriptor);
+ _describedDescriptorRegistry.put(descriptor, type);
+ }
+ _describedTypesClassRegistry.put(clazz, type);
+ }
+
+ public void writeNull()
+ {
+ _nullType.write();
+ }
+
+ public void writeBoolean(final boolean bool)
+ {
+ _booleanType.writeValue(bool);
+ }
+
+ public void writeBoolean(final Boolean bool)
+ {
+ if(bool == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _booleanType.write(bool);
+ }
+ }
+
+ public void writeUnsignedByte(final UnsignedByte ubyte)
+ {
+ if(ubyte == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _unsignedByteType.write(ubyte);
+ }
+ }
+
+ public void writeUnsignedShort(final UnsignedShort ushort)
+ {
+ if(ushort == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _unsignedShortType.write(ushort);
+ }
+ }
+
+ public void writeUnsignedInteger(final UnsignedInteger uint)
+ {
+ if(uint == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _unsignedIntegerType.write(uint);
+ }
+ }
+
+ public void writeUnsignedLong(final UnsignedLong ulong)
+ {
+ if(ulong == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _unsignedLongType.write(ulong);
+ }
+ }
+
+ public void writeByte(final byte b)
+ {
+ _byteType.write(b);
+ }
+
+ public void writeByte(final Byte b)
+ {
+ if(b == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ writeByte(b.byteValue());
+ }
+ }
+
+ public void writeShort(final short s)
+ {
+ _shortType.write(s);
+ }
+
+ public void writeShort(final Short s)
+ {
+ if(s == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ writeShort(s.shortValue());
+ }
+ }
+
+ public void writeInteger(final int i)
+ {
+ _integerType.write(i);
+ }
+
+ public void writeInteger(final Integer i)
+ {
+ if(i == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ writeInteger(i.intValue());
+ }
+ }
+
+ public void writeLong(final long l)
+ {
+ _longType.write(l);
+ }
+
+ public void writeLong(final Long l)
+ {
+
+ if(l == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ writeLong(l.longValue());
+ }
+ }
+
+ public void writeFloat(final float f)
+ {
+ _floatType.write(f);
+ }
+
+ public void writeFloat(final Float f)
+ {
+ if(f == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ writeFloat(f.floatValue());
+ }
+ }
+
+ public void writeDouble(final double d)
+ {
+ _doubleType.write(d);
+ }
+
+ public void writeDouble(final Double d)
+ {
+ if(d == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ writeDouble(d.doubleValue());
+ }
+ }
+
+ public void writeDecimal32(final Decimal32 d)
+ {
+ if(d == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _decimal32Type.write(d);
+ }
+ }
+
+ public void writeDecimal64(final Decimal64 d)
+ {
+ if(d == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _decimal64Type.write(d);
+ }
+ }
+
+ public void writeDecimal128(final Decimal128 d)
+ {
+ if(d == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _decimal128Type.write(d);
+ }
+ }
+
+ public void writeCharacter(final char c)
+ {
+ // TODO - java character may be half of a pair, should probably throw exception then
+ _characterType.write(c);
+ }
+
+ public void writeCharacter(final Character c)
+ {
+ if(c == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ writeCharacter(c.charValue());
+ }
+ }
+
+ public void writeTimestamp(final long d)
+ {
+ _timestampType.write(d);
+ }
+
+ public void writeTimestamp(final Date d)
+ {
+ if(d == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ writeTimestamp(d.getTime());
+ }
+ }
+
+ public void writeUUID(final UUID uuid)
+ {
+ if(uuid == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _uuidType.write(uuid);
+ }
+
+ }
+
+ public void writeBinary(final Binary b)
+ {
+ if(b == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _binaryType.write(b);
+ }
+ }
+
+ public void writeString(final String s)
+ {
+ if(s == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _stringType.write(s);
+ }
+ }
+
+ public void writeSymbol(final Symbol s)
+ {
+ if(s == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _symbolType.write(s);
+ }
+
+ }
+
+ public void writeList(final List l)
+ {
+ if(l == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _listType.write(l);
+ }
+ }
+
+ public void writeMap(final Map m)
+ {
+
+ if(m == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _mapType.write(m);
+ }
+ }
+
+ public void writeDescribedType(final DescribedType d)
+ {
+ if(d == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _buffer.put(DESCRIBED_TYPE_OP);
+ writeObject(d.getDescriptor());
+ writeObject(d.getDescribed());
+ }
+ }
+
+ public void writeArray(final boolean[] a)
+ {
+ if(a == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _arrayType.write(a);
+ }
+ }
+
+ public void writeArray(final byte[] a)
+ {
+ if(a == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _arrayType.write(a);
+ }
+ }
+
+ public void writeArray(final short[] a)
+ {
+ if(a == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _arrayType.write(a);
+ }
+ }
+
+ public void writeArray(final int[] a)
+ {
+ if(a == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _arrayType.write(a);
+ }
+ }
+
+ public void writeArray(final long[] a)
+ {
+ if(a == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _arrayType.write(a);
+ }
+ }
+
+ public void writeArray(final float[] a)
+ {
+ if(a == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _arrayType.write(a);
+ }
+ }
+
+ public void writeArray(final double[] a)
+ {
+ if(a == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _arrayType.write(a);
+ }
+ }
+
+ public void writeArray(final char[] a)
+ {
+ if(a == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _arrayType.write(a);
+ }
+ }
+
+ public void writeArray(final Object[] a)
+ {
+ if(a == null)
+ {
+ writeNull();
+ }
+ else
+ {
+ _arrayType.write(a);
+ }
+ }
+
+ public void writeObject(final Object o)
+ {
+ AMQPType type = _typeRegistry.get(o == null ? Void.class : o.getClass());
+
+ if(type == null)
+ {
+ if(o.getClass().isArray())
+ {
+ Class<?> componentType = o.getClass().getComponentType();
+ if(componentType.isPrimitive())
+ {
+ if(componentType == Boolean.TYPE)
+ {
+ writeArray((boolean[])o);
+ }
+ else if(componentType == Byte.TYPE)
+ {
+ writeArray((byte[])o);
+ }
+ else if(componentType == Short.TYPE)
+ {
+ writeArray((short[])o);
+ }
+ else if(componentType == Integer.TYPE)
+ {
+ writeArray((int[])o);
+ }
+ else if(componentType == Long.TYPE)
+ {
+ writeArray((long[])o);
+ }
+ else if(componentType == Float.TYPE)
+ {
+ writeArray((float[])o);
+ }
+ else if(componentType == Double.TYPE)
+ {
+ writeArray((double[])o);
+ }
+ else if(componentType == Character.TYPE)
+ {
+ writeArray((char[])o);
+ }
+ else
+ {
+ throw new IllegalArgumentException("Cannot write arrays of type " + componentType.getName());
+ }
+ }
+ else
+ {
+ writeArray((Object[]) o);
+ }
+ }
+ else if(o instanceof List)
+ {
+ writeList((List)o);
+ }
+ else if(o instanceof Map)
+ {
+ writeMap((Map)o);
+ }
+ else if(o instanceof DescribedType)
+ {
+ writeDescribedType((DescribedType)o);
+ }
+ else
+ {
+ throw new IllegalArgumentException("Do not know how to write Objects of class " + o.getClass()
+ .getName());
+
+ }
+ }
+ else
+ {
+ type.write(o);
+ }
+ }
+
+ void writeRaw(final byte b)
+ {
+ _buffer.put(b);
+ }
+
+ void writeRaw(final short s)
+ {
+ _buffer.putShort(s);
+ }
+
+ void writeRaw(final int i)
+ {
+ _buffer.putInt(i);
+ }
+
+ void writeRaw(final long l)
+ {
+ _buffer.putLong(l);
+ }
+
+ void writeRaw(final float f)
+ {
+ _buffer.putFloat(f);
+ }
+
+ void writeRaw(final double d)
+ {
+ _buffer.putDouble(d);
+ }
+
+ void writeRaw(byte[] src, int offset, int length)
+ {
+ _buffer.put(src, offset, length);
+ }
+
+ void writeRaw(Writable writable)
+ {
+ writable.writeTo(_buffer);
+ }
+
+
+
+ public static class ExampleObject implements DescribedType
+ {
+ public static final UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(254l);
+
+ private int _handle;
+ private String _name;
+
+ private final ExampleObjectWrapper _wrapper = new ExampleObjectWrapper();
+
+ public ExampleObject()
+ {
+
+ }
+
+ public ExampleObject(final int handle, final String name)
+ {
+ _handle = handle;
+ _name = name;
+
+ }
+
+ public int getHandle()
+ {
+ return _handle;
+ }
+
+ public String getName()
+ {
+ return _name;
+ }
+
+ public void setHandle(final int handle)
+ {
+ _handle = handle;
+ }
+
+ public void setName(final String name)
+ {
+ _name = name;
+ }
+
+ public Object getDescriptor()
+ {
+ return DESCRIPTOR;
+ }
+
+ public Object getDescribed()
+ {
+ return _wrapper;
+ }
+
+ private class ExampleObjectWrapper extends AbstractList
+ {
+
+ @Override
+ public Object get(final int index)
+ {
+ switch(index)
+ {
+ case 0:
+ return getHandle();
+ case 1:
+ return getName();
+ }
+ throw new IllegalStateException("Unknown index " + index);
+ }
+
+ @Override
+ public int size()
+ {
+ return getName() == null ? getHandle() == 0 ? 0 : 1 : 2;
+ }
+ }
+
+ }
+
+
+ public static void main(String[] args)
+ {
+ byte[] data = new byte[1024];
+
+ DecoderImpl decoder = new DecoderImpl(ByteBuffer.wrap(data));
+ Encoder enc = new EncoderImpl(ByteBuffer.wrap(data), decoder);
+
+ decoder.register(ExampleObject.DESCRIPTOR, new DescribedTypeConstructor<ExampleObject>()
+ {
+ public ExampleObject newInstance(final Object described)
+ {
+ List l = (List) described;
+ ExampleObject o = new ExampleObject();
+ switch(l.size())
+ {
+ case 2:
+ o.setName((String)l.get(1));
+ case 1:
+ o.setHandle((Integer)l.get(0));
+ }
+ return o;
+ }
+
+ public Class<ExampleObject> getTypeClass()
+ {
+ return ExampleObject.class;
+ }
+ }
+ );
+
+ ExampleObject[] examples = new ExampleObject[2];
+ examples[0] = new ExampleObject(7,"fred");
+ examples[1] = new ExampleObject(19, "bret");
+
+ enc.writeObject(examples);
+
+ enc.writeObject(new Object[][]{new Integer[]{1,2,3}, new Long[]{4l,5L,6L}, new String[] {"hello", "world"}});
+ enc.writeObject(new int[][]{{256,27}, {1}});
+
+ Object rval = decoder.readObject();
+ rval = decoder.readObject();
+
+ System.out.println("Hello");
+
+
+ }
+
+
+}
Added: qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/EncodingCodes.java
URL: http://svn.apache.org/viewvc/qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/EncodingCodes.java?rev=1299173&view=auto
==============================================================================
--- qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/EncodingCodes.java (added)
+++ qpid/proton/proton-j/codec/src/org/apache/qpid/proton/codec/EncodingCodes.java Sat Mar 10 09:48:50 2012
@@ -0,0 +1,89 @@
+/*
+ *
+ * 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;
+
+interface EncodingCodes
+{
+ public static final byte DESCRIBED_TYPE_INDICATOR = (byte) 0x00;
+
+ public static final byte NULL = (byte) 0x40;
+
+ public static final byte BOOLEAN = (byte) 0x56;
+ public static final byte BOOLEAN_TRUE = (byte) 0x41;
+ public static final byte BOOLEAN_FALSE = (byte) 0x42;
+
+ public static final byte UBYTE = (byte) 0x50;
+
+ public static final byte USHORT = (byte) 0x60;
+
+ public static final byte UINT = (byte) 0x70;
+ public static final byte SMALLUINT = (byte) 0x52;
+ public static final byte UINT0 = (byte) 0x43;
+
+ public static final byte ULONG = (byte) 0x80;
+ public static final byte SMALLULONG = (byte) 0x53;
+ public static final byte ULONG0 = (byte) 0x44;
+
+ public static final byte BYTE = (byte) 0x51;
+
+ public static final byte SHORT = (byte) 0x61;
+
+ public static final byte INT = (byte) 0x71;
+ public static final byte SMALLINT = (byte) 0x54;
+
+ public static final byte LONG = (byte) 0x81;
+ public static final byte SMALLLONG = (byte) 0x55;
+
+ public static final byte FLOAT = (byte) 0x72;
+
+ public static final byte DOUBLE = (byte) 0x82;
+
+ public static final byte DECIMAL32 = (byte) 0x74;
+
+ public static final byte DECIMAL64 = (byte) 0x84;
+
+ public static final byte DECIMAL128 = (byte) 0x94;
+
+ public static final byte CHAR = (byte) 0x73;
+
+ public static final byte TIMESTAMP = (byte) 0x83;
+
+ public static final byte UUID = (byte) 0x98;
+
+ public static final byte VBIN8 = (byte) 0xa0;
+ public static final byte VBIN32 = (byte) 0xb0;
+
+ public static final byte STR8 = (byte) 0xa1;
+ public static final byte STR32 = (byte) 0xb1;
+
+ public static final byte SYM8 = (byte) 0xa3;
+ public static final byte SYM32 = (byte) 0xb3;
+
+ public static final byte LIST8 = (byte) 0xc0;
+ public static final byte LIST32 = (byte) 0xd0;
+
+ public static final byte MAP8 = (byte) 0xc1;
+ public static final byte MAP32 = (byte) 0xd1;
+
+ public static final byte ARRAY8 = (byte) 0xe0;
+ public static final byte ARRAY32 = (byte) 0xf0;
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org