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

de.schlichtherle.io.rof.AbstractReadOnlyFile Maven / Gradle / Ivy

Go to download

TrueZIP is a Java based Virtual File System (VFS) to enable transparent, multi-threaded read/write access to archive files (ZIP, TAR etc.) as if they were directories. Archive files may be arbitrarily nested and the nesting level is only limited by heap and file system size.

The newest version!
/*
 * Copyright (C) 2007-2010 Schlichtherle IT Services
 *
 * 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 de.schlichtherle.io.rof;

import java.io.*;

/**
 * A base class for {@code ReadOnlyFile} implementations which
 * implements the common boilerplate.
 *
 * @author Christian Schlichtherle
 * @version $Id: AbstractReadOnlyFile.java,v 1.4 2010/08/20 13:09:47 christian_schlichtherle Exp $
 * @since TrueZIP 6.5
 */
public abstract class AbstractReadOnlyFile implements ReadOnlyFile {

    public int read(byte[] b) throws IOException {
        return read(b, 0, b.length);
    }

    public void readFully(byte[] b) throws IOException {
        readFully(b, 0, b.length);
    }

    public void readFully(final byte[] buf, final int off, final int len)
    throws IOException {
        int total = 0;
	do {
	    final int read = read(buf, off + total, len - total);
	    if (read < 0)
		throw new EOFException();
	    total += read;
	} while (total < len);
    }

    /** @deprecated Use {@link #seek} instead. */
    public int skipBytes(int n) throws IOException {
        if (n <= 0)
            return 0; // for compatibility to RandomAccessFile

        final long fp = getFilePointer(); // should fail when closed
        final long len = length(); // may succeed when closed
        final long rem = len - fp;
        if (n > rem)
            n = (int) rem;
        seek(fp + n);
        assert getFilePointer() == fp + n;
        return n;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy