http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e57c2a52/proton-j/codec/src/org/apache/qpid/proton/codec/DecoderImpl.java ---------------------------------------------------------------------- diff --git a/proton-j/codec/src/org/apache/qpid/proton/codec/DecoderImpl.java b/proton-j/codec/src/org/apache/qpid/proton/codec/DecoderImpl.java deleted file mode 100644 index 3bafb10..0000000 --- a/proton-j/codec/src/org/apache/qpid/proton/codec/DecoderImpl.java +++ /dev/null @@ -1,972 +0,0 @@ -/* - * - * 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; - } - } -}
http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e57c2a52/proton-j/codec/src/org/apache/qpid/proton/codec/DescribedTypeConstructor.java ---------------------------------------------------------------------- diff --git a/proton-j/codec/src/org/apache/qpid/proton/codec/DescribedTypeConstructor.java b/proton-j/codec/src/org/apache/qpid/proton/codec/DescribedTypeConstructor.java deleted file mode 100644 index 073a60e..0000000 --- a/proton-j/codec/src/org/apache/qpid/proton/codec/DescribedTypeConstructor.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * - * 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(); -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e57c2a52/proton-j/codec/src/org/apache/qpid/proton/codec/DoubleType.java ---------------------------------------------------------------------- diff --git a/proton-j/codec/src/org/apache/qpid/proton/codec/DoubleType.java b/proton-j/codec/src/org/apache/qpid/proton/codec/DoubleType.java deleted file mode 100644 index 8c6c84c..0000000 --- a/proton-j/codec/src/org/apache/qpid/proton/codec/DoubleType.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * - * 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; - } - } -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e57c2a52/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicDescribedType.java ---------------------------------------------------------------------- diff --git a/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicDescribedType.java b/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicDescribedType.java deleted file mode 100644 index de29f27..0000000 --- a/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicDescribedType.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * - * 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(); - } - } -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e57c2a52/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicTypeConstructor.java ---------------------------------------------------------------------- diff --git a/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicTypeConstructor.java b/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicTypeConstructor.java deleted file mode 100644 index e7edac7..0000000 --- a/proton-j/codec/src/org/apache/qpid/proton/codec/DynamicTypeConstructor.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * - * 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(); - } -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e57c2a52/proton-j/codec/src/org/apache/qpid/proton/codec/EncodeException.java ---------------------------------------------------------------------- diff --git a/proton-j/codec/src/org/apache/qpid/proton/codec/EncodeException.java b/proton-j/codec/src/org/apache/qpid/proton/codec/EncodeException.java deleted file mode 100644 index 5aa28bc..0000000 --- a/proton-j/codec/src/org/apache/qpid/proton/codec/EncodeException.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * - * 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); - } -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e57c2a52/proton-j/codec/src/org/apache/qpid/proton/codec/Encoder.java ---------------------------------------------------------------------- diff --git a/proton-j/codec/src/org/apache/qpid/proton/codec/Encoder.java b/proton-j/codec/src/org/apache/qpid/proton/codec/Encoder.java deleted file mode 100644 index 301c91a..0000000 --- a/proton-j/codec/src/org/apache/qpid/proton/codec/Encoder.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * - * 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); - -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e57c2a52/proton-j/codec/src/org/apache/qpid/proton/codec/EncoderImpl.java ---------------------------------------------------------------------- diff --git a/proton-j/codec/src/org/apache/qpid/proton/codec/EncoderImpl.java b/proton-j/codec/src/org/apache/qpid/proton/codec/EncoderImpl.java deleted file mode 100644 index 38f4a87..0000000 --- a/proton-j/codec/src/org/apache/qpid/proton/codec/EncoderImpl.java +++ /dev/null @@ -1,886 +0,0 @@ -/* - * - * 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"); - - - } - - -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e57c2a52/proton-j/codec/src/org/apache/qpid/proton/codec/EncodingCodes.java ---------------------------------------------------------------------- diff --git a/proton-j/codec/src/org/apache/qpid/proton/codec/EncodingCodes.java b/proton-j/codec/src/org/apache/qpid/proton/codec/EncodingCodes.java deleted file mode 100644 index 352a2db..0000000 --- a/proton-j/codec/src/org/apache/qpid/proton/codec/EncodingCodes.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * - * 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; - -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e57c2a52/proton-j/codec/src/org/apache/qpid/proton/codec/FixedSizePrimitiveTypeEncoding.java ---------------------------------------------------------------------- diff --git a/proton-j/codec/src/org/apache/qpid/proton/codec/FixedSizePrimitiveTypeEncoding.java b/proton-j/codec/src/org/apache/qpid/proton/codec/FixedSizePrimitiveTypeEncoding.java deleted file mode 100644 index 2762064..0000000 --- a/proton-j/codec/src/org/apache/qpid/proton/codec/FixedSizePrimitiveTypeEncoding.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * - * 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(); -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e57c2a52/proton-j/codec/src/org/apache/qpid/proton/codec/FloatType.java ---------------------------------------------------------------------- diff --git a/proton-j/codec/src/org/apache/qpid/proton/codec/FloatType.java b/proton-j/codec/src/org/apache/qpid/proton/codec/FloatType.java deleted file mode 100644 index f00af4e..0000000 --- a/proton-j/codec/src/org/apache/qpid/proton/codec/FloatType.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * - * 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; - } - } -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e57c2a52/proton-j/codec/src/org/apache/qpid/proton/codec/FloatingSizePrimitiveTypeEncoding.java ---------------------------------------------------------------------- diff --git a/proton-j/codec/src/org/apache/qpid/proton/codec/FloatingSizePrimitiveTypeEncoding.java b/proton-j/codec/src/org/apache/qpid/proton/codec/FloatingSizePrimitiveTypeEncoding.java deleted file mode 100644 index eb6969d..0000000 --- a/proton-j/codec/src/org/apache/qpid/proton/codec/FloatingSizePrimitiveTypeEncoding.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * - * 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); -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e57c2a52/proton-j/codec/src/org/apache/qpid/proton/codec/IntegerType.java ---------------------------------------------------------------------- diff --git a/proton-j/codec/src/org/apache/qpid/proton/codec/IntegerType.java b/proton-j/codec/src/org/apache/qpid/proton/codec/IntegerType.java deleted file mode 100644 index a4fd0f5..0000000 --- a/proton-j/codec/src/org/apache/qpid/proton/codec/IntegerType.java +++ /dev/null @@ -1,213 +0,0 @@ -/* - * - * 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; - } - } -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e57c2a52/proton-j/codec/src/org/apache/qpid/proton/codec/LargeFloatingSizePrimitiveTypeEncoding.java ---------------------------------------------------------------------- diff --git a/proton-j/codec/src/org/apache/qpid/proton/codec/LargeFloatingSizePrimitiveTypeEncoding.java b/proton-j/codec/src/org/apache/qpid/proton/codec/LargeFloatingSizePrimitiveTypeEncoding.java deleted file mode 100644 index e6be002..0000000 --- a/proton-j/codec/src/org/apache/qpid/proton/codec/LargeFloatingSizePrimitiveTypeEncoding.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * - * 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)); - } -} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
