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

org.jpedal.fonts.FileInput Maven / Gradle / Ivy

There is a newer version: 7.15.25
Show newest version
/*
 * ===========================================
 * Java Pdf Extraction Decoding Access Library
 * ===========================================
 *
 * Project Info:  http://www.idrsolutions.com
 * Help section for developers at http://www.idrsolutions.com/support/
 *
 * (C) Copyright 1997-2017 IDRsolutions and Contributors.
 *
 * This file is part of JPedal/JPDF2HTML5
 *
 @LICENSE@
 *
 * ---------------
 * FileInput.java
 * ---------------
 */
package org.jpedal.fonts;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class FileInput {

    private int pos;
    private final RandomAccessFile ra;
    private final int len;
    byte[] temp;
    int tSize = 8192;
    int ts, te;

    public FileInput(final File f) throws IOException {
        ra = new RandomAccessFile(f, "r");
        len = (int) f.length();
        tSize = Math.min(tSize, len);
        temp = new byte[tSize];
        te = tSize;
        ra.read(temp);
    }

    public int getU8() throws IOException {
        if (pos >= ts && pos < te) {
            final int v = temp[pos - ts] & 0xff;
            pos++;
            return v;
        } else {
            ts = pos;
            te = ts + tSize;
            ra.seek(pos);
            final int max = Math.min(len - pos, tSize);
            ra.read(temp, 0, max);
            pos++;
            return temp[0] & 0xff;
        }
    }

    public int getU16() throws IOException {
        return getU8() << 8 | getU8();
    }

    public int getU24() throws IOException {
        return (getU8() << 16) | (getU8() << 8) | getU8();
    }

    public int getU32() throws IOException {
        return (getU8() << 24) | (getU8() << 16) | (getU8() << 8) | getU8();
    }

    public int getOffset(final int size) throws IOException {
        switch (size) {
            case 1:
                return getU8();
            case 2:
                return getU16();
            case 3:
                return getU24();
            default:
                return getU32();
        }
    }

    public long getU64() throws IOException {
        final long a = getU8();
        final long b = getU8();
        final long c = getU8();
        final long d = getU8();
        final long e = getU8();
        final long f = getU8();
        final long g = getU8();
        final long h = getU8();
        return a << 56 | b << 48 | c << 40 | d << 32 | e << 24 | f << 16 | g << 8 | h;
    }

    public void read(final byte[] copyTo) throws IOException {
        for (int i = 0, ii = Math.min(copyTo.length, len - pos); i < ii; i++) {
            copyTo[i] = (byte) getU8();
        }
    }

    public int getPosition() {
        return pos;
    }

    public void skip(final int n) {
        pos += n;
    }

    public void moveTo(final int p) {
        pos = p;
    }

    public int getLength() {
        return len;
    }

    public void close() throws IOException {
        ra.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy