Created
June 20, 2012 13:19
-
-
Save Alxandr/2959847 to your computer and use it in GitHub Desktop.
Context-changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BarCodeUploaderApplet extends Applet { | |
final String fileName = "C:\\A\\b.dat"; | |
final String backFileName = "C:\\A\\b.old.dat"; | |
public Boolean fileExists() { | |
final Result<Boolean> result = new Result<>(false); | |
final JSObject win = (JSObject) JSObject.getWindow(this); | |
run(new Runnable() { | |
@Override | |
public void run() { | |
try | |
{ | |
result.SetValue(new File(fileName).exists()); | |
} catch (Throwable e) | |
{ | |
win.eval("alert('Feil under lesing av fil, feilmelding: " + e.getMessage().replaceAll("'", "\'") + "');"); | |
} | |
} | |
}); | |
return result.GetValue(); | |
} | |
public String getContent() { | |
final Result<String> result = new Result<>(null); | |
final JSObject win = (JSObject) JSObject.getWindow(this); | |
run(new Runnable() { | |
@Override | |
public void run() { | |
FileInputStream stream = null; | |
try { | |
stream = new FileInputStream(new File(fileName)); | |
FileChannel fc = stream.getChannel(); | |
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); | |
/* Instead of using default, pass in a decoder. */ | |
result.SetValue(Charset.defaultCharset().decode(bb).toString()); | |
} catch (IOException e) { | |
win.eval("alert('Feil under lesing av fil, feilmelding: " + e.getMessage().replaceAll("'", "\'") + "');"); | |
} finally { | |
try { | |
if(stream != null) | |
stream.close(); | |
} catch (Throwable e) { | |
/* ignore */ | |
} | |
} | |
} | |
}); | |
return result.GetValue(); | |
} | |
public void archive() { | |
run(new Runnable() { | |
@Override | |
public void run() { | |
File b = new File(backFileName); | |
if(b.exists()) | |
b.delete(); | |
new File(fileName).renameTo(b); | |
} | |
}); | |
} | |
private void run(final Runnable runnable) { | |
final ManualResetEvent m = new ManualResetEvent(false); | |
final JSObject win = (JSObject) JSObject.getWindow(this); | |
final javax.swing.Timer timer = new javax.swing.Timer(10, new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent ae) { | |
AccessController.doPrivileged(new PrivilegedAction() { | |
@Override | |
public Object run() { | |
runnable.run(); | |
return null; | |
} | |
}); | |
m.set(); | |
if(ae.getSource().getClass() == javax.swing.Timer.class) | |
((javax.swing.Timer)ae.getSource()).stop(); | |
} | |
}); | |
timer.setRepeats(false); | |
timer.start(); | |
try { | |
m.waitOne(5000, TimeUnit.MILLISECONDS); | |
} catch (InterruptedException ex) { | |
/* ignore */ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment