All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.fastorm.rdb.codec.BlobValueCodec Maven / Gradle / Ivy

The newest version!
package org.zodiac.fastorm.rdb.codec;

import com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zodiac.fastorm.core.ValueCodec;
import org.zodiac.fastorm.rdb.utils.FeatureUtils;
import org.zodiac.sdk.toolkit.util.ExceptionUtil;

import reactor.core.publisher.Mono;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.sql.Blob;
import java.util.concurrent.TimeUnit;

public class BlobValueCodec implements ValueCodec {

    private static Logger log = LoggerFactory.getLogger(BlobValueCodec.class);

    public BlobValueCodec() {
        super();
    }

    @Override
    public Object encode(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Blob) {
            return (value);
        }
        if (!(value instanceof byte[])) {
            try {
                if (value instanceof Serializable) {
                    try (ByteArrayOutputStream output = new ByteArrayOutputStream();
                         ObjectOutputStream object = new ObjectOutputStream(output)) {
                        object.writeObject(value);
                        object.flush();
                        object.close();
                        value = output.toByteArray();
                    }
                } else {
                    throw new NotSerializableException("unsupported encode type " + value.getClass());
                }
            } catch (Exception e) {
                ExceptionUtil.chuck(e);
            }
        }
        return ( value);
    }

    @Override
    public Object decode(Object data) {
        try {
            if (data == null) {
                return null;
            }
            if (data instanceof Blob) {
                Blob blobValue = ((Blob) data);
                try (InputStream inputStream = blobValue.getBinaryStream()) {
                    /*Try to convert to object.*/
                    try {
                        ObjectInputStream inputStream1 = new ObjectInputStream(inputStream);
                        return inputStream1.readObject();
                    } catch (IOException e) {
                        /*Probably not an object.*/
                    } catch (ClassNotFoundException e) {
                        log.warn(e.getMessage(), e);
                    }
                    /*Convert to bytes.*/
                    return blobValue.getBytes(1, (int) blobValue.length());
                } catch (Exception e) {
                    log.warn("blob data error", e);
                }
            } else if (FeatureUtils.r2dbcIsAlive()) {
                Mono mono = null;
                if (data instanceof io.r2dbc.spi.Blob) {
                    mono = Mono.from(((io.r2dbc.spi.Blob) data).stream())
                            .map(ByteBufferBackedInputStream::new)
                            .map(input -> {
                                /*Try to convert to object.*/
                                try {
                                    ObjectInputStream inputStream1 = new ObjectInputStream(input);
                                    return inputStream1.readObject();
                                } catch (IOException e) {
                                    /*Probably not an object.*/
                                } catch (ClassNotFoundException e) {
                                    log.warn(e.getMessage(), e);
                                }
                                return input;
                            });
                }
                if (mono != null) {
                    /*TODO: A better way?*/
                    return mono.toFuture().get(10, TimeUnit.SECONDS);
                }
            }
        }catch (Exception e) {
            ExceptionUtil.chuck(e);
        }
        return data;
    }

    public static BlobValueCodec getInstance() {
        return BlobValueCodecHolder.INSTANCE;
    }

    private static class BlobValueCodecHolder {
        private static final BlobValueCodec INSTANCE = new BlobValueCodec();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy