Skip to content

Commit f09261d

Browse files
committed
Make sure that opening a file for writing in Java ME creates it if it
doesn't exist. Also, example code in script.hcl. File opening was failing for nonexistant files in Java ME, and we also were not utilizing HeclChannel, which was a mistake. Now there is also some test code in the example script.
1 parent e546ef4 commit f09261d

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

files/org/hecl/files/FileCmds.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,14 @@ public Thing operate(int cmd, Interp interp, Thing[] argv)
369369
write = true;
370370
}
371371
}
372-
Object retval;
372+
HeclChannel retval;
373373
if (write) {
374-
retval = fconn.openDataOutputStream();
374+
if (!fconn.exists()) {
375+
fconn.create();
376+
}
377+
retval = new HeclChannel(fconn.openDataOutputStream());
375378
} else {
376-
retval = fconn.openDataInputStream();
379+
retval = new HeclChannel(fconn.openDataInputStream());
377380
}
378381
return ObjectThing.create(retval);
379382
}
@@ -500,8 +503,8 @@ public Thing operate(int cmd, Interp interp, Thing[] argv)
500503
+ cmd + "'.");
501504
}
502505
} catch (IOException e) {
503-
throw new HeclException("IO Exception in " +
504-
argv[0].toString() + ": " + e.toString());
506+
throw new HeclException("IO Exception: " +
507+
HeclException.argvToString(argv) + " : " + e.toString());
505508
}
506509
//#endif
507510
}

midp20/script.hcl

+9-3
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ $form setcurrent
254254

255255
AddSample "File Browser" {
256256

257-
proc FileSelect {infohash bselect bback binfo cmd menu} {
257+
proc FileSelect {infohash bselect bback binfo bwrite cmd menu} {
258258
set root [hget $infohash root]
259259

260260
set index [$menu selection get]
@@ -294,6 +294,10 @@ proc FileSelect {infohash bselect bback binfo cmd menu} {
294294
$bform append [lcdui.stringitem -label "Directory?" -text $directory]
295295
$bform append [lcdui.stringitem -label "Open?" -text [file.isopen $cpath]]
296296
$bform setcurrent
297+
} elseif {eq $cmd $bwrite} {
298+
set fl [open "${cpath}hecl.txt" w]
299+
$fl writeln "Hecl was here"
300+
$fl close
297301
}
298302

299303
hset $infohash paths $lst
@@ -303,7 +307,7 @@ proc FileSelect {infohash bselect bback binfo cmd menu} {
303307
}
304308

305309
#Re-add the infohash into the -commandaction.
306-
$menu configure -commandaction [list FileSelect $infohash $bselect $bback $binfo]
310+
$menu configure -commandaction [list FileSelect $infohash $bselect $bback $binfo $bwrite]
307311
}
308312

309313
set h [hash {}]
@@ -314,10 +318,12 @@ hset $h root {}
314318
set bselect [lcdui.command -label Select -longlabel Select -type item]
315319
set bback [lcdui.command -label Back -longlabel Back -type item]
316320
set binfo [lcdui.command -label Info -longlabel "File Info" -type item]
321+
set bwrite [lcdui.command -label Write -longlabel "Write File" -type item]
317322
set browser [lcdui.list -selectcommand $bselect -title "File Browser" \
318-
-commandaction [list FileSelect $h $bselect $bback $binfo]]
323+
-commandaction [list FileSelect $h $bselect $bback $binfo $bwrite]]
319324
$browser addcommand $bback
320325
$browser addcommand $binfo
326+
$browser addcommand $bwrite
321327
$browser setcurrent
322328
foreach d $devs {
323329
$browser append $d

0 commit comments

Comments
 (0)