/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.util.io.base;

import net.sf.mmm.util.exception.api.NlsNullPointerException;

/**
 * This class is similar to {@link java.nio.ByteBuffer} but a lot simpler.
 *
 * @see java.nio.ByteBuffer#wrap(byte[], int, int)
 *
 * @author Joerg Hohwiller (hohwille at users.sourceforge.net)
 * @since 1.1.0
 */
public class ByteArrayImpl extends AbstractByteArray {

  private final byte[] buffer;

  private int minimumIndex;

  private int maximumIndex;

  /**
   * The constructor.
   *
   * @param capacity is the {@code length} of the internal {@link #getBytes() buffer}.
   */
  public ByteArrayImpl(int capacity) {

    this(new byte[capacity], 0, -1);
  }

  /**
   * The constructor.
   *
   * @param buffer is the internal {@link #getBytes() buffer}.
   */
  public ByteArrayImpl(byte[] buffer) {

    this(buffer, 0, buffer.length - 1);
  }

  /**
   * The constructor.
   *
   * @param buffer is the internal {@link #getBytes() buffer}.
   * @param startIndex is the {@link #getCurrentIndex() current index} as well as the {@link #getMinimumIndex() minimum
   *        index}.
   * @param maximumIndex is the {@link #getMaximumIndex() maximum index}.
   */
  public ByteArrayImpl(byte[] buffer, int startIndex, int maximumIndex) {

    super();
    if (buffer == null) {
      throw new NlsNullPointerException("buffer");
    }
    this.buffer = buffer;
    this.minimumIndex = startIndex;
    this.maximumIndex = maximumIndex;
  }

  @Override
  public byte[] getBytes() {

    return this.buffer;
  }

  @Override
  public int getCurrentIndex() {

    return this.minimumIndex;
  }

  @Override
  public int getMinimumIndex() {

    return this.minimumIndex;
  }

  @Override
  public int getMaximumIndex() {

    return this.maximumIndex;
  }

  /**
   * This method sets the {@link #getMaximumIndex() maximumIndex}. This may be useful if the buffer should be reused.
   * <br>
   * <b>ATTENTION:</b><br>
   * Be very careful and only use this method if you know what you are doing!
   *
   * @param maximumIndex is the {@link #getMaximumIndex() maximumIndex} to set. It has to be in the range from {@code 0}
   *        ( <code>{@link #getCurrentIndex() currentIndex} - 1</code>) to <code>{@link #getBytes()}.length</code>.
   */
  protected void setMaximumIndex(int maximumIndex) {

    this.maximumIndex = maximumIndex;
  }

  @Override
  public ByteArrayImpl createSubArray(int minimum, int maximum) {

    checkSubArray(minimum, maximum);
    return new ByteArrayImpl(this.buffer, minimum, maximum);
  }

  @Override
  public String toString() {

    return new String(this.buffer, this.minimumIndex, getBytesAvailable());
  }

}
