Search in sources :

Example 16 with ByteSource

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) {
    }
}
Also used : ByteSource(org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource) BufferUnderflowException(java.nio.BufferUnderflowException) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) UnitTest(org.apache.geode.test.junit.categories.UnitTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 17 with ByteSource

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);
}
Also used : ByteSource(org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource)

Example 18 with ByteSource

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;
}
Also used : ByteSource(org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource)

Example 19 with ByteSource

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;
}
Also used : InternalGemFireException(org.apache.geode.InternalGemFireException) ByteSource(org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource)

Example 20 with ByteSource

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) {
    }
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) ByteSource(org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) UnitTest(org.apache.geode.test.junit.categories.UnitTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

ByteSource (org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource)33 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)29 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)29 UnitTest (org.apache.geode.test.junit.categories.UnitTest)29 Test (org.junit.Test)29 ByteBuffer (java.nio.ByteBuffer)16 BufferUnderflowException (java.nio.BufferUnderflowException)9 CharBuffer (java.nio.CharBuffer)2 DoubleBuffer (java.nio.DoubleBuffer)2 FloatBuffer (java.nio.FloatBuffer)2 IntBuffer (java.nio.IntBuffer)2 LongBuffer (java.nio.LongBuffer)2 ShortBuffer (java.nio.ShortBuffer)2 InternalGemFireException (org.apache.geode.InternalGemFireException)1 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)1 Version (org.apache.geode.internal.Version)1