FileDocCategorySizeDatePackage
GameLibrary.javaAPI DocExample5281Sat Sep 12 03:01:00 BST 1998borland.samples.apps.chess.server

GameLibrary

public class GameLibrary extends Object

Fields Summary
static Hashtable
dir
File
library
String
libraryName
String[]
dirlist
String
gameList
GameLibrary
parent
Constructors Summary
public GameLibrary(String directoryName, GameLibrary parent)


       
    try {
    if (directoryName == null && parent == null)
        directoryName = "Library";
    library = new File(directoryName);
    if (!library.isDirectory())
      System.out.println( library.getAbsolutePath()  + " is not a directory");
    libraryName = directoryName;
    dirlist = library.list();
    this.parent = parent;
    }
    catch (Exception e) {
      e.printStackTrace();
    }  
  
Methods Summary
public java.lang.Stringget(java.lang.String gameName)

    if (gameName.startsWith(".") )
      return getList();
    else
      return getGame(gameName);
  
public java.lang.StringgetGame(java.lang.String filename)

    System.out.println("getGame" + filename);
    FileInputStream fs = null;
    String moves = "";
    try {
      File gamefile = new File(getLibraryName(),filename);
      if (gamefile != null) {
        if (gamefile.isDirectory()) {
          return null;
        }
        else {
          fs = new FileInputStream(gamefile) ;
          DataInputStream ds = new DataInputStream(fs);
          InputStreamReader isr  = new InputStreamReader(ds);
          BufferedReader br = new BufferedReader(isr);
          try {

        ///    int offset = 0;
         //   int arraySize = 1024;
         //   byte moveBytes[] = new byte[1024];
         //   int  length = ds.read(moveBytes,offset,arraySize);
         //   while (length > 0)   {
         //     moves =  moves + new String(moveBytes,255,0,length-1);
           //   length = ds.read(moveBytes,offset,arraySize);

         //   }
            String line = br.readLine() ;
            moves = "";
            while (line != null )  {
              moves =  moves + line + " ";
            line = br.readLine()  ;

            }
          }
          catch (IOException e) {}
          fs.close();
        }
      }
      else
        System.out.println("nullFile " +  getLibraryName() + " " + filename);
    }
    catch (Exception e) {System.out.println("GameLibrary.getGame " + e);}
    return moves;
   
public static borland.samples.apps.chess.server.GameLibrarygetLibrary(java.lang.String filename, borland.samples.apps.chess.server.GameLibrary gl)


    try {
       if (gl ==  null) {
         System.out.println("GameLibrary is null");
         if (filename != null && GameLibrary.dir.containsKey(filename))
           return (GameLibrary) GameLibrary.dir.get(filename);
         else
           return new GameLibrary (filename,null);
      }
      if (filename.equals(".."))    {
         System.out.println("filename = ..");
         if (gl.parent != null)
            return gl.parent;
         else
            return gl;
      }
      if (filename.equals(".")) {
         System.out.println("filename = ..");
         return gl   ;
      }
      File gamefile = new File(gl.getLibraryName(),filename);
      if (gamefile == null )
         System.out.println("gamefile is null");
      else {
        if (gamefile.isDirectory()) {
          GameLibrary g ;
          String dirKey =  gl.getLibraryName() + "\\" + filename;
          if (GameLibrary.dir.containsKey(dirKey))
            g =(GameLibrary) GameLibrary.dir.get(dirKey);
          else {
            g = new GameLibrary(dirKey,gl);
            GameLibrary.dir.put(g,dirKey);
          }
          return g;
        }
      }
    }
    catch (Exception e) {System.out.println("GameLibrary.getLibrary " + e);}
    return gl;
  
java.lang.StringgetLibraryName()

     return libraryName;
    // if (parent == null)
    //    return libraryName;
    // else
    //    return parent.getLibraryName() + "\\" + libraryName;
  
public java.lang.StringgetList()

    if (gameList == null)
    if (dirlist != null) {
      if (parent == null)
        gameList = "M" + dirlist[0] + "?";
      else
        gameList = "M..?M" + dirlist[0] + "?";
      for (int i=1; i<dirlist.length;++i) {
        gameList = gameList + "M" + dirlist[i] + "?";
      }
    }
    else
      System.out.println("Library directory is empty");
    return gameList;