File Input

I know this is probably the wrong forum, but here goes.
I have code that reads my 2d world from a file into a hacked version of the GAGE 2d API. Iy seems vary slow in doing so. I know nio is supposed to be the fasted io around, but I don’t really know where to start in porting this. Here it is as it currently stands:

/*
 * ParallaxReader.java
 *
 * Created on June 4, 2003, 12:01 PM
 */

package com.krypto.k2.editor.io;

import com.dnsalias.java.gage.collision.CollisionMap;
import com.dnsalias.java.gage.map.*;
import com.dnsalias.java.gage.map.Tile;
import com.krypto.k2.editor.Grid;
import com.krypto.k2.editor.GridCell;
import com.krypto.k2.graphics.*;
import com.krypto.k2.graphics.GFXEngine;
import com.krypto.k2.graphics.sprites.SpriteManager;
import java.awt.*;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Hashtable;
import javax.imageio.ImageIO;


/**
 *
 * @author  Krypto
 */
public final class GageParallaxReader
{
    private static Hashtable managers = new Hashtable(4);
    /** Creates a new instance of ParallaxWriter */
    public GageParallaxReader()
    {
    }
        
    public static Parallax read(final File _file)
    {
      
       System.out.println("Reading File: " + _file.getName ());
       Parallax parallax = new Parallax();
        try
        {

            RandomAccessFile file = new RandomAccessFile(_file,"r");
            
            
            file.readUTF();
            file.readUTF();
            int numMaps = file.readInt();
            int numManagers = file.readInt();
            
            readManagerSections(numManagers,file);
            readMapSections(numMaps,file,parallax);
            SpriteManager.get().read(file);
            file.close ();       
        } 
        catch (IOException e)
        {
            System.out.println("Error Writing to File");
        }
        return parallax;
    }
    
 private static void readManagerSections( final int numManagers,
                                          final RandomAccessFile file )
 {
     System.out.println("attempting to read in " + numManagers + " Managers");
     TileManager[] manager = new TileManager[numManagers];
     try
     {         
         for(int i=0;i<numManagers;i++)
         {

             String name = file.readUTF ();

             int tileWidth = file.readInt ();
             int tileHeight = file.readInt ();
             String tileSetName = file.readUTF();
             String tileName = null;
             manager[i] = new TileManager(tileWidth,tileHeight);
             BufferedImage tileSetImage = ImageManager.getImage(tileSetName); 
             int col = (int)Math.floor(tileSetImage.getWidth()/tileWidth);
             int row = (int)Math.ceil(tileSetImage.getHeight()/tileHeight);
             Grid grid = new Grid(col,row,tileWidth,tileHeight);
             GridCell cell = null;
             int count = grid.getLength();

             for(int j=0;j<count;j++)
             {
                 //  Point point = grid.getPoint(i);
                 cell = grid.getCell(j);
                 
                 //BufferedImage tileImage = new BufferedImage(tileWidth,tileHeight,BufferedImage.TYPE_INT_ARGB);
                 BufferedImage tileImage = GFXEngine.getGC().createCompatibleImage(tileWidth,tileHeight,Transparency.BITMASK);
                 Graphics2D g = (Graphics2D)tileImage.getGraphics();
                 
                 g.drawImage(tileSetImage,0,0,tileWidth,tileHeight,cell.getX(),cell.getY(),cell.getX()+tileWidth,tileHeight+cell.getY(),null);
                 Tile tile = new Tile(tileImage);
                 
                 manager[i].addTile(tile);
                 //System.out.println("added tile: " + j);
             }
             

             managers.put(name, manager[i]);
             //TileTool.get().addTileset (manager[i]);
         }
     }
     catch(final java.io.IOException e)
     {
         System.out.println("Failed writing TileManager Section!!");
     }
 }
    
 private static void readMapSections(final int numMaps,
                                     final RandomAccessFile file,
                                     final Parallax parallax)
 {
      System.out.println("attempting to read in " + numMaps + " Maps");
     for(int i=0;i<4;i++)
     {
         try
         {
           //  file.readShort ();
             String name = file.readUTF ();
              String managerName = file.readUTF ();
             int cellsX = file.readInt ();
              int cellsY = file.readInt ();
              int count = file.readInt ();
              int width = file.readInt ();
              int height = file.readInt ();
             String background = file.readUTF();
             int[] tiles = new int[count];//maps[i].getTiles ();
           System.out.println("attempting to read in " + count + " Tiles");
             for(int j=0;j<count;j++)
             {              
                 tiles[j] = file.readInt ();
                 //System.out.println("Tile: " + tiles[j]);
             }
             
             TileManager manager = (TileManager)managers.get(managerName);
             //Map map = new Map(manager,cellsX,cellsY,640,380);
             Map map = new Map(manager,cellsX,cellsY,640,380);
             System.out.println("Background is: " + background);
            // map.setName(name);
             
            //for(int i=0; i<images.length; i++) ids[i] = manager.addTile(images[i]);
//             Tile[] ids = manager.getTiles();
//            for(int k=0; k<tiles.length; k++)
//            {
//                if(tiles[k] > 0) tiles[k] = ids[tiles[k]-1].;
//            }
             map.copyMap (tiles);
            // map.setBackground (Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource(background)));
             parallax.addMap (map);
         }
         catch(final java.io.IOException e)
         {
             System.out.println("failed Writing Map Section");
         }
     }
      
      try
      {
      System.out.println("Reading Collision Map");
      String name = file.readUTF ();
              String managerName = file.readUTF ();
             int cellsX = file.readInt ();
              int cellsY = file.readInt ();
              int count = file.readInt ();
              int width = file.readInt ();
              int height = file.readInt ();
             String background = file.readUTF();
             int[] grid = new int[count];//maps[i].getTiles ();
          // System.out.println("attempting to read in " + count + " Tiles");
             for(int j=0;j<count;j++)
             {              
                 grid[j] = file.readInt ();
                 //System.out.println("Tile: " + tiles[j]);
             }
             
          TileManager manager = (TileManager)managers.get(managerName);
           CollisionMap cmap = new CollisionMap(manager,cellsX,cellsY,640,380);            
           cmap.copyGrid(grid);
           //cmap.copyMap(grid);
           parallax.setCollisionMap(cmap);
      }
      catch(final IOException e)
      {
          System.out.println("Failed to initialize CollisionMap");
      }
 }
}

Any suggestions on how th speed this up?

If the file isn’t too big then you could just read it all into a byte array (in one go) and then process it from that. It’d be considerably faster most probably…

Kev

Kevin;
Could you post an example?
Thanks. ;D

Ok, bearing in mind I’m not really “into” using the Readers/Writers and I tend to go for InputStream/OutputStream I’d do something like this:

Note: This is typed from memory, so I haven’t caught exceptions, and there may be a few issues hanging around…



// find out how big the file is going to be and reserve
// some memory
File file = new File("mydatafile.dat");
byte[] data = new byte[file.length()];

// read the complete file into memory
FileInputStream fin = new FileInputStream(file);
fin.read(data);
fin.close();

// process the file as though its any other stream
ByteArrayInputStream bin = new ByteArrayInputStream(data);
DataInputStream din = new DataInputStream(bin);

// do my data reads
String charName = din.readUTF();
int charAge = din.readInt();
int charHeight = din.readDouble();

// tidy up
din.close();


Hope this is of some help,

Kev

Thanks Kevin, I’ll give it a try.