ACCUMULO-4113 Fix ByteBuffer misuse thats occurs in 1.7 and not in 1.6
Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/07205156 Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/07205156 Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/07205156 Branch: refs/heads/master Commit: 07205156b6f87780d7ca04c475ccafdc0ba5fc36 Parents: 1b905ce Author: Keith Turner <ktur...@apache.org> Authored: Wed Jan 20 13:24:41 2016 -0500 Committer: Keith Turner <ktur...@apache.org> Committed: Wed Jan 20 13:24:41 2016 -0500 ---------------------------------------------------------------------- .../client/impl/AuthenticationTokenIdentifier.java | 3 ++- .../org/apache/accumulo/core/util/ByteBufferUtil.java | 11 +++++++++++ .../apache/accumulo/core/util/ThriftMessageUtil.java | 8 -------- .../apache/accumulo/core/util/ByteBufferUtilTest.java | 14 ++++++++++++++ .../accumulo/core/util/ThriftMessageUtilTest.java | 8 -------- .../java/org/apache/accumulo/proxy/ProxyServer.java | 2 +- .../server/security/delegation/AuthenticationKey.java | 3 ++- 7 files changed, 30 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/accumulo/blob/07205156/core/src/main/java/org/apache/accumulo/core/client/impl/AuthenticationTokenIdentifier.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/client/impl/AuthenticationTokenIdentifier.java b/core/src/main/java/org/apache/accumulo/core/client/impl/AuthenticationTokenIdentifier.java index 1f548bc..eaf0ce7 100644 --- a/core/src/main/java/org/apache/accumulo/core/client/impl/AuthenticationTokenIdentifier.java +++ b/core/src/main/java/org/apache/accumulo/core/client/impl/AuthenticationTokenIdentifier.java @@ -25,6 +25,7 @@ import java.nio.ByteBuffer; import org.apache.accumulo.core.client.admin.DelegationTokenConfig; import org.apache.accumulo.core.security.thrift.TAuthenticationTokenIdentifier; +import org.apache.accumulo.core.util.ByteBufferUtil; import org.apache.accumulo.core.util.ThriftMessageUtil; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.hadoop.io.Text; @@ -131,7 +132,7 @@ public class AuthenticationTokenIdentifier extends TokenIdentifier { ThriftMessageUtil msgUtil = new ThriftMessageUtil(); ByteBuffer serialized = msgUtil.serialize(impl); out.writeInt(serialized.limit()); - out.write(serialized.array(), serialized.arrayOffset(), serialized.limit()); + ByteBufferUtil.write(out, serialized); } else { out.writeInt(0); } http://git-wip-us.apache.org/repos/asf/accumulo/blob/07205156/core/src/main/java/org/apache/accumulo/core/util/ByteBufferUtil.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/util/ByteBufferUtil.java b/core/src/main/java/org/apache/accumulo/core/util/ByteBufferUtil.java index ad41540..85c3e12 100644 --- a/core/src/main/java/org/apache/accumulo/core/util/ByteBufferUtil.java +++ b/core/src/main/java/org/apache/accumulo/core/util/ByteBufferUtil.java @@ -18,6 +18,8 @@ package org.apache.accumulo.core.util; import static java.nio.charset.StandardCharsets.UTF_8; +import java.io.DataOutput; +import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; @@ -93,4 +95,13 @@ public class ByteBufferUtil { return ByteBuffer.wrap(bs.toArray()); } } + + public static void write(DataOutput out, ByteBuffer buffer) throws IOException { + if (buffer.hasArray()) { + out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); + } else { + out.write(toBytes(buffer)); + } + + } } http://git-wip-us.apache.org/repos/asf/accumulo/blob/07205156/core/src/main/java/org/apache/accumulo/core/util/ThriftMessageUtil.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/util/ThriftMessageUtil.java b/core/src/main/java/org/apache/accumulo/core/util/ThriftMessageUtil.java index 611d21c..7405f27 100644 --- a/core/src/main/java/org/apache/accumulo/core/util/ThriftMessageUtil.java +++ b/core/src/main/java/org/apache/accumulo/core/util/ThriftMessageUtil.java @@ -69,14 +69,6 @@ public class ThriftMessageUtil { } /** - * @see #deserialize(byte[], int, int, TBase) - */ - public <T extends TBase<?,?>> T deserialize(ByteBuffer serialized, T instance) throws IOException { - checkNotNull(serialized); - return deserialize(serialized.array(), serialized.arrayOffset(), serialized.limit(), instance); - } - - /** * Assumes the entire contents of the byte array compose the serialized {@code instance} * * @see #deserialize(byte[], int, int, TBase) http://git-wip-us.apache.org/repos/asf/accumulo/blob/07205156/core/src/test/java/org/apache/accumulo/core/util/ByteBufferUtilTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/util/ByteBufferUtilTest.java b/core/src/test/java/org/apache/accumulo/core/util/ByteBufferUtilTest.java index f4ca0e9..f27a1ba 100644 --- a/core/src/test/java/org/apache/accumulo/core/util/ByteBufferUtilTest.java +++ b/core/src/test/java/org/apache/accumulo/core/util/ByteBufferUtilTest.java @@ -17,6 +17,9 @@ package org.apache.accumulo.core.util; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.nio.ByteBuffer; import java.util.Collections; import java.util.List; @@ -40,6 +43,17 @@ public class ByteBufferUtilTest { Assert.assertEquals(expected, new String(bal.get(0), Charsets.UTF_8)); Assert.assertEquals(new ArrayByteSequence(expected), new ArrayByteSequence(bb)); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + try { + ByteBufferUtil.write(dos, bb); + dos.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Assert.assertEquals(expected, new String(baos.toByteArray(), Charsets.UTF_8)); } @Test http://git-wip-us.apache.org/repos/asf/accumulo/blob/07205156/core/src/test/java/org/apache/accumulo/core/util/ThriftMessageUtilTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/util/ThriftMessageUtilTest.java b/core/src/test/java/org/apache/accumulo/core/util/ThriftMessageUtilTest.java index 765d9ca..a84af88 100644 --- a/core/src/test/java/org/apache/accumulo/core/util/ThriftMessageUtilTest.java +++ b/core/src/test/java/org/apache/accumulo/core/util/ThriftMessageUtilTest.java @@ -37,14 +37,6 @@ public class ThriftMessageUtilTest { } @Test - public void testSerialization() throws IOException { - ByteBuffer buff = util.serialize(msg); - TAuthenticationTokenIdentifier bbMsg = new TAuthenticationTokenIdentifier(); - util.deserialize(buff, bbMsg); - assertEquals(msg, bbMsg); - } - - @Test public void testSerializationAsByteArray() throws IOException { ByteBuffer buff = util.serialize(msg); TAuthenticationTokenIdentifier copy = new TAuthenticationTokenIdentifier(); http://git-wip-us.apache.org/repos/asf/accumulo/blob/07205156/proxy/src/main/java/org/apache/accumulo/proxy/ProxyServer.java ---------------------------------------------------------------------- diff --git a/proxy/src/main/java/org/apache/accumulo/proxy/ProxyServer.java b/proxy/src/main/java/org/apache/accumulo/proxy/ProxyServer.java index 88dad8d..d8b678a 100644 --- a/proxy/src/main/java/org/apache/accumulo/proxy/ProxyServer.java +++ b/proxy/src/main/java/org/apache/accumulo/proxy/ProxyServer.java @@ -226,7 +226,7 @@ public class ProxyServer implements AccumuloProxy.Iface { } protected Connector getConnector(ByteBuffer login) throws Exception { - String[] pair = new String(login.array(), login.position(), login.remaining(), UTF_8).split(",", 2); + String[] pair = ByteBufferUtil.toString(login).split(",", 2); if (instance.getInstanceID().equals(pair[0])) { Credentials creds = Credentials.deserialize(pair[1]); return instance.getConnector(creds.getPrincipal(), creds.getToken()); http://git-wip-us.apache.org/repos/asf/accumulo/blob/07205156/server/base/src/main/java/org/apache/accumulo/server/security/delegation/AuthenticationKey.java ---------------------------------------------------------------------- diff --git a/server/base/src/main/java/org/apache/accumulo/server/security/delegation/AuthenticationKey.java b/server/base/src/main/java/org/apache/accumulo/server/security/delegation/AuthenticationKey.java index 35fd115..2907099 100644 --- a/server/base/src/main/java/org/apache/accumulo/server/security/delegation/AuthenticationKey.java +++ b/server/base/src/main/java/org/apache/accumulo/server/security/delegation/AuthenticationKey.java @@ -29,6 +29,7 @@ import java.util.Objects; import javax.crypto.SecretKey; import org.apache.accumulo.core.security.thrift.TAuthenticationKey; +import org.apache.accumulo.core.util.ByteBufferUtil; import org.apache.accumulo.core.util.ThriftMessageUtil; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.hadoop.io.Writable; @@ -124,7 +125,7 @@ public class AuthenticationKey implements Writable { ThriftMessageUtil util = new ThriftMessageUtil(); ByteBuffer serialized = util.serialize(authKey); WritableUtils.writeVInt(out, serialized.limit() - serialized.arrayOffset()); - out.write(serialized.array(), serialized.arrayOffset(), serialized.limit()); + ByteBufferUtil.write(out, serialized); } @Override