001package gu.dtalk;
002
003import com.google.common.base.Throwables;
004
005import net.gdface.utils.FaceUtilits;
006
007/**
008 * 二进制数据选项基类
009 * @author guyadong
010 *
011 */
012public abstract class BaseBinary extends BaseOption<byte[]> {
013
014        public BaseBinary() {
015                super(byte[].class);
016        }
017
018        @Override
019        public String contentOfValue() {
020                byte[] value = (byte[])getValue();
021                if(value == null){
022                        return super.contentOfValue();
023                }
024                if(value.length <=32){
025                        return FaceUtilits.toHex(value);
026                }
027                return "BINARY";
028        }
029        /**
030         * 从input中读取字节流转为byte[]调用{@link #setValue(Object)}
031         * @param <T> 参见 {@link FaceUtilits#getBytes(Object)}
032         */
033        protected <T>BaseOption<byte[]> asValue(T input) {
034                try {
035                        return setValue(FaceUtilits.getBytes(input));
036                } catch (Exception e) {
037                        Throwables.throwIfUnchecked(e);
038                        throw new RuntimeException(e);
039                }
040        }
041        /**
042         * 从input中读取字节流转为byte[]调用{@link #setDefaultValue(Object)}
043         * @param <T> 参见 {@link FaceUtilits#getBytes(Object)}
044         */
045        protected <T>BaseOption<byte[]> asDefaultValue(T input) {
046                try {
047                        return setDefaultValue(FaceUtilits.getBytes(input));
048                } catch (Exception e) {
049                        Throwables.throwIfUnchecked(e);
050                        throw new RuntimeException(e);
051                }
052        }
053}