Example usage for org.apache.commons.io EndianUtils readSwappedInteger

List of usage examples for org.apache.commons.io EndianUtils readSwappedInteger

Introduction

In this page you can find the example usage for org.apache.commons.io EndianUtils readSwappedInteger.

Prototype

public static int readSwappedInteger(InputStream input) throws IOException 

Source Link

Document

Reads a "int" value from an InputStream.

Usage

From source file:com.warfrog.bitmapallthethings.BattEngine.java

private void decodeBitmap(String filename) throws IOException {
    System.out.println("Decoding " + filename);

    File inputFile = new File(filename);
    File outputFile = new File(
            outputDirectory + File.separator + FilenameUtils.removeExtension(inputFile.getName()));

    FileInputStream fis = new FileInputStream(filename);
    //skip 6 bytes
    fis.skip(6);//from   w  w  w . j a v a  2 s.c o m
    //read the length we encoded
    int fileSize = EndianUtils.readSwappedInteger(fis);
    //skip the rest of the header
    fis.skip(44);
    Files.copy(fis, outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    //truncate the file
    FileChannel outChan = new FileOutputStream(outputFile, true).getChannel();
    outChan.truncate(fileSize);
    outChan.close();

    //clean up
    if (isCleanUp()) {
        //delete the bitmap
        System.out.println("Deleting: " + inputFile);
        FileUtils.deleteQuietly(inputFile);
    }
}

From source file:org.apache.xmlgraphics.ps.PSFontUtils.java

/**
 * This method reads a Type 1 font from a stream and embeds it into a PostScript stream.
 * Note: Only the IBM PC Format as described in section 3.3 of the Adobe Technical Note #5040
 * is supported./*from   w ww . j av a2s.com*/
 * @param gen The PostScript generator
 * @param in the InputStream from which to read the Type 1 font
 * @throws IOException in case an I/O problem occurs
 */
public static void embedType1Font(PSGenerator gen, InputStream in) throws IOException {
    boolean finished = false;
    while (!finished) {
        int segIndicator = in.read();
        if (segIndicator < 0) {
            throw new IOException("Unexpected end-of-file while reading segment indicator");
        } else if (segIndicator != 128) {
            throw new IOException("Expected ASCII 128, found: " + segIndicator);
        }
        int segType = in.read();
        if (segType < 0) {
            throw new IOException("Unexpected end-of-file while reading segment type");
        }
        int dataSegLen = 0;
        switch (segType) {
        case 1: //ASCII
            dataSegLen = EndianUtils.readSwappedInteger(in);

            BufferedReader reader = new BufferedReader(
                    new java.io.InputStreamReader(new SubInputStream(in, dataSegLen), "US-ASCII"));
            String line;
            while ((line = reader.readLine()) != null) {
                gen.writeln(line);
            }
            break;
        case 2: //binary
            dataSegLen = EndianUtils.readSwappedInteger(in);

            SubInputStream sin = new SubInputStream(in, dataSegLen);
            ASCIIHexOutputStream hexOut = new ASCIIHexOutputStream(gen.getOutputStream());
            IOUtils.copy(sin, hexOut);
            gen.newLine();
            break;
        case 3: //EOF
            finished = true;
            break;
        default:
            throw new IOException("Unsupported segment type: " + segType);
        }
    }
}

From source file:thv.th.reader.FramesReader.java

public static THFrame readByIndex(int frameIndex, FileInputStream frameStream, FileInputStream listStream,
        FileInputStream elementStream, File tabFile, File chunksFile, THPalette palette) throws IOException {
    frameStream.getChannel().position(frameIndex * 10);

    int listIndex = EndianUtils.readSwappedInteger(frameStream) * 2;
    int width = frameStream.read();
    int height = frameStream.read();

    if (width == 0 || height == 0)
        return null;

    frameStream.skip(1);//from   w  ww.  j  a va 2 s. c om
    int flags = frameStream.read();
    int nextFrame = EndianUtils.readSwappedShort(frameStream);

    THFrame res = new THFrame(frameIndex, width, height, flags, nextFrame, chunksFile, tabFile, palette);

    Vector<Integer> elementList = new Vector<Integer>();

    listStream.getChannel().position(listIndex);

    while (true) {
        int elementIndex = EndianUtils.readSwappedShort(listStream);

        if (elementIndex == -1)
            break;

        SpriteElement element = SpriteElementReader.readByIndex(elementStream, elementIndex);
        res.addElement(element);
    }

    return res;
}