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

com.helger.commons.codec.IByteArrayDecoder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2014-2024 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.helger.commons.codec;

import java.nio.charset.Charset;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.helger.commons.annotation.ReturnsMutableCopy;

/**
 * Interface for a single decoder of bytes.
 *
 * @author Philip Helger
 */
@FunctionalInterface
public interface IByteArrayDecoder extends IDecoder 
{
  /**
   * Get the maximum decoded length based on the provided encoded length. This
   * is purely for performance reasons.
   *
   * @param nEncodedLen
   *        The encoded length. Always ≥ 0.
   * @return The maximum decoded length. Always ≥ 0.
   * @since 9.3.6
   */
  @Nonnegative
  default int getMaximumDecodedLength (@Nonnegative final int nEncodedLen)
  {
    return nEncodedLen;
  }

  /**
   * Decode a byte array.
   *
   * @param aEncodedBuffer
   *        The byte array to be decoded. May be null.
   * @return The decoded byte array or null if the parameter was
   *         null.
   * @throws DecodeException
   *         in case something goes wrong
   */
  @Nullable
  @ReturnsMutableCopy
  default byte [] getDecoded (@Nullable final byte [] aEncodedBuffer)
  {
    if (aEncodedBuffer == null)
      return null;
    return getDecoded (aEncodedBuffer, 0, aEncodedBuffer.length);
  }

  /**
   * Decode a byte array.
   *
   * @param aEncodedBuffer
   *        The byte array to be decoded. May be null.
   * @param nOfs
   *        Offset into the byte array to start from.
   * @param nLen
   *        Number of bytes starting from offset to consider.
   * @return The decoded byte array or null if the parameter was
   *         null.
   * @throws DecodeException
   *         in case something goes wrong
   */
  @Nullable
  @ReturnsMutableCopy
  byte [] getDecoded (@Nullable final byte [] aEncodedBuffer, @Nonnegative final int nOfs, @Nonnegative final int nLen);

  /**
   * Decode the passed string.
   *
   * @param sEncoded
   *        The string to be decoded. May be null.
   * @param aCharset
   *        The charset to be used. May not be null.
   * @return null if the input string is null.
   * @throws DecodeException
   *         in case something goes wrong
   */
  @Nullable
  @ReturnsMutableCopy
  default byte [] getDecoded (@Nullable final String sEncoded, @Nonnull final Charset aCharset)
  {
    if (sEncoded == null)
      return null;

    final byte [] aEncoded = sEncoded.getBytes (aCharset);
    return getDecoded (aEncoded);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy