Skip to content

Commit 89041d5

Browse files
committed
merge bug20154 into default
2 parents ea62faa + c98773d commit 89041d5

File tree

9 files changed

+647
-237
lines changed

9 files changed

+647
-237
lines changed

src/com/rabbitmq/client/impl/ContentHeaderPropertyReader.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
*/
4646
public class ContentHeaderPropertyReader {
4747
/** Stream we are reading from */
48-
private final DataInputStream in;
48+
private final ValueReader in;
4949

5050
/** Current field flag word */
5151
public int flagWord;
@@ -57,7 +57,7 @@ public class ContentHeaderPropertyReader {
5757
* Protected API - Constructs a reader from the given input stream
5858
*/
5959
public ContentHeaderPropertyReader(DataInputStream in) throws IOException {
60-
this.in = in;
60+
this.in = new ValueReader(in);
6161
this.flagWord = 1; // just the continuation bit
6262
this.bitCount = 15; // forces a flagWord read
6363
}
@@ -94,41 +94,41 @@ public void finishPresence() throws IOException {
9494

9595
/** Reads and returns an AMQP short string content header field. */
9696
public String readShortstr() throws IOException {
97-
return MethodArgumentReader.readShortstr(in);
97+
return in.readShortstr();
9898
}
9999

100100
/** Reads and returns an AMQP "long string" (binary) content header field. */
101101
public LongString readLongstr() throws IOException {
102-
return MethodArgumentReader.readLongstr(in);
102+
return in.readLongstr();
103103
}
104104

105105
/** Reads and returns an AMQP short integer content header field. */
106106
public Integer readShort() throws IOException {
107-
return new Integer(in.readUnsignedShort());
107+
return in.readShort();
108108
}
109109

110110
/** Reads and returns an AMQP integer content header field. */
111111
public Integer readLong() throws IOException {
112-
return in.readInt();
112+
return in.readLong();
113113
}
114114

115115
/** Reads and returns an AMQP long integer content header field. */
116116
public Long readLonglong() throws IOException {
117-
return new Long(in.readLong());
117+
return in.readLonglong();
118118
}
119119

120120
/** Reads and returns an AMQP table content header field. */
121121
public Map<String, Object> readTable() throws IOException {
122-
return MethodArgumentReader.readTable(in);
122+
return in.readTable();
123123
}
124124

125125
/** Reads and returns an AMQP octet content header field. */
126126
public Integer readOctet() throws IOException {
127-
return in.readUnsignedByte();
127+
return in.readOctet();
128128
}
129129

130130
/** Reads and returns an AMQP timestamp content header field. */
131131
public Date readTimestamp() throws IOException {
132-
return MethodArgumentReader.readTimestamp(in);
132+
return in.readTimestamp();
133133
}
134134
}

src/com/rabbitmq/client/impl/ContentHeaderPropertyWriter.java

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@
3838
import java.util.Date;
3939
import java.util.Map;
4040

41-
import org.apache.commons.io.IOUtils;
42-
4341
import com.rabbitmq.client.ContentHeader;
4442

4543
/**
4644
* Generates an AMQP wire-protocol packet from a {@link ContentHeader}. Methods on this object are usually called from autogenerated code.
4745
*/
4846
public class ContentHeaderPropertyWriter {
49-
/** DataOutputStream wrapper for outBytes */
50-
public DataOutputStream out;
47+
/** Accumulates our output */
48+
private final ValueWriter out;
5149

5250
/** Current flags word being accumulated */
5351
public int flagWord;
@@ -59,7 +57,7 @@ public class ContentHeaderPropertyWriter {
5957
* Constructs a fresh ContentHeaderPropertyWriter.
6058
*/
6159
public ContentHeaderPropertyWriter(DataOutputStream out) {
62-
this.out = out;
60+
this.out = new ValueWriter(out);
6361
this.flagWord = 0;
6462
this.bitCount = 0;
6563
}
@@ -87,75 +85,38 @@ public void finishPresence() throws IOException {
8785
}
8886

8987
public void writeShortstr(String str) throws IOException {
90-
byte[] bytes = str.getBytes("utf-8");
91-
out.writeByte(bytes.length);
92-
out.write(bytes);
88+
out.writeShortstr(str);
9389
}
9490

9591
public void writeLongstr(String str) throws IOException {
96-
byte[] bytes = str.getBytes("utf-8");
97-
out.writeInt(bytes.length);
98-
out.write(bytes);
92+
out.writeLongstr(str);
9993
}
10094

10195
public void writeLongstr(LongString str) throws IOException {
102-
out.writeInt((int) str.length());
103-
IOUtils.copy(str.getStream(), out);
96+
out.writeLongstr(str);
10497
}
10598

10699
public void writeShort(Integer s) throws IOException {
107100
out.writeShort(s);
108101
}
109102

110103
public void writeLong(Integer l) throws IOException {
111-
out.writeInt(l);
104+
out.writeLong(l);
112105
}
113106

114107
public void writeLonglong(Long ll) throws IOException {
115-
out.writeLong(ll.longValue());
108+
out.writeLonglong(ll);
116109
}
117110

118111
public void writeTable(Map<String, Object> table) throws IOException {
119-
out.writeInt((int) Frame.tableSize(table));
120-
for (Map.Entry<String, Object> entry : table.entrySet()) {
121-
122-
writeShortstr(entry.getKey());
123-
124-
Object value = entry.getValue();
125-
if (value instanceof String) {
126-
out.writeByte('S');
127-
writeLongstr((String) value);
128-
} else if (value instanceof LongString) {
129-
out.writeByte('S');
130-
writeLongstr((LongString) value);
131-
} else if (value instanceof Integer) {
132-
out.writeByte('I');
133-
writeLong((Integer) value);
134-
} else if (value instanceof BigDecimal) {
135-
out.writeByte('D');
136-
BigDecimal decimal = (BigDecimal) value;
137-
out.writeByte(decimal.scale());
138-
BigInteger unscaled = decimal.unscaledValue();
139-
if (unscaled.bitLength() > 32) /* Integer.SIZE in Java 1.5 */
140-
throw new IllegalArgumentException("BigDecimal too large to be encoded");
141-
out.writeInt(decimal.unscaledValue().intValue());
142-
} else if (value instanceof Date) {
143-
out.writeByte('T');
144-
writeTimestamp((Date) value);
145-
} else if (value instanceof Map) {
146-
out.writeByte('F');
147-
writeTable((Map<String, Object>) value);
148-
} else {
149-
throw new IllegalArgumentException("Invalid value type: " + value.getClass().getName() + " for key " + entry.getKey());
150-
}
151-
}
112+
out.writeTable(table);
152113
}
153114

154115
public void writeOctet(Integer octet) throws IOException {
155-
out.writeByte(octet);
116+
out.writeOctet(octet);
156117
}
157118

158119
public void writeTimestamp(Date timestamp) throws IOException {
159-
out.writeLong(timestamp.getTime() / 1000);
120+
out.writeTimestamp(timestamp);
160121
}
161122
}

src/com/rabbitmq/client/impl/Frame.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,7 @@ public static long tableSize(Map<String, Object> table)
273273
acc += longStrSize((String)entry.getValue());
274274
}
275275
else if(value instanceof LongString) {
276-
acc += 4;
277-
acc += ((LongString)value).length();
276+
acc += 4 + ((LongString)value).length();
278277
}
279278
else if(value instanceof Integer) {
280279
acc += 4;
@@ -286,8 +285,30 @@ else if(value instanceof Date || value instanceof Timestamp) {
286285
acc += 8;
287286
}
288287
else if(value instanceof Map) {
288+
acc += 4 + tableSize((Map<String, Object>) value);
289+
}
290+
else if (value instanceof Byte) {
291+
acc += 1;
292+
}
293+
else if(value instanceof Double) {
294+
acc += 8;
295+
}
296+
else if(value instanceof Float) {
289297
acc += 4;
290-
acc += tableSize((Map<String, Object>) value);
298+
}
299+
else if(value instanceof Long) {
300+
acc += 8;
301+
}
302+
else if(value instanceof Short) {
303+
acc += 2;
304+
}
305+
else if(value instanceof Boolean) {
306+
acc += 1;
307+
}
308+
else if(value instanceof byte[]) {
309+
acc += 4 + ((byte[])value).length;
310+
}
311+
else if(value == null) {
291312
}
292313
else {
293314
throw new IllegalArgumentException("invalid value in table");

0 commit comments

Comments
 (0)