Ja genau! Das Applet soll NUR auf die, auf dem Server liegende Datei zugreifen.
Ich habe mal den Ursprung der "Computer.java" aus meinem JavaCPC-Emulator hier parat.
In dieser Datei wird "Files.txt" vom Server geladen und verarbeitet.
package jemu.core.device;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.zip.*;
import java.lang.reflect.*;
import java.net.*;
import java.util.*;
import jemu.core.*;
import jemu.core.cpu.*;
import jemu.core.device.memory.*;
import jemu.ui.*;
import jemu.util.diss.*;
public abstract class Computer extends Device implements Runnable {
// Entries are Name, Key, Class, Shown in list
public static final ComputerDescriptor[] COMPUTERS = {
new ComputerDescriptor("CPC464", "Amstrad CPC464", "jemu.system.cpc.CPC", true),
new ComputerDescriptor("CPC664", "Amstrad CPC664", "jemu.system.cpc.CPC", true),
new ComputerDescriptor("CPC6128", "Amstrad CPC6128", "jemu.system.cpc.CPC", true)
};
public static final String DEFAULT_COMPUTER = "CPC464";
public static final int STOP = 0;
public static final int STEP = 1;
public static final int STEP_OVER = 2;
public static final int RUN = 3;
public static final int MAX_FRAME_SKIP = 20;
public static final int MAX_FILE_SIZE = 1024 * 1024; // 1024K maximum
public Applet applet;
protected Thread thread = new Thread(this);
protected boolean stopped = false;
protected int action = STOP;
protected boolean running = false;
protected boolean waiting = false;
protected long startTime;
protected long startCycles;
protected String name;
protected String romPath;
protected String filePath;
protected Vector files = null;
protected Display display;
protected int frameSkip = 0;
protected int runTo = -1;
protected int mode = STOP;
// Listeners for stopped emulation
protected Vector listeners = new Vector(1);
public static Computer createComputer(Applet applet, String name) throws Exception {
for (int index = 0; index < COMPUTERS.length; index++) {
if (COMPUTERS[index].key.equalsIgnoreCase(name)) {
Class cl = Util.findClass(null,COMPUTERS[index].className);
Constructor con = cl.getConstructor(new Class[] { Applet.class, String.class });
return (Computer)con.newInstance(new Object[] { applet, name });
}
}
throw new Exception("Computer " + name + " not found");
}
public Computer(Applet applet, String name) {
super("Computer: " + name);
this.applet = applet;
this.name = name;
// thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
protected void setBasePath(String path) {
romPath = "system/" + path + "/rom/";
filePath = "system/" + path + "/file/";
}
public void initialise() {
reset();
}
public InputStream openFile(String name) throws Exception {
System.out.println("File: " + name);
InputStream result;
try {
result = new URL(applet.getCodeBase(),name).openStream();
} catch(Exception e) {
// e.printStackTrace();
result = new FileInputStream(name);
}
if (name.toLowerCase().endsWith(".zip")) {
ZipInputStream str = new ZipInputStream(result);
str.getNextEntry();
result = str;
}
return result;
}
~~~~~
gekürzt
~~~~~
protected int readStream(InputStream stream, byte[] buffer, int offs, int size)
throws Exception
{
return readStream(stream,buffer,offs,size,true);
}
protected int readStream(InputStream stream, byte[] buffer, int offs, int size, boolean error)
throws Exception
{
while(size > 0) {
int read = stream.read(buffer,offs,size);
if (read == -1) {
if (error)
throw new Exception("Unexpected end of stream");
else
break;
}
else {
offs += read;
size -= read;
}
}
return offs;
}
public byte[] getFile(String name) {
return getFile(name,MAX_FILE_SIZE,true);
}
public byte[] getFile(String name, int size) {
return getFile(name,size,false);
}
public byte[] getFile(String name, int size, boolean crop) {
byte[] buffer = new byte[size];
int offs = 0;
try {
InputStream stream = null;
try {
stream = openFile(name);
while (size > 0) {
int read = stream.read(buffer,offs,size);
if (read == -1)
break;
else {
offs += read;
size -= read;
}
}
} finally {
if (stream != null)
stream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
if (crop && offs < buffer.length) {
byte[] result = new byte[offs];
System.arraycopy(buffer,0,result,0,offs);
buffer = result;
}
return buffer;
}
~~~~~
gekürzt
~~~~~
public String getROMPath() {
return romPath;
}
public String getFilePath() {
return filePath;
}
~~~~~
gekürzt
~~~~~
public Vector getFiles() {
if (files == null) {
files = new Vector();
LineNumberReader reader = null;
try {
reader = new LineNumberReader(new InputStreamReader(openFile(filePath +
"Files.txt")));
String line;
while((line = reader.readLine()) != null) {
int iDesc = line.indexOf('=');
if (iDesc != -1) {
String desc = line.substring(0,iDesc).trim();
int iName = line.indexOf(',',iDesc + 1);
if (iName == -1)
iName = line.length();
String name = line.substring(iDesc + 1,iName).trim();
String instructions = iName < line.length() ?
line.substring(iName + 1).trim().replace('|','\n') : "";
files.addElement(new FileDescriptor(desc,name,instructions));
}
}
} catch(Exception e) {
System.out.println("Cannot get file list for " + this);
} finally {
if (reader != null)
try {
reader.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
return files;
}
public String getFileInfo(String fileName) {
String result = null;
getFiles();
for (int i = 0; i < files.size(); i++) {
FileDescriptor file = (FileDescriptor)files.elementAt(i);
if (file.filename.equalsIgnoreCase(fileName)) {
result = file.instructions;
break;
}
}
return result;
}
~~~~~
gekürzt
~~~~~
}
Natürlich steht auch noch viel drumherum hier, aber vielleicht hilft es ja, eine Möglichkeit zu finden.
Gruss,
Markus
EDIT: Aufgrund der Länge musste ich Teile aus dem Code entfernen.