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

jetbrains.exodus.vfs.VfsInputStream Maven / Gradle / Ivy

There is a newer version: 2.0.1
Show newest version
/**
 * Copyright 2010 - 2020 JetBrains s.r.o.
 *
 * 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 jetbrains.exodus.vfs;

import jetbrains.exodus.env.Transaction;
import org.jetbrains.annotations.NotNull;

import java.io.InputStream;

public class VfsInputStream extends InputStream {

    private final ClusterIterator clusterIterator;

    VfsInputStream(@NotNull final VirtualFileSystem vfs, @NotNull final Transaction txn, final long fileDescriptor) {
        this(vfs, txn, fileDescriptor, 0);
    }

    VfsInputStream(@NotNull final VirtualFileSystem vfs,
                   @NotNull final Transaction txn,
                   final long fileDescriptor,
                   long position) {
        clusterIterator = new ClusterIterator(vfs, txn, fileDescriptor, position);
        final Cluster current = clusterIterator.getCurrent();
        if (current != null) {
            position -= current.getStartingPosition();
            final long skipped = current.skip(position);
            if (skipped != position) {
                throw new VfsException();
            }
        }
    }

    @Override
    public int read() {
        while (true) {
            final Cluster current = clusterIterator.getCurrent();
            if (current == null) {
                return -1;
            }
            if (current.hasNext()) {
                return current.next() & 0xff;
            }
            clusterIterator.moveToNext();
        }
    }

    @Override
    public int read(@NotNull byte[] b, int off, int len) {
        Cluster current = clusterIterator.getCurrent();
        if (current == null) {
            return -1;
        }
        int readBytes = 0;
        while (current != null) {
            readBytes += current.nextBytes(b, off + readBytes, len - readBytes);
            if (readBytes == len) {
                break;
            }
            clusterIterator.moveToNext();
            current = clusterIterator.getCurrent();
        }
        return readBytes;
    }

    @Override
    public void close() {
        clusterIterator.close();
    }

    public boolean isOpen() {
        return !clusterIterator.isClosed();
    }

    @Override
    public long skip(final long n) {
        long skipped = 0;
        while (clusterIterator.hasCluster()) {
            skipped += clusterIterator.getCurrent().skip(n - skipped);
            if (skipped == n) {
                break;
            }
            clusterIterator.moveToNext();
        }
        return skipped;
    }

    public boolean isObsolete() {
        return clusterIterator.isObsolete();
    }

    public Transaction getTxn() {
        return clusterIterator.getTxn();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy