Reading a GIF Header | Heaton Research

Reading a GIF Header

To test this program, I ran it on a variety of systems. I tested it on the little-endian platforms of Windows NT and x86 Linux. It was also tested on the big-endian platform of Sun. There are two example programs given. The first, seen in ScanGIF.java, reads the header of a GIF file. The second, seen in BinaryExample.java, opens a file named “test.dat” and then proceeds to write several of the data types. The file is then closed, reopened and the same data types are read back.

To read a GIF file header, the file is first opened and passed into a BinaryFile object. To match the format of a GIF file, the options of little-endian and unsigned are selected. The GIF file consists of a fixed with type, then a fixed with version, followed by a height and width. This is read in, with the following method calls.

type = bin.readFixedString(3);
version = bin.readFixedString(3);
height = bin.readWord();
width = bin.readWord();

Listing 2: Reading a GIF Header (BinaryExample.java)

import java.io.*;

/**
 * A short example of how to use some of the functions in BinaryFile. First
 * creates a binary file that contains various types, and then rereads those
 * same types.
 * 
 * @author Jeff Heaton(http://www.jeffheaton.com)
 * @version 1.0
 */
class BinaryExample
{

  /**
   * The main function. Used to run the test.
   * 
   * @param args
   *          Not really used, but required by Java.
   * @exception java.io.FileNotFoundException
   */
  public static void main(String args[]) throws FileNotFoundException
  {
    int i;
    String stra, strb, strc, strd;
    RandomAccessFile file;
    BinaryFile bin;
    // set the endian mode to run the test in
    final short endian = BinaryFile.BIG_ENDIAN;
    // set the signed mode to run the test in
    final boolean signed = true;

    try
    {
      file = new RandomAccessFile("./test.dat", "rw");
      bin = new BinaryFile(file);

      bin.setEndian(endian);
      bin.setSigned(signed);
      bin.writeFixedString("Fixed String", 80);
      bin.writeFixedZeroString("Fixed zero string", 80);
      bin.writeLengthPrefixString("Pascal String");
      bin.writeZeroString("Zero String");
      bin.writeByte((short) 100);
      bin.writeWord(1000);
      bin.writeDWord(100000);
      file.close();

      file = new RandomAccessFile("test.dat", "r");
      bin = new BinaryFile(file);
      bin.setEndian(endian);
      bin.setSigned(signed);
      stra = bin.readFixedString(80);
      strb = bin.readFixedZeroString(80);
      strc = bin.readLengthPrefixString();
      strd = bin.readZeroString();
      short b = bin.readByte();
      int w = bin.readWord();
      long dw = bin.readDWord();
      file.close();

      System.out.println("Str a = " + stra);
      System.out.println("Str b = " + strb);
      System.out.println("Str c = " + strc);
      System.out.println("Str d = " + strd);
      System.out.println("**B=" + b);
      System.out.println("**W=" + w);
      System.out.println("**DW=" + dw);
    } catch (Exception e)
    {
      System.out.println("**Error: " + e.getMessage());
    }
  }
}
Copyright 2005-2008 by Heaton Research, Inc.