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

com.github.akurilov.commons.io.collection.CircularArrayInput Maven / Gradle / Ivy

There is a newer version: 2.3.6
Show newest version
package com.github.akurilov.commons.io.collection;

import java.io.EOFException;
import java.io.IOException;
import java.util.List;

/**
 An array input which may use fixed count of the items for unlimited (circular) retrieving.
 */
public class CircularArrayInput
extends ArrayInput {

	public CircularArrayInput(final T[] items) {
		super(items);
	}

	/**
	 @return next item
	 */
	@Override
	public T get()
	throws IOException {
		if(i >= size) {
			reset();
		}
		return items[i ++];
	}

	/**
	 @param buffer buffer for the items
	 @param maxCount the count limit
	 @return the actual count of the items got in the buffer
	 @throws EOFException doesn't throw
	 */
	@Override
	public int get(final List buffer, final int maxCount)
	throws EOFException, IOException {
		int n = 0;
		while(n < maxCount) {
			if(i >= size) {
				reset();
			}
			n += super.get(buffer, Math.min(size - i, maxCount - n));
		}
		return n;
	}

	@Override
	public String toString() {
		return "circularArrayInput<" + items.hashCode() + ">";
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy