Skip to content

Commit

Permalink
[JVM] Support some more modes for opening files
Browse files Browse the repository at this point in the history
Makes same tests from S32-io/open.t pass:
* open PATH, :create
* open PATH, :truncate, :create
  • Loading branch information
usev6 committed Mar 10, 2018
1 parent a53d1fe commit a90c011
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/io/FileHandle.java
Expand Up @@ -10,6 +10,7 @@
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.perl6.nqp.runtime.ExceptionHandling;
Expand Down Expand Up @@ -62,6 +63,19 @@ public FileHandle(ThreadContext tc, String filename, String mode) {
OpenOption[] opts = resolveOpenMode(mode);
if(opts == null)
ExceptionHandling.dieInternal(tc, "Unhandled file open mode '" + mode + "'");

// work around differences between Perl 6 and FileChannel.open
if (Arrays.asList(opts).contains(StandardOpenOption.READ) && !Arrays.asList(opts).contains(StandardOpenOption.WRITE)) {
// TRUNCATE_EXISTING is ignored when the file is opened only for reading.
if (Arrays.asList(opts).contains(StandardOpenOption.TRUNCATE_EXISTING))
if (Files.exists(p))
Files.write(p, new byte[0]);
// CREATE is ignored when the file is opened only for reading.
if (Arrays.asList(opts).contains(StandardOpenOption.CREATE))
if (!Files.exists(p))
Files.createFile(p);
}

fc = FileChannel.open(p, opts);
chan = fc;
setEncoding(tc, Charset.forName("UTF-8"));
Expand Down

0 comments on commit a90c011

Please sign in to comment.