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

org.jpedal.fonts.FileOutput 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@
 *
 * ---------------
 * FileOutput.java
 * ---------------
 */
package org.jpedal.fonts;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutput {

    private static final int MAXLEN = 8192;
    private static final int MINP = MAXLEN - 10;
    private int p;
    private int len;
    private final FileOutputStream out;
    private final byte[] buf = new byte[MAXLEN];

    public FileOutput(final File file) throws IOException {
        out = new FileOutputStream(file);
    }

    public void putU8(final int x) throws IOException {
        if (p > MINP) {
            flushNow();
        }
        buf[p++] = (byte) x;
    }

    public void putU16(final int x) throws IOException {
        if (p > MINP) {
            flushNow();
        }
        buf[p++] = (byte) ((x >>> 8) & 0xff);
        buf[p++] = (byte) (x & 0xff);
    }

    public void putU24(final int x) throws IOException {
        if (p > MINP) {
            flushNow();
        }
        buf[p++] = (byte) ((x >>> 16) & 0xff);
        buf[p++] = (byte) ((x >>> 8) & 0xff);
        buf[p++] = (byte) (x & 0xff);
    }

    public void putU32(final int x) throws IOException {
        if (p > MINP) {
            flushNow();
        }
        buf[p++] = (byte) ((x >>> 24) & 0xff);
        buf[p++] = (byte) ((x >>> 16) & 0xff);
        buf[p++] = (byte) ((x >>> 8) & 0xff);
        buf[p++] = (byte) (x & 0xff);
    }

    public void putU64(final long x) throws IOException {
        if (p > MINP) {
            flushNow();
        }
        buf[p++] = (byte) ((x >>> 56) & 0xff);
        buf[p++] = (byte) ((x >>> 48) & 0xff);
        buf[p++] = (byte) ((x >>> 40) & 0xff);
        buf[p++] = (byte) ((x >>> 32) & 0xff);
        buf[p++] = (byte) ((x >>> 24) & 0xff);
        buf[p++] = (byte) ((x >>> 16) & 0xff);
        buf[p++] = (byte) ((x >>> 8) & 0xff);
        buf[p++] = (byte) (x & 0xff);
    }

    public void write(final byte[] copyTo) throws IOException {
        if (copyTo.length + p > MINP) {
            flushNow();
            out.write(copyTo);
            len += copyTo.length;
        } else {
            System.arraycopy(copyTo, 0, buf, p, copyTo.length);
            p += copyTo.length;
        }
    }

    public int getLength() {
        return len + p;
    }

    private void flushNow() throws IOException {
        out.write(buf, 0, p);
        len += p;
        p = 0;
    }

    public void close() throws IOException {
        flushNow();
        out.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy