home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-05-08 | 5.2 KB | 169 lines |
- /*
- * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
- *
- * This SOURCE CODE FILE, which has been provided by Borland as part
- * of a Borland product for use ONLY by licensed users of the product,
- * includes CONFIDENTIAL and PROPRIETARY information of Borland.
- *
- * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
- * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
- * THE PRODUCT.
- *
- * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
- * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
- * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
- * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
- * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
- * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
- * CODE FILE.
- */
- package borland.samples.apps.chess.server;
-
- import java.util.*;
- import java.io.*;
-
- class GameLibrary
- {
- static Hashtable dir = new Hashtable();
- File library;
- String libraryName;
- String [] dirlist;
- String gameList = null;
- GameLibrary parent;
-
- 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();
- }
- }
-
- public String get(String gameName) {
- if (gameName.startsWith(".") )
- return getList();
- else
- return getGame(gameName);
- }
-
- public String getList() {
- 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;
- }
-
- String getLibraryName() {
- return libraryName;
- // if (parent == null)
- // return libraryName;
- // else
- // return parent.getLibraryName() + "\\" + libraryName;
- }
- //return the contents of the file
- static public GameLibrary getLibrary(String filename,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;
- }
-
- public String getGame(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;
- }
-
- }
-