Make BBU.string validate input for the desired Charset

patch by jbellis; reviewed by slebresne for CASSANDRA-2091

git-svn-id: https://svn.apache.org/repos/asf/cassandra/branches/cassandra-0.7@1067490 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jonathan Ellis 2011-02-05 19:38:35 +00:00
parent a8c2274780
commit 46ea05fa12
11 changed files with 71 additions and 36 deletions

View File

@ -19,6 +19,7 @@ package org.apache.cassandra.cli;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.util.*; import java.util.*;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
@ -936,7 +937,7 @@ public class CliClient extends CliUserHelp
} }
private void executeList(Tree statement) private void executeList(Tree statement)
throws TException, InvalidRequestException, NotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException, UnavailableException, TimedOutException throws TException, InvalidRequestException, NotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException, UnavailableException, TimedOutException, CharacterCodingException
{ {
if (!CliMain.isConnected() || !hasKeySpace()) if (!CliMain.isConnected() || !hasKeySpace())
return; return;
@ -1896,7 +1897,7 @@ public class CliClient extends CliUserHelp
* @throws NoSuchFieldException - column not found * @throws NoSuchFieldException - column not found
*/ */
private void printSliceList(CfDef columnFamilyDef, List<KeySlice> slices) private void printSliceList(CfDef columnFamilyDef, List<KeySlice> slices)
throws NotFoundException, TException, IllegalAccessException, InstantiationException, NoSuchFieldException throws NotFoundException, TException, IllegalAccessException, InstantiationException, NoSuchFieldException, CharacterCodingException
{ {
AbstractType validator; AbstractType validator;
String columnFamilyName = columnFamilyDef.getName(); String columnFamilyName = columnFamilyDef.getName();

View File

@ -23,6 +23,7 @@ import java.lang.management.ManagementFactory;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.util.*; import java.util.*;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
@ -229,12 +230,17 @@ public class HintedHandOffManager implements HintedHandOffManagerMBean
int index = ByteBufferUtil.lastIndexOf(joined, SEPARATOR.getBytes()[0], joined.limit()); int index = ByteBufferUtil.lastIndexOf(joined, SEPARATOR.getBytes()[0], joined.limit());
if (index == -1 || index < (joined.position() + 1)) if (index == -1 || index < (joined.position() + 1))
throw new RuntimeException("Corrupted hint name " + ByteBufferUtil.string(joined)); throw new RuntimeException("Corrupted hint name " + ByteBufferUtil.bytesToHex(joined));
return new String[] { try
ByteBufferUtil.string(joined, joined.position(), index - joined.position()), {
ByteBufferUtil.string(joined, index + 1, joined.limit() - (index + 1)) return new String[] { ByteBufferUtil.string(joined, joined.position(), index - joined.position()),
}; ByteBufferUtil.string(joined, index + 1, joined.limit() - (index + 1)) };
}
catch (CharacterCodingException e)
{
throw new RuntimeException(e);
}
} }
private int waitForSchemaAgreement(InetAddress endpoint) throws InterruptedException private int waitForSchemaAgreement(InetAddress endpoint) throws InterruptedException

View File

@ -22,6 +22,7 @@ package org.apache.cassandra.db.marshal;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
@ -36,7 +37,14 @@ public class AsciiType extends BytesType
@Override @Override
public String getString(ByteBuffer bytes) public String getString(ByteBuffer bytes)
{ {
return ByteBufferUtil.string(bytes, Charsets.US_ASCII); try
{
return ByteBufferUtil.string(bytes, Charsets.US_ASCII);
}
catch (CharacterCodingException e)
{
throw new MarshalException("Invalid ascii bytes " + ByteBufferUtil.bytesToHex(bytes));
}
} }
public ByteBuffer fromString(String source) public ByteBuffer fromString(String source)

View File

@ -22,12 +22,10 @@ package org.apache.cassandra.db.marshal;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException; import java.nio.charset.CharacterCodingException;
import java.util.Arrays;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import org.apache.cassandra.utils.ByteBufferUtil; import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.cassandra.utils.FBUtilities;
public class UTF8Type extends BytesType public class UTF8Type extends BytesType
{ {
@ -39,11 +37,11 @@ public class UTF8Type extends BytesType
{ {
try try
{ {
return FBUtilities.decodeToUTF8(bytes); return ByteBufferUtil.string(bytes, Charsets.UTF_8);
} }
catch (CharacterCodingException e) catch (CharacterCodingException e)
{ {
throw new MarshalException("invalid UTF8 bytes " + ByteBufferUtil.string(bytes)); throw new MarshalException("invalid UTF8 bytes " + ByteBufferUtil.bytesToHex(bytes));
} }
} }

View File

@ -25,7 +25,9 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import org.apache.cassandra.utils.FBUtilities; import com.google.common.base.Charsets;
import org.apache.cassandra.utils.ByteBufferUtil;
public class CollatingOrderPreservingPartitioner extends AbstractByteOrderedPartitioner public class CollatingOrderPreservingPartitioner extends AbstractByteOrderedPartitioner
{ {
@ -39,7 +41,7 @@ public class CollatingOrderPreservingPartitioner extends AbstractByteOrderedPart
String skey; String skey;
try try
{ {
skey = FBUtilities.decodeToUTF8(key); skey = ByteBufferUtil.string(key, Charsets.UTF_8);
} }
catch (CharacterCodingException e) catch (CharacterCodingException e)
{ {

View File

@ -123,7 +123,14 @@ public class OrderPreservingPartitioner implements IPartitioner<StringToken>
public Token<String> fromByteArray(ByteBuffer bytes) public Token<String> fromByteArray(ByteBuffer bytes)
{ {
return new StringToken(ByteBufferUtil.string(bytes, Charsets.UTF_8)); try
{
return new StringToken(ByteBufferUtil.string(bytes, Charsets.UTF_8));
}
catch (CharacterCodingException e)
{
throw new RuntimeException(e);
}
} }
public String toString(Token<String> stringToken) public String toString(Token<String> stringToken)
@ -152,7 +159,7 @@ public class OrderPreservingPartitioner implements IPartitioner<StringToken>
String skey; String skey;
try try
{ {
skey = FBUtilities.decodeToUTF8(key); skey = ByteBufferUtil.string(key, Charsets.UTF_8);
} }
catch (CharacterCodingException e) catch (CharacterCodingException e)
{ {

View File

@ -21,6 +21,7 @@ package org.apache.cassandra.dht;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.util.*; import java.util.*;
import org.apache.cassandra.db.DecoratedKey; import org.apache.cassandra.db.DecoratedKey;
@ -61,7 +62,15 @@ public class RandomPartitioner implements IPartitioner<BigIntegerToken>
assert splitPoint != -1; assert splitPoint != -1;
// and decode the token and key // and decode the token and key
String token = ByteBufferUtil.string(fromdisk, fromdisk.position(), splitPoint - fromdisk.position(), UTF_8); String token = null;
try
{
token = ByteBufferUtil.string(fromdisk, fromdisk.position(), splitPoint - fromdisk.position(), UTF_8);
}
catch (CharacterCodingException e)
{
throw new RuntimeException(e);
}
ByteBuffer key = fromdisk.duplicate(); ByteBuffer key = fromdisk.duplicate();
key.position(splitPoint + 1); key.position(splitPoint + 1);
return new DecoratedKey<BigIntegerToken>(new BigIntegerToken(token), key); return new DecoratedKey<BigIntegerToken>(new BigIntegerToken(token), key);

View File

@ -25,6 +25,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Arrays; import java.util.Arrays;
@ -100,28 +101,27 @@ public class ByteBufferUtil
return compareUnsigned(o1, ByteBuffer.wrap(o2)); return compareUnsigned(o1, ByteBuffer.wrap(o2));
} }
public static String string(ByteBuffer buffer) public static String string(ByteBuffer buffer) throws CharacterCodingException
{ {
return string(buffer, Charset.defaultCharset()); return string(buffer, Charset.defaultCharset());
} }
public static String string(ByteBuffer buffer, Charset charset) public static String string(ByteBuffer buffer, int offset, int length) throws CharacterCodingException
{
return string(buffer, buffer.position(), buffer.remaining(), charset);
}
public static String string(ByteBuffer buffer, int offset, int length)
{ {
return string(buffer, offset, length, Charset.defaultCharset()); return string(buffer, offset, length, Charset.defaultCharset());
} }
public static String string(ByteBuffer buffer, int offset, int length, Charset charset) public static String string(ByteBuffer buffer, int offset, int length, Charset charset) throws CharacterCodingException
{ {
if (buffer.hasArray()) ByteBuffer copy = buffer.duplicate();
return new String(buffer.array(), buffer.arrayOffset() + offset, length, charset); copy.position(buffer.position() + offset);
copy.limit(copy.position() + length);
return string(buffer, charset);
}
byte[] buff = getArray(buffer, offset, length); public static String string(ByteBuffer buffer, Charset charset) throws CharacterCodingException
return new String(buff, charset); {
return charset.newDecoder().decode(buffer.duplicate()).toString();
} }
/** /**

View File

@ -398,11 +398,6 @@ public class FBUtilities
return utflen; return utflen;
} }
public static String decodeToUTF8(ByteBuffer bytes) throws CharacterCodingException
{
return Charsets.UTF_8.newDecoder().decode(bytes.duplicate()).toString();
}
public static ByteBuffer toByteBuffer(long n) public static ByteBuffer toByteBuffer(long n)
{ {
byte[] bytes = new byte[8]; byte[] bytes = new byte[8];

View File

@ -19,6 +19,7 @@
package org.apache.cassandra.db; package org.apache.cassandra.db;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.*; import java.util.*;
@ -511,7 +512,14 @@ public class TableTest extends CleanupHelper
List<String> L = new ArrayList<String>(); List<String> L = new ArrayList<String>();
for (IColumn column : columns) for (IColumn column : columns)
{ {
L.add(ByteBufferUtil.string(column.name())); try
{
L.add(ByteBufferUtil.string(column.name()));
}
catch (CharacterCodingException e)
{
throw new AssertionError(e);
}
} }
List<String> names = new ArrayList<String>(columnNames.length); List<String> names = new ArrayList<String>(columnNames.length);

View File

@ -26,6 +26,7 @@ import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException; import java.nio.charset.CharacterCodingException;
import java.util.Arrays; import java.util.Arrays;
import com.google.common.base.Charsets;
import org.junit.Test; import org.junit.Test;
public class FBUtilitiesTest public class FBUtilitiesTest
@ -89,6 +90,6 @@ public class FBUtilitiesTest
public void testDecode() throws IOException public void testDecode() throws IOException
{ {
ByteBuffer bytes = ByteBuffer.wrap(new byte[]{(byte)0xff, (byte)0xfe}); ByteBuffer bytes = ByteBuffer.wrap(new byte[]{(byte)0xff, (byte)0xfe});
FBUtilities.decodeToUTF8(bytes); ByteBufferUtil.string(bytes, Charsets.UTF_8);
} }
} }