Search in sources :

Example 1 with PdxInputStream

use of org.apache.geode.pdx.internal.PdxInputStream in project geode by apache.

the class InternalDataSerializer method readPdxInstance.

/**
   * Reads a PdxInstance from dataBytes and returns it. If the first object read is not pdx encoded
   * returns null.
   */
public static PdxInstance readPdxInstance(final byte[] dataBytes, InternalCache internalCache) {
    try {
        byte type = dataBytes[0];
        if (type == PDX) {
            PdxInputStream in = new PdxInputStream(dataBytes);
            // throw away the type byte
            in.readByte();
            int len = in.readInt();
            int typeId = in.readInt();
            PdxType pdxType = internalCache.getPdxRegistry().getType(typeId);
            if (pdxType == null) {
                throw new IllegalStateException("Unknown pdx type=" + typeId);
            }
            return new PdxInstanceImpl(pdxType, in, len);
        } else if (type == DSCODE.PDX_ENUM) {
            PdxInputStream in = new PdxInputStream(dataBytes);
            // throw away the type byte
            in.readByte();
            int dsId = in.readByte();
            int tmp = readArrayLength(in);
            int enumId = dsId << 24 | tmp & 0xFFFFFF;
            TypeRegistry tr = internalCache.getPdxRegistry();
            EnumInfo ei = tr.getEnumInfoById(enumId);
            if (ei == null) {
                throw new IllegalStateException("Unknown pdx enum id=" + enumId);
            }
            return ei.getPdxInstance(enumId);
        } else if (type == DSCODE.PDX_INLINE_ENUM) {
            PdxInputStream in = new PdxInputStream(dataBytes);
            // throw away the type byte
            in.readByte();
            String className = DataSerializer.readString(in);
            String enumName = DataSerializer.readString(in);
            int enumOrdinal = InternalDataSerializer.readArrayLength(in);
            return new PdxInstanceEnum(className, enumName, enumOrdinal);
        }
    } catch (IOException ignore) {
    }
    return null;
}
Also used : PdxInstanceEnum(org.apache.geode.pdx.internal.PdxInstanceEnum) PdxType(org.apache.geode.pdx.internal.PdxType) PdxInstanceImpl(org.apache.geode.pdx.internal.PdxInstanceImpl) EnumInfo(org.apache.geode.pdx.internal.EnumInfo) PdxInputStream(org.apache.geode.pdx.internal.PdxInputStream) IOException(java.io.IOException) GemFireIOException(org.apache.geode.GemFireIOException) TypeRegistry(org.apache.geode.pdx.internal.TypeRegistry)

Example 2 with PdxInputStream

use of org.apache.geode.pdx.internal.PdxInputStream in project geode by apache.

the class BlobHelper method deserializeOffHeapBlob.

/**
   * A blob is a serialized Object. This method returns the deserialized object. If a PdxInstance is
   * returned then it will refer to Chunk's off-heap memory with an unretained reference.
   */
@Unretained
public static Object deserializeOffHeapBlob(StoredObject blob) throws IOException, ClassNotFoundException {
    Object result;
    final long start = startDeserialization();
    // For both top level and nested pdxs we just want a reference to this off-heap blob.
    // No copies.
    // For non-pdx we want a stream that will read directly from the chunk.
    PdxInputStream is = new PdxInputStream(blob);
    result = DataSerializer.readObject(is);
    endDeserialization(start, blob.getDataSize());
    return result;
}
Also used : PdxInputStream(org.apache.geode.pdx.internal.PdxInputStream) StoredObject(org.apache.geode.internal.offheap.StoredObject) Unretained(org.apache.geode.internal.offheap.annotations.Unretained)

Example 3 with PdxInputStream

use of org.apache.geode.pdx.internal.PdxInputStream in project geode by apache.

the class BlobHelper method deserializeBlob.

/**
   * A blob is a serialized Object. This method returns the deserialized object.
   */
public static Object deserializeBlob(byte[] blob, Version version, ByteArrayDataInput in) throws IOException, ClassNotFoundException {
    Object result;
    final long start = startDeserialization();
    if (blob.length > 0 && blob[0] == DSCODE.PDX) {
        // If the first byte of blob indicates a pdx then wrap
        // blob in a PdxInputStream instead.
        // This will prevent us from making a copy of the byte[]
        // every time we deserialize a PdxInstance.
        PdxInputStream is = new PdxInputStream(blob);
        result = DataSerializer.readObject(is);
    } else {
        // just have the pdx bytes and not the outer objects bytes.
        if (in == null) {
            in = new ByteArrayDataInput();
        }
        in.initialize(blob, version);
        result = DataSerializer.readObject(in);
    }
    endDeserialization(start, blob.length);
    return result;
}
Also used : PdxInputStream(org.apache.geode.pdx.internal.PdxInputStream) StoredObject(org.apache.geode.internal.offheap.StoredObject) ByteArrayDataInput(org.apache.geode.internal.ByteArrayDataInput)

Aggregations

PdxInputStream (org.apache.geode.pdx.internal.PdxInputStream)3 StoredObject (org.apache.geode.internal.offheap.StoredObject)2 IOException (java.io.IOException)1 GemFireIOException (org.apache.geode.GemFireIOException)1 ByteArrayDataInput (org.apache.geode.internal.ByteArrayDataInput)1 Unretained (org.apache.geode.internal.offheap.annotations.Unretained)1 EnumInfo (org.apache.geode.pdx.internal.EnumInfo)1 PdxInstanceEnum (org.apache.geode.pdx.internal.PdxInstanceEnum)1 PdxInstanceImpl (org.apache.geode.pdx.internal.PdxInstanceImpl)1 PdxType (org.apache.geode.pdx.internal.PdxType)1 TypeRegistry (org.apache.geode.pdx.internal.TypeRegistry)1