use of org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource in project geode by apache.
the class ByteSourceJUnitTest method testGet.
@Test
public void testGet() {
ByteSource bs = createByteSource(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 });
byte b = bs.get();
assertEquals(1, b);
assertEquals(1, bs.position());
b = bs.get();
assertEquals(2, b);
assertEquals(2, bs.position());
bs.position(bs.limit() - 1);
b = bs.get();
assertEquals(0, b);
assertEquals(10, bs.position());
try {
bs.get();
fail("expected BufferUnderflowException");
} catch (BufferUnderflowException expected) {
}
}
use of org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource in project geode by apache.
the class PdxReaderImpl method readPdxString.
/**
* @return returns {@link PdxString}
*/
public PdxString readPdxString(PdxField ft) {
ByteSource buffer = dis.getBuffer();
byte[] bytes = null;
if (buffer.hasArray()) {
bytes = buffer.array();
} else {
throw new IllegalStateException();
}
int offset = getPositionForField(ft) + buffer.arrayOffset();
// Do not create PdxString if the field is NULL
if (bytes[offset] == DSCODE.NULL || bytes[offset] == DSCODE.NULL_STRING) {
return null;
}
return new PdxString(bytes, offset);
}
use of org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource in project geode by apache.
the class PdxReaderImpl method getPdxStringFromObjectField.
/**
* This method checks whether Object field is String type. If its String then it returns PdxString
* otherwise null.
*/
private PdxString getPdxStringFromObjectField(PdxField ft) {
if (ft.getFieldType() == FieldType.OBJECT) {
ByteSource buffer = dis.getBuffer();
byte[] bytes = null;
if (buffer.hasArray()) {
bytes = buffer.array();
} else {
throw new IllegalStateException();
}
int offset = getPositionForField(ft) + buffer.arrayOffset();
// Do not create PdxString if the field is NULL
if (bytes[offset] == DSCODE.STRING || bytes[offset] == DSCODE.STRING_BYTES || bytes[offset] == DSCODE.HUGE_STRING || bytes[offset] == DSCODE.HUGE_STRING_BYTES) {
return new PdxString(bytes, offset);
}
}
return null;
}
use of org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource in project geode by apache.
the class PdxInstanceImpl method hashCode.
@Override
public int hashCode() {
if (this.cachedHashCode != UNUSED_HASH_CODE) {
// Already computed.
return this.cachedHashCode;
}
PdxReaderImpl ur = getUnmodifiableReader();
// Compute hash code.
Collection<PdxField> fields = ur.getPdxType().getSortedIdentityFields();
int hashCode = 1;
for (PdxField ft : fields) {
switch(ft.getFieldType()) {
case CHAR:
case BOOLEAN:
case BYTE:
case SHORT:
case INT:
case LONG:
case DATE:
case FLOAT:
case DOUBLE:
case STRING:
case BOOLEAN_ARRAY:
case CHAR_ARRAY:
case BYTE_ARRAY:
case SHORT_ARRAY:
case INT_ARRAY:
case LONG_ARRAY:
case FLOAT_ARRAY:
case DOUBLE_ARRAY:
case STRING_ARRAY:
case ARRAY_OF_BYTE_ARRAYS:
{
ByteSource buffer = ur.getRaw(ft);
if (!buffer.equals(ByteSourceFactory.create(ft.getFieldType().getDefaultBytes()))) {
hashCode = hashCode * 31 + buffer.hashCode();
}
break;
}
case OBJECT_ARRAY:
{
Object[] oArray = ur.readObjectArray(ft);
if (oArray != null) {
// default value of null does not modify hashCode.
hashCode = hashCode * 31 + Arrays.deepHashCode(oArray);
}
break;
}
case OBJECT:
{
Object objectValue = ur.readObject(ft);
if (objectValue == null) {
// default value of null does not modify hashCode.
} else if (objectValue.getClass().isArray()) {
Class<?> myComponentType = objectValue.getClass().getComponentType();
if (myComponentType.isPrimitive()) {
ByteSource buffer = getRaw(ft);
hashCode = hashCode * 31 + buffer.hashCode();
} else {
hashCode = hashCode * 31 + Arrays.deepHashCode((Object[]) objectValue);
}
} else {
hashCode = hashCode * 31 + objectValue.hashCode();
}
break;
}
default:
throw new InternalGemFireException("Unhandled field type " + ft.getFieldType());
}
}
int result = (hashCode == UNUSED_HASH_CODE) ? (hashCode + 1) : hashCode;
this.cachedHashCode = result;
return result;
}
use of org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource in project geode by apache.
the class ByteSourceJUnitTest method testGetDouble.
@Test
public void testGetDouble() {
ByteBuffer bb = ByteBuffer.allocate(40);
DoubleBuffer db = bb.asDoubleBuffer();
db.put(1.1d);
db.put(2.2d);
db.put(3.3d);
db.put(4.4d);
db.put(5.5d);
byte[] bytes = bb.array();
ByteSource bs = createByteSource(bytes);
double d = bs.getDouble();
assertEquals(1.1d, d, 0.0001);
assertEquals(8, bs.position());
d = bs.getDouble();
assertEquals(2.2d, d, 0.0001);
assertEquals(16, bs.position());
bs.position(4 * 8);
d = bs.getDouble();
assertEquals(5.5d, d, 0.0001);
assertEquals(40, bs.position());
try {
bs.getDouble();
fail("expected BufferUnderflowException");
} catch (BufferUnderflowException expected) {
}
}
Aggregations