📄 onlineentry.java
字号:
/*
* Copyright, 2005, S.E.Morris, C.Speirs
*
* This file is part of SudokuME.
*
* SudokuME is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* SudokuME is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SudokuME; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package sudoku;
import java.io.*;
import javax.microedition.io.Connector;
// *********************************************************************
// Encapsulates all the functionality needed to load games from across
// a network. Games are packed into sets, called 'bundles'. There is
// a 'catalogue' which details all known bundles.
//
// The process goes as follows:
// - The JAD property "SudokuME-Catalogue-Addr" is used to determine the
// location of the catalogue file.
// - The catalogue file is loaded and parsed (see catalogue()). It
// contains the title, URL and number of games for each bundle. Each
// Bundle is represented by a single OnlineEntry object.
// - When a bundle in the catalogue is selected, the bundle is loaded
// (see load()) to get its games and game titles (see gameTitle and
// gameGrids).
// - The user selects a game in the bundle to play.
// *********************************************************************
class OnlineEntry
{ String title; // Title of bundle
String location; // URL of bundle
String games; // Number of games in bundle
String display; // Display text (handy)
String[] gameTitles; // When loaded, titles
byte[][][] gameGrids; // When loaded, game data
private static String parseStr=null; // Parse variables
private static int parsePos=0; // Parse variables
// Loading (see Selector)
static int catalogueLoadToGo=Integer.MAX_VALUE;
static String catalogueLoadError=null;
int bundleLoadToGo=Integer.MAX_VALUE;
String bundleLoadError=null;
// Enable this to emulate a slow network connection
private final static boolean DEBUG_SLOWNETWORK=false;
// -----------------------------------------------------------------
// CONSTRUCTOR: parse one line from the catalogue file, to create
// an object which represents one bundle of games.
// -----------------------------------------------------------------
OnlineEntry(String s)
{ parseStr=s; parsePos=0;
title=nextString();
location=nextString();
games=nextString();
StringBuffer sb = new StringBuffer(title);
sb.append(" (").append(games).append(")");
display = sb.toString();
parseStr=null; parsePos=0;
}
private String nextString()
{ int a=parsePos,p=parsePos+1;
while(p<parseStr.length() && parseStr.charAt(p)!='\t') p++;
parsePos=p+1;
return parseStr.substring(a,p);
}
static void reset()
{ catalogueLoadToGo=Integer.MAX_VALUE;
catalogueLoadError=null;
}
// -----------------------------------------------------------------
// Load the games in this bundle.
// -----------------------------------------------------------------
void load()
{ if(gameTitles!=null && gameGrids!=null) return;
try
{ DataInputStream dis = new DataInputStream(
Connector.openDataInputStream(location) );
// -----Magic
byte b1 = dis.readByte();
byte b2 = dis.readByte();
byte b3 = dis.readByte();
if(b1!=29 || b2!=6 || b3!=5) throw new IOException("Wrong magic number");
// -----First is the number of games
int sz = dis.readByte()&0xff;
gameTitles = new String[sz]; gameGrids = new byte[sz][][];
// -----Load the grid size for each game
int[] sizes = new int[sz];
for(int i=0;i<sz;i++) sizes[i]=dis.readByte()&0xff;
// -----Load each game
for(int i=0;i<sz;i++)
{ bundleLoadToGo=sz-i;
// -----Load string
StringBuffer sb = new StringBuffer();
for(int j=0;j<20;j++)
{ int v=dis.readByte()&0xff;
if(v>0) sb.append((char)v);
}
gameTitles[i]=sb.toString();
// -----Load grid
int gridSz=sizes[i];
gameGrids[i] = new byte[gridSz][gridSz];
GameData._readRLE_8(gameGrids[i],dis);
if(DEBUG_SLOWNETWORK)
try { Thread.sleep(1000); } catch(InterruptedException e){}
}
bundleLoadToGo=0;
dis.close();
}
catch(Exception e)
{ gameTitles = new String[0]; gameGrids = new byte[0][][];
bundleLoadToGo=0;
bundleLoadError=e.toString();
if(SudokuME.debug) e.printStackTrace();
}
}
// -----------------------------------------------------------------
// STATIC: Parse the catalogue file and construct an OnlineEntry
// object to represent each bundle.
// -----------------------------------------------------------------
static OnlineDirectory catalogue()
{ try
{ // -----Very little error checking!!!
InputStreamReader isr = new InputStreamReader(
Connector.openDataInputStream(SudokuME.catalogueAddr) );
StringBuffer sb = new StringBuffer(100);
String magic=readLine(isr,sb);
if(!magic.equals("#SudokuME#")) throw new IOException("Wrong magic number");
int sz = Integer.parseInt(readLine(isr,sb));
OnlineDirectory root = new OnlineDirectory(sz);
readDirectory(isr,root,sb);
catalogueLoadToGo=0;
isr.close();
return root;
}
catch(Exception e)
{ catalogueLoadToGo=0;
catalogueLoadError=e.toString();
if(SudokuME.debug) e.printStackTrace();
return null;
}
}
private static void readDirectory(InputStreamReader isr,OnlineDirectory dir,StringBuffer sb)
throws IOException
{ for(int i=0;i<dir.array.length;i++)
{ // -----If root directory, update loading countdown?
if(dir.parent==null)
catalogueLoadToGo=dir.array.length-i;
String line = readLine(isr,sb);
if(line.startsWith("+"))
{ OnlineDirectory d = new OnlineDirectory(dir,line);
dir.add(d);
readDirectory(isr,d,sb);
}
else
{ dir.add(new OnlineEntry(line));
}
}
}
private static String readLine(InputStreamReader isr,StringBuffer sb) throws IOException
{ sb.delete(0,sb.length());
int chr=isr.read();
if(chr<0) return null;
while(chr>=0 && chr!='\n')
{ sb.append((char)chr);
chr=isr.read();
}
if(DEBUG_SLOWNETWORK)
try { Thread.sleep(1000); } catch(InterruptedException e){}
return sb.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -