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

io.github.mmm.binary.codec.BaseFormatCodec Maven / Gradle / Ivy

There is a newer version: 0.9.8
Show newest version
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.binary.codec;

import java.util.Objects;

import io.github.mmm.binary.Binary;

/**
 * Implementation of {@link BinaryCodec} that applies a given {@link BaseFormat} to a {@link Base} codec.
 *
 * @since 1.0.0
 */
public class BaseFormatCodec implements BinaryCodec {

  private final Base base;

  private final BaseFormat format;

  /**
   * The constructor.
   *
   * @param base the raw {@link Base} encoder.
   * @param format the {@link BaseFormat} to apply.
   */
  public BaseFormatCodec(Base base, BaseFormat format) {

    super();
    Objects.requireNonNull(base, "base");
    Objects.requireNonNull(format, "format");
    this.base = base;
    this.format = format;
  }

  @Override
  public String encode(byte[] data) {

    if (data == null) {
      return null;
    }
    if (data.length == 0) {
      return "";
    }
    return this.base.doEncode(data, this.format);
  }

  @Override
  public byte[] decode(String encodedData) {

    if (encodedData == null) {
      return null;
    }
    if (encodedData.isEmpty()) {
      return Binary.EMPTY_BYTE_ARRAY;
    }
    return this.base.doDecode(encodedData, this.format);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy