HCOLL-256 Fixed a bug in CharArrayMarshaller and made some other marshallers more robust

This commit is contained in:
leventov
2014-12-16 20:41:46 +07:00
parent c7b7af8e2b
commit d196d01fda
5 changed files with 42 additions and 24 deletions
@@ -42,6 +42,8 @@ import java.io.Serializable;
*
* @Override
* public LongPair[] read(Bytes bytes, long size, LongPair[] toReuse) {
* if (size > Integer.MAX_VALUE * 16L)
* throw new IllegalStateException("LongPair[] size couldn't be " + (size / 16L));
* int resLen = (int) (size / 16L);
* LongPair[] res;
* if (toReuse != null) {
@@ -55,19 +55,24 @@ public enum ByteArrayMarshaller implements BytesInterop<byte[]>, BytesReader<byt
@Override
public byte[] read(Bytes bytes, long size) {
byte[] ba = new byte[(int) size];
byte[] ba = new byte[resLen(size)];
bytes.read(ba);
return ba;
}
@Override
public byte[] read(Bytes bytes, long size, byte[] toReuse) {
if (size < 0L || size > (long) Integer.MAX_VALUE)
throw new IllegalArgumentException("byte[] size should be non-negative int, " +
size + " given. Memory corruption?");
if (toReuse == null || size != toReuse.length)
toReuse = new byte[(int) size];
int resLen = resLen(size);
if (toReuse == null || resLen != toReuse.length)
toReuse = new byte[resLen];
bytes.read(toReuse);
return toReuse;
}
private int resLen(long size) {
if (size < 0L || size > (long) Integer.MAX_VALUE)
throw new IllegalArgumentException("byte[] size should be non-negative int, " +
size + " given. Memory corruption?");
return (int) size;
}
}
@@ -45,19 +45,28 @@ public enum CharArrayMarshaller implements BytesInterop<char[]>, BytesReader<cha
@Override
public char[] read(Bytes bytes, long size) {
char[] chars = new char[(int) size];
char[] chars = new char[resLen(size)];
bytes.readFully(chars);
return chars;
}
@Override
public char[] read(Bytes bytes, long size, char[] toReuse) {
if (toReuse == null || toReuse.length != size)
toReuse = new char[(int) size];
int resLen = resLen(size);
if (toReuse == null || toReuse.length != resLen)
toReuse = new char[resLen];
bytes.readFully(toReuse);
return toReuse;
}
private int resLen(long size) {
long resLen = size / 2L;
if (resLen < 0 || resLen > Integer.MAX_VALUE)
throw new IllegalArgumentException("char[] size should be non-negative int, " +
resLen + " given. Memory corruption?");
return (int) resLen;
}
@Override
public long size(char[] chars) {
return chars.length * 2L;