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

org.yangcentral.yangkit.model.impl.codec.BinaryStringValueCodecImpl Maven / Gradle / Ivy

There is a newer version: 1.4.5
Show newest version
package org.yangcentral.yangkit.model.impl.codec;

import org.yangcentral.yangkit.base.ErrorCode;
import org.yangcentral.yangkit.model.api.codec.BinaryStringValueCodec;
import org.yangcentral.yangkit.model.api.codec.YangCodecException;
import org.yangcentral.yangkit.model.api.restriction.Binary;
import org.yangcentral.yangkit.model.api.restriction.Restriction;

import java.util.Base64;

public class BinaryStringValueCodecImpl extends StringValueCodecImpl implements BinaryStringValueCodec {
   public String serialize(Restriction restriction, byte[] output) throws YangCodecException {
      if (!(restriction instanceof Binary)) {
         throw new IllegalArgumentException("wrong restriction type");
      } else {
         boolean bool = restriction.evaluated(output);
         if (bool) {
            throw new YangCodecException(ErrorCode.INVALID_VALUE.getFieldName());
         } else {
            return Base64.getEncoder().encodeToString(output);
         }
      }
   }

   public byte[] deserialize(Restriction restriction, String input) throws YangCodecException {
      byte[] decode;
      try {
         decode = Base64.getDecoder().decode(input);
      } catch (IllegalArgumentException e) {
         throw new YangCodecException(ErrorCode.INVALID_VALUE.getFieldName());
      }

      boolean bool = restriction.evaluated(decode);
      if (!bool) {
         throw new YangCodecException(ErrorCode.INVALID_VALUE.getFieldName());
      } else {
         return decode;
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy