Java Code Examples for org.ehcache.spi.serialization.SerializerException

Following code examples demonstrate how to use org.ehcache.spi.serialization.SerializerExceptionfrom ehcache. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.ehcache.spi.serialization.SerializerExceptionand various code implementation of this class.

  public T decode(ByteBuffer byteBuffer) {
    try {
      return serializer.read(byteBuffer);
    } catch (ClassNotFoundException e) {
      throw new SerializerException(e);
    }
  } 


  public boolean equals(Object o, ByteBuffer byteBuffer) {
    try {
      @SuppressWarnings("unchecked")
      T otherValue = (T) o;
      return serializer.equals(otherValue, byteBuffer);
    } catch (ClassNotFoundException e) {
      throw new SerializerException(e);
    }
  } 

    public T read(ByteBuffer binary) throws ClassNotFoundException, SerializerException {
        byte[] bytes = new byte[binary.remaining()];
        binary.get(bytes);
        T object = null;
        if (isList) {
            String json = new String(bytes, UTF_8);
            object = (T) JSON.parseArray(json, targetClass);
        } else {
            object = JSON.parseObject(bytes, targetClass);
        }

        return object;
    } 

    public ByteBuffer serialize(T object) throws SerializerException {
        byte[] bytes = null;
        if (isList) {
            String json = JSON.toJSONString(object);
            bytes = json.getBytes(UTF_8);
        } else {
            bytes = JSON.toJSONBytes(object);
        }
        ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);
        byteBuffer.put(bytes).flip();
        return byteBuffer;
    } 

    public User read(final ByteBuffer binary) throws ClassNotFoundException, SerializerException {
        Input input = new Input(new ByteBufferInputStream(binary));
        return kryo.readObject(input, User.class);
    } 

    public ByteBuffer serialize(User object) throws SerializerException {
        Output output = new Output(new ByteArrayOutputStream());
        kryo.writeObject(output, object);
        output.close();

        return ByteBuffer.wrap(output.getBuffer());
    } 

        public boolean equals(XVertex v, ByteBuffer bb) throws ClassNotFoundException, SerializerException {
            log.info( "equals {}", v );
            return v.equals( read( bb ) );
        } 

        public ByteBuffer serialize(XVertex v) throws SerializerException {
            log.info( "serialize {}", v );
            try (KryoSerializer ks = KryoSerializer.of()) {
                return ks.serialize( v );
            }
        } 

        public XVertex read(ByteBuffer bb) throws ClassNotFoundException, SerializerException {
            log.info( "deserialize {}", bb.array() );
            try (KryoSerializer ks = KryoSerializer.of()) {
                XVertex v = ks.deserialize( bb, XVertex.class );
                if (null == v) {
                    throw new SerializerException( "Failed to deserialize: " + bb.array() );
                }
                return v;
            }
        } 

    public Optional<T> read (ByteBuffer binary) throws ClassNotFoundException, SerializerException {
      if(binary.array().length > 0) {
        return Optional.of(serializer.read(binary));
      }
      return Optional.empty();
    } 

  public ByteBuffer serialize(final Employee object) throws SerializerException {
    Output output = new Output(new ByteArrayOutputStream());
    kryo.writeObject(output, object);
    output.close();

    return ByteBuffer.wrap(output.getBuffer());
  } 

  public Employee read(final ByteBuffer binary) throws ClassNotFoundException, SerializerException {
    Input input =  new Input(new ByteBufferInputStream(binary)) ;
    return kryo.readObject(input, Employee.class);
  } 

    public ByteBuffer serialize(XElement.Properties re) throws SerializerException {
        log.info( "serialize {}", re );
        try (KryoSerializer ks = KryoSerializer.of()) {
            return ks.serialize( re );
        }
    } 

    public XElement.Properties read(ByteBuffer bb) throws ClassNotFoundException, SerializerException {
        log.info( "deserialize {}", bb.array() );
        try (KryoSerializer ks = KryoSerializer.of()) {
            XElement.Properties re = ks.deserialize( bb, XElement.Properties.class );
            if (null == re) {
                throw new SerializerException( "Unable to deserialize: " + bb.array() );
            }
            return re;
        }
    } 

      public ByteBuffer serialize(MyClass object) throws SerializerException {
        try(ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream oout = new ObjectOutputStream(bout)) {
          oout.writeUTF(object.getValue());
          oout.flush();
          return ByteBuffer.wrap(bout.toByteArray());
        } catch (IOException e) {
          throw new SerializerException(e);
        }
      } 

      public MyClass read(ByteBuffer binary) throws ClassNotFoundException, SerializerException {
        try(ObjectInputStream oin = new ObjectInputStream(new ByteBufferInputStream(binary))) {
            return new MyClass(oin.readUTF());
        } catch (IOException e) {
          throw new SerializerException(e);
        }
      } 

    public T read(ByteBuffer byteBuffer) throws ClassNotFoundException, SerializerException {
        byte[] bytes = new byte[byteBuffer.remaining()];
        byteBuffer.get(bytes);
        try {
            byte[] srcBytes = Snappy.uncompress(bytes);
            return KryoUtil.readFromByteArrayOpt(srcBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    } 

    public ByteBuffer serialize(Object o) throws SerializerException {
        byte[] bytes = KryoUtil.writeToByteArrayOpt(o);
        try {
            byte[] compressBytes = Snappy.compress(bytes);
            return ByteBuffer.wrap(compressBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    } 

  public ByteBuffer serialize(final String object) throws SerializerException {
    Integer currentId = stringIdMap.get(object);
    if(currentId == null) {
      stringIdMap.put(object, id);
      idStringMap.put(id, object);
      currentId = id++;
    }

    ByteBuffer buff = ByteBuffer.allocate(4);
    buff.putInt(currentId).flip();
    return buff;
  } 

  public String read(final ByteBuffer binary) throws ClassNotFoundException, SerializerException {
    Integer mapping = binary.getInt();
    String obj = idStringMap.get(mapping);
    if(obj == null) {
      throw new SerializerException("Unable to serialize: " + binary.array() + ". No value mapping found for " + mapping);
    }
    return obj;
  } 

	public OSObject read(ByteBuffer binary) throws ClassNotFoundException, SerializerException {
		Input input = new Input(new ByteBufferInputStream(binary));
		return (OSObject)kryo.readClassAndObject(input);
	} 

	public boolean equals(OSObject osObject, ByteBuffer binary) throws ClassNotFoundException, SerializerException {
		return osObject.equals(read(binary));

	} 

	public ByteBuffer serialize(OSObject osObject) throws SerializerException {
		Output output = new Output(new ByteArrayOutputStream());
		kryo.writeClassAndObject(output, osObject);
		output.close();

		return ByteBuffer.wrap(output.getBuffer());
	} 

Advertisement
Javadoc
Thrown by a Serializer when it cannot serialize or deserialize an instance.
Advertisement