HBASE-2463 Various Bytes.* functions silently ignore invalid arguments
git-svn-id: https://svn.apache.org/repos/asf/hadoop/hbase/trunk@935301 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f6fb68ab0c
commit
446583b0b1
|
@ -277,6 +277,8 @@ Release 0.21.0 - Unreleased
|
|||
HBASE-2458 Client stuck in TreeMap,remove (Todd Lipcon via Stack)
|
||||
HBASE-2460 add_table.rb deletes any tables for which the target table name
|
||||
is a prefix (Todd Lipcon via Stack)
|
||||
HBASE-2463 Various Bytes.* functions silently ignore invalid arguments
|
||||
(Benoit Sigoure via Stack)
|
||||
|
||||
IMPROVEMENTS
|
||||
HBASE-1760 Cleanup TODOs in HTable
|
||||
|
|
|
@ -101,12 +101,12 @@ abstract class TableOperation implements HConstants {
|
|||
Bytes.toStringBinary(values.getRow()));
|
||||
continue;
|
||||
}
|
||||
String serverAddress =
|
||||
final String serverAddress =
|
||||
Bytes.toString(values.getValue(CATALOG_FAMILY, SERVER_QUALIFIER));
|
||||
long startCode =
|
||||
Bytes.toLong(values.getValue(CATALOG_FAMILY, STARTCODE_QUALIFIER));
|
||||
String serverName = null;
|
||||
if (serverAddress != null && serverAddress.length() > 0) {
|
||||
long startCode =
|
||||
Bytes.toLong(values.getValue(CATALOG_FAMILY, STARTCODE_QUALIFIER));
|
||||
serverName = HServerInfo.getServerName(serverAddress, startCode);
|
||||
}
|
||||
if (Bytes.compareTo(info.getTableDesc().getName(), tableName) > 0) {
|
||||
|
|
|
@ -21,6 +21,8 @@ package org.apache.hadoop.hbase.util;
|
|||
|
||||
import org.apache.hadoop.hbase.HConstants;
|
||||
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.hadoop.io.RawComparator;
|
||||
import org.apache.hadoop.io.WritableComparator;
|
||||
import org.apache.hadoop.io.WritableUtils;
|
||||
|
@ -40,10 +42,12 @@ import java.util.Comparator;
|
|||
*/
|
||||
public class Bytes {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(Bytes.class);
|
||||
|
||||
/**
|
||||
* Size of boolean in bytes
|
||||
*/
|
||||
public static final int SIZEOF_BOOLEAN = Byte.SIZE/Byte.SIZE;
|
||||
public static final int SIZEOF_BOOLEAN = Byte.SIZE / Byte.SIZE;
|
||||
|
||||
/**
|
||||
* Size of byte in bytes
|
||||
|
@ -53,32 +57,32 @@ public class Bytes {
|
|||
/**
|
||||
* Size of char in bytes
|
||||
*/
|
||||
public static final int SIZEOF_CHAR = Character.SIZE/Byte.SIZE;
|
||||
public static final int SIZEOF_CHAR = Character.SIZE / Byte.SIZE;
|
||||
|
||||
/**
|
||||
* Size of double in bytes
|
||||
*/
|
||||
public static final int SIZEOF_DOUBLE = Double.SIZE/Byte.SIZE;
|
||||
public static final int SIZEOF_DOUBLE = Double.SIZE / Byte.SIZE;
|
||||
|
||||
/**
|
||||
* Size of float in bytes
|
||||
*/
|
||||
public static final int SIZEOF_FLOAT = Float.SIZE/Byte.SIZE;
|
||||
public static final int SIZEOF_FLOAT = Float.SIZE / Byte.SIZE;
|
||||
|
||||
/**
|
||||
* Size of int in bytes
|
||||
*/
|
||||
public static final int SIZEOF_INT = Integer.SIZE/Byte.SIZE;
|
||||
public static final int SIZEOF_INT = Integer.SIZE / Byte.SIZE;
|
||||
|
||||
/**
|
||||
* Size of long in bytes
|
||||
*/
|
||||
public static final int SIZEOF_LONG = Long.SIZE/Byte.SIZE;
|
||||
public static final int SIZEOF_LONG = Long.SIZE / Byte.SIZE;
|
||||
|
||||
/**
|
||||
* Size of short in bytes
|
||||
*/
|
||||
public static final int SIZEOF_SHORT = Short.SIZE/Byte.SIZE;
|
||||
public static final int SIZEOF_SHORT = Short.SIZE / Byte.SIZE;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -248,6 +252,12 @@ public class Bytes {
|
|||
return toString(b, 0, b.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Joins two byte arrays together using a separator.
|
||||
* @param b1 The first byte array.
|
||||
* @param sep The separator to use.
|
||||
* @param b2 The second byte array.
|
||||
*/
|
||||
public static String toString(final byte [] b1,
|
||||
String sep,
|
||||
final byte [] b2) {
|
||||
|
@ -265,19 +275,18 @@ public class Bytes {
|
|||
* @return String made from <code>b</code> or null
|
||||
*/
|
||||
public static String toString(final byte [] b, int off, int len) {
|
||||
if(b == null) {
|
||||
if (b == null) {
|
||||
return null;
|
||||
}
|
||||
if(len == 0) {
|
||||
if (len == 0) {
|
||||
return "";
|
||||
}
|
||||
String result = null;
|
||||
try {
|
||||
result = new String(b, off, len, HConstants.UTF8_ENCODING);
|
||||
return new String(b, off, len, HConstants.UTF8_ENCODING);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
LOG.error("UTF-8 not supported?", e);
|
||||
return null;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -325,8 +334,8 @@ public class Bytes {
|
|||
result.append(String.format("\\x%02X", ch));
|
||||
}
|
||||
}
|
||||
} catch ( UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
LOG.error("ISO-8859-1 not supported?", e);
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
@ -369,8 +378,8 @@ public class Bytes {
|
|||
char hd2 = in.charAt(i+3);
|
||||
|
||||
// they need to be A-F0-9:
|
||||
if ( ! isHexDigit(hd1) ||
|
||||
! isHexDigit(hd2) ) {
|
||||
if (!isHexDigit(hd1) ||
|
||||
!isHexDigit(hd2)) {
|
||||
// bogus escape code, ignore:
|
||||
continue;
|
||||
}
|
||||
|
@ -395,16 +404,12 @@ public class Bytes {
|
|||
* @return the byte array
|
||||
*/
|
||||
public static byte[] toBytes(String s) {
|
||||
if (s == null) {
|
||||
throw new IllegalArgumentException("string cannot be null");
|
||||
}
|
||||
byte [] result = null;
|
||||
try {
|
||||
result = s.getBytes(HConstants.UTF8_ENCODING);
|
||||
return s.getBytes(HConstants.UTF8_ENCODING);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
LOG.error("UTF-8 not supported?", e);
|
||||
return null;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -415,9 +420,7 @@ public class Bytes {
|
|||
* @return <code>b</code> encoded in a byte array.
|
||||
*/
|
||||
public static byte [] toBytes(final boolean b) {
|
||||
byte [] bb = new byte[1];
|
||||
bb[0] = b? (byte)-1: (byte)0;
|
||||
return bb;
|
||||
return new byte[] { b ? (byte) -1 : (byte) 0 };
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -426,10 +429,10 @@ public class Bytes {
|
|||
* @return True or false.
|
||||
*/
|
||||
public static boolean toBoolean(final byte [] b) {
|
||||
if (b == null || b.length > 1) {
|
||||
throw new IllegalArgumentException("Array is wrong size");
|
||||
if (b.length != 1) {
|
||||
throw new IllegalArgumentException("Array has wrong size: " + b.length);
|
||||
}
|
||||
return b[0] != (byte)0;
|
||||
return b[0] != (byte) 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -440,11 +443,11 @@ public class Bytes {
|
|||
*/
|
||||
public static byte[] toBytes(long val) {
|
||||
byte [] b = new byte[8];
|
||||
for (int i=7; i>0; i--) {
|
||||
b[i] = (byte)(val);
|
||||
for (int i = 7; i > 0; i--) {
|
||||
b[i] = (byte) val;
|
||||
val >>>= 8;
|
||||
}
|
||||
b[0] = (byte)(val);
|
||||
b[0] = (byte) val;
|
||||
return b;
|
||||
}
|
||||
|
||||
|
@ -455,7 +458,7 @@ public class Bytes {
|
|||
* @return the long value
|
||||
*/
|
||||
public static long toLong(byte[] bytes) {
|
||||
return toLong(bytes, 0);
|
||||
return toLong(bytes, 0, SIZEOF_LONG);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -477,36 +480,55 @@ public class Bytes {
|
|||
* @param offset offset into array
|
||||
* @param length length of data (must be {@link #SIZEOF_LONG})
|
||||
* @return the long value
|
||||
* @throws IllegalArgumentException if length is not {@link #SIZEOF_LONG} or
|
||||
* if there's not enough room in the array at the offset indicated.
|
||||
*/
|
||||
public static long toLong(byte[] bytes, int offset, final int length) {
|
||||
if (bytes == null || length != SIZEOF_LONG ||
|
||||
(offset + length > bytes.length)) {
|
||||
return -1L;
|
||||
if (length != SIZEOF_LONG || offset + length > bytes.length) {
|
||||
throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_LONG);
|
||||
}
|
||||
long l = 0;
|
||||
for(int i = offset; i < (offset + length); i++) {
|
||||
for(int i = offset; i < offset + length; i++) {
|
||||
l <<= 8;
|
||||
l ^= (long)bytes[i] & 0xFF;
|
||||
l ^= bytes[i] & 0xFF;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
private static IllegalArgumentException
|
||||
explainWrongLengthOrOffset(final byte[] bytes,
|
||||
final int offset,
|
||||
final int length,
|
||||
final int expectedLength) {
|
||||
String reason;
|
||||
if (length != expectedLength) {
|
||||
reason = "Wrong length: " + length + ", expected " + expectedLength;
|
||||
} else {
|
||||
reason = "offset (" + offset + ") + length (" + length + ") exceed the"
|
||||
+ " capacity of the array: " + bytes.length;
|
||||
}
|
||||
return new IllegalArgumentException(reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put a long value out to the specified byte array position.
|
||||
* @param bytes the byte array
|
||||
* @param offset position in the array
|
||||
* @param val long to write out
|
||||
* @return incremented offset
|
||||
* @throws IllegalArgumentException if the byte array given doesn't have
|
||||
* enough room at the offset specified.
|
||||
*/
|
||||
public static int putLong(byte[] bytes, int offset, long val) {
|
||||
if (bytes == null || (bytes.length - offset < SIZEOF_LONG)) {
|
||||
return offset;
|
||||
if (bytes.length - offset < SIZEOF_LONG) {
|
||||
throw new IllegalArgumentException("Not enough room to put a long at"
|
||||
+ " offset " + offset + " in a " + bytes.length + " byte array");
|
||||
}
|
||||
for(int i=offset+7;i>offset;i--) {
|
||||
bytes[i] = (byte)(val);
|
||||
for(int i = offset + 7; i > offset; i--) {
|
||||
bytes[i] = (byte) val;
|
||||
val >>>= 8;
|
||||
}
|
||||
bytes[offset] = (byte)(val);
|
||||
bytes[offset] = (byte) val;
|
||||
return offset + SIZEOF_LONG;
|
||||
}
|
||||
|
||||
|
@ -526,19 +548,17 @@ public class Bytes {
|
|||
* @return Float made from passed byte array.
|
||||
*/
|
||||
public static float toFloat(byte [] bytes, int offset) {
|
||||
int i = toInt(bytes, offset);
|
||||
return Float.intBitsToFloat(i);
|
||||
return Float.intBitsToFloat(toInt(bytes, offset, SIZEOF_INT));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bytes byte array
|
||||
* @param offset offset to write to
|
||||
* @param f float value
|
||||
* @return New offset in <code>bytes</bytes>
|
||||
* @return New offset in <code>bytes</code>
|
||||
*/
|
||||
public static int putFloat(byte [] bytes, int offset, float f) {
|
||||
int i = Float.floatToRawIntBits(f);
|
||||
return putInt(bytes, offset, i);
|
||||
return putInt(bytes, offset, Float.floatToRawIntBits(f));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -547,8 +567,7 @@ public class Bytes {
|
|||
*/
|
||||
public static byte [] toBytes(final float f) {
|
||||
// Encode it as int
|
||||
int i = Float.floatToRawIntBits(f);
|
||||
return Bytes.toBytes(i);
|
||||
return Bytes.toBytes(Float.floatToRawIntBits(f));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -565,8 +584,7 @@ public class Bytes {
|
|||
* @return Return double made from passed bytes.
|
||||
*/
|
||||
public static double toDouble(final byte [] bytes, final int offset) {
|
||||
long l = toLong(bytes, offset);
|
||||
return Double.longBitsToDouble(l);
|
||||
return Double.longBitsToDouble(toLong(bytes, offset, SIZEOF_LONG));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -576,8 +594,7 @@ public class Bytes {
|
|||
* @return New offset into array <code>bytes</code>
|
||||
*/
|
||||
public static int putDouble(byte [] bytes, int offset, double d) {
|
||||
long l = Double.doubleToLongBits(d);
|
||||
return putLong(bytes, offset, l);
|
||||
return putLong(bytes, offset, Double.doubleToLongBits(d));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -589,8 +606,7 @@ public class Bytes {
|
|||
*/
|
||||
public static byte [] toBytes(final double d) {
|
||||
// Encode it as a long
|
||||
long l = Double.doubleToRawLongBits(d);
|
||||
return Bytes.toBytes(l);
|
||||
return Bytes.toBytes(Double.doubleToRawLongBits(d));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -601,10 +617,10 @@ public class Bytes {
|
|||
public static byte[] toBytes(int val) {
|
||||
byte [] b = new byte[4];
|
||||
for(int i = 3; i > 0; i--) {
|
||||
b[i] = (byte)(val);
|
||||
b[i] = (byte) val;
|
||||
val >>>= 8;
|
||||
}
|
||||
b[0] = (byte)(val);
|
||||
b[0] = (byte) val;
|
||||
return b;
|
||||
}
|
||||
|
||||
|
@ -614,7 +630,7 @@ public class Bytes {
|
|||
* @return the int value
|
||||
*/
|
||||
public static int toInt(byte[] bytes) {
|
||||
return toInt(bytes, 0);
|
||||
return toInt(bytes, 0, SIZEOF_INT);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -633,11 +649,12 @@ public class Bytes {
|
|||
* @param offset offset into array
|
||||
* @param length length of int (has to be {@link #SIZEOF_INT})
|
||||
* @return the int value
|
||||
* @throws IllegalArgumentException if length is not {@link #SIZEOF_INT} or
|
||||
* if there's not enough room in the array at the offset indicated.
|
||||
*/
|
||||
public static int toInt(byte[] bytes, int offset, final int length) {
|
||||
if (bytes == null || length != SIZEOF_INT ||
|
||||
(offset + length > bytes.length)) {
|
||||
return -1;
|
||||
if (length != SIZEOF_INT || offset + length > bytes.length) {
|
||||
throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_INT);
|
||||
}
|
||||
int n = 0;
|
||||
for(int i = offset; i < (offset + length); i++) {
|
||||
|
@ -653,16 +670,19 @@ public class Bytes {
|
|||
* @param offset position in the array
|
||||
* @param val int to write out
|
||||
* @return incremented offset
|
||||
* @throws IllegalArgumentException if the byte array given doesn't have
|
||||
* enough room at the offset specified.
|
||||
*/
|
||||
public static int putInt(byte[] bytes, int offset, int val) {
|
||||
if (bytes == null || (bytes.length - offset < SIZEOF_INT)) {
|
||||
return offset;
|
||||
if (bytes.length - offset < SIZEOF_INT) {
|
||||
throw new IllegalArgumentException("Not enough room to put an int at"
|
||||
+ " offset " + offset + " in a " + bytes.length + " byte array");
|
||||
}
|
||||
for(int i= offset+3; i > offset; i--) {
|
||||
bytes[i] = (byte)(val);
|
||||
for(int i= offset + 3; i > offset; i--) {
|
||||
bytes[i] = (byte) val;
|
||||
val >>>= 8;
|
||||
}
|
||||
bytes[offset] = (byte)(val);
|
||||
bytes[offset] = (byte) val;
|
||||
return offset + SIZEOF_INT;
|
||||
}
|
||||
|
||||
|
@ -673,9 +693,9 @@ public class Bytes {
|
|||
*/
|
||||
public static byte[] toBytes(short val) {
|
||||
byte[] b = new byte[SIZEOF_SHORT];
|
||||
b[1] = (byte)(val);
|
||||
b[1] = (byte) val;
|
||||
val >>= 8;
|
||||
b[0] = (byte)(val);
|
||||
b[0] = (byte) val;
|
||||
return b;
|
||||
}
|
||||
|
||||
|
@ -685,7 +705,7 @@ public class Bytes {
|
|||
* @return the short value
|
||||
*/
|
||||
public static short toShort(byte[] bytes) {
|
||||
return toShort(bytes, 0);
|
||||
return toShort(bytes, 0, SIZEOF_SHORT);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -704,11 +724,12 @@ public class Bytes {
|
|||
* @param offset offset into array
|
||||
* @param length length, has to be {@link #SIZEOF_SHORT}
|
||||
* @return the short value
|
||||
* @throws IllegalArgumentException if length is not {@link #SIZEOF_SHORT}
|
||||
* or if there's not enough room in the array at the offset indicated.
|
||||
*/
|
||||
public static short toShort(byte[] bytes, int offset, final int length) {
|
||||
if (bytes == null || length != SIZEOF_SHORT ||
|
||||
(offset + length > bytes.length)) {
|
||||
return -1;
|
||||
if (length != SIZEOF_SHORT || offset + length > bytes.length) {
|
||||
throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_SHORT);
|
||||
}
|
||||
short n = 0;
|
||||
n ^= bytes[offset] & 0xFF;
|
||||
|
@ -723,14 +744,17 @@ public class Bytes {
|
|||
* @param offset position in the array
|
||||
* @param val short to write out
|
||||
* @return incremented offset
|
||||
* @throws IllegalArgumentException if the byte array given doesn't have
|
||||
* enough room at the offset specified.
|
||||
*/
|
||||
public static int putShort(byte[] bytes, int offset, short val) {
|
||||
if (bytes == null || (bytes.length - offset < SIZEOF_SHORT)) {
|
||||
return offset;
|
||||
if (bytes.length - offset < SIZEOF_SHORT) {
|
||||
throw new IllegalArgumentException("Not enough room to put a short at"
|
||||
+ " offset " + offset + " in a " + bytes.length + " byte array");
|
||||
}
|
||||
bytes[offset+1] = (byte)(val);
|
||||
bytes[offset+1] = (byte) val;
|
||||
val >>= 8;
|
||||
bytes[offset] = (byte)(val);
|
||||
bytes[offset] = (byte) val;
|
||||
return offset + SIZEOF_SHORT;
|
||||
}
|
||||
|
||||
|
@ -744,7 +768,7 @@ public class Bytes {
|
|||
byte [] result = new byte[size];
|
||||
int offset = 0;
|
||||
if (i >= -112 && i <= 127) {
|
||||
result[offset] = ((byte)i);
|
||||
result[offset] = (byte) i;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -760,7 +784,7 @@ public class Bytes {
|
|||
len--;
|
||||
}
|
||||
|
||||
result[offset++] = (byte)len;
|
||||
result[offset++] = (byte) len;
|
||||
|
||||
len = (len < -120) ? -(len + 120) : -(len + 112);
|
||||
|
||||
|
@ -789,7 +813,7 @@ public class Bytes {
|
|||
i = i << 8;
|
||||
i = i | (b & 0xFF);
|
||||
}
|
||||
return (WritableUtils.isNegativeVInt(firstByte) ? (~i) : i);
|
||||
return (WritableUtils.isNegativeVInt(firstByte) ? ~i : i);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -812,7 +836,7 @@ public class Bytes {
|
|||
i = i << 8;
|
||||
i = i | (b & 0xFF);
|
||||
}
|
||||
return (WritableUtils.isNegativeVInt(firstByte) ? (~i) : i);
|
||||
return (WritableUtils.isNegativeVInt(firstByte) ? ~i : i);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -858,9 +882,11 @@ public class Bytes {
|
|||
public static boolean equals(final byte [] left, final byte [] right) {
|
||||
// Could use Arrays.equals?
|
||||
//noinspection SimplifiableConditionalExpression
|
||||
return left == null && right == null? true:
|
||||
(left == null || right == null || (left.length != right.length))? false:
|
||||
compareTo(left, right) == 0;
|
||||
if (left == null && right == null) {
|
||||
return true;
|
||||
}
|
||||
return (left == null || right == null || (left.length != right.length)
|
||||
? false : compareTo(left, right) == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -932,7 +958,9 @@ public class Bytes {
|
|||
* @return First <code>length</code> bytes from <code>a</code>
|
||||
*/
|
||||
public static byte [] head(final byte [] a, final int length) {
|
||||
if(a.length < length) return null;
|
||||
if (a.length < length) {
|
||||
return null;
|
||||
}
|
||||
byte [] result = new byte[length];
|
||||
System.arraycopy(a, 0, result, 0, length);
|
||||
return result;
|
||||
|
@ -944,7 +972,9 @@ public class Bytes {
|
|||
* @return Last <code>length</code> bytes from <code>a</code>
|
||||
*/
|
||||
public static byte [] tail(final byte [] a, final int length) {
|
||||
if(a.length < length) return null;
|
||||
if (a.length < length) {
|
||||
return null;
|
||||
}
|
||||
byte [] result = new byte[length];
|
||||
System.arraycopy(a, a.length - length, result, 0, length);
|
||||
return result;
|
||||
|
@ -957,7 +987,9 @@ public class Bytes {
|
|||
*/
|
||||
public static byte [] padHead(final byte [] a, final int length) {
|
||||
byte [] padding = new byte[length];
|
||||
for(int i=0;i<length;i++) padding[i] = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
padding[i] = 0;
|
||||
}
|
||||
return add(padding,a);
|
||||
}
|
||||
|
||||
|
@ -968,7 +1000,9 @@ public class Bytes {
|
|||
*/
|
||||
public static byte [] padTail(final byte [] a, final int length) {
|
||||
byte [] padding = new byte[length];
|
||||
for(int i=0;i<length;i++) padding[i] = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
padding[i] = 0;
|
||||
}
|
||||
return add(a,padding);
|
||||
}
|
||||
|
||||
|
@ -985,29 +1019,34 @@ public class Bytes {
|
|||
byte [] aPadded;
|
||||
byte [] bPadded;
|
||||
if (a.length < b.length) {
|
||||
aPadded = padTail(a,b.length-a.length);
|
||||
aPadded = padTail(a, b.length - a.length);
|
||||
bPadded = b;
|
||||
} else if (b.length < a.length) {
|
||||
aPadded = a;
|
||||
bPadded = padTail(b,a.length-b.length);
|
||||
bPadded = padTail(b, a.length - b.length);
|
||||
} else {
|
||||
aPadded = a;
|
||||
bPadded = b;
|
||||
}
|
||||
if (compareTo(aPadded,bPadded) > 1) {
|
||||
if (compareTo(aPadded, bPadded) > 1) {
|
||||
throw new IllegalArgumentException("b > a");
|
||||
}
|
||||
if (num <= 0) throw new IllegalArgumentException("num cannot be < 0");
|
||||
if (num <= 0) {
|
||||
throw new IllegalArgumentException("num cannot be < 0");
|
||||
}
|
||||
byte [] prependHeader = {1, 0};
|
||||
BigInteger startBI = new BigInteger(add(prependHeader, aPadded));
|
||||
BigInteger stopBI = new BigInteger(add(prependHeader, bPadded));
|
||||
BigInteger diffBI = stopBI.subtract(startBI);
|
||||
BigInteger splitsBI = BigInteger.valueOf(num + 1);
|
||||
if(diffBI.compareTo(splitsBI) <= 0) return null;
|
||||
if(diffBI.compareTo(splitsBI) <= 0) {
|
||||
return null;
|
||||
}
|
||||
BigInteger intervalBI;
|
||||
try {
|
||||
intervalBI = diffBI.divide(splitsBI);
|
||||
} catch(Exception e) {
|
||||
LOG.error("Exception caught during division", e);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -1018,9 +1057,9 @@ public class Bytes {
|
|||
BigInteger curBI = startBI.add(intervalBI.multiply(BigInteger.valueOf(i)));
|
||||
byte [] padded = curBI.toByteArray();
|
||||
if (padded[1] == 0)
|
||||
padded = tail(padded,padded.length-2);
|
||||
padded = tail(padded, padded.length - 2);
|
||||
else
|
||||
padded = tail(padded,padded.length-1);
|
||||
padded = tail(padded, padded.length - 1);
|
||||
result[i] = padded;
|
||||
}
|
||||
result[num+1] = b;
|
||||
|
|
Loading…
Reference in New Issue