From 1ba9134c699fad05eb97ae1278475163ad9f4ac1 Mon Sep 17 00:00:00 2001 From: "Rafael H. Schloming" Date: Thu, 23 Aug 2007 15:32:13 +0000 Subject: moved Encoder, Decoder and related classes into the codec sub-package git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@569045 13f79535-47bb-0310-9956-ffa450edef68 --- java/common/generate | 8 +- .../java/org/apache/qpidity/AbstractDecoder.java | 192 ------------------ .../java/org/apache/qpidity/AbstractEncoder.java | 211 -------------------- .../main/java/org/apache/qpidity/BBDecoder.java | 53 ----- .../main/java/org/apache/qpidity/BBEncoder.java | 52 ----- .../src/main/java/org/apache/qpidity/Channel.java | 7 +- .../src/main/java/org/apache/qpidity/Decoder.java | 54 ------ .../main/java/org/apache/qpidity/Encodable.java | 37 ---- .../src/main/java/org/apache/qpidity/Encoder.java | 56 ------ .../java/org/apache/qpidity/FragmentDecoder.java | 111 ----------- .../main/java/org/apache/qpidity/Functions.java | 2 +- .../java/org/apache/qpidity/HeaderHandler.java | 2 + .../java/org/apache/qpidity/MethodDecoder.java | 3 + .../java/org/apache/qpidity/SegmentEncoder.java | 172 ----------------- .../main/java/org/apache/qpidity/SizeEncoder.java | 83 -------- .../src/main/java/org/apache/qpidity/Struct.java | 4 +- .../src/main/java/org/apache/qpidity/Stub.java | 4 + .../org/apache/qpidity/codec/AbstractDecoder.java | 195 +++++++++++++++++++ .../org/apache/qpidity/codec/AbstractEncoder.java | 215 +++++++++++++++++++++ .../java/org/apache/qpidity/codec/BBDecoder.java | 53 +++++ .../java/org/apache/qpidity/codec/BBEncoder.java | 52 +++++ .../java/org/apache/qpidity/codec/Decoder.java | 58 ++++++ .../java/org/apache/qpidity/codec/Encodable.java | 37 ++++ .../java/org/apache/qpidity/codec/Encoder.java | 60 ++++++ .../org/apache/qpidity/codec/FragmentDecoder.java | 111 +++++++++++ .../org/apache/qpidity/codec/SegmentEncoder.java | 175 +++++++++++++++++ .../java/org/apache/qpidity/codec/SizeEncoder.java | 83 ++++++++ 27 files changed, 1063 insertions(+), 1027 deletions(-) delete mode 100644 java/common/src/main/java/org/apache/qpidity/AbstractDecoder.java delete mode 100644 java/common/src/main/java/org/apache/qpidity/AbstractEncoder.java delete mode 100644 java/common/src/main/java/org/apache/qpidity/BBDecoder.java delete mode 100644 java/common/src/main/java/org/apache/qpidity/BBEncoder.java delete mode 100644 java/common/src/main/java/org/apache/qpidity/Decoder.java delete mode 100644 java/common/src/main/java/org/apache/qpidity/Encodable.java delete mode 100644 java/common/src/main/java/org/apache/qpidity/Encoder.java delete mode 100644 java/common/src/main/java/org/apache/qpidity/FragmentDecoder.java delete mode 100644 java/common/src/main/java/org/apache/qpidity/SegmentEncoder.java delete mode 100644 java/common/src/main/java/org/apache/qpidity/SizeEncoder.java create mode 100644 java/common/src/main/java/org/apache/qpidity/codec/AbstractDecoder.java create mode 100644 java/common/src/main/java/org/apache/qpidity/codec/AbstractEncoder.java create mode 100644 java/common/src/main/java/org/apache/qpidity/codec/BBDecoder.java create mode 100644 java/common/src/main/java/org/apache/qpidity/codec/BBEncoder.java create mode 100644 java/common/src/main/java/org/apache/qpidity/codec/Decoder.java create mode 100644 java/common/src/main/java/org/apache/qpidity/codec/Encodable.java create mode 100644 java/common/src/main/java/org/apache/qpidity/codec/Encoder.java create mode 100644 java/common/src/main/java/org/apache/qpidity/codec/FragmentDecoder.java create mode 100644 java/common/src/main/java/org/apache/qpidity/codec/SegmentEncoder.java create mode 100644 java/common/src/main/java/org/apache/qpidity/codec/SizeEncoder.java (limited to 'java') diff --git a/java/common/generate b/java/common/generate index 8e6957803e..ac49c6283d 100755 --- a/java/common/generate +++ b/java/common/generate @@ -23,6 +23,10 @@ class Output: self.line("import java.util.Map;") self.line("import java.util.UUID;") self.line() + self.line("import org.apache.qpidity.codec.Decoder;") + self.line("import org.apache.qpidity.codec.Encodable;") + self.line("import org.apache.qpidity.codec.Encoder;") + self.line() self.line() def line(self, l = ""): @@ -80,9 +84,9 @@ const.line("{") for d in spec.query["amqp/constant"]: name = d["@name"] val = d["@value"] - datatype = d["@datatype"] + datatype = d["@datatype"] if datatype == None: - const.line("public static final int %s = %s;" % (scream(name), val)) + const.line("public static final int %s = %s;" % (scream(name), val)) const.line("}") const.write() diff --git a/java/common/src/main/java/org/apache/qpidity/AbstractDecoder.java b/java/common/src/main/java/org/apache/qpidity/AbstractDecoder.java deleted file mode 100644 index ca728812bd..0000000000 --- a/java/common/src/main/java/org/apache/qpidity/AbstractDecoder.java +++ /dev/null @@ -1,192 +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.qpidity; - -import java.util.Map; -import java.util.UUID; - -import static org.apache.qpidity.Functions.*; - - -/** - * AbstractDecoder - * - * @author Rafael H. Schloming - */ - -abstract class AbstractDecoder implements Decoder -{ - - private final byte major; - private final byte minor; - - protected AbstractDecoder(byte major, byte minor) - { - this.major = major; - this.minor = minor; - } - - protected abstract byte get(); - - protected abstract void get(byte[] bytes); - - protected short uget() - { - return unsigned(get()); - } - - private byte bits = 0x0; - private byte nbits = 0; - - public boolean readBit() - { - if (nbits == 0) - { - bits = get(); - nbits = 8; - } - - nbits -= 1; - - boolean result = ((bits >>> nbits) & 0x01) != 0; - return result; - } - - private void clearBits() - { - bits = 0x0; - nbits = 0; - } - - public short readOctet() - { - clearBits(); - return uget(); - } - - public int readShort() - { - clearBits(); - int i = uget() << 8; - i |= uget(); - return i; - } - - public long readLong() - { - clearBits(); - long l = uget() << 24; - l |= uget() << 16; - l |= uget() << 8; - l |= uget(); - return l; - } - - public long readLonglong() - { - clearBits(); - long l = uget() << 56; - l |= uget() << 48; - l |= uget() << 40; - l |= uget() << 32; - l |= uget() << 24; - l |= uget() << 16; - l |= uget() << 8; - l |= uget(); - return l; - } - - public long readTimestamp() - { - return readLonglong(); - } - - - public String readShortstr() - { - short size = readOctet(); - byte[] bytes = new byte[size]; - get(bytes); - return new String(bytes); - } - - public String readLongstr() - { - long size = readLong(); - assert size <= Integer.MAX_VALUE; - byte[] bytes = new byte[(int) size]; - get(bytes); - return new String(bytes); - } - - public Map readTable() - { - //throw new Error("TODO"); - return null; - } - - public RangeSet readRfc1982LongSet() - { - int count = readShort()/8; - if (count == 0) - { - return null; - } - else - { - RangeSet ranges = new RangeSet(); - for (int i = 0; i < count; i++) - { - ranges.add(readLong(), readLong()); - } - return ranges; - } - } - - public UUID readUuid() - { - long msb = readLong(); - long lsb = readLong(); - return new UUID(msb, lsb); - } - - public String readContent() - { - throw new Error("Deprecated"); - } - - public Struct readLongStruct() - { - long size = readLong(); - if (size == 0) - { - return null; - } - else - { - int type = readShort(); - Struct result = Struct.create(type); - result.read(this, major, minor); - return result; - } - } - -} diff --git a/java/common/src/main/java/org/apache/qpidity/AbstractEncoder.java b/java/common/src/main/java/org/apache/qpidity/AbstractEncoder.java deleted file mode 100644 index ecd274615b..0000000000 --- a/java/common/src/main/java/org/apache/qpidity/AbstractEncoder.java +++ /dev/null @@ -1,211 +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.qpidity; - -import java.nio.ByteBuffer; - -import java.util.Map; -import java.util.UUID; - -import static org.apache.qpidity.Functions.*; - - -/** - * AbstractEncoder - * - * @author Rafael H. Schloming - */ - -abstract class AbstractEncoder implements Encoder -{ - - private final byte major; - private final byte minor; - - protected AbstractEncoder(byte major, byte minor) - { - this.major = major; - this.minor = minor; - } - - protected abstract void put(byte b); - - protected abstract void put(ByteBuffer src); - - private byte bits = 0x0; - private byte nbits = 0; - - public void writeBit(boolean b) - { - if (b) - { - bits |= 1 << nbits; - } - - nbits += 1; - - if (nbits == 8) - { - flushBits(); - } - } - - private void flushBits() - { - if (nbits > 0) - { - put(bits); - bits = 0x0; - nbits = 0; - } - } - - public void writeOctet(short b) - { - assert b < 0x100; - - flushBits(); - put((byte) b); - } - - public void writeShort(int s) - { - assert s < 0x10000; - - flushBits(); - put(lsb(s >>> 8)); - put(lsb(s)); - } - - public void writeLong(long i) - { - assert i < 0x100000000L; - - flushBits(); - put(lsb(i >>> 24)); - put(lsb(i >>> 16)); - put(lsb(i >>> 8)); - put(lsb(i)); - } - - public void writeLonglong(long l) - { - flushBits(); - put(lsb(l >>> 56)); - put(lsb(l >>> 48)); - put(lsb(l >>> 40)); - put(lsb(l >>> 32)); - put(lsb(l >>> 24)); - put(lsb(l >>> 16)); - put(lsb(l >>> 8)); - put(lsb(l)); - } - - - public void writeTimestamp(long l) - { - flushBits(); - writeLonglong(l); - } - - - public void writeShortstr(String s) - { - if (s == null) { s = ""; } - if (s.length() > 255) { - throw new IllegalArgumentException(s); - } - writeOctet((short) s.length()); - put(ByteBuffer.wrap(s.getBytes())); - } - - public void writeLongstr(String s) - { - if (s == null) { s = ""; } - writeLong(s.length()); - put(ByteBuffer.wrap(s.getBytes())); - } - - - public void writeTable(Map table) - { - //throw new Error("TODO"); - } - - public void writeRfc1982LongSet(RangeSet ranges) - { - if (ranges == null) - { - writeShort((short) 0); - } - else - { - writeShort(ranges.size() * 8); - for (Range range : ranges) - { - writeLong(range.getLower()); - writeLong(range.getUpper()); - } - } - } - - public void writeUuid(UUID uuid) - { - long msb = 0; - long lsb = 0; - if (uuid != null) - { - msb = uuid.getMostSignificantBits(); - uuid.getLeastSignificantBits(); - } - writeLong(msb); - writeLong(lsb); - } - - public void writeContent(String c) - { - throw new Error("Deprecated"); - } - - public void writeLongStruct(Struct s) - { - if (s == null) - { - writeLong(0); - } - else - { - SizeEncoder sizer = new SizeEncoder(major, minor); - sizer.writeShort(s.getEncodedType()); - s.write(sizer, major, minor); - - writeLong(sizer.getSize()); - writeShort(s.getEncodedType()); - s.write(this, major, minor); - } - } - - public void flush() - { - flushBits(); - } - -} diff --git a/java/common/src/main/java/org/apache/qpidity/BBDecoder.java b/java/common/src/main/java/org/apache/qpidity/BBDecoder.java deleted file mode 100644 index afbb0eddab..0000000000 --- a/java/common/src/main/java/org/apache/qpidity/BBDecoder.java +++ /dev/null @@ -1,53 +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.qpidity; - -import java.nio.ByteBuffer; - - -/** - * BBDecoder - * - * @author Rafael H. Schloming - */ - -class BBDecoder extends AbstractDecoder -{ - - private final ByteBuffer in; - - public BBDecoder(byte major, byte minor, ByteBuffer in) - { - super(major, minor); - this.in = in; - } - - protected byte get() - { - return in.get(); - } - - protected void get(byte[] bytes) - { - in.get(bytes); - } - -} diff --git a/java/common/src/main/java/org/apache/qpidity/BBEncoder.java b/java/common/src/main/java/org/apache/qpidity/BBEncoder.java deleted file mode 100644 index 3e0a0bb2ba..0000000000 --- a/java/common/src/main/java/org/apache/qpidity/BBEncoder.java +++ /dev/null @@ -1,52 +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.qpidity; - -import java.nio.ByteBuffer; - - -/** - * BBEncoder - * - * @author Rafael H. Schloming - */ - -class BBEncoder extends AbstractEncoder -{ - - private final ByteBuffer out; - - public BBEncoder(byte major, byte minor, ByteBuffer out) { - super(major, minor); - this.out = out; - } - - @Override protected void put(byte b) - { - out.put(b); - } - - @Override protected void put(ByteBuffer src) - { - out.put(src); - } - -} diff --git a/java/common/src/main/java/org/apache/qpidity/Channel.java b/java/common/src/main/java/org/apache/qpidity/Channel.java index 483b5e7f21..9df40106fc 100644 --- a/java/common/src/main/java/org/apache/qpidity/Channel.java +++ b/java/common/src/main/java/org/apache/qpidity/Channel.java @@ -25,6 +25,9 @@ import java.nio.ByteBuffer; import java.util.List; import java.util.ArrayList; +import org.apache.qpidity.codec.SegmentEncoder; +import org.apache.qpidity.codec.SizeEncoder; + import static org.apache.qpidity.Frame.*; import static org.apache.qpidity.Functions.*; @@ -49,14 +52,14 @@ public class Channel extends Invoker implements Handler private Method method = null; private List data = null; private int dataSize; - + public Channel(Connection connection, int channel, SessionDelegate delegate) { this.connection = connection; this.channel = channel; this.delegate = new ChannelDelegate(); this.sessionDelegate = delegate; - + tracks = new TrackSwitch(); tracks.map(L1, new MethodHandler (getMajor(), getMinor(), connection.getConnectionDelegate())); diff --git a/java/common/src/main/java/org/apache/qpidity/Decoder.java b/java/common/src/main/java/org/apache/qpidity/Decoder.java deleted file mode 100644 index 13c3d0b7b8..0000000000 --- a/java/common/src/main/java/org/apache/qpidity/Decoder.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.qpidity; - -import java.util.Map; -import java.util.UUID; - -/** - * Decoder - * - * @author Rafael H. Schloming - */ - -public interface Decoder -{ - - boolean readBit(); - short readOctet(); - int readShort(); - long readLong(); - long readLonglong(); - - long readTimestamp(); - - String readShortstr(); - String readLongstr(); - - Map readTable(); - RangeSet readRfc1982LongSet(); - UUID readUuid(); - - String readContent(); - - Struct readLongStruct(); - -} diff --git a/java/common/src/main/java/org/apache/qpidity/Encodable.java b/java/common/src/main/java/org/apache/qpidity/Encodable.java deleted file mode 100644 index 6a734a1791..0000000000 --- a/java/common/src/main/java/org/apache/qpidity/Encodable.java +++ /dev/null @@ -1,37 +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.qpidity; - - -/** - * Encodable - * - * @author Rafael H. Schloming - */ - -public interface Encodable -{ - - void write(Encoder enc, byte major, byte minor); - - void read(Decoder dec, byte major, byte minor); - -} diff --git a/java/common/src/main/java/org/apache/qpidity/Encoder.java b/java/common/src/main/java/org/apache/qpidity/Encoder.java deleted file mode 100644 index 990cef7081..0000000000 --- a/java/common/src/main/java/org/apache/qpidity/Encoder.java +++ /dev/null @@ -1,56 +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.qpidity; - -import java.util.Map; -import java.util.UUID; - -/** - * Encoder - * - * @author Rafael H. Schloming - */ - -public interface Encoder -{ - - void writeBit(boolean b); - void writeOctet(short b); - void writeShort(int s); - void writeLong(long i); - void writeLonglong(long l); - - void writeTimestamp(long l); - - void writeShortstr(String s); - void writeLongstr(String s); - - void writeTable(Map table); - void writeRfc1982LongSet(RangeSet ranges); - void writeUuid(UUID uuid); - - void writeContent(String c); - - void flush(); - - void writeLongStruct(Struct s); - -} diff --git a/java/common/src/main/java/org/apache/qpidity/FragmentDecoder.java b/java/common/src/main/java/org/apache/qpidity/FragmentDecoder.java deleted file mode 100644 index b389d54390..0000000000 --- a/java/common/src/main/java/org/apache/qpidity/FragmentDecoder.java +++ /dev/null @@ -1,111 +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.qpidity; - -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; - -import java.util.Iterator; - -import static java.lang.Math.*; - - -/** - * FragmentDecoder - * - * @author Rafael H. Schloming - */ - -class FragmentDecoder extends AbstractDecoder -{ - - private final Iterator fragments; - private ByteBuffer current; - - public FragmentDecoder(byte major, byte minor, Iterator fragments) - { - super(major, minor); - this.fragments = fragments; - this.current = null; - } - - public boolean hasRemaining() - { - advance(); - return current != null || fragments.hasNext(); - } - - private void advance() - { - while (current == null && fragments.hasNext()) - { - current = fragments.next(); - if (current.hasRemaining()) - { - break; - } - else - { - current = null; - } - } - } - - private void preRead() - { - advance(); - - if (current == null) - { - throw new BufferUnderflowException(); - } - } - - private void postRead() - { - if (current.remaining() == 0) - { - current = null; - } - } - - @Override protected byte get() - { - preRead(); - byte b = current.get(); - postRead(); - return b; - } - - @Override protected void get(byte[] bytes) - { - int remaining = bytes.length; - while (remaining > 0) - { - preRead(); - int size = min(remaining, current.remaining()); - current.get(bytes, 0, size); - remaining -= size; - postRead(); - } - } - -} diff --git a/java/common/src/main/java/org/apache/qpidity/Functions.java b/java/common/src/main/java/org/apache/qpidity/Functions.java index f5217bf038..1008966b01 100644 --- a/java/common/src/main/java/org/apache/qpidity/Functions.java +++ b/java/common/src/main/java/org/apache/qpidity/Functions.java @@ -29,7 +29,7 @@ import java.nio.ByteBuffer; * @author Rafael H. Schloming */ -class Functions +public class Functions { public static final short unsigned(byte b) diff --git a/java/common/src/main/java/org/apache/qpidity/HeaderHandler.java b/java/common/src/main/java/org/apache/qpidity/HeaderHandler.java index 1855681756..8737932712 100644 --- a/java/common/src/main/java/org/apache/qpidity/HeaderHandler.java +++ b/java/common/src/main/java/org/apache/qpidity/HeaderHandler.java @@ -25,6 +25,8 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Iterator; +import org.apache.qpidity.codec.FragmentDecoder; + /** * HeaderHandler diff --git a/java/common/src/main/java/org/apache/qpidity/MethodDecoder.java b/java/common/src/main/java/org/apache/qpidity/MethodDecoder.java index c1cf9b888c..96c6e4d52c 100644 --- a/java/common/src/main/java/org/apache/qpidity/MethodDecoder.java +++ b/java/common/src/main/java/org/apache/qpidity/MethodDecoder.java @@ -24,6 +24,9 @@ import java.nio.ByteBuffer; import java.util.Iterator; +import org.apache.qpidity.codec.Decoder; +import org.apache.qpidity.codec.FragmentDecoder; + /** * MethodDecoder diff --git a/java/common/src/main/java/org/apache/qpidity/SegmentEncoder.java b/java/common/src/main/java/org/apache/qpidity/SegmentEncoder.java deleted file mode 100644 index 25e29bdcf3..0000000000 --- a/java/common/src/main/java/org/apache/qpidity/SegmentEncoder.java +++ /dev/null @@ -1,172 +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.qpidity; - -import java.nio.BufferOverflowException; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - -import static java.lang.Math.*; - -import static org.apache.qpidity.Frame.*; - - -/** - * SegmentEncoder - * - * @author Rafael H. Schloming - */ - -class SegmentEncoder extends AbstractEncoder -{ - - private final Handler handler; - private final int max; - private final byte flags; - private final byte track; - private final byte type; - private final int channel; - - private int remaining; - private ByteBuffer frame; - private boolean first; - - public SegmentEncoder(byte major, byte minor, Handler handler, - int max, byte flags, byte track, byte type, - int channel, int remaining) - { - super(major, minor); - if (max < HEADER_SIZE + 1) - { - throw new IllegalArgumentException - ("max frame size must be large enough to include header"); - } - - this.handler = handler; - this.max = max; - this.flags = flags; - this.track = track; - this.type = type; - this.channel = channel; - this.remaining = remaining; - this.frame = null; - this.first = true; - } - - private void preWrite() { - if (remaining == 0) - { - throw new BufferOverflowException(); - } - - if (frame == null) - { - frame = ByteBuffer.allocate(min(max, remaining + HEADER_SIZE)); - frame.order(ByteOrder.BIG_ENDIAN); - - byte frameFlags = flags; - if (first) { frameFlags |= FIRST_FRAME; first = false; } - if (remaining <= (frame.remaining() - HEADER_SIZE)) - { - frameFlags |= LAST_FRAME; - } - - frame.put(frameFlags); - frame.put(type); - frame.putShort((short) frame.limit()); - frame.put(RESERVED); - frame.put(track); - frame.putShort((short) channel); - frame.put(RESERVED); - frame.put(RESERVED); - frame.put(RESERVED); - frame.put(RESERVED); - - assert frame.position() == HEADER_SIZE; - } - } - - private void postWrite() { - if (!frame.hasRemaining()) - { - frame.flip(); - handler.handle(frame); - frame = null; - } - } - - @Override public void put(byte b) - { - preWrite(); - frame.put(b); - remaining -= 1; - postWrite(); - } - - @Override public void put(ByteBuffer src) - { - if (src.remaining() > remaining) - { - throw new BufferOverflowException(); - } - - while (src.hasRemaining()) - { - preWrite(); - int limit = src.limit(); - src.limit(src.position() + min(frame.remaining(), src.remaining())); - remaining -= src.remaining(); - frame.put(src); - src.limit(limit); - postWrite(); - } - } - - public static final void main(String[] args) { - ByteBuffer buf = ByteBuffer.allocate(1024); - buf.put("AMQP_PROTOCOL_HEADER".getBytes()); - buf.flip(); - - SegmentEncoder enc = new SegmentEncoder((byte) 0, (byte) 10, - new Handler() - { - public void handle(ByteBuffer frame) - { - System.out.println(frame); - } - }, - 16, - (byte) 0x0, - (byte) Frame.L1, - (byte) Frame.METHOD, - 0, - 7 + buf.remaining()); - enc.put((byte)0); - enc.put((byte)1); - enc.put((byte)2); - enc.put((byte)3); - enc.put((byte)4); - enc.put((byte)5); - enc.put((byte)6); - enc.put(buf); - } - -} diff --git a/java/common/src/main/java/org/apache/qpidity/SizeEncoder.java b/java/common/src/main/java/org/apache/qpidity/SizeEncoder.java deleted file mode 100644 index 141c501104..0000000000 --- a/java/common/src/main/java/org/apache/qpidity/SizeEncoder.java +++ /dev/null @@ -1,83 +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.qpidity; - -import java.nio.ByteBuffer; - -import java.util.Map; - - -/** - * SizeEncoder - * - * @author Rafael H. Schloming - */ - -class SizeEncoder extends AbstractEncoder -{ - - private int size; - - public SizeEncoder(byte major, byte minor) { - this(major, minor, 0); - } - - public SizeEncoder(byte major, byte minor, int size) { - super(major, minor); - this.size = size; - } - - public int getSize() { - return size; - } - - public void setSize(int size) { - this.size = size; - } - - @Override protected void put(byte b) - { - size += 1; - } - - @Override protected void put(ByteBuffer src) - { - size += src.remaining(); - } - - @Override public void writeShortstr(String s) - { - if (s == null) { s = ""; } - if (s.length() > 255) { - throw new IllegalArgumentException(s); - } - writeOctet((byte) s.length()); - size += s.length(); - } - - @Override public void writeLongstr(String s) - { - if (s == null) { s = ""; } - writeLong(s.length()); - size += s.length(); - } - -} diff --git a/java/common/src/main/java/org/apache/qpidity/Struct.java b/java/common/src/main/java/org/apache/qpidity/Struct.java index cf8a5c246f..50da4910ab 100644 --- a/java/common/src/main/java/org/apache/qpidity/Struct.java +++ b/java/common/src/main/java/org/apache/qpidity/Struct.java @@ -20,6 +20,8 @@ */ package org.apache.qpidity; +import org.apache.qpidity.codec.Encodable; + /** * Struct @@ -35,6 +37,6 @@ public abstract class Struct implements Delegator, Encodable return StructFactory.create(type); } - abstract int getEncodedType(); + public abstract int getEncodedType(); } diff --git a/java/common/src/main/java/org/apache/qpidity/Stub.java b/java/common/src/main/java/org/apache/qpidity/Stub.java index 94d5932d20..7aa5030672 100644 --- a/java/common/src/main/java/org/apache/qpidity/Stub.java +++ b/java/common/src/main/java/org/apache/qpidity/Stub.java @@ -5,6 +5,10 @@ import java.lang.annotation.*; import java.nio.ByteBuffer; +import org.apache.qpidity.codec.BBEncoder; +import org.apache.qpidity.codec.Encoder; +import org.apache.qpidity.codec.SizeEncoder; + import static org.apache.qpidity.Option.*; diff --git a/java/common/src/main/java/org/apache/qpidity/codec/AbstractDecoder.java b/java/common/src/main/java/org/apache/qpidity/codec/AbstractDecoder.java new file mode 100644 index 0000000000..134c9cdf47 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpidity/codec/AbstractDecoder.java @@ -0,0 +1,195 @@ +/* + * + * 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.qpidity.codec; + +import java.util.Map; +import java.util.UUID; + +import org.apache.qpidity.RangeSet; +import org.apache.qpidity.Struct; + +import static org.apache.qpidity.Functions.*; + + +/** + * AbstractDecoder + * + * @author Rafael H. Schloming + */ + +abstract class AbstractDecoder implements Decoder +{ + + private final byte major; + private final byte minor; + + protected AbstractDecoder(byte major, byte minor) + { + this.major = major; + this.minor = minor; + } + + protected abstract byte get(); + + protected abstract void get(byte[] bytes); + + protected short uget() + { + return unsigned(get()); + } + + private byte bits = 0x0; + private byte nbits = 0; + + public boolean readBit() + { + if (nbits == 0) + { + bits = get(); + nbits = 8; + } + + nbits -= 1; + + boolean result = ((bits >>> nbits) & 0x01) != 0; + return result; + } + + private void clearBits() + { + bits = 0x0; + nbits = 0; + } + + public short readOctet() + { + clearBits(); + return uget(); + } + + public int readShort() + { + clearBits(); + int i = uget() << 8; + i |= uget(); + return i; + } + + public long readLong() + { + clearBits(); + long l = uget() << 24; + l |= uget() << 16; + l |= uget() << 8; + l |= uget(); + return l; + } + + public long readLonglong() + { + clearBits(); + long l = uget() << 56; + l |= uget() << 48; + l |= uget() << 40; + l |= uget() << 32; + l |= uget() << 24; + l |= uget() << 16; + l |= uget() << 8; + l |= uget(); + return l; + } + + public long readTimestamp() + { + return readLonglong(); + } + + + public String readShortstr() + { + short size = readOctet(); + byte[] bytes = new byte[size]; + get(bytes); + return new String(bytes); + } + + public String readLongstr() + { + long size = readLong(); + assert size <= Integer.MAX_VALUE; + byte[] bytes = new byte[(int) size]; + get(bytes); + return new String(bytes); + } + + public Map readTable() + { + //throw new Error("TODO"); + return null; + } + + public RangeSet readRfc1982LongSet() + { + int count = readShort()/8; + if (count == 0) + { + return null; + } + else + { + RangeSet ranges = new RangeSet(); + for (int i = 0; i < count; i++) + { + ranges.add(readLong(), readLong()); + } + return ranges; + } + } + + public UUID readUuid() + { + long msb = readLong(); + long lsb = readLong(); + return new UUID(msb, lsb); + } + + public String readContent() + { + throw new Error("Deprecated"); + } + + public Struct readLongStruct() + { + long size = readLong(); + if (size == 0) + { + return null; + } + else + { + int type = readShort(); + Struct result = Struct.create(type); + result.read(this, major, minor); + return result; + } + } + +} diff --git a/java/common/src/main/java/org/apache/qpidity/codec/AbstractEncoder.java b/java/common/src/main/java/org/apache/qpidity/codec/AbstractEncoder.java new file mode 100644 index 0000000000..b6f875bc5d --- /dev/null +++ b/java/common/src/main/java/org/apache/qpidity/codec/AbstractEncoder.java @@ -0,0 +1,215 @@ +/* + * + * 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.qpidity.codec; + +import java.nio.ByteBuffer; + +import java.util.Map; +import java.util.UUID; + +import org.apache.qpidity.Range; +import org.apache.qpidity.RangeSet; +import org.apache.qpidity.Struct; + +import static org.apache.qpidity.Functions.*; + + +/** + * AbstractEncoder + * + * @author Rafael H. Schloming + */ + +abstract class AbstractEncoder implements Encoder +{ + + private final byte major; + private final byte minor; + + protected AbstractEncoder(byte major, byte minor) + { + this.major = major; + this.minor = minor; + } + + protected abstract void put(byte b); + + protected abstract void put(ByteBuffer src); + + private byte bits = 0x0; + private byte nbits = 0; + + public void writeBit(boolean b) + { + if (b) + { + bits |= 1 << nbits; + } + + nbits += 1; + + if (nbits == 8) + { + flushBits(); + } + } + + private void flushBits() + { + if (nbits > 0) + { + put(bits); + bits = 0x0; + nbits = 0; + } + } + + public void writeOctet(short b) + { + assert b < 0x100; + + flushBits(); + put((byte) b); + } + + public void writeShort(int s) + { + assert s < 0x10000; + + flushBits(); + put(lsb(s >>> 8)); + put(lsb(s)); + } + + public void writeLong(long i) + { + assert i < 0x100000000L; + + flushBits(); + put(lsb(i >>> 24)); + put(lsb(i >>> 16)); + put(lsb(i >>> 8)); + put(lsb(i)); + } + + public void writeLonglong(long l) + { + flushBits(); + put(lsb(l >>> 56)); + put(lsb(l >>> 48)); + put(lsb(l >>> 40)); + put(lsb(l >>> 32)); + put(lsb(l >>> 24)); + put(lsb(l >>> 16)); + put(lsb(l >>> 8)); + put(lsb(l)); + } + + + public void writeTimestamp(long l) + { + flushBits(); + writeLonglong(l); + } + + + public void writeShortstr(String s) + { + if (s == null) { s = ""; } + if (s.length() > 255) { + throw new IllegalArgumentException(s); + } + writeOctet((short) s.length()); + put(ByteBuffer.wrap(s.getBytes())); + } + + public void writeLongstr(String s) + { + if (s == null) { s = ""; } + writeLong(s.length()); + put(ByteBuffer.wrap(s.getBytes())); + } + + + public void writeTable(Map table) + { + //throw new Error("TODO"); + } + + public void writeRfc1982LongSet(RangeSet ranges) + { + if (ranges == null) + { + writeShort((short) 0); + } + else + { + writeShort(ranges.size() * 8); + for (Range range : ranges) + { + writeLong(range.getLower()); + writeLong(range.getUpper()); + } + } + } + + public void writeUuid(UUID uuid) + { + long msb = 0; + long lsb = 0; + if (uuid != null) + { + msb = uuid.getMostSignificantBits(); + uuid.getLeastSignificantBits(); + } + writeLong(msb); + writeLong(lsb); + } + + public void writeContent(String c) + { + throw new Error("Deprecated"); + } + + public void writeLongStruct(Struct s) + { + if (s == null) + { + writeLong(0); + } + else + { + SizeEncoder sizer = new SizeEncoder(major, minor); + sizer.writeShort(s.getEncodedType()); + s.write(sizer, major, minor); + + writeLong(sizer.getSize()); + writeShort(s.getEncodedType()); + s.write(this, major, minor); + } + } + + public void flush() + { + flushBits(); + } + +} diff --git a/java/common/src/main/java/org/apache/qpidity/codec/BBDecoder.java b/java/common/src/main/java/org/apache/qpidity/codec/BBDecoder.java new file mode 100644 index 0000000000..b4e3389dde --- /dev/null +++ b/java/common/src/main/java/org/apache/qpidity/codec/BBDecoder.java @@ -0,0 +1,53 @@ +/* + * + * 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.qpidity.codec; + +import java.nio.ByteBuffer; + + +/** + * BBDecoder + * + * @author Rafael H. Schloming + */ + +public class BBDecoder extends AbstractDecoder +{ + + private final ByteBuffer in; + + public BBDecoder(byte major, byte minor, ByteBuffer in) + { + super(major, minor); + this.in = in; + } + + protected byte get() + { + return in.get(); + } + + protected void get(byte[] bytes) + { + in.get(bytes); + } + +} diff --git a/java/common/src/main/java/org/apache/qpidity/codec/BBEncoder.java b/java/common/src/main/java/org/apache/qpidity/codec/BBEncoder.java new file mode 100644 index 0000000000..bbc6ba0ad9 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpidity/codec/BBEncoder.java @@ -0,0 +1,52 @@ +/* + * + * 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.qpidity.codec; + +import java.nio.ByteBuffer; + + +/** + * BBEncoder + * + * @author Rafael H. Schloming + */ + +public class BBEncoder extends AbstractEncoder +{ + + private final ByteBuffer out; + + public BBEncoder(byte major, byte minor, ByteBuffer out) { + super(major, minor); + this.out = out; + } + + @Override protected void put(byte b) + { + out.put(b); + } + + @Override protected void put(ByteBuffer src) + { + out.put(src); + } + +} diff --git a/java/common/src/main/java/org/apache/qpidity/codec/Decoder.java b/java/common/src/main/java/org/apache/qpidity/codec/Decoder.java new file mode 100644 index 0000000000..ec91c877f8 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpidity/codec/Decoder.java @@ -0,0 +1,58 @@ +/* + * + * 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.qpidity.codec; + +import java.util.Map; +import java.util.UUID; + +import org.apache.qpidity.RangeSet; +import org.apache.qpidity.Struct; + + +/** + * Decoder + * + * @author Rafael H. Schloming + */ + +public interface Decoder +{ + + boolean readBit(); + short readOctet(); + int readShort(); + long readLong(); + long readLonglong(); + + long readTimestamp(); + + String readShortstr(); + String readLongstr(); + + Map readTable(); + RangeSet readRfc1982LongSet(); + UUID readUuid(); + + String readContent(); + + Struct readLongStruct(); + +} diff --git a/java/common/src/main/java/org/apache/qpidity/codec/Encodable.java b/java/common/src/main/java/org/apache/qpidity/codec/Encodable.java new file mode 100644 index 0000000000..b57c12fefe --- /dev/null +++ b/java/common/src/main/java/org/apache/qpidity/codec/Encodable.java @@ -0,0 +1,37 @@ +/* + * + * 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.qpidity.codec; + + +/** + * Encodable + * + * @author Rafael H. Schloming + */ + +public interface Encodable +{ + + void write(Encoder enc, byte major, byte minor); + + void read(Decoder dec, byte major, byte minor); + +} diff --git a/java/common/src/main/java/org/apache/qpidity/codec/Encoder.java b/java/common/src/main/java/org/apache/qpidity/codec/Encoder.java new file mode 100644 index 0000000000..b879ed5cd7 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpidity/codec/Encoder.java @@ -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.qpidity.codec; + +import java.util.Map; +import java.util.UUID; + +import org.apache.qpidity.RangeSet; +import org.apache.qpidity.Struct; + + +/** + * Encoder + * + * @author Rafael H. Schloming + */ + +public interface Encoder +{ + + void writeBit(boolean b); + void writeOctet(short b); + void writeShort(int s); + void writeLong(long i); + void writeLonglong(long l); + + void writeTimestamp(long l); + + void writeShortstr(String s); + void writeLongstr(String s); + + void writeTable(Map table); + void writeRfc1982LongSet(RangeSet ranges); + void writeUuid(UUID uuid); + + void writeContent(String c); + + void flush(); + + void writeLongStruct(Struct s); + +} diff --git a/java/common/src/main/java/org/apache/qpidity/codec/FragmentDecoder.java b/java/common/src/main/java/org/apache/qpidity/codec/FragmentDecoder.java new file mode 100644 index 0000000000..53183e5007 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpidity/codec/FragmentDecoder.java @@ -0,0 +1,111 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpidity.codec; + +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; + +import java.util.Iterator; + +import static java.lang.Math.*; + + +/** + * FragmentDecoder + * + * @author Rafael H. Schloming + */ + +public class FragmentDecoder extends AbstractDecoder +{ + + private final Iterator fragments; + private ByteBuffer current; + + public FragmentDecoder(byte major, byte minor, Iterator fragments) + { + super(major, minor); + this.fragments = fragments; + this.current = null; + } + + public boolean hasRemaining() + { + advance(); + return current != null || fragments.hasNext(); + } + + private void advance() + { + while (current == null && fragments.hasNext()) + { + current = fragments.next(); + if (current.hasRemaining()) + { + break; + } + else + { + current = null; + } + } + } + + private void preRead() + { + advance(); + + if (current == null) + { + throw new BufferUnderflowException(); + } + } + + private void postRead() + { + if (current.remaining() == 0) + { + current = null; + } + } + + @Override protected byte get() + { + preRead(); + byte b = current.get(); + postRead(); + return b; + } + + @Override protected void get(byte[] bytes) + { + int remaining = bytes.length; + while (remaining > 0) + { + preRead(); + int size = min(remaining, current.remaining()); + current.get(bytes, 0, size); + remaining -= size; + postRead(); + } + } + +} diff --git a/java/common/src/main/java/org/apache/qpidity/codec/SegmentEncoder.java b/java/common/src/main/java/org/apache/qpidity/codec/SegmentEncoder.java new file mode 100644 index 0000000000..8fb396f278 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpidity/codec/SegmentEncoder.java @@ -0,0 +1,175 @@ +/* + * + * 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.qpidity.codec; + +import java.nio.BufferOverflowException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +import org.apache.qpidity.Frame; +import org.apache.qpidity.Handler; + +import static java.lang.Math.*; + +import static org.apache.qpidity.Frame.*; + + +/** + * SegmentEncoder + * + * @author Rafael H. Schloming + */ + +public class SegmentEncoder extends AbstractEncoder +{ + + private final Handler handler; + private final int max; + private final byte flags; + private final byte track; + private final byte type; + private final int channel; + + private int remaining; + private ByteBuffer frame; + private boolean first; + + public SegmentEncoder(byte major, byte minor, Handler handler, + int max, byte flags, byte track, byte type, + int channel, int remaining) + { + super(major, minor); + if (max < HEADER_SIZE + 1) + { + throw new IllegalArgumentException + ("max frame size must be large enough to include header"); + } + + this.handler = handler; + this.max = max; + this.flags = flags; + this.track = track; + this.type = type; + this.channel = channel; + this.remaining = remaining; + this.frame = null; + this.first = true; + } + + private void preWrite() { + if (remaining == 0) + { + throw new BufferOverflowException(); + } + + if (frame == null) + { + frame = ByteBuffer.allocate(min(max, remaining + HEADER_SIZE)); + frame.order(ByteOrder.BIG_ENDIAN); + + byte frameFlags = flags; + if (first) { frameFlags |= FIRST_FRAME; first = false; } + if (remaining <= (frame.remaining() - HEADER_SIZE)) + { + frameFlags |= LAST_FRAME; + } + + frame.put(frameFlags); + frame.put(type); + frame.putShort((short) frame.limit()); + frame.put(RESERVED); + frame.put(track); + frame.putShort((short) channel); + frame.put(RESERVED); + frame.put(RESERVED); + frame.put(RESERVED); + frame.put(RESERVED); + + assert frame.position() == HEADER_SIZE; + } + } + + private void postWrite() { + if (!frame.hasRemaining()) + { + frame.flip(); + handler.handle(frame); + frame = null; + } + } + + @Override public void put(byte b) + { + preWrite(); + frame.put(b); + remaining -= 1; + postWrite(); + } + + @Override public void put(ByteBuffer src) + { + if (src.remaining() > remaining) + { + throw new BufferOverflowException(); + } + + while (src.hasRemaining()) + { + preWrite(); + int limit = src.limit(); + src.limit(src.position() + min(frame.remaining(), src.remaining())); + remaining -= src.remaining(); + frame.put(src); + src.limit(limit); + postWrite(); + } + } + + public static final void main(String[] args) { + ByteBuffer buf = ByteBuffer.allocate(1024); + buf.put("AMQP_PROTOCOL_HEADER".getBytes()); + buf.flip(); + + SegmentEncoder enc = new SegmentEncoder((byte) 0, (byte) 10, + new Handler() + { + public void handle(ByteBuffer frame) + { + System.out.println(frame); + } + }, + 16, + (byte) 0x0, + (byte) Frame.L1, + (byte) Frame.METHOD, + 0, + 7 + buf.remaining()); + enc.put((byte)0); + enc.put((byte)1); + enc.put((byte)2); + enc.put((byte)3); + enc.put((byte)4); + enc.put((byte)5); + enc.put((byte)6); + enc.put(buf); + } + +} diff --git a/java/common/src/main/java/org/apache/qpidity/codec/SizeEncoder.java b/java/common/src/main/java/org/apache/qpidity/codec/SizeEncoder.java new file mode 100644 index 0000000000..a12cbef50a --- /dev/null +++ b/java/common/src/main/java/org/apache/qpidity/codec/SizeEncoder.java @@ -0,0 +1,83 @@ +/* + * + * 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.qpidity.codec; + +import java.nio.ByteBuffer; + +import java.util.Map; + + +/** + * SizeEncoder + * + * @author Rafael H. Schloming + */ + +public class SizeEncoder extends AbstractEncoder +{ + + private int size; + + public SizeEncoder(byte major, byte minor) { + this(major, minor, 0); + } + + public SizeEncoder(byte major, byte minor, int size) { + super(major, minor); + this.size = size; + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + + @Override protected void put(byte b) + { + size += 1; + } + + @Override protected void put(ByteBuffer src) + { + size += src.remaining(); + } + + @Override public void writeShortstr(String s) + { + if (s == null) { s = ""; } + if (s.length() > 255) { + throw new IllegalArgumentException(s); + } + writeOctet((byte) s.length()); + size += s.length(); + } + + @Override public void writeLongstr(String s) + { + if (s == null) { s = ""; } + writeLong(s.length()); + size += s.length(); + } + +} -- cgit v1.2.1