You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by kw...@apache.org on 2016/11/04 16:45:30 UTC
svn commit: r1768064 - in
/qpid/java/trunk/common/src/main/java/org/apache/qpid/transport/codec:
AbstractDecoder.java AbstractEncoder.java
Author: kwall
Date: Fri Nov 4 16:45:29 2016
New Revision: 1768064
URL: http://svn.apache.org/viewvc?rev=1768064&view=rev
Log:
QPID-6786: [Java Common] Prevent AbstractEncoder generating str8 or str16 that exceed the type's limits
Also use StandardCharsets.UTF_8 to avoid the exception handling clutter.
Modified:
qpid/java/trunk/common/src/main/java/org/apache/qpid/transport/codec/AbstractDecoder.java
qpid/java/trunk/common/src/main/java/org/apache/qpid/transport/codec/AbstractEncoder.java
Modified: qpid/java/trunk/common/src/main/java/org/apache/qpid/transport/codec/AbstractDecoder.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/common/src/main/java/org/apache/qpid/transport/codec/AbstractDecoder.java?rev=1768064&r1=1768063&r2=1768064&view=diff
==============================================================================
--- qpid/java/trunk/common/src/main/java/org/apache/qpid/transport/codec/AbstractDecoder.java (original)
+++ qpid/java/trunk/common/src/main/java/org/apache/qpid/transport/codec/AbstractDecoder.java Fri Nov 4 16:45:29 2016
@@ -27,7 +27,7 @@ import org.apache.qpid.transport.RangeSe
import org.apache.qpid.transport.Struct;
import org.apache.qpid.transport.Type;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
@@ -120,23 +120,6 @@ public abstract class AbstractDecoder im
return readUint64();
}
- private static final String decode(byte[] bytes, int offset, int length, String charset)
- {
- try
- {
- return new String(bytes, offset, length, charset);
- }
- catch (UnsupportedEncodingException e)
- {
- throw new RuntimeException(e);
- }
- }
-
- private static final String decode(byte[] bytes, String charset)
- {
- return decode(bytes, 0, bytes.length, charset);
- }
-
public String readStr8()
{
short size = readUint8();
@@ -145,7 +128,7 @@ public abstract class AbstractDecoder im
if (str == null)
{
- str = decode(bin.array(), bin.offset(), bin.size(), "UTF-8");
+ str = new String(bin.array(), bin.offset(), bin.size(), StandardCharsets.UTF_8);
if(bin.hasExcessCapacity())
{
str8cache.put(bin.copy(), str);
@@ -163,7 +146,7 @@ public abstract class AbstractDecoder im
int size = readUint16();
byte[] bytes = new byte[size];
get(bytes);
- return decode(bytes, "UTF-8");
+ return new String(bytes, StandardCharsets.UTF_8);
}
public byte[] readVbin8()
@@ -221,11 +204,6 @@ public abstract class AbstractDecoder im
return new UUID(msb, lsb);
}
- public String readContent()
- {
- throw new Error("Deprecated");
- }
-
public Struct readStruct(int type)
{
Struct st = Struct.create(type);
@@ -276,10 +254,10 @@ public abstract class AbstractDecoder im
if (count == 0)
{
- return Collections.EMPTY_MAP;
+ return Collections.emptyMap();
}
- Map<String,Object> result = new LinkedHashMap();
+ Map<String,Object> result = new LinkedHashMap<>();
for (int i = 0; i < count; i++)
{
String key = readStr8();
@@ -305,10 +283,10 @@ public abstract class AbstractDecoder im
if (count == 0)
{
- return Collections.EMPTY_LIST;
+ return Collections.emptyList();
}
- List<Object> result = new ArrayList();
+ List<Object> result = new ArrayList<>();
for (int i = 0; i < count; i++)
{
byte code = get();
@@ -334,10 +312,10 @@ public abstract class AbstractDecoder im
if (count == 0)
{
- return Collections.EMPTY_LIST;
+ return Collections.emptyList();
}
- List<Object> result = new ArrayList<Object>();
+ List<Object> result = new ArrayList<>();
for (int i = 0; i < count; i++)
{
Object value = read(t);
Modified: qpid/java/trunk/common/src/main/java/org/apache/qpid/transport/codec/AbstractEncoder.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/common/src/main/java/org/apache/qpid/transport/codec/AbstractEncoder.java?rev=1768064&r1=1768063&r2=1768064&view=diff
==============================================================================
--- qpid/java/trunk/common/src/main/java/org/apache/qpid/transport/codec/AbstractEncoder.java (original)
+++ qpid/java/trunk/common/src/main/java/org/apache/qpid/transport/codec/AbstractEncoder.java Fri Nov 4 16:45:29 2016
@@ -22,8 +22,8 @@ package org.apache.qpid.transport.codec;
import static org.apache.qpid.transport.util.Functions.lsb;
-import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
@@ -145,18 +145,6 @@ public abstract class AbstractEncoder im
writeUint64(l);
}
- private static final byte[] encode(String s, String charset)
- {
- try
- {
- return s.getBytes(charset);
- }
- catch (UnsupportedEncodingException e)
- {
- throw new RuntimeException(e);
- }
- }
-
public void writeStr8(String s)
{
if (s == null)
@@ -167,7 +155,11 @@ public abstract class AbstractEncoder im
byte[] bytes = str8cache.get(s);
if (bytes == null)
{
- bytes = encode(s, "UTF-8");
+ bytes = s.getBytes(StandardCharsets.UTF_8);
+ if (bytes.length > 255)
+ {
+ throw new IllegalArgumentException(String.format("String too long (%d) for str8", bytes.length));
+ }
str8cache.put(s, bytes);
}
writeUint8((short) bytes.length);
@@ -181,7 +173,12 @@ public abstract class AbstractEncoder im
s = "";
}
- byte[] bytes = encode(s, "UTF-8");
+ byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
+ if (bytes.length > 65535)
+ {
+ throw new IllegalArgumentException(String.format("String too long (%d) for str16", bytes.length));
+ }
+
writeUint16(bytes.length);
put(bytes);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org