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

tfw.immutable.iis.shortiis.ShortIisFromShortIla Maven / Gradle / Ivy

Go to download

The FrameWork for building highly scalable and maintainable applications

The newest version!
package tfw.immutable.iis.shortiis;

import java.io.IOException;
import tfw.check.Argument;
import tfw.immutable.ila.shortila.ShortIla;

public final class ShortIisFromShortIla {
    private ShortIisFromShortIla() {}

    public static ShortIis create(final ShortIla ila) {
        return new ShortIisImpl(ila);
    }

    private static class ShortIisImpl extends AbstractShortIis {
        private final ShortIla ila;

        private long index = 0;

        public ShortIisImpl(final ShortIla ila) {
            Argument.assertNotNull(ila, "ila");

            this.ila = ila;
        }

        @Override
        public void closeImpl() throws IOException {
            index = ila.length();
        }

        @Override
        protected int readImpl(short[] array, int offset, int length) throws IOException {
            if (index == ila.length()) {
                return -1;
            }

            final int elementsToGet = (int) Math.min(ila.length() - index, length);

            ila.get(array, offset, index, elementsToGet);

            index += elementsToGet;

            return elementsToGet;
        }

        @Override
        protected long skipImpl(long n) throws IOException {
            if (index == ila.length()) {
                return -1;
            }

            final long originalIndex = index;

            index = Math.min(ila.length(), index + n);

            return index - originalIndex;
        }
    }
}
// AUTO GENERATED FROM TEMPLATE




© 2015 - 2024 Weber Informatics LLC | Privacy Policy