home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BUG 15
/
BUGCD1998_06.ISO
/
aplic
/
jbuilder
/
jsamples.z
/
GameLibrary.java
< prev
next >
Wrap
Text File
|
1997-07-03
|
4KB
|
150 lines
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;
}
}