net.sf.mmm.util.io.impl.ByteArrayBufferImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mmm-util-io Show documentation
Show all versions of mmm-util-io Show documentation
Utilities for input/output and streaming.
The 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 net.sf.mmm.util.io.impl;
import java.util.NoSuchElementException;
import net.sf.mmm.util.exception.api.ValueOutOfRangeException;
import net.sf.mmm.util.io.api.ByteArrayBuffer;
import net.sf.mmm.util.io.base.ByteArrayImpl;
/**
* This class is similar to {@link java.nio.ByteBuffer} but a lot simpler. However it allows to
* {@link #setCurrentIndex(int) set the current index} so the internal {@link #getBytes() buffer}-array can be consumed
* externally and proceeded very fast.
* ATTENTION:
* This class is NOT intended to be exposed. It should only be used internally by some class or component.
*
* @see java.nio.ByteBuffer#wrap(byte[], int, int)
*
* @author Joerg Hohwiller (hohwille at users.sourceforge.net)
* @since 1.0.1
*/
public class ByteArrayBufferImpl extends ByteArrayImpl implements ByteArrayBuffer {
private int currentIndex;
/**
* The constructor.
*
* @param capacity is the {@code length} of the internal {@link #getBytes() buffer}.
*/
public ByteArrayBufferImpl(int capacity) {
super(capacity);
}
/**
* The constructor.
*
* @param buffer is the internal {@link #getBytes() buffer}.
*/
public ByteArrayBufferImpl(byte[] buffer) {
super(buffer);
this.currentIndex = 0;
}
/**
* The constructor.
*
* @param buffer is the internal {@link #getBytes() buffer}.
* @param currentIndex is the {@link #getCurrentIndex() current index}.
* @param maximumIndex is the {@link #getMaximumIndex() maximum index}.
*/
public ByteArrayBufferImpl(byte[] buffer, int currentIndex, int maximumIndex) {
super(buffer, currentIndex, maximumIndex);
this.currentIndex = currentIndex;
}
@Override
public int getCurrentIndex() {
return this.currentIndex;
}
@Override
public void setCurrentIndex(int currentIndex) {
ValueOutOfRangeException.checkRange(Integer.valueOf(currentIndex), Integer.valueOf(getMinimumIndex()), Integer.valueOf(getMaximumIndex() + 1),
"currentIndex");
this.currentIndex = currentIndex;
}
@Override
public void setMaximumIndex(int maximumIndex) {
super.setMaximumIndex(maximumIndex);
}
@Override
public byte next() throws NoSuchElementException {
if (this.currentIndex > getMaximumIndex()) {
throw new NoSuchElementException();
}
return getBytes()[this.currentIndex++];
}
@Override
public byte peek() throws NoSuchElementException {
if (this.currentIndex > getMaximumIndex()) {
throw new NoSuchElementException();
}
return getBytes()[this.currentIndex];
}
@Override
public boolean hasNext() {
return (this.currentIndex <= getMaximumIndex());
}
@Override
public long skip(long byteCount) {
int bytesLeft = getMaximumIndex() - this.currentIndex + 1;
int skip;
if (bytesLeft > byteCount) {
skip = (int) byteCount;
} else {
skip = bytesLeft;
}
this.currentIndex = this.currentIndex + skip;
return skip;
}
@Override
public ByteArrayImpl createSubArray(int minimum, int maximum) {
checkSubArray(minimum, maximum);
return new ByteArrayBufferImpl(getBytes(), minimum, maximum);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy